index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import http from './interface'
  2. export const $http = (url, method, data, json, isloading=true) => {
  3. //设置请求前拦截器
  4. http.interceptor.request = (config) => {
  5. if(isloading){
  6. uni.showLoading({
  7. title:'加载中...'
  8. })
  9. }
  10. config.header = {
  11. 'content-type': json ? 'application/json' : 'application/x-www-form-urlencoded',
  12. "token": uni.getStorageSync('token'),
  13. "userId": uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).id:'',
  14. }
  15. }
  16. //设置请求结束后拦截器
  17. http.interceptor.response = async (response) => {
  18. //判断返回状态 执行相应操作
  19. if(isloading){
  20. uni.hideLoading()
  21. }
  22. if (response?.data?.code === 401 || response?.data?.msg?.indexOf('未授权') > -1 || response?.data?.msg?.indexOf('重新登录') > -1) {
  23. return uni.showModal({
  24. title: '温馨提示',
  25. content:'当前登录已失效,是否返回首页?',
  26. success: (res) => {
  27. if (res.confirm) {
  28. uni.clearStorageSync();
  29. uni.reLaunch({
  30. url:'/pages/home'
  31. })
  32. }
  33. }
  34. })
  35. }
  36. // 请根据后端规定的状态码判定
  37. if (response?.data?.code === 300) {//token失效
  38. // return response.data = await doRequest(response, url)//动态刷新token,并重新完成request请求
  39. }else{
  40. if(response?.data?.code==10021&&response?.data?.msg){
  41. uni.showToast({
  42. title:response?.data?.msg,
  43. icon:'none',
  44. duration:1500
  45. })
  46. }
  47. }
  48. return response;
  49. }
  50. return http.request({
  51. method: method,
  52. url: url,
  53. dataType: 'json',
  54. data,
  55. })
  56. }
  57. function postJson(url, data, isloading=true) {
  58. return $http(url, 'POST', data, isloading)
  59. }
  60. function get(url, data, isloading=true) {
  61. return $http(url, 'GET', data, true, isloading)
  62. }
  63. function post(url, data, isloading=true) {
  64. return $http(url, 'POST', data, true, isloading)
  65. }
  66. function put(url, data, isloading=true) {
  67. return $http(url, 'PUT', data, true, isloading)
  68. }
  69. function del(url, data, isloading=true) {
  70. return $http(url, 'DELETE', data, true, isloading)
  71. }
  72. //检测文本和图像
  73. import { BaseApi } from './baseApi.js';
  74. const labelCfg = {
  75. 10001:'含有广告内容',
  76. 20001:'含有时政内容',
  77. 20002:'含有色情内容',
  78. 20003:'含有辱骂内容',
  79. 20006:'含有违法犯罪内容',
  80. 20008:'含有欺诈内容',
  81. 20012:'含有低俗内容',
  82. 20013:'含有版权内容',
  83. 21000:'含有其他违规内容'
  84. }
  85. async function detectionContent(content,type=1){
  86. if(type==1){
  87. if(!uni.getStorageSync('userInfo')){
  88. return uni.showToast({
  89. title:'请先进行登录',
  90. icon:'none',
  91. duration:1500
  92. })
  93. }
  94. return new Promise((resolve,reject)=>{
  95. wx.request({
  96. method: 'POST',
  97. url:`${BaseApi}/wx/secCheckMsg`,
  98. dataType:'json',
  99. data:{
  100. content:encodeURIComponent(content),
  101. openId:JSON.parse(uni.getStorageSync('userInfo')).openId
  102. },
  103. success:res=>{
  104. let result = {};
  105. if(res.data.code===0){
  106. let errcode = res.data.data.errcode;
  107. result.code = errcode;
  108. if(errcode===0){
  109. let resu = res.data.data.result;
  110. if(resu.label===100){
  111. result.code = 0;
  112. result.msg = '系统繁忙,请稍后再试';
  113. }else{
  114. result.code = resu.label;
  115. result.msg = labelCfg[resu.label]||'含有其他违规内容';
  116. }
  117. }else{
  118. if(errcode==-1) result.msg = '系统繁忙,请稍后再试';
  119. else if(errcode==87014) result.msg = '内容包含敏感违规信息';
  120. else if(errcode==40003) result.msg = 'openid无效,请重新登录';
  121. else result.msg = '系统错误';
  122. }
  123. }
  124. return resolve(result)
  125. },
  126. fail: err => {
  127. return reject(err)
  128. }
  129. })
  130. })
  131. }else if(type==2){
  132. return new Promise((resolve,reject)=>{
  133. wx.uploadFile({
  134. method:'POST',
  135. url:`${BaseApi}/wx/secCheckImg`,
  136. filePath: content,
  137. name: 'file',
  138. header: {
  139. 'Content-Type': 'application/octet-stream'
  140. },
  141. formData:{
  142. media:content
  143. },
  144. success: res => {
  145. let data = JSON.parse(res.data);
  146. let result = {code:999,msg:'数据错误'};
  147. if(data&&data.code===0){
  148. let resu = JSON.parse(data.data);
  149. if(!resu) return;
  150. result.code = resu.errcode;
  151. if(resu.errcode==87014) result.msg = '图片含有敏感违规信息';
  152. else if(resu.errcode==40001) result.msg = 'token无效';
  153. else if(resu.errcode==40003) result.msg = 'openid无效';
  154. else if(resu.errcode==61010) result.msg = '用户访问记录超时';
  155. else{
  156. result.code = 0;
  157. result.msg = '内容正常';
  158. }
  159. }
  160. return resolve(result)
  161. },
  162. fail: err => {
  163. return reject(err)
  164. }
  165. });
  166. })
  167. }
  168. }
  169. export default {
  170. postJson,
  171. get,
  172. post,
  173. put,
  174. del,
  175. detectionContent
  176. }