index.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. //检测文本和图像
  294. import { BaseApi } from './baseApi.js';
  295. const labelCfg = {
  296. 10001:'含有广告内容',
  297. 20001:'含有时政内容',
  298. 20002:'含有色情内容',
  299. 20003:'含有辱骂内容',
  300. 20006:'含有违法犯罪内容',
  301. 20008:'含有欺诈内容',
  302. 20012:'含有低俗内容',
  303. 20013:'含有版权内容',
  304. 21000:'含有其他违规内容'
  305. }
  306. async function detectionContent(content,type=1){
  307. if(type==1){
  308. if(!uni.getStorageSync('userInfo')){
  309. return uni.showToast({
  310. title:'请先进行登录',
  311. icon:'none',
  312. duration:1500
  313. })
  314. }
  315. return new Promise((resolve,reject)=>{
  316. wx.request({
  317. method: 'POST',
  318. url:`${BaseApi}/api/app/wx/secCheckMsg`,
  319. dataType:'json',
  320. data:{
  321. content,
  322. openId:JSON.parse(uni.getStorageSync('userInfo')).openId
  323. },
  324. success:res=>{
  325. let result = {};
  326. if(res.data.code===0){
  327. let errcode = res.data.data.errcode;
  328. result.code = errcode;
  329. if(errcode===0){
  330. let resu = res.data.data.result;
  331. if(resu.label===100){
  332. result.code = 0;
  333. result.msg = '系统繁忙,请稍后再试';
  334. }else{
  335. result.code = resu.label;
  336. result.msg = labelCfg[resu.label]||'含有其他违规内容';
  337. }
  338. }else{
  339. if(errcode==-1) result.msg = '系统繁忙,请稍后再试';
  340. else if(errcode==87014) result.msg = '内容包含敏感违规信息';
  341. else if(errcode==40003) result.msg = 'openid无效,请重新登录';
  342. else result.msg = '系统错误';
  343. }
  344. }
  345. return resolve(result)
  346. },
  347. fail: err => {
  348. return reject(err)
  349. }
  350. })
  351. })
  352. }else if(type==2){
  353. return new Promise((resolve,reject)=>{
  354. wx.uploadFile({
  355. method:'POST',
  356. url:`${BaseApi}/api/app/wx/secCheckImg`,
  357. filePath: content,
  358. name: 'file',
  359. header: {
  360. 'Content-Type': 'application/octet-stream'
  361. },
  362. formData:{
  363. media:content
  364. },
  365. success: res => {
  366. let data = JSON.parse(res.data);
  367. let result = {code:999,msg:'数据错误'};
  368. if(data&&data.code===0){
  369. let resu = JSON.parse(data.data);
  370. if(!resu) return;
  371. result.code = resu.errcode;
  372. if(resu.errcode==87014) result.msg = '图片含有敏感违规信息';
  373. else if(resu.errcode==40001) result.msg = 'token无效';
  374. else if(resu.errcode==40003) result.msg = 'openid无效';
  375. else if(resu.errcode==61010) result.msg = '用户访问记录超时';
  376. else{
  377. result.code = 0;
  378. result.msg = '内容正常';
  379. }
  380. }
  381. return resolve(result)
  382. },
  383. fail: err => {
  384. return reject(err)
  385. }
  386. });
  387. })
  388. }
  389. }
  390. export default {
  391. postJson,
  392. get,
  393. post,
  394. put,
  395. del,
  396. detectionContent
  397. }