request.js 2.8 KB

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