Browse Source

小程序直传阿里云oss

master
396316021 1 year ago
parent
commit
953b12c74f
  1. 12
      api/other.js
  2. 3
      config/host.js
  3. 44
      utils/functions.js

12
api/other.js

@ -0,0 +1,12 @@
/**
* 阿里云文件上传STS
* @param {Object} data
*/
export function getSTS(data) {
return request({
url: `${host}/other/get-aliyun-sts`,
method: "get",
data
});
}

3
config/host.js

@ -4,5 +4,6 @@ const host='http://47.109.60.201:9501/api'
// const host='http://192.168.2.92:9503/api'
const imghost='http://live.admin.jinghkb.com'
const title = '汪汪行天下'
const oss = 'https://fanyi-guanjia-1.'+'oss-cn-chengdu.aliyuncs.com'
// const codehost='http://code.baomingle.cn'
export {host,imghost,title}
export {host,imghost,title,oss}

44
utils/functions.js

@ -5,6 +5,7 @@ import log from "@/utils/log";
const QQMapWX=require('./qqmap-wx-jssdk.min.js')
import * as QQMapwx from '@/utils/qqmap-wx-jssdk.min.js'
import {getSTS} from "@/api/other";
var qqmapsdk;
import { imghost,host } from '@/config/host.js'
const api = {
@ -282,6 +283,47 @@ const api = {
uni.setStorageSync('sysInfo',data);
resolve(data);
})
}
},
uploadOssFile:(file)=>{
return new Promise((resolve, reject) => {
getSTS({}).then(res => {
// const client = new OSS(res.data);
// console.log(file);
uni.showLoading({
title:'上传中'
})
let timer = new Date();
let address = timer.getFullYear() + '' + (timer.getMonth() + 1) + '' + timer.getDate();
address = 'upload/' + address + '/';
var imageSrc = file;
let str = file.substr(file.lastIndexOf('.'));
let nameStr = address + timer.getTime() + str;
uni.uploadFile({
url: `${oss}`, //仅为示例,非真实的接口地址
filePath:file,
name: 'file',
header: {
"content-type": "application/json;charset=utf-8",
},
formData:Object.assign({
name:nameStr,
key:nameStr,
success_action_status: '200',
},res.data),
success: (uploadFileRes) => {
uni.hideLoading()
resolve ({show_path:oss+'/' + nameStr});
// relove(JSON.parse(uploadFileRes.data).data)
// console.log(JSON.parse(uploadFileRes.data).data);
},
fail:(res) =>{
uni.hideLoading()
console.log(res)
return {};
}
});
});
})
},
}
export default api;

Loading…
Cancel
Save