// pages/shop/publishCopyright/publishCopyright.js import Shop from '../../../net/api/shop' import { upShopImgUrl, sImgPrefix } from '../../../net/mainUrl' const Cache = require('../../../utils/storage') const docFix = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'] const imgFix = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg', 'ico', 'tiff', 'tif'] Page({ /** * 页面的初始数据 */ data: { msgHint: '', msgType: 'info', msgShow: false, files: [], maxCount: 4, replenishId: '', replenish: null, replenishFiles: [], //附件 downloading: false, //是否下载 downloadProgress: 0, //下载进度 remark: '', showActionsheet: false, groups: [{ text: '图片', value: 'img' }, { text: '文件', value: 'file' } ] }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { wx.setNavigationBarTitle({ title: '资料补充', }) wx.setNavigationBarColor({ frontColor: '#000000', // 必写项,字体颜色仅支持#ffffff和#000000 backgroundColor: '#FFFFFF', // 传递的颜色值,仅支持十六进制颜色 animation: { // 可选项 duration: 500, timingFunc: 'easeIn' } }) this.setData({ selectFile: this.selectFile.bind(this), uploadFile: this.uploadFile.bind(this) }) const id = options.id if (id && id != '') { this.setData({ replenishId: id }) this.doGetReplenishDetail() } else { this.setData({ msgHint: '数据有误,请稍后重试', msgType: 'error', msgShow: true }) setTimeout(() => { wx.navigateBack() }, 1500); } }, //获取补充详情 async doGetReplenishDetail() { try { wx.showLoading({ title: '加载中...', }) const res = await Shop.doGetReplenishDetail(this.data.replenishId) this.setData({ replenish: res }) wx.hideLoading() //去获取附件信息 if (this.data.replenish.correctionFiles && this.data.replenish.correctionFiles != '') { this.doGetFileInfo(this.data.replenish.correctionFiles) } } catch (err) { wx.hideLoading() this.setData({ msgType: 'error', msgHint: err.msg ? err.msg : '网络错误,请稍后重试', msgShow: true }) setTimeout(() => { wx.navigateBack() }, 1500); } }, //获取文件信息 doGetFileInfo(ids) { const data = { ids: ids } wx.showLoading({ title: '加载中...', }) Shop.doGetFileInfos(data) .then(res => { wx.hideLoading() if (res && res != null) { const list = this.addPrefix(res) this.setData({ replenishFiles: list }) } }) .catch(err => { wx.hideLoading() this.setData({ msgHint: err.msg ? err.msg : '获取附件失败,请稍后重试', msgType: 'error', msgShow: true }) }) }, inputRemark(e) { this.setData({ remark: e.detail.value }) }, //添加baseUrl addPrefix(list) { list.map(item => { item.netUrl = sImgPrefix + item.fileId }) return list }, doPreImg(e) { wx.previewImage({ urls: [e], }) }, deleteImage(e) { var index = e.detail.index; this.data.files.splice(index, 1); }, selectFile(files) { console.log('files', files) }, uploadFile(files) { var tempFilePaths = files.tempFilePaths; var token = Cache.get('token') var header = {} if (token) { header.Auth = 'Bearer ' + token; } var that = this for (var i = 0; i < tempFilePaths.length; i++) { wx.uploadFile({ url: upShopImgUrl, header: header, filePath: tempFilePaths[i], name: 'image', success: function (res) { console.log(res) var result = JSON.parse(res.data) that.data.files.push(result.data) }, fail: function (err) { console.log(err); } }) } // 文件上传的函数,返回一个promise return new Promise(function (resolve, reject) { var result = {}; result['urls'] = tempFilePaths; resolve(result); }) }, //下载文件 doDownloadFile(e) { const item = e.currentTarget.dataset.item //判断是否是图片 if (imgFix.indexOf(item.fileType) != -1) { this.doPreImg(item.netUrl) } else { //判断是否支持打开 if (docFix.indexOf(item.fileType) != -1) { //去下载文件 this.goDownloadFile(item) } else { this.setData({ msgHint: '该文件无法在小程序中打开,请前往电脑端查看', msgType: 'info', msgShow: true }) } } }, goDownloadFile(item) { const _self = this _self.setData({ downloadProgress: 0, downloading: true, //显示下载进度 }) const token = Cache.get('token') const header = {} if (token) { header.Auth = `Bearer ${token}`; } const downloadTask = wx.downloadFile({ url: item.netUrl, header: header, success(res) { console.log('下载成功', res) _self.setData({ downloadProgress: 0, downloading: false }) if (res.statusCode === 200) { _self.setData({ successHint: '下载成功', showSuccess: true }) wx.getFileSystemManager().saveFile({ tempFilePath: res.tempFilePath, filePath: wx.env.USER_DATA_PATH + '/' + item.fileName, success: function (res) { wx.openDocument({ filePath: res.savedFilePath, showMenu: true }) }, fail: function (err) { console.error('文件保存失败', err); _self.showErr('很抱歉,文件下载出现问题。建议您稍作等待,之后再尝试下载。') } }) } else { _self.showErr('很抱歉,文件下载出现问题。建议您稍作等待,之后再尝试下载。') } }, fail(err) { _self.showErr('很抱歉,文件下载出现问题。建议您稍作等待,之后再尝试下载。') console.log(`下载失败${err}`) } }) downloadTask.onProgressUpdate(res => { console.log(res.progress) _self.setData({ downloadProgress: res.progress }) }); }, backPageRefresh: function () { var pages = getCurrentPages(); var beforePage = pages[pages.length - 2]; beforePage.setData({ needRefresh: true }) wx.navigateBack() }, showErr(msg) { this.setData({ msgHint: msg, msgType: 'error', msgShow: true, downloading: false, downloadProgress: 0 }) }, bindChooseWay(e) { if (e.detail.value == 'img') { wx.chooseMedia({ count: 4, mediaType: ['image'], sourceType: ['album'], success(res) { console.log(res) }, fail(err) { } }) } else { wx.chooseMessageFile({ count: 1, type: 'file', extension:docFix, success(res) { console.log(res) }, fail(err) { } }) } }, bindChooseFile() { this.setData({ showActionsheet: true }) } })