util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // const { baseUrl } = require('./env.js').prod; // 正式
  2. const {
  3. baseUrl
  4. } = require('./env.js').test; // 测试
  5. // token
  6. let Authorization = "";
  7. const jsonType = {
  8. "content-type": "application/json"
  9. }
  10. const urlencodeType = {
  11. "content-type": "application/x-www-form-urlencoded"
  12. }
  13. import strings from '../utils/strings.js';
  14. // 错误类型
  15. let statusCode = {
  16. '1': "请求数据失败",
  17. "400": "参数列表错误",
  18. "401": "请先登录",
  19. "403": "禁止访问",
  20. "404": "资源,服务未找到",
  21. "405": "错误请求方法",
  22. "409": "冲突",
  23. "413": "上传文件太大",
  24. "500": "服务器错误",
  25. "1000": "账户已在别处登录"
  26. }
  27. const formatTime = date => {
  28. const year = date.getFullYear()
  29. const month = date.getMonth() + 1
  30. const day = date.getDate()
  31. const hour = date.getHours()
  32. const minute = date.getMinutes()
  33. const second = date.getSeconds()
  34. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  35. }
  36. const formatNumber = n => {
  37. n = n.toString()
  38. return n[1] ? n : `0${n}`
  39. };
  40. // 封装wx.request
  41. function _show_error(error_code) {
  42. wx.showToast({
  43. "title": statusCode[error_code],
  44. "icon": "error",
  45. "duration": 1500
  46. });
  47. }
  48. //
  49. function request(url, data = {}, method = "GET", header = urlencodeType, isToken = true) {
  50. if (isToken) {
  51. try {
  52. var value = wx.getStorageSync('token') || "";
  53. if (!strings.isEmpty(value)) {
  54. const resData = JSON.parse(value);
  55. Authorization = resData.token;
  56. }
  57. } catch (e) {
  58. Authorization = "";
  59. }
  60. }
  61. // 拼接地址
  62. let fullurl = `${baseUrl}${url}`;
  63. //返回异步操作成功的结果和返回异步操作失败的结果
  64. if (!strings.isTrue(data.unShowLoad)) {
  65. wx.showLoading({
  66. title: '加载中',
  67. });
  68. }
  69. console.log("请求参数:", fullurl, data)
  70. console.log("gadsfwqerqewqr====aaa==" + fullurl);
  71. console.log("gadsfwqerqewqr====bbb==" + JSON.stringify(data));
  72. return new Promise((resolve, reject) => {
  73. wx.request({
  74. url: fullurl,
  75. method,
  76. data,
  77. header: Object.assign({
  78. "content-type": "application/x-www-form-urlencoded",
  79. "appid": "wx08f94a3e90881910",
  80. "Authorization": isToken ? Authorization : undefined
  81. }, header),
  82. success(res) {
  83. console.log("接口:", fullurl, res)
  84. console.log("gadsfwqerqewqr====00==" + JSON.stringify(res));
  85. if (res.statusCode !== 200) {
  86. wx.hideLoading();
  87. wx.showToast({
  88. "title": "请求错误",
  89. "icon": "error",
  90. "duration": 1500
  91. });
  92. resolve(res.data);
  93. return;
  94. }
  95. if (!strings.isTrue(data.unShowLoad)) {
  96. wx.hideLoading();
  97. }
  98. if (res.data.code === 0) {
  99. resolve(res.data.data);
  100. } else if (res.data.code === 401) {
  101. resolve(res.data);
  102. wx.showToast({
  103. title: '登录过期'
  104. })
  105. // token过期
  106. try {
  107. getCurrentPages()[0].logOut();
  108. } catch (e) {
  109. console.log("可能没有这个缓存")
  110. };
  111. // getCurrentPages()[0].login();
  112. // request(url,data,method,header,isToken);
  113. } else {
  114. resolve(res.data);
  115. _show_error(res.data.code)
  116. }
  117. },
  118. fail: (err) => {
  119. console.log("请求失败" + JSON.stringify(err));
  120. console.log("gadsfwqerqewqr====11==" + JSON.stringify(err));
  121. _show_error("1");
  122. wx.hideLoading();
  123. reject({
  124. code: -1,
  125. msg: JSON.stringify(err)
  126. })
  127. },
  128. complete() {}
  129. })
  130. })
  131. }
  132. function isCN(str) {
  133. if (/^[\u3220-\uFA29]+$/.test(str)) {
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. module.exports = {
  140. formatTime,
  141. request,
  142. formatNumber,
  143. isCN,
  144. urlencodeType,
  145. jsonType
  146. }