request.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const baseUrl = "https://www.cqna.gov.cn/mnazw"
  2. const imgUrl = "https://www.cqna.gov.cn/mnazw/applet/"
  3. import {
  4. cacheGet
  5. } from "../../../utils/cacheUtil"
  6. const request = (options) => {
  7. islogin(options.url)
  8. // let token = wx.getStorageSync('token')
  9. let token = cacheGet('token')
  10. return new Promise((resolve, reject) => {
  11. options.url = baseUrl + options.url
  12. let contentType;
  13. if (options.contentType) {
  14. contentType = options.contentType;
  15. } else if (options.method && options.method === 'POST') {
  16. contentType = 'application/x-www-form-urlencoded';
  17. } else if (options.method === 'GET') {
  18. contentType = 'application/json;charset=UTF-8';
  19. }
  20. wx.request({
  21. // 配置 "wx.request" 请求参数
  22. ...options,
  23. header: {
  24. 'content-type': contentType,
  25. 'mini-token': `${token}`
  26. },
  27. success: function (res) {
  28. // console.log("network-res=>", res);
  29. if(res.data.msg == '请重新登录' && !res.data.result && token != null && token != '') {
  30. wx.showModal({
  31. title: '未授权',
  32. content: '您登录授权已过期,请重新登录授权',
  33. showCancel: false,
  34. confirmText: '登录',
  35. success: function (res) {
  36. wx.navigateTo({
  37. url: '/pages/login/login',
  38. });
  39. },
  40. });
  41. }
  42. // 返回成功信息
  43. resolve(res.data)
  44. },
  45. fail: function (error) {
  46. // console.log("network-err=>", error);
  47. // 返回错误信息
  48. reject(error)
  49. }
  50. })
  51. })
  52. }
  53. function islogin(data) {
  54. let urls = data.split("\/")
  55. if (!noNeedLogin(urls)) {
  56. // let token = wx.getStorageSync('token')
  57. let token = cacheGet('token')
  58. if (token == null || token == '') {
  59. wx.showModal({
  60. title: '未授权',
  61. content: '您登录授权已过期,请重新登录授权',
  62. showCancel: false,
  63. confirmText: '登录',
  64. success: function (res) {
  65. wx.navigateTo({
  66. url: '/pages/login/login',
  67. });
  68. },
  69. });
  70. }
  71. }
  72. }
  73. var noNeedLoginList = ["login", "getGoodOrBadByYearAndArea", "getOfficeNumberByAreaInfo"]
  74. function noNeedLogin(data) {
  75. for (const element of noNeedLoginList) {
  76. let a = data[data.length - 1].split("?")[0]
  77. if (a === element) {
  78. return true;
  79. }
  80. }
  81. }
  82. export {
  83. request,
  84. imgUrl,
  85. baseUrl
  86. }