canteen-request.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const baseUrl = "https://www.cqna.gov.cn/data"
  2. const imgUrl = "https://www.cqna.gov.cn/data/upload/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. // showDialog()
  31. }
  32. // 返回成功信息
  33. resolve(res.data)
  34. },
  35. fail: function (error) {
  36. // console.log("network-err=>", error);
  37. // 返回错误信息
  38. reject(error)
  39. }
  40. })
  41. })
  42. }
  43. function islogin(data) {
  44. let urls = data.split("\/")
  45. if (!noNeedLogin(urls)) {
  46. // let token = wx.getStorageSync('token')
  47. let token = cacheGet('token')
  48. if (token == null || token == '') {
  49. // showDialog()
  50. }
  51. }
  52. }
  53. var noNeedLoginList = ["login", "getGoodOrBadByYearAndArea", "getOfficeNumberByAreaInfo"]
  54. function noNeedLogin(data) {
  55. for (const element of noNeedLoginList) {
  56. let a = data[data.length - 1].split("?")[0]
  57. if (a === element) {
  58. return true;
  59. }
  60. }
  61. }
  62. function showDialog() {
  63. wx.showModal({
  64. title: '掌新南岸欢迎您!',
  65. content: '登录,体验更完整!',
  66. confirmText: '登录',
  67. cancelText:"取消",
  68. success: function (res) {
  69. if (res.confirm) {
  70. wx.navigateTo({
  71. url: '/pages/login/login',
  72. });
  73. }if (res.cancel){
  74. wx.navigateTo({
  75. url: '/pages/index/index',
  76. })
  77. }
  78. },
  79. });
  80. }
  81. export {
  82. request,
  83. imgUrl,
  84. baseUrl
  85. }