request.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import unirequest from '../plugins/uni_request.js'
  2. import store from "../store";
  3. // import { autoAuth } from '@/utils/common.js';
  4. import md5 from 'js-md5';
  5. import {
  6. uniShowToast,
  7. successToast,
  8. loadingToast,
  9. uniHideToast,
  10. uniHideLoading,
  11. uniShowModal
  12. } from './uni_api'
  13. import {
  14. VUE_APP_URL
  15. } from '../config.js'
  16. // 初始化自定义参数 login 是否需要登录权限 cancel 是否取消上次请求
  17. const defaultOptions = {
  18. login: true,
  19. auth: false,
  20. cancel: false,
  21. loading: true, //是否显示 请求加载中
  22. warn: true //是否异常弹窗提示
  23. };
  24. // 初始化请求
  25. let instance = unirequest.create({
  26. baseURL: VUE_APP_URL,
  27. timeout: 100000,
  28. debug: true
  29. });
  30. let setCancel;
  31. // 请求拦截器
  32. instance.interceptors.request.use(
  33. config => {
  34. var token = store.state.token;//'7221b77a-899b-492b-8263-9507f5c470e9';// store.state.token
  35. console.error('token'+token);
  36. //console.log("frontlixiangsid:"+token);
  37. config.header['frontlixiangsid']= token;//登录token 标志id
  38. config.header['platType']= 'slb';
  39. config.header['mpType'] = 'engineer';
  40. config.header['appType'] = 'ma';
  41. config.cancelToken = new unirequest.CancelToken(function executor(c) {
  42. setCancel = c; //记录当前请求
  43. });
  44. config.timeStamp= new Date().getTime();
  45. var jsToken ="";//防重复提交
  46. jsToken = md5(JSON.stringify(config))
  47. console.log('jsToken' + jsToken);
  48. console.log(config);
  49. config.data['tokenId'] = jsToken;
  50. return config
  51. })
  52. // 响应拦截器
  53. instance.interceptors.response.use(
  54. response => {
  55. // console.log('响应成功', response)
  56. return response
  57. })
  58. // 读取cookie
  59. function getCookie(cookie,c_name) {
  60. if (cookie && cookie.length > 0){
  61. let c_start = cookie.indexOf(c_name + "=")
  62. if (c_start != -1){
  63. c_start = c_start + c_name.length + 1
  64. let c_end = cookie.indexOf(";", c_start)
  65. if (c_end == -1)
  66. c_end = cookie.length
  67. return unescape(cookie.substring(c_start, c_end))
  68. }
  69. }
  70. return ""
  71. }
  72. // 请求主体
  73. function baseRequest(config, options) {
  74. const {
  75. login,
  76. auth,
  77. cancel,
  78. loading = true,
  79. warn
  80. } = options;
  81. if (options.cancel) {
  82. cancelRequest()
  83. }
  84. loadingStatus(loading, true)
  85. if (login && !store.state.token) {
  86. loadingStatus(loading, false);
  87. // autoAuth();
  88. }
  89. return instance.request(config).then(res => {
  90. if(config.url==='/slbMpAutoLogin'||config.url==='/slbSmLogin'){
  91. try{
  92. var cookie = res.header["set-cookie"]||res.header["Set-Cookie"];
  93. let frontlixiangsid = getCookie(cookie,"frontlixiangsid")||res.resultMap.frontlixiangsid;
  94. console.log('cookie',frontlixiangsid);
  95. if(frontlixiangsid&&frontlixiangsid!='deleteMe'){
  96. // uni.setStorageSync('frontlixiangsid',frontlixiangsid)
  97. store.state.token = frontlixiangsid;
  98. }
  99. }catch(err){
  100. console.log(err);
  101. }
  102. }
  103. //console.log(config);
  104. loadingStatus(loading, false)
  105. //console.log(res);
  106. if (res.statusCode) {
  107. let data = res.data;
  108. console.log('instance.request',res);
  109. console.log('instance.request data',data);
  110. if (res.statusCode === 200) {
  111. console.log('instance.request data 1',data);
  112. if (data.state == 1) {
  113. return Promise.resolve(data);
  114. }
  115. else if(data.msg&&(data.msg=='请勿频繁操作'||data.msg.indexOf('不能再次使用')>-1)){
  116. uniShowToast(data.msg);
  117. return;
  118. }
  119. else if(data._redirect||data.msg=='未登录'){
  120. uni.removeStorageSync('userNo');
  121. // if(uni.getStorageSync('userNo')){
  122. // uniShowModal('', '账号过期,重新登录?', {
  123. // showCancel: true,
  124. // success: (res) => {
  125. // if(res.confirm){
  126. // //添加跳转 微信绑定公众号页面
  127. // uni.redirectTo({
  128. // url: '/pages/login/login'
  129. // });
  130. // }else{
  131. // return Promise.resolve(data);
  132. // }
  133. // }
  134. // })
  135. // }
  136. // store.commit("LOGOUT");
  137. // uni.reLaunch({
  138. // url: '/pages/index/auth'
  139. // });
  140. return Promise.resolve(data);
  141. }else if (data.state == -1) {
  142. return Promise.resolve(data);
  143. }
  144. else if(data=='' || data=='success'|| data._redirect){
  145. console.log('instance.request data 2',data);
  146. // store.commit("LOGOUT");
  147. //未登录
  148. if (!store.state.isGOAuth) {
  149. store.commit("SET_GO_AUTH", true);
  150. //判断是否是第一次加载第一次加载未登录不用弹框提醒
  151. if(store.state.isFirstLoad){
  152. store.state.isFirstLoad=false;
  153. uni.redirectTo({
  154. url: '/pages/Login/Login'
  155. });
  156. return;
  157. }else{
  158. store.state.isFirstLoad=false;
  159. uniShowModal('', '账号过期重新登录认证', {
  160. showCancel: false,
  161. success: (res) => {
  162. //添加跳转 微信绑定公众号页面
  163. uni.redirectTo({
  164. url: '/pages/auth/auth'
  165. });
  166. }
  167. })
  168. return;
  169. }
  170. } else {
  171. console.log('路由已存在,不再重复跳转')
  172. return;
  173. }
  174. data.msg='未登录';
  175. return Promise.reject(data);
  176. } else {
  177. console.log('instance.request data 3',data);
  178. //data.state == -301||
  179. if(data.msg == undefined){
  180. if(uni.getStorageSync("loginFlag")){
  181. uni.redirectTo({
  182. url: "/pages/Login/Login"
  183. });
  184. }
  185. return;
  186. }
  187. if (warn) {
  188. uni.redirectTo({
  189. url: '/pages/error/error?code='+data.state+'&msg='+data.msg
  190. });
  191. return;
  192. }
  193. return Promise.resolve(data);
  194. }
  195. return Promise.resolve(res.data);
  196. } else {
  197. if (warn) {
  198. responseError({
  199. code: res.statusCode
  200. })
  201. }
  202. return Promise.reject(res.data);
  203. }
  204. } else {
  205. return Promise.reject(res.data);
  206. }
  207. loadingStatus(loading, false);
  208. }).catch(err => {
  209. loadingStatus(loading, false)
  210. // if (warn) {
  211. uniShowToast(err)
  212. // }
  213. return Promise.reject(err);
  214. })
  215. }
  216. function cancelRequest() {
  217. if (typeof setCancel === 'function') {
  218. setCancel('取消请求')
  219. }
  220. loadingStatus(loading, false);
  221. }
  222. // 定义请求提示
  223. const loadingStatus = (loading, type) => {
  224. if (loading) {
  225. if (type) {
  226. // loadingToast('正在请求中');
  227. setTimeout(function () {
  228. uniHideLoading();
  229. }, 5000);//防水异常加载不关闭影响操作
  230. } else {
  231. uniHideLoading();
  232. }
  233. } else {
  234. return
  235. }
  236. }
  237. // 请求返回statusCode
  238. const responseError = ({
  239. code
  240. }) => {
  241. let content = '';
  242. switch (code) {
  243. case 400:
  244. content = '请求参数错误';
  245. break
  246. case 401:
  247. content = '未授权,请重新登录-401';
  248. break
  249. case 403:
  250. content = '没有访问权限-403';
  251. break
  252. case 404:
  253. content = '请求错误,未找到该资源-404';
  254. break
  255. case 405:
  256. content = '请求方式错误-405';
  257. break
  258. case 408:
  259. content = '请求超时';
  260. break
  261. case 500:
  262. content = '服务器异常-500';
  263. break
  264. case 501:
  265. content = '服务未实现';
  266. break
  267. case 502:
  268. content = '网关错误';
  269. break
  270. case 503:
  271. content = '服务不可用';
  272. break
  273. case 504:
  274. content = '网关超时';
  275. break
  276. case 505:
  277. content = 'HTTP版本不受支持';
  278. break
  279. default:
  280. content = '请求异常-' + code;
  281. break
  282. }
  283. uniShowModal('', content, {
  284. showCancel: false,
  285. success: (res) => {
  286. console.log(res)
  287. }
  288. })
  289. }
  290. const request = ['get', 'post', 'put', 'delete', 'connect', 'head', 'options', 'trace'].reduce((request, method) => {
  291. request[method] = (url, data, options = {}) => baseRequest({
  292. url,
  293. method,
  294. data
  295. }, { ...defaultOptions,
  296. ...options
  297. });
  298. return request;
  299. }, {});
  300. export default request;