index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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, isTm) {
  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(isTm) data[k] = tmRules(data[k],_t.type);
  128. }
  129. if(typeof data[k] === 'object'){
  130. dealJmTmData(data[k],isTm);
  131. }else if(Array.isArray(data[k])){
  132. data[k].forEach(dk=>{
  133. if(typeof dk === 'object'){
  134. dealJmTmData(dk,isTm);
  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. //数据解密脱敏处理
  183. if(response.data&&response.data.data){
  184. let t = apiList.find(a=>url.indexOf(a)>-1);
  185. if(!t) dealJmTmData(response.data.data,true);
  186. else dealJmTmData(response.data.data,false);
  187. }
  188. if ((response.data && response.data.code && response.data.code === 401) ||
  189. (response.data && response.data.msg && (response.data.msg.indexOf('未授权') > -1 ||
  190. response.data.msg.indexOf('重新登录') > -1))) {
  191. return uni.showModal({
  192. title: '温馨提示',
  193. content: '当前登录已失效,是否重新登录?',
  194. success: (res) => {
  195. if (res.confirm) {
  196. uni.clearStorageSync();
  197. // #ifdef APP-PLUS
  198. uni.reLaunch({
  199. url: '/pages/login/appIndex'
  200. })
  201. // #endif
  202. // #ifdef MP-WEIXIN
  203. uni.reLaunch({
  204. url: '/pages/login/index'
  205. })
  206. // #endif
  207. }
  208. }
  209. })
  210. }
  211. // 请根据后端规定的状态码判定
  212. if (response.data.code === 300) { //token失效
  213. // return response.data = await doRequest(response, url)//动态刷新token,并重新完成request请求
  214. } else {
  215. if (response.data.code == 10021 && response.data.msg) {
  216. uni.showToast({
  217. title: response.data.msg,
  218. icon: 'none',
  219. duration: 1500
  220. })
  221. }
  222. }
  223. return response;
  224. }
  225. return http.request({
  226. method: method,
  227. url: url,
  228. dataType: 'json',
  229. data,
  230. })
  231. }
  232. async function login() {
  233. return new Promise(resolve => {
  234. uni.login({
  235. provider: 'weixin',
  236. success(loginRes) {
  237. resolve(loginRes.code)
  238. },
  239. fail() {}
  240. });
  241. })
  242. }
  243. function postJson(url, data, json = true, isAuth = true, isBuffer = false) {
  244. return $http(url, 'POST', data, json, isAuth, isBuffer)
  245. }
  246. function get(url, data, json = true, isAuth = true, isBuffer = false) {
  247. return $http(url, 'GET', data, json, isAuth, isBuffer)
  248. }
  249. function post(url, data, json = true, isAuth = true, isBuffer = false) {
  250. return $http(url, 'POST', data, json, isAuth, isBuffer)
  251. }
  252. function put(url, data, json = true, isAuth = true, isBuffer = false) {
  253. return $http(url, 'PUT', data, json, isAuth, isBuffer)
  254. }
  255. function del(url, data, json = true, isAuth = true, isBuffer = false) {
  256. return $http(url, 'DELETE', data, json, isAuth, isBuffer)
  257. }
  258. export default {
  259. postJson,
  260. get,
  261. post,
  262. put,
  263. del
  264. }