index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. '/api/merchant/food/merchant'
  125. ]
  126. //解密脱敏处理
  127. function dealJmTmData(data, isTm) {
  128. if (!data) return;
  129. let keys = Object.keys(data);
  130. keys.forEach(k => {
  131. let _t = tmList.find(t => t.prop == k);
  132. if (_t) {
  133. data[k] = data[k] ? (decrypt(data[k]) || data[k]) : '';
  134. if (isTm) data[k] = tmRules(data[k], _t.type);
  135. }
  136. if (typeof data[k] === 'object') {
  137. dealJmTmData(data[k], isTm);
  138. } else if (Array.isArray(data[k])) {
  139. data[k].forEach(dk => {
  140. if (typeof dk === 'object') {
  141. dealJmTmData(dk, isTm);
  142. }
  143. })
  144. }
  145. })
  146. }
  147. //将null和undefined转为空字符串
  148. function nullToKongStr(data){
  149. if (!data) return;
  150. let keys = Object.keys(data);
  151. keys.forEach(k => {
  152. if(data[k]===null||data[k]===undefined) data[k] = "";
  153. if (typeof data[k] === 'object') {
  154. nullToKongStr(data[k]);
  155. } else if (Array.isArray(data[k])) {
  156. data[k].forEach(dk => {
  157. if (typeof dk === 'object') {
  158. nullToKongStr(dk);
  159. }
  160. })
  161. }
  162. })
  163. }
  164. // 脱敏规则
  165. function tmRules(value, type) {
  166. if (!value) return;
  167. let res = '';
  168. if (type == 1) {
  169. let arr = Array.from(value)
  170. if (arr.length === 2) {
  171. res = arr[0] + '*'
  172. } else if (arr.length > 2) {
  173. for (let i = 1; i < arr.length - 1; i++) {
  174. arr[i] = '*'
  175. }
  176. res = arr.join("")
  177. } else {
  178. res = value
  179. }
  180. } else if (type == 2) {
  181. res = value.replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2");
  182. } else if (type == 3) {
  183. res = value.replace(/^(.{4})(?:\d+)(.{4})$/, "$1**********$2");
  184. }
  185. return res;
  186. }
  187. export const $http = (url, method, data, json, isAuth, isBuffer) => {
  188. let authorization = uni.getStorageSync('authorization') || 'Basic cmluZ3psZV9tZXJjaGFudDpyaW5nemxlX21lcmNoYW50';
  189. let access_token = uni.getStorageSync('access_token') || '';
  190. //设置请求前拦截器
  191. http.interceptor.request = (config) => {
  192. uni.showLoading({
  193. title: '加载中...'
  194. })
  195. config.header = {
  196. 'content-type': json ? 'application/json' : 'application/x-www-form-urlencoded',
  197. 'access_token': access_token
  198. }
  199. if (isAuth) config.header.authorization = authorization;
  200. if (isBuffer) config.responseType = 'arrayBuffer';
  201. }
  202. //设置请求结束后拦截器
  203. http.interceptor.response = async (response) => {
  204. //判断返回状态 执行相应操作
  205. uni.hideLoading()
  206. //数据解密脱敏处理
  207. if (response.data && response.data.data) {
  208. nullToKongStr(response.data.data);
  209. if (url.indexOf('/merchant/hotel/order/getOrderDetail/') > -1) {
  210. //房态入住人不脱敏不加密数据备份
  211. let checkInPersonList = response.data.data.detailFormList[0].checkInPersonList;
  212. if (checkInPersonList.length != 0) {
  213. checkInPersonList.forEach((ele, i) => {
  214. ele.checkInName2 = ele.checkInName;
  215. ele.idCard2 = ele.idCard;
  216. ele.checkInPhone2 = ele.checkInPhone;
  217. })
  218. }
  219. }
  220. let t = apiList.find(a => url.indexOf(a) > -1);
  221. if (!t) dealJmTmData(response.data.data, true);
  222. else dealJmTmData(response.data.data, false);
  223. }
  224. if ((response.data && response.data.code && response.data.code === 401) ||
  225. (response.data && response.data.msg && (response.data.msg.indexOf('未授权') > -1 ||
  226. response.data.msg.indexOf('重新登录') > -1))) {
  227. return uni.showModal({
  228. title: '温馨提示',
  229. content: '当前登录已失效,是否重新登录?',
  230. success: (res) => {
  231. if (res.confirm) {
  232. uni.clearStorageSync();
  233. // #ifdef APP-PLUS
  234. uni.reLaunch({
  235. url: '/pages/login/appIndex'
  236. })
  237. // #endif
  238. // #ifdef MP-WEIXIN
  239. uni.reLaunch({
  240. url: '/pages/login/index'
  241. })
  242. // #endif
  243. }
  244. }
  245. })
  246. }
  247. // 请根据后端规定的状态码判定
  248. if (response.data.code === 300) { //token失效
  249. // return response.data = await doRequest(response, url)//动态刷新token,并重新完成request请求
  250. } else {
  251. if (response.data.code == 10021 && response.data.msg) {
  252. uni.showToast({
  253. title: response.data.msg,
  254. icon: 'none',
  255. duration: 1500
  256. })
  257. }
  258. }
  259. return response;
  260. }
  261. return http.request({
  262. method: method,
  263. url: url,
  264. dataType: 'json',
  265. data,
  266. })
  267. }
  268. async function login() {
  269. return new Promise(resolve => {
  270. uni.login({
  271. provider: 'weixin',
  272. success(loginRes) {
  273. resolve(loginRes.code)
  274. },
  275. fail() {}
  276. });
  277. })
  278. }
  279. function postJson(url, data, json = true, isAuth = true, isBuffer = false) {
  280. return $http(url, 'POST', data, json, isAuth, isBuffer)
  281. }
  282. function get(url, data, json = true, isAuth = true, isBuffer = false) {
  283. return $http(url, 'GET', data, json, isAuth, isBuffer)
  284. }
  285. function post(url, data, json = true, isAuth = true, isBuffer = false) {
  286. return $http(url, 'POST', data, json, isAuth, isBuffer)
  287. }
  288. function put(url, data, json = true, isAuth = true, isBuffer = false) {
  289. return $http(url, 'PUT', data, json, isAuth, isBuffer)
  290. }
  291. function del(url, data, json = true, isAuth = true, isBuffer = false) {
  292. return $http(url, 'DELETE', data, json, isAuth, isBuffer)
  293. }
  294. //检测文本和图像
  295. import { BaseApi } from './baseApi.js';
  296. const labelCfg = {
  297. 10001:'含有广告内容',
  298. 20001:'含有时政内容',
  299. 20002:'含有色情内容',
  300. 20003:'含有辱骂内容',
  301. 20006:'含有违法犯罪内容',
  302. 20008:'含有欺诈内容',
  303. 20012:'含有低俗内容',
  304. 20013:'含有版权内容',
  305. 21000:'含有其他违规内容'
  306. }
  307. async function detectionContent(content,type=1){
  308. if(type==1){
  309. if(!uni.getStorageSync('userInfo')){
  310. return uni.showToast({
  311. title:'请先进行登录',
  312. icon:'none',
  313. duration:1500
  314. })
  315. }
  316. return new Promise((resolve,reject)=>{
  317. wx.request({
  318. method: 'POST',
  319. url:`${BaseApi}/api/app/wx/secCheckMsg`,
  320. dataType:'json',
  321. data:{
  322. content,
  323. openId:uni.getStorageSync('userInfo').openId
  324. },
  325. success:res=>{
  326. let result = {};
  327. if(res.data.code===0){
  328. let errcode = res.data.data.errcode;
  329. result.code = errcode;
  330. if(errcode===0){
  331. let resu = res.data.data.result;
  332. if(resu.label===100){
  333. result.code = 0;
  334. result.msg = '系统繁忙,请稍后再试';
  335. }else{
  336. result.code = resu.label;
  337. result.msg = labelCfg[resu.label]||'含有其他违规内容';
  338. }
  339. }else{
  340. if(errcode==-1) result.msg = '系统繁忙,请稍后再试';
  341. else if(errcode==87014) result.msg = '内容包含敏感违规信息';
  342. else if(errcode==40003) result.msg = 'openid无效,请重新登录';
  343. else result.msg = '系统错误';
  344. }
  345. }
  346. return resolve(result)
  347. },
  348. fail: err => {
  349. return reject(err)
  350. }
  351. })
  352. })
  353. }else if(type==2){
  354. return new Promise((resolve,reject)=>{
  355. wx.uploadFile({
  356. method:'POST',
  357. url:`${BaseApi}/api/app/wx/secCheckImg`,
  358. filePath: content,
  359. name: 'file',
  360. header: {
  361. 'Content-Type': 'application/octet-stream'
  362. },
  363. formData:{
  364. media:content
  365. },
  366. success: res => {
  367. let data = JSON.parse(res.data);
  368. let result = {code:999,msg:'数据错误'};
  369. if(data&&data.code===0){
  370. let resu = JSON.parse(data.data);
  371. if(!resu) return;
  372. result.code = resu.errcode;
  373. if(resu.errcode==87014) result.msg = '图片含有敏感违规信息';
  374. else if(resu.errcode==40001) result.msg = 'token无效';
  375. else if(resu.errcode==40003) result.msg = 'openid无效';
  376. else if(resu.errcode==61010) result.msg = '用户访问记录超时';
  377. else{
  378. result.code = 0;
  379. result.msg = '内容正常';
  380. }
  381. }
  382. return resolve(result)
  383. },
  384. fail: err => {
  385. return reject(err)
  386. }
  387. });
  388. })
  389. }
  390. }
  391. export default {
  392. postJson,
  393. get,
  394. post,
  395. put,
  396. del,
  397. detectionContent
  398. }