ts_aimz/pages/shop/reportReplenish/reportReplenish.js
2025-06-16 15:04:57 +08:00

415 lines
12 KiB
JavaScript

// pages/shop/publishCopyright/publishCopyright.js
import Shop from '../../../net/api/shop'
var {
upShopImgUrl,
sImgPrefix,
upShopFileUrl
} = require('../../../net/mainUrl');
var Cache = require('../../../utils/storage');
var docFix = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'];
var imgFix = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg', 'ico', 'tiff', 'tif'];
// 工具函数:检查文件类型
function getFileExtension(filename) {
if (!filename || typeof filename !== 'string') return '';
var lastDot = filename.lastIndexOf('.');
return lastDot === -1 ? '' : filename.substring(lastDot + 1).toLowerCase();
}
function isImageFile(filename) {
return imgFix.indexOf(getFileExtension(filename)) !== -1;
}
function isDocumentFile(filename) {
return docFix.indexOf(getFileExtension(filename)) !== -1;
}
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: '文件(Word、PDF)',
value: 'file'
}
]
},
onLoad: function (options) {
var that = this;
wx.setNavigationBarTitle({
title: '资料补充'
});
wx.setNavigationBarColor({
frontColor: '#000000',
backgroundColor: '#FFFFFF',
animation: {
duration: 500,
timingFunc: 'easeIn'
}
});
var id = options.id;
if (id && id !== '') {
this.setData({
replenishId: id
});
this.doGetReplenishDetail();
} else {
this.showMessage('数据有误,请稍后重试', 'error');
setTimeout(function () {
wx.navigateBack();
}, 1500);
}
},
// 获取补充详情
doGetReplenishDetail: function () {
var that = this;
wx.showLoading({
title: '加载中...'
});
Shop.doGetReplenishDetail(this.data.replenishId)
.then(function (res) {
wx.hideLoading();
that.setData({
replenish: res
});
console.log('详情', res)
// 获取附件信息
if (that.data.replenish.correctionFiles && that.data.replenish.correctionFiles !== '') {
that.doGetFileInfo(that.data.replenish.correctionFiles);
}
})
.catch(function (err) {
wx.hideLoading();
that.showMessage(err.msg || '网络错误,请稍后重试', 'error');
setTimeout(function () {
wx.navigateBack();
}, 1500);
});
},
// 获取文件信息
doGetFileInfo: function (ids) {
var that = this;
var data = {
ids: ids
};
wx.showLoading({
title: '加载中...'
});
Shop.doGetFileInfos(data)
.then(function (res) {
wx.hideLoading();
if (res && res !== null) {
var list = that.addPrefix(res);
that.setData({
replenishFiles: list
});
}
})
.catch(function (err) {
wx.hideLoading();
that.showMessage(err.msg || '获取附件失败,请稍后重试', 'error');
});
},
inputRemark: function (e) {
this.setData({
remark: e.detail.value
});
},
// 添加baseUrl
addPrefix: function (list) {
return list.map(function (item) {
item.netUrl = sImgPrefix + item.fileId;
return item;
});
},
doPreImg: function (e) {
var url = e.currentTarget.dataset.value;
wx.previewImage({
urls: [url]
});
},
// 下载文件
doDownloadFile: function (e) {
var item = e.currentTarget.dataset.item;
// 判断是否是图片
if (isImageFile(item.fileName)) {
wx.previewImage({
urls: [item.netUrl]
});
} else {
// 判断是否支持打开
if (isDocumentFile(item.fileName)) {
// 去下载文件
this.goDownloadFile(item);
} else {
this.showMessage('该文件无法在小程序中打开,请前往电脑端查看', 'info');
}
}
},
goDownloadFile: function (item) {
var that = this;
that.setData({
downloadProgress: 0,
downloading: true
});
var token = Cache.get('token');
var header = {};
if (token) {
header.Auth = 'Bearer ' + token;
}
var downloadTask = wx.downloadFile({
url: item.netUrl,
header: header,
success: function (res) {
that.setData({
downloadProgress: 0,
downloading: false
});
if (res.statusCode === 200) {
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);
that.showMessage('很抱歉,文件下载出现问题。建议您稍作等待,之后再尝试下载。', 'error');
}
});
} else {
that.showMessage('很抱歉,文件下载出现问题。建议您稍作等待,之后再尝试下载。', 'error');
}
},
fail: function (err) {
console.log('下载失败', err);
that.showMessage('很抱歉,文件下载出现问题。建议您稍作等待,之后再尝试下载。', 'error');
}
});
downloadTask.onProgressUpdate(function (res) {
that.setData({
downloadProgress: res.progress
});
});
},
backPageRefresh: function () {
var pages = getCurrentPages();
var beforePage = pages[pages.length - 2];
beforePage.setData({
needRefresh: true
});
wx.navigateBack();
},
showMessage: function (msg, type) {
this.setData({
msgHint: msg,
msgType: type || 'info',
msgShow: true
});
},
bindChooseWay: function (e) {
this.setData({
showActionsheet: false
});
var that = this;
var value = e.detail.value;
if (value === 'img') {
wx.chooseMedia({
count: 4,
mediaType: ['image'],
sourceType: ['album'],
success: function (res) {
if (res && res.tempFiles.length > 0) {
that.doUploadFile(1, res.tempFiles);
}
},
fail: function (err) {
that.showMessage(err.errMsg || '选择文件失败,请稍后重试', 'error');
}
});
} else {
wx.chooseMessageFile({
count: 1,
type: 'file',
extension: docFix,
success: function (res) {
if (res && res.tempFiles.length > 0) {
that.doUploadFile(2, res.tempFiles);
}
},
fail: function (err) {
that.showMessage(err.errMsg || '选择文件失败,请稍后重试', 'error');
}
});
}
},
bindChooseFile: function () {
this.setData({
showActionsheet: true
});
},
// 上传文件 type=1 图片 =2文件
doUploadFile: function (type, files) {
var that = this;
wx.showLoading({
title: '上传中...'
});
var upUrl = type === 1 ? upShopImgUrl : upShopFileUrl;
var upType = type === 1 ? 'image' : 'file';
var token = Cache.get('token');
var header = {};
if (token) {
header.Auth = 'Bearer ' + token;
}
var uploadPromises = files.map(function (file, index) {
return new Promise(function (resolve, reject) {
var filePath = type === 1 ? file.tempFilePath : file.path;
wx.uploadFile({
url: upUrl,
header: header,
filePath: filePath,
name: upType,
success: function (res) {
try {
var result = JSON.parse(res.data);
result.data.netUrl = sImgPrefix + result.data.fileId;
resolve(result.data);
} catch (err) {
reject(new Error('解析上传结果失败'));
}
},
fail: function (err) {
reject(err);
}
});
});
});
Promise.all(uploadPromises)
.then(function (results) {
var newFiles = that.data.files.concat(results);
that.setData({
files: newFiles
});
wx.hideLoading();
})
.catch(function (err) {
console.error('上传失败', err);
wx.hideLoading();
that.showMessage('上传文件失败,请稍后重试', 'error');
});
},
doDelFile(e) {
const index = e.currentTarget.dataset.index
var tempFiles = this.data.files
if (index >= 0 && index < tempFiles.length) {
tempFiles.splice(index, 1);
}
this.setData({
files: tempFiles
});
},
//校验参数
checkParams() {
const isRemarkValid = this.data.remark.trim() !== '';
const isFileValid = this.data.files.length > 0;
if (!isRemarkValid && !isFileValid) {
this.setData({
msgHint: '请输入补充内容或上传附件(至少完成一项)',
msgType: 'error',
msgShow: true
});
return false;
}
return true;
},
//提交
doSubmit() {
var isLegal = this.checkParams()
if (isLegal) {
wx.showLoading({
title: '提交中...',
})
var fileIds = ''
if (this.data.files.length > 0) {
fileIds = this.data.files.map(item => item.fileId)
.join(',')
}
const data = {
correctionFiles: fileIds,
correctionParentId: this.data.replenish.correctionId,
correctionRemark: this.data.remark,
orderId: this.data.replenish.orderId
}
const _self = this
Shop.doSaveReplenish(data)
.then(res => {
wx.hideLoading()
_self.setData({
msgHint: '提交成功',
msgType: 'success',
msgShow: true
})
setTimeout(() => {
_self.backPageRefresh()
}, 1500);
})
.catch(err => {
wx.hideLoading()
_self.setData({
msgHint: err.msg ? err.msg : '网络错误,请稍后重试',
msgType: 'error',
msgShow: true
})
})
}
}
});