index.js 5.2 KB

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