util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // const formatTime = date => {
  2. // const year = date.getFullYear()
  3. // const month = date.getMonth() + 1
  4. // const day = date.getDate()
  5. // const hour = date.getHours()
  6. // const minute = date.getMinutes()
  7. // const second = date.getSeconds()
  8. // return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  9. // }
  10. // const formatNumber = n => {
  11. // n = n.toString()
  12. // return n[1] ? n : `0${n}`
  13. // }
  14. const app = getApp();
  15. const PATH = app.globalData.path;
  16. module.exports = {
  17. // formatTime,
  18. request: request,
  19. post: post,
  20. get: get,
  21. toast: toast,
  22. formatDate: formatDate
  23. }
  24. /**
  25. * POST请求
  26. * @param {*} options
  27. */
  28. function post(options) {
  29. wx.showLoading({
  30. title: '加载中...',
  31. mask: true
  32. });
  33. let header = {
  34. 'Authorization': app.globalData.Authorization,
  35. }
  36. if (options.header) {
  37. options.header = Object.assign(options.header, header);
  38. }
  39. console.info(options.header)
  40. wx.request({
  41. url: PATH + options.url,
  42. method: 'POST',
  43. data: options.data || {},
  44. header: options.header || header,
  45. success: options.success || function (res) {
  46. console.info(res);
  47. wx.hideLoading();
  48. },
  49. fail: options.fail || function (e) {
  50. console.info(e);
  51. wx.hideLoading();
  52. wx.showToast({
  53. title: '请求失败',
  54. icon: 'error'
  55. });
  56. }
  57. });
  58. }
  59. /**
  60. * GET请求
  61. * @param {*} options
  62. */
  63. function get(options) {
  64. wx.showLoading({
  65. title: '加载中...',
  66. mask: true
  67. });
  68. let header = {
  69. 'Authorization': app.globalData.Authorization,
  70. }
  71. if (options.header) {
  72. options.header = Object.assign(options.header, header);
  73. }
  74. console.info(options.header)
  75. console.info(header)
  76. wx.request({
  77. url: PATH + options.url,
  78. method: 'GET',
  79. data: options.data || {},
  80. header: options.header || header,
  81. success: options.success || function (res) {
  82. console.info(res);
  83. wx.hideLoading();
  84. },
  85. fail: options.fail || function (e) {
  86. console.info(e);
  87. wx.hideLoading();
  88. wx.showToast({
  89. title: '请求失败',
  90. icon: 'error'
  91. });
  92. }
  93. });
  94. }
  95. /**
  96. * 请求
  97. * @param {*} options
  98. */
  99. function request(options) {
  100. wx.showLoading({
  101. title: '加载中...',
  102. mask: true
  103. });
  104. let header = {
  105. 'Authorization': app.globalData.Authorization,
  106. }
  107. if (options.header) {
  108. options.header = Object.assign(options.header, header);
  109. }
  110. console.info(options.header)
  111. wx.request({
  112. url: PATH + options.url,
  113. method: options.method || 'GET',
  114. data: options.data || {},
  115. header: options.header || header,
  116. success: options.success || function (res) {
  117. console.info(res)
  118. wx.hideLoading();
  119. },
  120. fail: options.fail || function (e) {
  121. console.info(e)
  122. wx.hideLoading();
  123. wx.showToast({
  124. title: '请求失败',
  125. icon: 'error'
  126. });
  127. }
  128. });
  129. }
  130. function toast(msg, icon) {
  131. wx.showToast({
  132. title: msg || 'title',
  133. icon: icon || 'none',
  134. duration: 3000
  135. });
  136. }
  137. function formatDate(date, fmt) {
  138. if (date) {
  139. date = new Date(date);
  140. } else {
  141. date = new Date();
  142. }
  143. fmt = fmt || 'yyyy-MM-dd HH:mm';
  144. let ret;
  145. const opt = {
  146. "y+": date.getFullYear().toString(), // 年
  147. "M+": (date.getMonth() + 1).toString(), // 月
  148. "d+": date.getDate().toString(), // 日
  149. "H+": date.getHours().toString(), // 时
  150. "m+": date.getMinutes().toString(), // 分
  151. "s+": date.getSeconds().toString() // 秒
  152. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  153. };
  154. for (let k in opt) {
  155. ret = new RegExp("(" + k + ")").exec(fmt);
  156. if (ret) {
  157. fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
  158. };
  159. };
  160. return fmt;
  161. }