request.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const baseUrl = "https://www.cqna.gov.cn/data"
  2. const imgUrl = "https://www.cqna.gov.cn/data/upload/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. const requestNoLoginAndTest = (options) => {
  72. return new Promise((resolve, reject) => {
  73. options.url = "https://www.cqna.gov.cn/data/" + options.url
  74. wx.request({
  75. // 配置 "wx.request" 请求参数
  76. ...options,
  77. header: {
  78. 'content-type': 'application/json;charset=UTF-8'
  79. },
  80. success: function (res) {
  81. // console.log("network-res=>", res);
  82. // 返回成功信息
  83. resolve(res.data)
  84. },
  85. fail: function (error) {
  86. // console.log("network-err=>", error);
  87. // 返回错误信息
  88. reject(error)
  89. }
  90. })
  91. })
  92. }
  93. function islogin(data) {
  94. let urls = data.split("\/")
  95. if (!noNeedLogin(urls)) {
  96. // let token = wx.getStorageSync('token')
  97. let token = cacheGet('token')
  98. if (token == null || token == '') {
  99. let show = cacheGet('isshow')
  100. if (show) {
  101. cacheSet('isshow',false,3600 * 24)
  102. // showDialog()
  103. }
  104. }
  105. }
  106. }
  107. var noNeedLoginList = ["login", "getGoodOrBadByYearAndArea", "getOfficeNumberByAreaInfo","getSubscribeNumber"]
  108. function noNeedLogin(data) {
  109. for (const element of noNeedLoginList) {
  110. let a = data[data.length - 1].split("?")[0]
  111. if (a === element) {
  112. return true;
  113. }
  114. }
  115. }
  116. function showDialog() {
  117. wx.showModal({
  118. title: '掌新南岸欢迎您!',
  119. content: '登录,体验更完整!',
  120. confirmText: '登录',
  121. cancelText:"取消",
  122. success: function (res) {
  123. if (res.confirm) {
  124. wx.navigateTo({
  125. url: '/pages/login/login',
  126. });
  127. }if (res.cancel){
  128. wx.navigateTo({
  129. url: '/pages/index/index',
  130. })
  131. }
  132. },
  133. });
  134. }
  135. export {
  136. request,
  137. imgUrl,
  138. newImgUrl,
  139. baseUrl,
  140. request2,
  141. requestNoLoginAndTest
  142. }