util.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. wx.request({
  40. url: PATH + options.url,
  41. method: 'POST',
  42. data: options.data || {},
  43. header: options.header || header,
  44. success: options.success || function (res) {
  45. console.info(res);
  46. wx.hideLoading();
  47. },
  48. fail: options.fail || function (e) {
  49. console.info(e);
  50. wx.hideLoading();
  51. wx.showToast({
  52. title: '请求失败',
  53. icon: 'error'
  54. });
  55. }
  56. });
  57. }
  58. /**
  59. * GET请求
  60. * @param {*} options
  61. */
  62. function get(options) {
  63. wx.showLoading({
  64. title: '加载中...',
  65. mask: true
  66. });
  67. let header = {
  68. 'Authorization': app.globalData.Authorization,
  69. }
  70. if (options.header) {
  71. options.header = Object.assign(options.header, header);
  72. }
  73. wx.request({
  74. url: PATH + options.url,
  75. method: 'GET',
  76. data: options.data || {},
  77. header: options.header || header,
  78. success: options.success || function (res) {
  79. console.info(res);
  80. wx.hideLoading();
  81. },
  82. fail: options.fail || function (e) {
  83. console.info(e);
  84. wx.hideLoading();
  85. wx.showToast({
  86. title: '请求失败',
  87. icon: 'error'
  88. });
  89. }
  90. });
  91. }
  92. /**
  93. * 请求
  94. * @param {*} options
  95. */
  96. function request(options) {
  97. wx.showLoading({
  98. title: '加载中...',
  99. mask: true
  100. });
  101. let header = {
  102. 'Authorization': app.globalData.Authorization,
  103. }
  104. if (options.header) {
  105. options.header = Object.assign(options.header, header);
  106. }
  107. wx.request({
  108. url: PATH + options.url,
  109. method: options.method || 'GET',
  110. data: options.data || {},
  111. header: options.header || header,
  112. success: options.success || function (res) {
  113. console.info(res)
  114. wx.hideLoading();
  115. },
  116. fail: options.fail || function (e) {
  117. console.info(e)
  118. wx.hideLoading();
  119. wx.showToast({
  120. title: '请求失败',
  121. icon: 'error'
  122. });
  123. }
  124. });
  125. }
  126. /**
  127. * 封装微信提示
  128. * @param {*} msg
  129. * @param {*} icon
  130. */
  131. function toast(msg, icon) {
  132. wx.showToast({
  133. title: msg || '',
  134. icon: icon || 'none',
  135. duration: 3000
  136. });
  137. }
  138. /**
  139. * 自定义时间格式化
  140. * @param {*} date
  141. * @param {*} fmt
  142. */
  143. function formatDate(date, fmt) {
  144. if (date) {
  145. // 防止IOS系统日期显示NAN
  146. date = new Date(date.replace(/-/g, '/'));
  147. } else {
  148. date = new Date();
  149. }
  150. fmt = fmt || 'yyyy-MM-dd HH:mm';
  151. let ret;
  152. const opt = {
  153. "y+": date.getFullYear().toString(), // 年
  154. "M+": (date.getMonth() + 1).toString(), // 月
  155. "d+": date.getDate().toString(), // 日
  156. "H+": date.getHours().toString(), // 时
  157. "m+": date.getMinutes().toString(), // 分
  158. "s+": date.getSeconds().toString() // 秒
  159. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  160. };
  161. for (let k in opt) {
  162. ret = new RegExp("(" + k + ")").exec(fmt);
  163. if (ret) {
  164. fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
  165. };
  166. };
  167. return fmt;
  168. }