|
@@ -1,4 +1,7 @@
|
|
|
-import store from '../store'
|
|
|
+import store from '../store';
|
|
|
+import {
|
|
|
+ getUpimgKey
|
|
|
+} from '@/api/login.js';
|
|
|
let service = {
|
|
|
baseURL: store.state.baseURL, // 请求头
|
|
|
header: {
|
|
@@ -111,86 +114,96 @@ let service = {
|
|
|
// #endif
|
|
|
let config = {
|
|
|
// 上传图片的API
|
|
|
- upload_img_url: url
|
|
|
+ upload_img_url: url,
|
|
|
+ data: requestData.data,
|
|
|
+ file_name: requestData.data.file_name||'user/'+store.state.user.userInfo.uid
|
|
|
}
|
|
|
-
|
|
|
- let Uploader = {
|
|
|
- // 选择图片
|
|
|
- choose(num) {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- uni.chooseImage({
|
|
|
- count: num,
|
|
|
- sourceType: ['camera', 'album'],
|
|
|
- success(res) {
|
|
|
- // console.log(res);
|
|
|
- // 缓存文件路径
|
|
|
- resolve(res.tempFilePaths)
|
|
|
- },
|
|
|
- fail(err) {
|
|
|
- console.log(err)
|
|
|
- reject(err)
|
|
|
- }
|
|
|
- })
|
|
|
- })
|
|
|
-
|
|
|
- },
|
|
|
- // 上传图片
|
|
|
- upload_one(path) {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
+ uni.showLoading({
|
|
|
+ title: '请求key中'
|
|
|
+ })
|
|
|
+ getUpimgKey({
|
|
|
+ file_name: config.file_name
|
|
|
+ }).then(({
|
|
|
+ data
|
|
|
+ }) => {
|
|
|
+ uni.hideLoading()
|
|
|
+ uni.chooseImage({
|
|
|
+ count: 1,
|
|
|
+ sourceType: ['camera', 'album'],
|
|
|
+ success(res) {
|
|
|
uni.showLoading({
|
|
|
title: '上传中'
|
|
|
})
|
|
|
+ // console.log(res, '选中图片');
|
|
|
+ const time = (new Date()).getTime();
|
|
|
+ // #ifdef H5
|
|
|
+ const formData = {
|
|
|
+ "policy": data.sign.policy,
|
|
|
+ "OSSAccessKeyId": data.sign.accessid,
|
|
|
+ 'success_action_status': 200,
|
|
|
+ "signature": data.sign.signature,
|
|
|
+ "key": config.file_name+'/'+time+res.tempFiles[0].name
|
|
|
+ }
|
|
|
+ // #endif
|
|
|
+ // #ifndef H5
|
|
|
+ const formData = {
|
|
|
+ "policy": data.sign.policy,
|
|
|
+ "OSSAccessKeyId": data.sign.accessid,
|
|
|
+ 'success_action_status': 200,
|
|
|
+ "signature": data.sign.signature,
|
|
|
+ "key": config.file_name+'/'+time+'.png'
|
|
|
+ }
|
|
|
+ // #endif
|
|
|
+ // console.log(formData,'formData');
|
|
|
uni.uploadFile({
|
|
|
- url: config.upload_img_url, //仅为示例,非真实的接口地址
|
|
|
- filePath: path,
|
|
|
+ url: data.sign.host, //仅为示例,非真实的接口地址
|
|
|
+ filePath: res.tempFilePaths[0],
|
|
|
+ formData,
|
|
|
name: 'file',
|
|
|
header: {
|
|
|
- "Authori-zation": 'Bearer ' + uni.getStorageSync(
|
|
|
- 'token')
|
|
|
+ "Authori-zation": 'Bearer ' + uni
|
|
|
+ .getStorageSync('token')
|
|
|
},
|
|
|
- success: (uploadFileRes) => {
|
|
|
- if ("string" === typeof uploadFileRes.data) {
|
|
|
- resolve(JSON.parse(uploadFileRes.data).data)
|
|
|
- } else {
|
|
|
- resolve(uploadFileRes.data.data)
|
|
|
+ success: (suc) => {
|
|
|
+ if(suc.statusCode==200){
|
|
|
+ // #ifdef H5
|
|
|
+ const imgurl = [{url:data.sign.host+'/'+config.file_name+'/'+time+res.tempFiles[0].name}];
|
|
|
+ // #endif
|
|
|
+ // #ifndef H5
|
|
|
+ const imgurl = [{url:data.sign.host+'/'+config.file_name+'/'+time+'.png'}];
|
|
|
+ // #endif
|
|
|
+ resolve(imgurl)
|
|
|
}
|
|
|
+ uni.hideLoading()
|
|
|
},
|
|
|
- complete() {
|
|
|
+ file(res) {
|
|
|
uni.hideLoading()
|
|
|
+ // console.log(res, '图片上传错误');
|
|
|
+ reject(res)
|
|
|
}
|
|
|
+
|
|
|
});
|
|
|
- })
|
|
|
- },
|
|
|
- // 上传多个图片
|
|
|
- upload(path_arr) {
|
|
|
- let num = path_arr.length;
|
|
|
- return new Promise(async (resolve, reject) => {
|
|
|
- let img_urls = []
|
|
|
- for (let i = 0; i < num; i++) {
|
|
|
- let img_url = await this.upload_one(path_arr[i]);
|
|
|
- img_urls.push(img_url)
|
|
|
- };
|
|
|
- resolve(img_urls)
|
|
|
- })
|
|
|
- },
|
|
|
- // 触发上传图片事件
|
|
|
- choose_and_upload(num) {
|
|
|
- return new Promise(async (resolve, reject) => {
|
|
|
- // 选择图片
|
|
|
- let path_arr = await this.choose(num);
|
|
|
- // 上传图片
|
|
|
- await this.upload(path_arr).then((e) => {
|
|
|
- resolve(e);
|
|
|
- }).catch((e) => {
|
|
|
- reject(e)
|
|
|
+ },
|
|
|
+ complete() {
|
|
|
+ uni.hideLoading()
|
|
|
+ },
|
|
|
+ fail(err) {
|
|
|
+ // console.log(res, '图片上传错误');
|
|
|
+ uni.showToast({
|
|
|
+ title: '图片上传失败',
|
|
|
+ icon:"error"
|
|
|
});
|
|
|
- })
|
|
|
- }
|
|
|
- };
|
|
|
- Uploader.choose_and_upload(1).then((e) => {
|
|
|
- resolve(e);
|
|
|
- }).catch((e) => {
|
|
|
- console.log(e);
|
|
|
+ uni.hideLoading()
|
|
|
+ reject(err)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }).catch((res)=>{
|
|
|
+ uni.hideLoading()
|
|
|
+ uni.showToast({
|
|
|
+ title: 'key请求失败',
|
|
|
+ icon:"error"
|
|
|
+ });
|
|
|
+ reject(res)
|
|
|
})
|
|
|
})
|
|
|
},
|