index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import http from './interface'
  2. import {
  3. decrypt
  4. } from '../utils/aes.js'
  5. //解密脱敏字段集合 type(姓名 1、手机号 2、身份证 3)
  6. const tmList = [{
  7. prop: 'card',
  8. type: 3
  9. },
  10. {
  11. prop: 'phone',
  12. type: 2
  13. },
  14. {
  15. prop: 'passengerName',
  16. type: 1
  17. },
  18. {
  19. prop: 'contactNum',
  20. type: 2
  21. },
  22. {
  23. prop: 'linkPerson',
  24. type: 1
  25. },
  26. {
  27. prop: 'linkPhone',
  28. type: 2
  29. },
  30. {
  31. prop: 'passName',
  32. type: 1
  33. },
  34. {
  35. prop: 'credentialNum',
  36. type: 3
  37. },
  38. {
  39. prop: 'phoneNum',
  40. type: 2
  41. },
  42. {
  43. prop: 'guestName',
  44. type: 1
  45. },
  46. {
  47. prop: 'guestPhone',
  48. type: 2
  49. },
  50. {
  51. prop: 'touristName',
  52. type: 1
  53. },
  54. {
  55. prop: 'touristCode',
  56. type: 3
  57. },
  58. {
  59. prop: 'touristPhone',
  60. type: 2
  61. },
  62. {
  63. prop: 'landlinePhone',
  64. type: 2
  65. },
  66. {
  67. prop: 'linkName',
  68. type: 1
  69. },
  70. {
  71. prop: 'linkMobile',
  72. type: 2
  73. },
  74. {
  75. prop: 'idCode',
  76. type: 3
  77. },
  78. {
  79. prop: 'complainantName',
  80. type: 1
  81. },
  82. {
  83. prop: 'complainantPhone',
  84. type: 2
  85. },
  86. {
  87. prop: 'idCard',
  88. type: 3
  89. },
  90. {
  91. prop: 'guestName',
  92. type: 1
  93. },
  94. {
  95. prop: 'guestPhone',
  96. type: 2
  97. },
  98. {
  99. prop: 'checkInName',
  100. type: 1
  101. },
  102. {
  103. prop: 'checkInPhone',
  104. type: 2
  105. },
  106. {
  107. prop: 'legalPerson',
  108. type: 1
  109. },
  110. {
  111. prop: 'operatorCard',
  112. type: 3
  113. }
  114. ];
  115. // 不做脱敏处理的api集合(页面上需要编辑单独处理的)
  116. const apiList = [
  117. '/api/commonPerson/list',
  118. ]
  119. //解密脱敏处理
  120. function dealJmTmData(data, url) {
  121. if (!data) return;
  122. let keys = Object.keys(data);
  123. keys.forEach(k => {
  124. let _t = tmList.find(t => t.prop == k);
  125. if (_t) {
  126. data[k] = data[k] ? (decrypt(data[k]) || data[k]) : '';
  127. if (!apiList.includes(url)) data[k] = tmRules(data[k], _t.type);
  128. }
  129. if (typeof data[k] === 'object') {
  130. dealJmTmData(data[k], url);
  131. } else if (Array.isArray(data[k])) {
  132. data[k].forEach(dk => {
  133. if (typeof dk === 'object') {
  134. dealJmTmData(dk, url);
  135. }
  136. })
  137. }
  138. })
  139. }
  140. // 脱敏规则
  141. function tmRules(value, type) {
  142. if (!value) return;
  143. let res = '';
  144. if (type == 1) {
  145. let arr = Array.from(value)
  146. if (arr.length === 2) {
  147. res = arr[0] + '*'
  148. } else if (arr.length > 2) {
  149. for (let i = 1; i < arr.length - 1; i++) {
  150. arr[i] = '*'
  151. }
  152. res = arr.join("")
  153. } else {
  154. res = value
  155. }
  156. } else if (type == 2) {
  157. res = value.replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2");
  158. } else if (type == 3) {
  159. res = value.replace(/^(.{4})(?:\d+)(.{4})$/, "$1**********$2");
  160. }
  161. return res;
  162. }
  163. export const $http = (url, method, data, json, isAuth, isBuffer) => {
  164. let authorization = uni.getStorageSync('authorization') || 'Basic cmVucmVuaW86cmVucmVuaW8=';
  165. let access_token = uni.getStorageSync('access_token') || '';
  166. //设置请求前拦截器
  167. http.interceptor.request = (config) => {
  168. uni.showLoading({
  169. title: '加载中...'
  170. })
  171. config.header = {
  172. 'content-type': json ? 'application/json' : 'application/x-www-form-urlencoded',
  173. 'access_token': access_token
  174. }
  175. if (isAuth) config.header.authorization = authorization;
  176. if (isBuffer) config.responseType = 'arrayBuffer';
  177. }
  178. //设置请求结束后拦截器
  179. http.interceptor.response = async (response) => {
  180. //判断返回状态 执行相应操作
  181. uni.hideLoading()
  182. if ((response.data && response.data.code && response.data.code === 401) ||
  183. (response.data && response.data.msg && (response.data.msg.indexOf('未授权') > -1 ||
  184. response.data.msg.indexOf('重新登录') > -1))) {
  185. return uni.showModal({
  186. title: '温馨提示',
  187. content: '当前登录已失效,是否重新登录?',
  188. success: (res) => {
  189. if (res.confirm) {
  190. uni.clearStorageSync();
  191. // #ifdef APP-PLUS
  192. uni.reLaunch({
  193. url: '/pages/login/appIndex'
  194. })
  195. // #endif
  196. // #ifdef MP-WEIXIN
  197. uni.reLaunch({
  198. url: '/pages/login/index'
  199. })
  200. // #endif
  201. }
  202. }
  203. })
  204. }
  205. // 请根据后端规定的状态码判定
  206. if (response.data.code === 300) { //token失效
  207. // return response.data = await doRequest(response, url)//动态刷新token,并重新完成request请求
  208. } else {
  209. if (response.data.code == 10021 && response.data.msg) {
  210. uni.showToast({
  211. title: response.data.msg,
  212. icon: 'none',
  213. duration: 1500
  214. })
  215. }
  216. }
  217. //数据解密脱敏处理
  218. if (response.data && response.data.data) {
  219. dealJmTmData(response.data.data, url);
  220. }
  221. return response;
  222. }
  223. return http.request({
  224. method: method,
  225. url: url,
  226. dataType: 'json',
  227. data,
  228. })
  229. }
  230. async function login() {
  231. return new Promise(resolve => {
  232. uni.login({
  233. provider: 'weixin',
  234. success(loginRes) {
  235. resolve(loginRes.code)
  236. },
  237. fail() {}
  238. });
  239. })
  240. }
  241. function postJson(url, data, json = true, isAuth = true, isBuffer = false) {
  242. return $http(url, 'POST', data, json, isAuth, isBuffer)
  243. }
  244. function get(url, data, json = true, isAuth = true, isBuffer = false) {
  245. return $http(url, 'GET', data, json, isAuth, isBuffer)
  246. }
  247. function post(url, data, json = true, isAuth = true, isBuffer = false) {
  248. return $http(url, 'POST', data, json, isAuth, isBuffer)
  249. }
  250. function put(url, data, json = true, isAuth = true, isBuffer = false) {
  251. return $http(url, 'PUT', data, json, isAuth, isBuffer)
  252. }
  253. function del(url, data, json = true, isAuth = true, isBuffer = false) {
  254. return $http(url, 'DELETE', data, json, isAuth, isBuffer)
  255. }
  256. export default {
  257. postJson,
  258. get,
  259. post,
  260. put,
  261. del
  262. }