import http from './interface' export const $http = (url, method, data, json, isloading=true) => { //设置请求前拦截器 http.interceptor.request = (config) => { if(isloading){ uni.showLoading({ title:'加载中...' }) } config.header = { 'content-type': json ? 'application/json' : 'application/x-www-form-urlencoded', "token": uni.getStorageSync('token'), "userId": uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).id:'', } } //设置请求结束后拦截器 http.interceptor.response = async (response) => { //判断返回状态 执行相应操作 if(isloading){ uni.hideLoading() } if (response?.data?.code === 401 || response?.data?.msg?.indexOf('未授权') > -1 || response?.data?.msg?.indexOf('重新登录') > -1) { return uni.showModal({ title: '温馨提示', content:'当前登录已失效,是否返回首页?', success: (res) => { if (res.confirm) { uni.clearStorageSync(); uni.reLaunch({ url:'/pages/home' }) } } }) } // 请根据后端规定的状态码判定 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, }) } function postJson(url, data, isloading=true) { return $http(url, 'POST', data, isloading) } function get(url, data, isloading=true) { return $http(url, 'GET', data, true, isloading) } function post(url, data, isloading=true) { return $http(url, 'POST', data, true, isloading) } function put(url, data, isloading=true) { return $http(url, 'PUT', data, true, isloading) } function del(url, data, isloading=true) { return $http(url, 'DELETE', data, true, isloading) } //检测文本和图像 import { BaseApi } from './baseApi.js'; const labelCfg = { 10001:'含有广告内容', 20001:'含有时政内容', 20002:'含有色情内容', 20003:'含有辱骂内容', 20006:'含有违法犯罪内容', 20008:'含有欺诈内容', 20012:'含有低俗内容', 20013:'含有版权内容', 21000:'含有其他违规内容' } async function detectionContent(content,type=1){ if(type==1){ if(!uni.getStorageSync('userInfo')){ return uni.showToast({ title:'请先进行登录', icon:'none', duration:1500 }) } return new Promise((resolve,reject)=>{ wx.request({ method: 'POST', url:`${BaseApi}/wx/secCheckMsg`, dataType:'json', data:{ content:encodeURIComponent(content), openId:JSON.parse(uni.getStorageSync('userInfo')).openId }, success:res=>{ let result = {}; if(res.data.code===0){ let errcode = res.data.data.errcode; result.code = errcode; if(errcode===0){ let resu = res.data.data.result; if(resu.label===100){ result.code = 0; result.msg = '系统繁忙,请稍后再试'; }else{ result.code = resu.label; result.msg = labelCfg[resu.label]||'含有其他违规内容'; } }else{ if(errcode==-1) result.msg = '系统繁忙,请稍后再试'; else if(errcode==87014) result.msg = '内容包含敏感违规信息'; else if(errcode==40003) result.msg = 'openid无效,请重新登录'; else result.msg = '系统错误'; } } return resolve(result) }, fail: err => { return reject(err) } }) }) }else if(type==2){ return new Promise((resolve,reject)=>{ wx.uploadFile({ method:'POST', url:`${BaseApi}/wx/secCheckImg`, filePath: content, name: 'file', header: { 'Content-Type': 'application/octet-stream' }, formData:{ media:content }, success: res => { let data = JSON.parse(res.data); let result = {code:999,msg:'数据错误'}; if(data&&data.code===0){ let resu = JSON.parse(data.data); if(!resu) return; result.code = resu.errcode; if(resu.errcode==87014) result.msg = '图片含有敏感违规信息'; else if(resu.errcode==40001) result.msg = 'token无效'; else if(resu.errcode==40003) result.msg = 'openid无效'; else if(resu.errcode==61010) result.msg = '用户访问记录超时'; else{ result.code = 0; result.msg = '内容正常'; } } return resolve(result) }, fail: err => { return reject(err) } }); }) } } export default { postJson, get, post, put, del, detectionContent }