海南旅游项目 前端仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

74 lines
1.8 KiB

/*
*上传文件
*@param - filePath :图片的本地资源路径
*@param - dir:表示要传到哪个目录下
*@param - successc:成功回调
*@param - failc:失败回调
*/
const uploadFile = function (filePath, successc, failc) {
if (!filePath || filePath.length < 9) {
wx.showModal({
title: '图片错误',
content: '请重试',
showCancel: false,
})
return;
}
// 上传的服务器地址
let url = this.API.DEVURL;
if (this.API.DEV == 'prod') {
url = this.API.PRODURL;
}
const url_a = this.API.API_UPPLOAD_APPLY;
// 上传图片的目录
var nowTime = formatTime(new Date());
const dir = 'wxmini/images/' + nowTime + '/';
// 获取上传的文件类型 fileType
let fileTypeIndex = filePath.lastIndexOf('.');
let fileType = filePath.substring(fileTypeIndex);
uni.uploadFile({
url: url + url_a,//开发者服务器 url
filePath: filePath,//要上传文件资源的路径
name: 'image',
header: {
appid: 'wxb35ef055a4dd8ad4',
Authentication: uni.getStorageSync('userinfo').token
},
success: function (res) {
if (res.statusCode != 200 || !res.data) {
failc(new Error('上传错误:' + JSON.stringify(res)))
return;
}
let res_data = JSON.parse(res.data);
successc && successc(res_data.data.path);
},
fail: function (err) {
failc(err);
},
})
}
// 获取当前日期(年-月-日),并不足十位补零
function formatTime(date) {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('-')
// + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
uploadFile
};