Blame view

泰额版/Food Labeling Management App UniApp/src/services/passwordReset.ts 2.23 KB
59e51671   “wangming”   1
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
  import { buildApiUrl } from '../utils/apiBase'
  import { parseApiErrorMessage, unwrapApiPayload } from '../utils/usAppApiRequest'
  
  function isBusinessFailurePayload(data: unknown): boolean {
    if (data == null || typeof data !== 'object') return false
    const o = data as Record<string, unknown>
    if (o.succeeded === false || o.Succeeded === false) return true
    const inner = o.statusCode ?? o.StatusCode
    if (typeof inner === 'number' && inner >= 400) return true
    return false
  }
  
  /** 匿名 POST(与 `usAppApiRequest` 行为对齐,便于识别 404/501) */
  function postAnonymous(path: string, data: unknown): Promise<unknown> {
    return new Promise((resolve, reject) => {
      uni.request({
        url: buildApiUrl(path),
        method: 'POST',
        header: {
          'Content-Type': 'application/json',
          Accept: 'application/json',
        },
        data,
        success: (res) => {
          const status = res.statusCode ?? 0
          if (status === 404 || status === 501) {
            reject(new Error('RESET_API_NOT_AVAILABLE'))
            return
          }
          if (status === 401) {
            reject(new Error(parseApiErrorMessage(res.data) || 'Unauthorized'))
            return
          }
          if (status >= 400) {
            reject(new Error(parseApiErrorMessage(res.data)))
            return
          }
          const body = res.data
          if (isBusinessFailurePayload(body)) {
            reject(new Error(parseApiErrorMessage(body)))
            return
          }
          resolve(unwrapApiPayload(body))
        },
        fail: (err) => {
          reject(new Error(err.errMsg || 'Network error'))
        },
      })
    })
  }
  
  /**
   * 与 Web 管理端预留路径一致;后端未实现时多为 404。
   * POST /api/app/account/send-password-reset-code
   */
  export async function sendPasswordResetCode(email: string): Promise<void> {
    await postAnonymous('/api/app/account/send-password-reset-code', { email: email.trim() })
  }
  
  /** POST /api/app/account/confirm-password-reset */
  export async function confirmPasswordReset(input: {
    email: string
    code: string
    newPassword: string
  }): Promise<void> {
    await postAnonymous('/api/app/account/confirm-password-reset', {
      email: input.email.trim(),
      code: input.code.trim(),
      newPassword: input.newPassword,
    })
  }