index.js 6.6 KB

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