liujie
2026-08-04 e9e42b621afdf6ac94f5327ee843b32b10a401b4
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
import { GUIDE_LOGIN_KEY, guideProfile } from '../constants/mock'
import { request } from '../utils/request'
import { initializePush } from './push'
 
export function getLoginState() {
  const state = uni.getStorageSync(GUIDE_LOGIN_KEY) || null
  if (state && state.expiresAt && Number(state.expiresAt) <= Date.now()) {
    uni.removeStorageSync(GUIDE_LOGIN_KEY)
    return null
  }
  return state
}
 
export function isLoggedIn() {
  const state = getLoginState()
  return Boolean(state && state.token)
}
 
function resolveAuditStatus(status) {
  if (status === 1) return 'approved'
  if (status === 2) return 'pending'
  if (status === 0) return 'disabled'
  return 'pending'
}
 
export function loginByPhone({ phone, code }) {
  if (!phone || !code) {
    return Promise.reject(new Error('请输入手机号和验证码'))
  }
  return request({
    url: '/guide/auth/login',
    method: 'POST',
    authenticated: false,
    data: { phone, code }
  }).then((auth) => {
    const user = auth.user || {}
    const state = {
      token: auth.token,
      expiresIn: auth.expiresIn,
      expiresAt: Date.now() + Number(auth.expiresIn || 0) * 1000,
      guide: {
        ...guideProfile,
        id: user.id,
        name: user.realName || user.username || guideProfile.name,
        phone: user.mobile || phone,
        avatar: user.avatar || guideProfile.avatar,
        rating: Number(user.rating || 0),
        orderCount: user.acceptedOrderCount || 0,
        auditStatus: resolveAuditStatus(user.status),
        onlineStatus: 'offline',
        groupName: user.teamName || (user.teamType === '个人' ? '个人导游' : guideProfile.groupName)
      }
    }
    uni.setStorageSync(GUIDE_LOGIN_KEY, state)
    initializePush()
    return state
  })
}
 
export function sendLoginCode(phone) {
  return request({
    url: '/guide/auth/sms-code',
    method: 'POST',
    authenticated: false,
    data: { phone, scene: 'login' }
  })
}
 
export function logout() {
  const state = getLoginState()
  if (!state || !state.token) {
    uni.removeStorageSync(GUIDE_LOGIN_KEY)
    return Promise.resolve()
  }
  return request({ url: '/guide/push-device', method: 'DELETE' })
    .catch(() => undefined)
    .then(() => request({ url: '/guide/auth/logout', method: 'POST' }))
    .finally(() => uni.removeStorageSync(GUIDE_LOGIN_KEY))
}
 
export function cancelAccount() {
  return request({ url: '/guide/push-device', method: 'DELETE' })
    .catch(() => undefined)
    .then(() => request({ url: '/guide/auth/account', method: 'DELETE' }))
    .then((result) => {
      uni.removeStorageSync(GUIDE_LOGIN_KEY)
      return result
    })
}