liujie
2026-09-17 819124be8f16794d409a67f58d5257aef37d9b64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
  })
}