149 lines
3.1 KiB
JavaScript
149 lines
3.1 KiB
JavaScript
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() {
|
|
return request({
|
|
url: '/user/info'
|
|
})
|
|
},
|
|
|
|
updateUserInfo(data) {
|
|
return request({
|
|
url: '/user/update',
|
|
method: 'PUT',
|
|
data
|
|
})
|
|
},
|
|
|
|
updatePassword(data) {
|
|
return request({
|
|
url: '/user/password',
|
|
method: 'PUT',
|
|
data
|
|
})
|
|
},
|
|
|
|
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)}`
|
|
})
|
|
},
|
|
|
|
checkLoginAccountAvailability(account) {
|
|
return request({
|
|
url: `/user/check/login-account/${encodeURIComponent(account)}`
|
|
})
|
|
},
|
|
|
|
getLoginShopsByAccount(account) {
|
|
return request({
|
|
url: `/user/check/login-shops/account/${encodeURIComponent(account)}`
|
|
})
|
|
},
|
|
|
|
getLoginShopsByPhone(phone) {
|
|
return request({
|
|
url: `/user/check/login-shops/phone/${encodeURIComponent(phone)}`
|
|
})
|
|
},
|
|
|
|
checkPhoneAvailability(phone) {
|
|
return request({
|
|
url: `/user/check/phone/${encodeURIComponent(phone)}`
|
|
})
|
|
},
|
|
|
|
sendVerificationCode(data) {
|
|
return request({
|
|
url: '/user/send-verification-code',
|
|
method: 'POST',
|
|
data
|
|
})
|
|
}
|
|
}
|