Http.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. class HTTP {
  2. constructor() {
  3. this.ENV = wx.getAccountInfoSync().miniProgram.envVersion;
  4. this.urls = [{
  5. name: "美大正式",
  6. url: "https://crm.meida.com:16691"
  7. }, {
  8. name: "楚楚",
  9. url: "https://cucu.cnyunl.com:8079"
  10. }, {
  11. name: "开发环境",
  12. url: "http://61.164.207.46:8300"
  13. }]
  14. if (this.ENV === 'release') { // 正式版
  15. this.baseUrl = "https://crm.meida.com:16691";
  16. } else {
  17. // this.baseUrl = "http://61.164.207.46:8300";
  18. this.baseUrl = "https://crm.meida.com:16691";
  19. }
  20. //得到缩略图或者压缩图 getType默认得到缩略图传true得到压缩图
  21. this.getSpecifiedImage = (obj, getType = false) => {
  22. obj.url = this.getImageUrl(obj.url)
  23. try {
  24. let type = getType ? 'compressed' : 'thumbnail';
  25. let imgObj = obj.subfiles.find(v => v.type == type);
  26. return this.getImageUrl(imgObj.url || obj.url);
  27. } catch (error) {
  28. return this.getImageUrl(obj.url);
  29. }
  30. }
  31. // 判断图片是本地还是云存储
  32. this.getImageUrl = (url) => {
  33. if (!url) return '';
  34. //判断url中是否存在http,没有的话要拼接 this.baseUrl
  35. if (!/^https?:\/\//.test(url)) {
  36. url = this.baseUrl + url;
  37. }
  38. return url;
  39. }
  40. // 从本地存储加载JSESSIONID
  41. this.jsessionid = wx.getStorageSync('JSESSIONID') || '';
  42. console.log("接口地址", this.baseUrl);
  43. }
  44. request({
  45. url,
  46. data = {},
  47. method = "POST",
  48. header = {
  49. 'content-type': 'application/json',
  50. "accesstoken": data.accesstoken || wx.getStorageSync('userMsg').token || '',
  51. "Cookie": `JSESSIONID=${this.jsessionid}`
  52. },
  53. showLoading = '加载中...'
  54. }) {
  55. return new Promise((resolve, reject) => {
  56. this._request(url, resolve, reject, data, method, header, showLoading);
  57. })
  58. }
  59. _request(url, resolve, reject, data, method, header, showLoading) {
  60. /* if (showLoading) wx.showLoading({
  61. title: showLoading,
  62. mask: true
  63. }) */
  64. wx.request({
  65. url: this.baseUrl + '/yos/rest/index' + url,
  66. data,
  67. method,
  68. header,
  69. timeout: 60000,
  70. success: (res) => {
  71. resolve(res.data);
  72. },
  73. fial: (err) => {
  74. reject(err);
  75. },
  76. complete: (res) => {
  77. // if (showLoading) wx.hideLoading()
  78. this._handleSessionCookies(res);
  79. if (res.errMsg != 'request:ok') {
  80. wx.showToast({
  81. title: '网络异常,请重新进入',
  82. icon: "none"
  83. })
  84. } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
  85. this.jsessionid = '';
  86. wx.removeStorageSync('JSESSIONID');
  87. wx.redirectTo({
  88. url: '/pages/login/phone',
  89. });
  90. wx.showToast({
  91. title: res.msg,
  92. icon: "none"
  93. })
  94. }
  95. }
  96. })
  97. }
  98. // 处理会话Cookie
  99. _handleSessionCookies(res) {
  100. try {
  101. const cookies = res.cookies || [];
  102. const headers = res.header || {};
  103. // 检查响应头中的Set-Cookie
  104. const setCookie = headers['Set-Cookie'] || headers['set-cookie'];
  105. if (setCookie) {
  106. // 将字符串转换为数组
  107. const cookieArray = Array.isArray(setCookie) ?
  108. setCookie : [setCookie];
  109. cookies.push(...cookieArray);
  110. }
  111. // 查找JSESSIONID
  112. for (const cookie of cookies) {
  113. const match = cookie.match(/JSESSIONID=([^;]+)/i);
  114. if (match && match[1]) {
  115. const newSessionId = match[1];
  116. // 更新JSESSIONID
  117. if (newSessionId !== this.jsessionid) {
  118. this.jsessionid = newSessionId;
  119. wx.removeStorageSync('JSESSIONID');
  120. wx.setStorageSync('JSESSIONID', newSessionId);
  121. }
  122. break;
  123. }
  124. }
  125. } catch (error) {
  126. console.error('处理会话Cookie时出错:', error);
  127. }
  128. }
  129. }
  130. export {
  131. HTTP
  132. }