Commit 33a396c2 authored by linwenqing's avatar linwenqing

feat:新增上传图片接口

parent 974eb1f5
const UNI_APP = { const UNI_APP = {
apiUrl: 'http://192.168.0.208:8760', apiUrl: 'http://192.168.0.208:8760',
qn_base_url: 'https://qn-static.banshouhui.com/study/', qn_base_url: 'https://qn-static.banshouhui.com/study/',
argoDebugMode: 1, qn_asset_url: 'http://deudn.c.pp.cc/',
version: 'v1.0.0' uUrl: 'http://rest-u.yunya.com/',
argoDebugMode: 1,
version: 'v1.0.0',
} }
module.exports = UNI_APP module.exports = UNI_APP
const UNI_APP = { const UNI_APP = {
apiUrl: 'https://system.banshouhui.com', apiUrl: 'https://system.banshouhui.com',
qn_base_url: 'https://qn-static.banshouhui.com/study/', qn_base_url: 'https://qn-static.banshouhui.com/study/',
argoDebugMode: 1, qn_asset_url: 'https://img2.banshouhui.com/',
version: 'v1.0.0' uUrl: 'http://rest-u.banshouhui.com/',
argoDebugMode: 1,
version: 'v1.0.0',
} }
module.exports = UNI_APP module.exports = UNI_APP
const UNI_APP = { const UNI_APP = {
apiUrl: 'https://system.dev.banshouhui.com', apiUrl: 'https://system.dev.banshouhui.com',
qn_base_url: 'https://qn-static.banshouhui.com/study/', qn_base_url: 'https://qn-static.banshouhui.com/study/',
argoDebugMode: 1, qn_asset_url: 'https://img2.banshouhui.com/',
version: 'v1.0.0' uUrl: 'http://rest-u.test.ibanshou.cn/',
argoDebugMode: 1,
version: 'v1.0.0',
} }
module.exports = UNI_APP module.exports = UNI_APP
let commonApiFun = function(vm) { let commonApiFun = function(vm) {
let loginWechatAuth = async (params = {}) => await vm.$u.post('/wxapp/unify/authorization', params) const globalUrl = process.uniEnv || {}
let payOrder = async (params = {}) => await vm.$u.post('/pay/order', params) let ucenterApiUrl = globalUrl.uUrl
const commonApi = {
loginWechatAuth, let loginWechatAuth = async (params = {}) =>
payOrder await vm.$u.post('/wxapp/unify/authorization', params)
} let payOrder = async (params = {}) => await vm.$u.post('/pay/order', params)
return commonApi let getQiNiuToken = async (params = {}) =>
await vm.$u.post(ucenterApiUrl + 'v1/tokens', params)
const commonApi = {
loginWechatAuth,
payOrder,
getQiNiuToken,
}
return commonApi
} }
module.exports = { module.exports = {
commonApiFun commonApiFun,
} }
function hasPermissionOfCamera() {
//如果没权限提示文字,还没取得权限会自动吊起询问权限,已经有权限自动返回true
return new Promise((r, j) => {
uni.authorize({
scope: 'scope.camera',
success: function() {
r(true)
},
fail: function() {
j(false)
},
complete: function() {
console.log('done!')
},
})
})
}
//吊起设备原生相机相册取得文件,并自动上传七牛,返回七牛key,domain,本地file信息
function getPhoto(type, token) {
return new Promise((resolve, reject) => {
hasPermissionOfCamera().then((r) => {
if (!r) {
console.warn('调用相机失败')
}
})
let sourceType = type == 1 ? ['album'] : ['album', 'camera']
uni.chooseImage({
count: 6, //默认9
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: sourceType, //从相册选择
success: function(res) {
//上传七牛
let filePath = res.tempFiles[0].path //为需要上传的文件
upload(filePath, token)
.then((msg) => {
//最后七牛返回的key
resolve({
key: msg,
files: res.tempFiles,
})
})
.catch((msg) => {
console.error(msg)
reject(msg)
})
},
})
})
}
//上传七牛
function upload(filePath, token) {
return new Promise((resolve, reject) => {
let key =
new Date().getTime() + Math.random(1000) + filePath.split('/').pop() //key为自定义文件名
uni.uploadFile({
url: 'https://up.qiniup.com', // default ecn华北的地址,在七牛云中选择对应位置的网址
filePath: filePath,
name: 'file',
formData: {
token: token,
key: key,
},
complete: (res) => {
if (res.statusCode === 200) {
resolve(JSON.parse(res.data).key)
} else {
reject(res.errMsg)
}
},
})
})
}
export default {
upload,
getPhoto,
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment