index.js 6.7 KB

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