request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const baseUrl = "https://www.cqna.gov.cn/mnazw"
  2. const imgUrl = "https://www.cqna.gov.cn/mnazw/applet/"
  3. const newImgUrl = "https://www.cqna.gov.cn/data/"
  4. import {
  5. cacheGet,
  6. cacheSet
  7. } from "../../utils/cacheUtil"
  8. cacheSet('isshow',true,3600 * 24)
  9. const request = (options) => {
  10. islogin(options.url)
  11. // let token = wx.getStorageSync('token')
  12. let token = cacheGet('token')
  13. return new Promise((resolve, reject) => {
  14. options.url = baseUrl + options.url
  15. let contentType;
  16. if (options.contentType) {
  17. contentType = options.contentType;
  18. } else if (options.method && options.method === 'POST') {
  19. contentType = 'application/x-www-form-urlencoded';
  20. } else if (options.method === 'GET') {
  21. contentType = 'application/json;charset=UTF-8';
  22. }
  23. wx.request({
  24. // 配置 "wx.request" 请求参数
  25. ...options,
  26. header: {
  27. 'content-type': contentType,
  28. 'mini-token': `${token}`
  29. },
  30. success: function (res) {
  31. // console.log("network-res=>", res);
  32. if (res.data.msg == '请重新登录' && !res.data.result && token != null && token != '') {
  33. if (show) {
  34. cacheSet('isshow',false,3600 * 24)
  35. // showDialog()
  36. }
  37. }
  38. // 返回成功信息
  39. resolve(res.data)
  40. },
  41. fail: function (error) {
  42. // console.log("network-err=>", error);
  43. // 返回错误信息
  44. reject(error)
  45. }
  46. })
  47. })
  48. }
  49. const request2 = (options) => {
  50. return new Promise((resolve, reject) => {
  51. options.url = "https://www.cqna.gov.cn/data" + options.url
  52. wx.request({
  53. // 配置 "wx.request" 请求参数
  54. ...options,
  55. header: {
  56. 'content-type': 'application/json;charset=UTF-8'
  57. },
  58. success: function (res) {
  59. // console.log("network-res=>", res);
  60. // 返回成功信息
  61. resolve(res.data)
  62. },
  63. fail: function (error) {
  64. // console.log("network-err=>", error);
  65. // 返回错误信息
  66. reject(error)
  67. }
  68. })
  69. })
  70. }
  71. function islogin(data) {
  72. let urls = data.split("\/")
  73. if (!noNeedLogin(urls)) {
  74. // let token = wx.getStorageSync('token')
  75. let token = cacheGet('token')
  76. if (token == null || token == '') {
  77. let show = cacheGet('isshow')
  78. if (show) {
  79. cacheSet('isshow',false,3600 * 24)
  80. // showDialog()
  81. }
  82. }
  83. }
  84. }
  85. var noNeedLoginList = ["login", "getGoodOrBadByYearAndArea", "getOfficeNumberByAreaInfo","getSubscribeNumber"]
  86. function noNeedLogin(data) {
  87. for (const element of noNeedLoginList) {
  88. let a = data[data.length - 1].split("?")[0]
  89. if (a === element) {
  90. return true;
  91. }
  92. }
  93. }
  94. function showDialog() {
  95. wx.showModal({
  96. title: '掌新南岸欢迎您!',
  97. content: '登录,体验更完整!',
  98. confirmText: '登录',
  99. cancelText:"取消",
  100. success: function (res) {
  101. if (res.confirm) {
  102. wx.navigateTo({
  103. url: '/pages/login/login',
  104. });
  105. }if (res.cancel){
  106. wx.navigateTo({
  107. url: '/pages/index/index',
  108. })
  109. }
  110. },
  111. });
  112. }
  113. export {
  114. request,
  115. imgUrl,
  116. newImgUrl,
  117. baseUrl,
  118. request2
  119. }