import { request, uploadFile } from '../utils/request'
|
|
const REGISTRATION_STATE_KEY = 'mingdao_guide_registration_state'
|
|
export function getRegistrationState() {
|
return uni.getStorageSync(REGISTRATION_STATE_KEY) || null
|
}
|
|
export function clearRegistrationState() {
|
uni.removeStorageSync(REGISTRATION_STATE_KEY)
|
}
|
|
export function sendRegistrationCode(phone) {
|
return request({
|
url: '/guide/registration/sms-code',
|
method: 'POST',
|
authenticated: false,
|
data: { phone }
|
})
|
}
|
|
export function verifyRegistrationPhone(phone, code) {
|
return request({
|
url: '/guide/registration/verify-phone',
|
method: 'POST',
|
authenticated: false,
|
data: { phone, code }
|
}).then((result) => {
|
const state = { phone, ...result }
|
uni.setStorageSync(REGISTRATION_STATE_KEY, state)
|
return state
|
})
|
}
|
|
export function getRegistrationTeams() {
|
return request({
|
url: '/guide/registration/teams',
|
authenticated: false
|
})
|
}
|
|
export function uploadRegistrationDocument(filePath, registrationToken) {
|
return compressRegistrationImage(filePath).then((uploadPath) =>
|
uploadFile({
|
url: '/guide/registration/documents',
|
filePath: uploadPath,
|
header: { 'X-Registration-Token': registrationToken }
|
})
|
).then((result) => result.url)
|
}
|
|
function compressRegistrationImage(filePath) {
|
if (!filePath) {
|
return Promise.resolve(filePath)
|
}
|
|
if (typeof uni.compressImage !== 'function') {
|
return compressRegistrationImageInBrowser(filePath)
|
}
|
|
return new Promise((resolve) => {
|
uni.compressImage({
|
src: filePath,
|
quality: 60,
|
compressedWidth: 1280,
|
success: (result) => resolve(result && result.tempFilePath ? result.tempFilePath : filePath),
|
fail: () => compressRegistrationImageInBrowser(filePath).then(resolve)
|
})
|
})
|
}
|
|
function compressRegistrationImageInBrowser(filePath) {
|
if (typeof window === 'undefined' || typeof window.fetch !== 'function' || typeof window.Image !== 'function') {
|
return Promise.resolve(filePath)
|
}
|
|
return window.fetch(filePath)
|
.then((response) => response.blob())
|
.then((blob) => new Promise((resolve) => {
|
const image = new window.Image()
|
const sourceUrl = window.URL.createObjectURL(blob)
|
image.onload = () => {
|
const scale = Math.min(1, 1280 / image.width)
|
const canvas = document.createElement('canvas')
|
canvas.width = Math.max(1, Math.round(image.width * scale))
|
canvas.height = Math.max(1, Math.round(image.height * scale))
|
canvas.getContext('2d').drawImage(image, 0, 0, canvas.width, canvas.height)
|
canvas.toBlob((compressedBlob) => {
|
window.URL.revokeObjectURL(sourceUrl)
|
if (!compressedBlob) {
|
resolve(filePath)
|
return
|
}
|
resolve(window.URL.createObjectURL(compressedBlob))
|
}, 'image/jpeg', 0.6)
|
}
|
image.onerror = () => {
|
window.URL.revokeObjectURL(sourceUrl)
|
resolve(filePath)
|
}
|
image.src = sourceUrl
|
}))
|
.catch(() => filePath)
|
}
|
|
export function submitGuideRegistration(form) {
|
const state = getRegistrationState()
|
if (!state || !state.registrationToken) {
|
return Promise.reject(new Error('手机号验证已失效,请重新验证'))
|
}
|
const uploadTasks = [
|
uploadRegistrationDocument(form.idFront, state.registrationToken),
|
uploadRegistrationDocument(form.idBack, state.registrationToken),
|
uploadRegistrationDocument(form.guideLicense, state.registrationToken),
|
form.guideCert ? uploadRegistrationDocument(form.guideCert, state.registrationToken) : Promise.resolve('')
|
]
|
return Promise.all(uploadTasks).then(([idCardFrontUrl, idCardBackUrl, guideCertificateUrl, qualificationCertificateUrl]) =>
|
request({
|
url: '/guide/registration/applications',
|
method: 'POST',
|
authenticated: false,
|
data: {
|
realName: form.name,
|
gender: form.gender === '男' ? 1 : 2,
|
ageRange: form.age,
|
workYearRange: form.workYear,
|
guideType: form.guideType,
|
teamId: form.teamId,
|
idCardNo: form.idNo,
|
idCardFrontUrl,
|
idCardBackUrl,
|
guideCertificateUrl,
|
qualificationCertificateUrl
|
},
|
headers: { 'X-Registration-Token': state.registrationToken }
|
})
|
).then((result) => {
|
clearRegistrationState()
|
return result
|
})
|
}
|