util.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * 常用工具函数
  3. */
  4. class Utils {
  5. /*
  6. 格式化时间
  7. @date {Date} 传入的时间
  8. @status {Boolean} 是否需要时分秒开关 true 关闭时分秒选项 false 打开时分秒选项
  9. */
  10. formatTime(date, status = false) {
  11. date = date.replace(getRegExp('-', 'g'), '/');
  12. const _date = new Date(date);
  13. const year = _date.getFullYear();
  14. const month = _date.getMonth() + 1;
  15. const day = _date.getDate();
  16. const hour = _date.getHours();
  17. const minute = _date.getMinutes();
  18. const second = _date.getSeconds();
  19. if (status) {
  20. return [year, month, day].map(this.formatNumber).join('/');
  21. }
  22. return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':');
  23. }
  24. /* 格式化数字 */
  25. formatNumber(n) {
  26. n = n.toString();
  27. return n[1] ? n : '0' + n;
  28. }
  29. /*
  30. 获取实际数据类型
  31. @data {any} 数据
  32. */
  33. getDataType(data) {
  34. return /^\[object (.*)\]$/.exec(Object.prototype.toString.call(data))[1];
  35. }
  36. /*
  37. 获取url
  38. @url {String} 待处理的URL
  39. @return {String} 处理过的字符串
  40. 根据URL是否有http或者https判断是否拼接字符串
  41. */
  42. getUrl(url) {
  43. return new Promise(reslove => {
  44. const apiUrl = require('../config.js').apiUrl; // 加载接口地址文件
  45. if (url.indexOf('https://') === -1 && url.indexOf('http://') === -1) {
  46. reslove(apiUrl + url)
  47. } else {
  48. if (url.indexOf('http://') === -1) {
  49. reslove(url)
  50. return
  51. }
  52. reslove(url.replace('http://', 'https://'))
  53. }
  54. })
  55. }
  56. /**
  57. * 获取网络状态
  58. */
  59. getNetworkType() {
  60. return new Promise((reslove, reject) => {
  61. wx.getNetworkType({
  62. success: function (res) {
  63. if (res.networkType === 'none') {
  64. wx.$emit('offline', true)
  65. reslove('offline')
  66. } else {
  67. wx.$emit('offline', false)
  68. reslove(res.networkType)
  69. }
  70. },
  71. fail: function (res) {
  72. reject('fail')
  73. }
  74. })
  75. })
  76. }
  77. /**
  78. * 获取userid与userToken
  79. * @options {Object} 传入的对象参数
  80. * @return 返回参数对象
  81. * 设置request参数中的userID和usertoken
  82. */
  83. getUserId(options) {
  84. return new Promise((reslove, reject) => {
  85. const userid = wx.getStorageSync('userid');
  86. const usertoken = wx.getStorageSync('usertoken');
  87. options.data.user_id = userid;
  88. options.data.user_token = usertoken;
  89. reslove(options)
  90. })
  91. }
  92. /**
  93. * 上传图片
  94. * @pic_arr {Array} 图片数组
  95. * @return {Array}
  96. */
  97. upLoadImage(options) {
  98. const _type = this.getDataType(options.files);
  99. options.formData = options.formData || {};
  100. const formData = {
  101. user_id: wx.getStorageSync('userid'),
  102. user_token: wx.getStorageSync('usertoken'),
  103. ...options.formData
  104. };
  105. wx.showLoading({
  106. mask: true,
  107. title: '上传中...'
  108. })
  109. return new Promise(async (reslove, reject) => {
  110. if (_type === "Array") {
  111. const _arr = [];
  112. const _files = options.files;
  113. const _len = _files.length - 1;
  114. const _url = await this.getUrl(options.url);
  115. _files.forEach((val, key) => {
  116. wx.uploadFile({
  117. url: _url,
  118. filePath: val,
  119. name: options.name,
  120. formData,
  121. success: res => {
  122. if (res.statusCode === 200) {
  123. const data = JSON.parse(res.data);
  124. if (data.errcode === 0) {
  125. _arr.push(data.data);
  126. if (_len === key) {
  127. wx.hideLoading()
  128. reslove(_arr)
  129. }
  130. }
  131. }
  132. },
  133. fail: (e) => {
  134. console.log(e)
  135. reject({
  136. type: this.getDataType(val)
  137. })
  138. wx.hideLoading()
  139. throw new Error(e)
  140. }
  141. })
  142. })
  143. } else {
  144. reject({
  145. type: _type
  146. })
  147. wx.hideLoading()
  148. throw new Error('输入格式有误')
  149. }
  150. })
  151. }
  152. /**
  153. * 获取用户信息
  154. */
  155. getHwUserInfo(that) {
  156. return new Promise(reslove => {
  157. wx.kx_request({
  158. url: wx.kx_api.hwUser.getHwUserInfo,
  159. success: res => {
  160. if (res.data) {
  161. //成功后存入缓存
  162. wx.setStorageSync('userinfo', res.data.hwUser);
  163. reslove(res.data.hwUser)
  164. // 判断是否提交页面数据
  165. that && that.setData({
  166. userinfo: res.data.hwUser
  167. })
  168. }
  169. }
  170. })
  171. })
  172. }
  173. }
  174. module.exports = {
  175. Utils: new Utils()
  176. }