util.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. const api = require('./api.js')
  2. class Utils {
  3. constructor(that) {
  4. this.api = api;
  5. // this.message_hint = {
  6. // 100: '继续发送请求'
  7. // };
  8. that.kx_request = this.request.bind(this);
  9. that.kx_api = this.api;
  10. }
  11. /*
  12. 获取实际数据类型
  13. @data 数据
  14. */
  15. getDataType(data) {
  16. return /^\[object (.*)\]$/.exec(Object.prototype.toString.call(data))[1];
  17. }
  18. /*
  19. 格式化时间
  20. @date 传入的时间
  21. @status 是否需要时分秒开关
  22. */
  23. formatTime(date, status = false) {
  24. const _date = new Date(date);
  25. const year = _date.getFullYear();
  26. const month = _date.getMonth() + 1;
  27. const day = _date.getDate();
  28. const hour = _date.getHours();
  29. const minute = _date.getMinutes();
  30. const second = _date.getSeconds();
  31. if (status) {
  32. return [year, month, day].map(this.formatNumber).join('/');
  33. }
  34. return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':');
  35. }
  36. /* 格式化数字 */
  37. formatNumber(n) {
  38. n = n.toString();
  39. return n[1] ? n : '0' + n;
  40. }
  41. /* 获取url */
  42. getUrl(url) {
  43. const apiUrl = require('../config.js').apiUrl; // 加载接口地址文件
  44. if (url.indexOf('https://') === -1 && url.indexOf('http://') === -1) {
  45. return apiUrl + url
  46. } else {
  47. if (url.indexOf('http://') === -1) {
  48. return url
  49. }
  50. return url.replace('http://', 'https://')
  51. }
  52. }
  53. /* 获取Openid */
  54. getOpenid(options) {
  55. return new Promise((resolve, reject) => {
  56. wx.login({
  57. success: (res) => {
  58. let code = res.code;
  59. this.request({
  60. code,
  61. success: res => {
  62. wx.setStorage({
  63. key: 'openid',
  64. data: res.data.openid,
  65. success: res => {
  66. options.openid = res.data.openid
  67. resolve(options);
  68. }
  69. })
  70. }
  71. })
  72. }
  73. })
  74. })
  75. }
  76. /* 获取userid与userToken */
  77. getUserId(options) {
  78. const userid = wx.getStorageSync('userid');
  79. const usertoken = wx.getStorageSync('usertoken');
  80. options.data.user_id = userid;
  81. options.data.user_token = usertoken;
  82. return options
  83. }
  84. /*
  85. 请求方法
  86. @options 请求参数
  87. @status 是否携带userID
  88. */
  89. request(options, status = true) {
  90. /* 判断是否传入参数 */
  91. options.data = options.data ? options.data : {};
  92. /* 设置请求方式 */
  93. options.method = (options.type ? options.type : 'get').toUpperCase();
  94. /* 拼接接口地址 */
  95. if (this.getDataType(options.url) !== 'String') {
  96. throw new Error('接口路径格式错误')
  97. }
  98. options.url = this.getUrl(options.url);
  99. /* 消除空值字段 */
  100. // for (let key in options.data) {
  101. // if (options.data[key] === undefined || options.data[key] === '') {
  102. // delete options.data[key]
  103. // }
  104. // }
  105. /* 检测网络状况 并判断是否发起请求 */
  106. wx.getNetworkType({
  107. success: (res) => {
  108. const networkType = res.networkType
  109. if (networkType === 'none') {
  110. options.success && options.success('offline')
  111. return
  112. }
  113. /* 根据需要判断是否传入openID */
  114. if (status) {
  115. // const openid = wx.getStorageSync('openid');
  116. // if (openid) {
  117. // options.openid = openid;
  118. // this._request(options);
  119. // } else {
  120. // this.getOpenid(options).then((data) => {
  121. // this._request(data);
  122. // });
  123. // }
  124. this._request(this.getUserId(options));
  125. } else {
  126. this._request(options);
  127. }
  128. }
  129. })
  130. }
  131. /*
  132. 实际执行的请求方法体
  133. @opt 传入的参数对象
  134. */
  135. _request(opt) {
  136. return new Promise((resolve, reject) => {
  137. wx.request({
  138. url: opt.url,
  139. data: opt.data || {},
  140. header: opt.header || {},
  141. method: opt.method,
  142. dataType: opt.dataType || 'json',
  143. responseType: opt.responseType || 'text',
  144. success: function(res) {
  145. console.log('请求参数=>', opt);
  146. console.log('服务器响应结果=>',res);
  147. console.log('当前页面=>', getCurrentPages().map(val => val.route).pop());
  148. // 判断是否含有成功方法并执行
  149. if (opt.success && typeof opt.success === 'function') {
  150. if (res.statusCode === 200) {
  151. if (res.data.errcode === 0) {
  152. opt.success(res.data);
  153. resolve(res.data);
  154. } else {
  155. // 判断是否含有失败方法并执行
  156. if (opt.fail && typeof opt.fail === 'function') {
  157. opt.fail(res.data);
  158. reject(res.data);
  159. }
  160. if (!opt.model) {
  161. wx.showToast({
  162. icon: 'none',
  163. title: res.data.errmsg || '网络异常,请检查后重试',
  164. })
  165. }
  166. }
  167. } else {
  168. opt.success && opt.success('offline')
  169. wx.showToast({
  170. icon: 'none',
  171. title: '网络异常,请检查后重试',
  172. })
  173. }
  174. }
  175. },
  176. fail: function(res) {
  177. // 判断是否含有失败方法并执行
  178. if (opt.fail && typeof opt.fail === 'function') {
  179. opt.success && opt.success('offline')
  180. opt.fail(res.data);
  181. reject(res.data);
  182. wx.showToast({
  183. icon: 'none',
  184. title: '网络异常,请检查后重试',
  185. })
  186. }
  187. },
  188. complete: function(res) {
  189. // 判断是否含有complete方法并执行
  190. if (opt.complete && typeof opt.complete === 'function') {
  191. opt.complete(res.data);
  192. resolve(res.data);
  193. }
  194. },
  195. })
  196. })
  197. }
  198. }
  199. module.exports = {
  200. Utils: Utils
  201. }