路泰科技体检小程序UI设计新版本
wwl
2025-07-30 4e671437d52dd87946c0e38d334fce7a7c17d8e2
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
import store from '@/store'
import config from '@/config'
import { getToken } from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import { toast, showConfirm, tansParams } from '@/utils/common'
import { encrypt, decrypt } from '@/utils/crypto.js'
 
let timeout = 10000
const baseUrl = config.baseUrl
 
// ✅ 需要加密的接口白名单
const encryptApiList = [
  '/api/getHospList',
  '/api/getCusInfo',
  '/api/cusViewReport',
  '/api/cusReportData',
  '/api/viewReportUrl',
  '/api/getShenGaoTiZhong',
  '/api/updatePhone'
]
 
const request = config => {
  const isToken = (config.headers || {}).isToken === false
  config.header = config.header || {}
 
  if (getToken() && !isToken) {
    config.header['token'] = getToken()
    config.header['Authorization'] = 'Bearer ' + getToken()
  }
 
  const hospId = uni.getStorageSync('hospId')
  if (hospId) {
    config.header['hospId'] = hospId
  }
 
  // GET 请求映射 params 参数
  if (config.params) {
    let url = config.url + '?' + tansParams(config.params)
    url = url.slice(0, -1)
    config.url = url
  }
 
  // ✅ 判断是否需要加密
  const isEncryptApi = encryptApiList.includes(config.url)
 
  // 登录接口示例(假如你的登录接口是 /api/login)
  const isLoginRequest = config.url.includes('/login')
 
  let data = config.data
  if (isEncryptApi) {
        console.log('[加密前的data]', config.data)
    data = encrypt(JSON.stringify(config.data))
  } else if (isLoginRequest) {
    data = JSON.stringify(config.data)
  } else {
    // 其他接口保持原样
    data = config.data
  }
 
  return new Promise((resolve, reject) => {
    uni.request({
      method: config.method || 'get',
      timeout: config.timeout || timeout,
      url: config.baseUrl || baseUrl + config.url,
      data: data,
      header: config.header,
      responseType: config.responseType || 'text',
      dataType: config.responseType === 'arraybuffer' ? undefined : 'json'
    }).then(response => {
      let [error, res] = response
      if (error) {
        toast('后端接口连接异常')
        reject('后端接口连接异常')
        return
      }
 
      let resData
      try {
        if (isEncryptApi) {
          const decrypted = decrypt(res.data)
          resData = JSON.parse(decrypted)
        } else if (isLoginRequest) {
          resData = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
        } else {
          resData = res.data
        }
      } catch (e) {
        console.error('响应解析失败:', e)
        toast('响应数据处理失败')
        reject('响应数据处理失败')
        return
      }
 
      if (config.responseType === 'arraybuffer') {
        resolve(resData)
        return
      }
 
      const code = resData.code || 200
      const msg = errorCode[code] || resData.msg || errorCode['default']
 
      if (code === 401) {
        showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
          if (res.confirm) {
            store.dispatch('LogOut').then(() => {
              uni.reLaunch({ url: '/pages/login' })
            })
          }
        })
        reject('无效的会话,或者会话已过期,请重新登录。')
      } else if (code === 500) {
        toast(msg)
        reject(resData)
      } else if (code !== 200) {
        toast(msg)
        reject(resData)
      } else {
        resolve(resData)
      }
    }).catch(error => {
      let { message } = error
      if (message === 'Network Error') {
        message = '后端接口连接异常'
      } else if (message.includes('timeout')) {
        message = '系统接口请求超时'
      } else if (message.includes('Request failed with status code')) {
        message = '系统接口' + message.substr(message.length - 3) + '异常'
      }
      toast(message)
      reject(error)
    })
  })
}
 
export default request