123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- import http from './interface'
- import {
- decrypt
- } from '../utils/aes.js'
- //解密脱敏字段集合 type(姓名 1、手机号 2、身份证 3)
- const tmList = [{
- prop: 'card',
- type: 3
- },
- {
- prop: 'phone',
- type: 2
- },
- {
- prop: 'passengerName',
- type: 1
- },
- {
- prop: 'contactNum',
- type: 2
- },
- {
- prop: 'linkPerson',
- type: 1
- },
- {
- prop: 'linkPhone',
- type: 2
- },
- {
- prop: 'passName',
- type: 1
- },
- {
- prop: 'credentialNum',
- type: 3
- },
- {
- prop: 'phoneNum',
- type: 2
- },
- {
- prop: 'guestName',
- type: 1
- },
- {
- prop: 'guestPhone',
- type: 2
- },
- {
- prop: 'touristName',
- type: 1
- },
- {
- prop: 'touristCode',
- type: 3
- },
- {
- prop: 'touristPhone',
- type: 2
- },
- {
- prop: 'landlinePhone',
- type: 2
- },
- {
- prop: 'linkName',
- type: 1
- },
- {
- prop: 'linkMobile',
- type: 2
- },
- {
- prop: 'idCode',
- type: 3
- },
- {
- prop: 'complainantName',
- type: 1
- },
- {
- prop: 'complainantPhone',
- type: 2
- },
- {
- prop: 'idCard',
- type: 3
- },
- {
- prop: 'guestName',
- type: 1
- },
- {
- prop: 'guestPhone',
- type: 2
- },
- {
- prop: 'checkInName',
- type: 1
- },
- {
- prop: 'checkInPhone',
- type: 2
- },
- {
- prop: 'legalPerson',
- type: 1
- },
- {
- prop: 'operatorCard',
- type: 3
- }
- ];
- // 不做脱敏处理的api集合(页面上需要编辑单独处理的)
- const apiList = [
- '/api/commonPerson/list',
- '/scenic/api/order/scanCode'
- ]
- //解密脱敏处理
- function dealJmTmData(data, isTm) {
- if(!data) return;
- let keys = Object.keys(data);
- keys.forEach(k=>{
- let _t = tmList.find(t=>t.prop==k);
- if(_t){
- data[k] = data[k]?(decrypt(data[k])||data[k]):'';
- if(isTm) data[k] = tmRules(data[k],_t.type);
- }
-
- if(typeof data[k] === 'object'){
- dealJmTmData(data[k],isTm);
- }else if(Array.isArray(data[k])){
- data[k].forEach(dk=>{
- if(typeof dk === 'object'){
- dealJmTmData(dk,isTm);
- }
- })
- }
- })
- }
- // 脱敏规则
- function tmRules(value, type) {
- if (!value) return;
- let res = '';
- if (type == 1) {
- let arr = Array.from(value)
- if (arr.length === 2) {
- res = arr[0] + '*'
- } else if (arr.length > 2) {
- for (let i = 1; i < arr.length - 1; i++) {
- arr[i] = '*'
- }
- res = arr.join("")
- } else {
- res = value
- }
- } else if (type == 2) {
- res = value.replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2");
- } else if (type == 3) {
- res = value.replace(/^(.{4})(?:\d+)(.{4})$/, "$1**********$2");
- }
- return res;
- }
- export const $http = (url, method, data, json, isAuth, isBuffer) => {
- let authorization = uni.getStorageSync('authorization') || 'Basic cmVucmVuaW86cmVucmVuaW8=';
- let access_token = uni.getStorageSync('access_token') || '';
- //设置请求前拦截器
- http.interceptor.request = (config) => {
- uni.showLoading({
- title: '加载中...'
- })
- config.header = {
- 'content-type': json ? 'application/json' : 'application/x-www-form-urlencoded',
- 'access_token': access_token
- }
- if (isAuth) config.header.authorization = authorization;
- if (isBuffer) config.responseType = 'arrayBuffer';
- }
- //设置请求结束后拦截器
- http.interceptor.response = async (response) => {
- //判断返回状态 执行相应操作
- uni.hideLoading()
- //数据解密脱敏处理
- if(response.data&&response.data.data){
- let t = apiList.find(a=>url.indexOf(a)>-1);
- if(!t) dealJmTmData(response.data.data,true);
- else dealJmTmData(response.data.data,false);
- }
-
- if ((response.data && response.data.code && response.data.code === 401) ||
- (response.data && response.data.msg && (response.data.msg.indexOf('未授权') > -1 ||
- response.data.msg.indexOf('重新登录') > -1))) {
- return uni.showModal({
- title: '温馨提示',
- content: '当前登录已失效,是否重新登录?',
- success: (res) => {
- if (res.confirm) {
- uni.clearStorageSync();
- // #ifdef APP-PLUS
- uni.reLaunch({
- url: '/pages/login/appIndex'
- })
- // #endif
- // #ifdef MP-WEIXIN
- uni.reLaunch({
- url: '/pages/login/index'
- })
- // #endif
- }
- }
- })
- }
- // 请根据后端规定的状态码判定
- if (response.data.code === 300) { //token失效
- // return response.data = await doRequest(response, url)//动态刷新token,并重新完成request请求
- } else {
- if (response.data.code == 10021 && response.data.msg) {
- uni.showToast({
- title: response.data.msg,
- icon: 'none',
- duration: 1500
- })
- }
- }
-
- return response;
- }
- return http.request({
- method: method,
- url: url,
- dataType: 'json',
- data,
- })
- }
- async function login() {
- return new Promise(resolve => {
- uni.login({
- provider: 'weixin',
- success(loginRes) {
- resolve(loginRes.code)
- },
- fail() {}
- });
- })
- }
- function postJson(url, data, json = true, isAuth = true, isBuffer = false) {
- return $http(url, 'POST', data, json, isAuth, isBuffer)
- }
- function get(url, data, json = true, isAuth = true, isBuffer = false) {
- return $http(url, 'GET', data, json, isAuth, isBuffer)
- }
- function post(url, data, json = true, isAuth = true, isBuffer = false) {
- return $http(url, 'POST', data, json, isAuth, isBuffer)
- }
- function put(url, data, json = true, isAuth = true, isBuffer = false) {
- return $http(url, 'PUT', data, json, isAuth, isBuffer)
- }
- function del(url, data, json = true, isAuth = true, isBuffer = false) {
- return $http(url, 'DELETE', data, json, isAuth, isBuffer)
- }
- export default {
- postJson,
- get,
- post,
- put,
- del
- }
|