index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. if (url.indexOf('/merchant/hotel/order/getOrderDetail/') > -1) {
  186. //房态入住人不脱敏不加密数据备份
  187. console.log(response.data.data)
  188. let checkInPersonList = response.data.data.detailFormList[0].checkInPersonList;
  189. if (checkInPersonList.length != 0) {
  190. checkInPersonList.forEach((ele, i) => {
  191. ele.checkInName2 = ele.checkInName;
  192. ele.idCard2 = ele.idCard;
  193. ele.checkInPhone2 = ele.checkInPhone;
  194. })
  195. }
  196. }
  197. let t = apiList.find(a => url.indexOf(a) > -1);
  198. if (!t) dealJmTmData(response.data.data, true);
  199. else dealJmTmData(response.data.data, false);
  200. }
  201. if ((response.data && response.data.code && response.data.code === 401) ||
  202. (response.data && response.data.msg && (response.data.msg.indexOf('未授权') > -1 ||
  203. response.data.msg.indexOf('重新登录') > -1))) {
  204. return uni.showModal({
  205. title: '温馨提示',
  206. content: '当前登录已失效,是否重新登录?',
  207. success: (res) => {
  208. if (res.confirm) {
  209. uni.clearStorageSync();
  210. // #ifdef APP-PLUS
  211. uni.reLaunch({
  212. url: '/pages/login/appIndex'
  213. })
  214. // #endif
  215. // #ifdef MP-WEIXIN
  216. uni.reLaunch({
  217. url: '/pages/login/index'
  218. })
  219. // #endif
  220. }
  221. }
  222. })
  223. }
  224. // 请根据后端规定的状态码判定
  225. if (response.data.code === 300) { //token失效
  226. // return response.data = await doRequest(response, url)//动态刷新token,并重新完成request请求
  227. } else {
  228. if (response.data.code == 10021 && response.data.msg) {
  229. uni.showToast({
  230. title: response.data.msg,
  231. icon: 'none',
  232. duration: 1500
  233. })
  234. }
  235. }
  236. return response;
  237. }
  238. return http.request({
  239. method: method,
  240. url: url,
  241. dataType: 'json',
  242. data,
  243. })
  244. }
  245. async function login() {
  246. return new Promise(resolve => {
  247. uni.login({
  248. provider: 'weixin',
  249. success(loginRes) {
  250. resolve(loginRes.code)
  251. },
  252. fail() {}
  253. });
  254. })
  255. }
  256. function postJson(url, data, json = true, isAuth = true, isBuffer = false) {
  257. return $http(url, 'POST', data, json, isAuth, isBuffer)
  258. }
  259. function get(url, data, json = true, isAuth = true, isBuffer = false) {
  260. return $http(url, 'GET', data, json, isAuth, isBuffer)
  261. }
  262. function post(url, data, json = true, isAuth = true, isBuffer = false) {
  263. return $http(url, 'POST', data, json, isAuth, isBuffer)
  264. }
  265. function put(url, data, json = true, isAuth = true, isBuffer = false) {
  266. return $http(url, 'PUT', data, json, isAuth, isBuffer)
  267. }
  268. function del(url, data, json = true, isAuth = true, isBuffer = false) {
  269. return $http(url, 'DELETE', data, json, isAuth, isBuffer)
  270. }
  271. export default {
  272. postJson,
  273. get,
  274. post,
  275. put,
  276. del
  277. }