index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. '/scenic/api/order/scanCode'
  119. ]
  120. //解密脱敏处理
  121. function dealJmTmData(data, isTm) {
  122. if(!data) return;
  123. let keys = Object.keys(data);
  124. keys.forEach(k=>{
  125. let _t = tmList.find(t=>t.prop==k);
  126. if(_t){
  127. data[k] = data[k]?(decrypt(data[k])||data[k]):'';
  128. if(isTm) data[k] = tmRules(data[k],_t.type);
  129. }
  130. if(typeof data[k] === 'object'){
  131. dealJmTmData(data[k],isTm);
  132. }else if(Array.isArray(data[k])){
  133. data[k].forEach(dk=>{
  134. if(typeof dk === 'object'){
  135. dealJmTmData(dk,isTm);
  136. }
  137. })
  138. }
  139. })
  140. }
  141. // 脱敏规则
  142. function tmRules(value, type) {
  143. if (!value) return;
  144. let res = '';
  145. if (type == 1) {
  146. let arr = Array.from(value)
  147. if (arr.length === 2) {
  148. res = arr[0] + '*'
  149. } else if (arr.length > 2) {
  150. for (let i = 1; i < arr.length - 1; i++) {
  151. arr[i] = '*'
  152. }
  153. res = arr.join("")
  154. } else {
  155. res = value
  156. }
  157. } else if (type == 2) {
  158. res = value.replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2");
  159. } else if (type == 3) {
  160. res = value.replace(/^(.{4})(?:\d+)(.{4})$/, "$1**********$2");
  161. }
  162. return res;
  163. }
  164. export const $http = (url, method, data, json, isAuth, isBuffer) => {
  165. let authorization = uni.getStorageSync('authorization') || 'Basic cmVucmVuaW86cmVucmVuaW8=';
  166. let access_token = uni.getStorageSync('access_token') || '';
  167. //设置请求前拦截器
  168. http.interceptor.request = (config) => {
  169. uni.showLoading({
  170. title: '加载中...'
  171. })
  172. config.header = {
  173. 'content-type': json ? 'application/json' : 'application/x-www-form-urlencoded',
  174. 'access_token': access_token
  175. }
  176. if (isAuth) config.header.authorization = authorization;
  177. if (isBuffer) config.responseType = 'arrayBuffer';
  178. }
  179. //设置请求结束后拦截器
  180. http.interceptor.response = async (response) => {
  181. //判断返回状态 执行相应操作
  182. uni.hideLoading()
  183. //数据解密脱敏处理
  184. if(response.data&&response.data.data){
  185. let t = apiList.find(a=>url.indexOf(a)>-1);
  186. if(!t) dealJmTmData(response.data.data,true);
  187. else dealJmTmData(response.data.data,false);
  188. }
  189. if ((response.data && response.data.code && response.data.code === 401) ||
  190. (response.data && response.data.msg && (response.data.msg.indexOf('未授权') > -1 ||
  191. response.data.msg.indexOf('重新登录') > -1))) {
  192. return uni.showModal({
  193. title: '温馨提示',
  194. content: '当前登录已失效,是否重新登录?',
  195. success: (res) => {
  196. if (res.confirm) {
  197. uni.clearStorageSync();
  198. // #ifdef APP-PLUS
  199. uni.reLaunch({
  200. url: '/pages/login/appIndex'
  201. })
  202. // #endif
  203. // #ifdef MP-WEIXIN
  204. uni.reLaunch({
  205. url: '/pages/login/index'
  206. })
  207. // #endif
  208. }
  209. }
  210. })
  211. }
  212. // 请根据后端规定的状态码判定
  213. if (response.data.code === 300) { //token失效
  214. // return response.data = await doRequest(response, url)//动态刷新token,并重新完成request请求
  215. } else {
  216. if (response.data.code == 10021 && response.data.msg) {
  217. uni.showToast({
  218. title: response.data.msg,
  219. icon: 'none',
  220. duration: 1500
  221. })
  222. }
  223. }
  224. return response;
  225. }
  226. return http.request({
  227. method: method,
  228. url: url,
  229. dataType: 'json',
  230. data,
  231. })
  232. }
  233. async function login() {
  234. return new Promise(resolve => {
  235. uni.login({
  236. provider: 'weixin',
  237. success(loginRes) {
  238. resolve(loginRes.code)
  239. },
  240. fail() {}
  241. });
  242. })
  243. }
  244. function postJson(url, data, json = true, isAuth = true, isBuffer = false) {
  245. return $http(url, 'POST', data, json, isAuth, isBuffer)
  246. }
  247. function get(url, data, json = true, isAuth = true, isBuffer = false) {
  248. return $http(url, 'GET', data, json, isAuth, isBuffer)
  249. }
  250. function post(url, data, json = true, isAuth = true, isBuffer = false) {
  251. return $http(url, 'POST', data, json, isAuth, isBuffer)
  252. }
  253. function put(url, data, json = true, isAuth = true, isBuffer = false) {
  254. return $http(url, 'PUT', data, json, isAuth, isBuffer)
  255. }
  256. function del(url, data, json = true, isAuth = true, isBuffer = false) {
  257. return $http(url, 'DELETE', data, json, isAuth, isBuffer)
  258. }
  259. export default {
  260. postJson,
  261. get,
  262. post,
  263. put,
  264. del
  265. }