功能:收口小程序前后台安全与防重复提交
This commit is contained in:
@@ -1,4 +1,36 @@
|
||||
const request = require('../utils/request')
|
||||
const actionGuard = require('../utils/action-guard')
|
||||
const { API_BASE_URL, REQUEST_TIMEOUT } = require('../utils/config')
|
||||
|
||||
function normalizeUploadResponse(response) {
|
||||
if (!response) {
|
||||
throw new Error('头像上传失败')
|
||||
}
|
||||
|
||||
if (response.statusCode === 401) {
|
||||
throw new Error('登录已过期,请重新登录')
|
||||
}
|
||||
|
||||
if (response.statusCode >= 400) {
|
||||
throw new Error('头像上传失败')
|
||||
}
|
||||
|
||||
let payload = response.data
|
||||
if (typeof payload === 'string') {
|
||||
try {
|
||||
payload = JSON.parse(payload)
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
if (payload && typeof payload === 'object' && Object.prototype.hasOwnProperty.call(payload, 'code')) {
|
||||
if (payload.code === 200) {
|
||||
return payload.data
|
||||
}
|
||||
throw new Error(payload.message || '头像上传失败')
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getUserInfo() {
|
||||
@@ -23,6 +55,59 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
bindPhone(data) {
|
||||
return request({
|
||||
url: '/user/bind-phone',
|
||||
method: 'POST',
|
||||
data
|
||||
})
|
||||
},
|
||||
|
||||
updatePhone(data) {
|
||||
return request({
|
||||
url: '/user/phone',
|
||||
method: 'PUT',
|
||||
data
|
||||
})
|
||||
},
|
||||
|
||||
deactivateAccount() {
|
||||
return request({
|
||||
url: '/user/deactivate',
|
||||
method: 'DELETE'
|
||||
})
|
||||
},
|
||||
|
||||
uploadAvatar(filePath) {
|
||||
const signedHeaders = request.buildSignatureHeaders({
|
||||
url: '/user/upload-avatar',
|
||||
method: 'POST',
|
||||
formData: {}
|
||||
})
|
||||
|
||||
return actionGuard.run(null, `POST:/user/upload-avatar:${filePath || ''}`, () => new Promise((resolve, reject) => {
|
||||
wx.uploadFile({
|
||||
url: `${API_BASE_URL}/user/upload-avatar`,
|
||||
filePath,
|
||||
name: 'file',
|
||||
timeout: REQUEST_TIMEOUT,
|
||||
header: signedHeaders,
|
||||
success: response => {
|
||||
try {
|
||||
resolve(normalizeUploadResponse(response))
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
},
|
||||
fail: error => {
|
||||
reject(new Error(error.errMsg || '头像上传失败'))
|
||||
}
|
||||
})
|
||||
}), {
|
||||
message: '上传处理中,请勿重复操作'
|
||||
})
|
||||
},
|
||||
|
||||
checkAccountAvailability(account) {
|
||||
return request({
|
||||
url: `/user/check/account/${encodeURIComponent(account)}`
|
||||
|
||||
Reference in New Issue
Block a user