request.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import store from '@/store'
  2. import { getToken } from '@/utils/auth'
  3. import {server_url} from '@/utils/config.js'
  4. import Vue from 'vue'
  5. import * as dd from 'dingtalk-jsapi'; // 此方式为整体加载,也可按需进行加载
  6. let timeout = 30000
  7. function service(options = {}) {
  8. options.timeout = options.timeout || timeout,
  9. options.url = `${server_url}${options.url}`;
  10. options.data = options.params || options.data;
  11. options.dataType = 'json';
  12. options.header = {
  13. 'Authorization': 'Bearer ' + getToken()
  14. };
  15. let method = options.method.toUpperCase()
  16. if(method == 'PUT') {
  17. options.header['content-type'] = 'application/x-www-form-urlencoded'
  18. }
  19. return new Promise((resolved, rejected) => {
  20. options.success = (res) => {
  21. // console.log(res,'res')
  22. let code = Number(res.data.code);
  23. // Do something with response data
  24. switch (code) {
  25. case 0:
  26. resolved(res.data);
  27. break;
  28. case 401:
  29. if(Vue.prototype.hasHostPlat(["dingH5"]) && Vue.prototype.hasPlat(["enterprises"]) && dd.env.platform != "notInDingTalk") {
  30. let corpId = uni.getStorageSync('corpId')
  31. if(corpId) {
  32. // console.log('test111')
  33. store.dispatch('LogOut').then(res => {
  34. getApp().handleDingLogin({corpId})
  35. })
  36. }
  37. } else if(Vue.prototype.hasHostPlat(["dingH5Ins"]) && Vue.prototype.hasPlat(["enterprises"]) && dd.env.platform != "notInDingTalk") {
  38. let corpId = uni.getStorageSync('corpId')
  39. if(corpId) {
  40. // console.log('test111')
  41. store.dispatch('LogOut').then(res => {
  42. getApp().handleDingInsLogin({corpId})
  43. })
  44. }
  45. } else {
  46. uni.showModal({
  47. title: '提示',
  48. content: '登录状态已过期,您可以继续留在该页面,或者重新登录?',
  49. cancelText: '取消',
  50. confirmText: '确定',
  51. success: function(res) {
  52. if (res.confirm) {
  53. store.dispatch('LogOut').then(res => {
  54. uni.reLaunch({ url: '/pages/login/login' })
  55. })
  56. }
  57. }
  58. })
  59. }
  60. rejected(res.data.msg);
  61. break;
  62. default:
  63. uni.showToast({
  64. icon: 'none',
  65. title: res.data.msg
  66. })
  67. rejected(res.data.msg);
  68. break;
  69. }
  70. }
  71. options.fail = (error) => {
  72. //Do something with request error
  73. let { errMsg } = error
  74. console.log(error,'errorerrorerror')
  75. if (errMsg === 'Network Error') {
  76. errMsg = '后端接口连接异常'
  77. } else if (errMsg.includes('timeout')) {
  78. errMsg = '系统接口请求超时'
  79. } else if (errMsg.includes('Request failed with status code')) {
  80. errMsg = '系统接口' + errMsg.substr(errMsg.length - 3) + '异常'
  81. }
  82. uni.showToast({
  83. icon: 'none',
  84. title: errMsg,
  85. })
  86. rejected(error)
  87. }
  88. uni.request(options);
  89. });
  90. }
  91. export default service;