1. 程式人生 > >微信小程序 - 上傳頭像

微信小程序 - 上傳頭像

顯示 hid oos part als image chang pre 隱藏

// 上傳文件 function uploadFile(url, filePath, name, formData, cb) { wx.uploadFile({ url: ‘https://www.lenfshion.com/projectmng/‘ + url, filePath: filePath, name: name, header: { ‘content-type‘: ‘multipart/form-data‘ }, // 設置請求的 header formData: formData, // HTTP 請求中其他額外的 form data success: function (res) { if (res.statusCode == 200 && !res.data.result_code) { return typeof cb == "function" && cb(res.data) } else { return typeof cb == "function" && cb(false) } }, fail: function () { return typeof cb == "function" && cb(false) } }) } changeImage: function () { var that = this; wx.chooseImage({ count: 1, // 默認9 sizeType: [‘original‘, ‘compressed‘], // 可以指定是原圖還是壓縮圖,默認二者都有 sourceType: [‘album‘, ‘camera‘], // 可以指定來源是相冊還是相機,默認二者都有 success: function (res) { // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標簽的src屬性顯示圖片,只有一張圖片獲取下標為0 var tempFilePaths = res.tempFilePaths[0]; that.setData({ actionSheetHidden: !that.data.actionSheetHidden }); // that.setData({ // infoFace: tempFilePaths, // actionSheetHidden: !that.data.actionSheetHidden // }); // 圖片截取 wx.navigateTo({ url: ‘../upload/upload?src=‘+tempFilePaths+‘‘ }) // } }) }, // 查看原圖 viewImage: function () { var that = this; wx.previewImage({ current: ‘‘, // 當前顯示圖片的http鏈接 urls: [that.data.infoFace] // 需要預覽的圖片http鏈接列表 }) }, // 更換頭像 actionSheetHidden: true, // 是否顯示底部可選菜單 actionSheetItems: [ { bindtap: ‘changeImage‘, txt: ‘修改頭像‘ }, { bindtap: ‘viewImage‘, txt: ‘查看頭像‘ } ] // 底部可選菜單 // 點擊頭像 顯示底部菜單 clickImage: function () { var that = this; that.setData({ actionSheetHidden: !that.data.actionSheetHidden }) }, // 點擊其他區域 隱藏底部菜單 actionSheetbindchange: function () { var that = this; that.setData({ actionSheetHidden: !that.data.actionSheetHidden }) }, <action-sheet hidden="{{actionSheetHidden}}" bindchange="actionSheetbindchange"> <block wx:for="{{actionSheetItems}}" wx:key="unique"> <action-sheet-item bindtap="{{item.bindtap}}">{{item.txt}}</action-sheet-item> </block> <action-sheet-cancel class="cancel">取消</action-sheet-cancel> </action-sheet> if (this.data.isChooseImg){ uploadFile(‘appupload/upload‘, that.data.infoFace, ‘img‘, {}, function (res) { if (null != res) { var obj = JSON.parse(res); if (obj) { faceImg = obj.pd.files[0]; if (faceImg) { that.setData({ infoFace: imgUrl + faceImg, isChooseImg:false }) } } } else { // 顯示消息提示框 wx.showToast({ title: ‘上傳失敗‘, icon: ‘error‘, duration: 2000 }) } }); }

微信小程序 - 上傳頭像