util.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import config from './config.js'
  2. let util = {
  3. formatTime(times = new Date().getTime() / 1000, format = 'yyyy/MM/dd hh:mm:ss') {
  4. const date = new Date(times * 1000)
  5. const reg = {
  6. 'M+': date.getMonth() + 1,
  7. 'd+': date.getDate(),
  8. 'h+': date.getHours(),
  9. 'm+': date.getMinutes(),
  10. 's+': date.getSeconds()
  11. }
  12. const year = date.getFullYear()
  13. if (/(y+)/.test(format)) {
  14. format = format.replace(RegExp.$1, `${year}`.substr(4 - RegExp.$1.length))
  15. }
  16. for (let k in reg) {
  17. if (new RegExp(`(${k})`).test(format)) {
  18. format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? reg[k] : (`00${reg[k]}`.substr(`${reg[k]}`.length)))
  19. }
  20. }
  21. return format
  22. },
  23. fastClickTips () {
  24. wx.showToast({
  25. title: '不要频繁操作,请稍后再试',
  26. icon: 'none'
  27. })
  28. // setTimeout(() => {
  29. // wx.hideToast()
  30. // }, 500)
  31. },
  32. routeTo(url) {
  33. wx.navigateTo({
  34. url: url,
  35. success: function() {},
  36. fail: function() {}
  37. })
  38. },
  39. ajax(options) {
  40. if (options.data && options.data.HideLoading) {
  41. delete options.data.HideLoading
  42. } else if (options.url !== '/mp/wx/getuserinfo') {
  43. wx.showLoading({
  44. mask: true,
  45. title: '加载中',
  46. })
  47. }
  48. let hideToast = false
  49. if (options.data && options.data.HideToast) {
  50. hideToast = true
  51. delete options.data.HideToast
  52. }
  53. options.data = {
  54. ...options.data,
  55. app_id: config.baseParams.app_id,
  56. client_id: '',
  57. format: 'json',
  58. ...config.gameParams,
  59. ...config.agentParams,
  60. ...config.deviceParams,
  61. ts: parseInt(new Date().getTime() / 1000),
  62. token: wx.getStorageSync('userInfo').user_token || '',
  63. sign: ''
  64. }
  65. options.header = {
  66. 'BOX_USER_AGENT': 'miniProgram'
  67. }
  68. if (options.method) {
  69. options.method = options.method.toUpperCase()
  70. if (options.method === 'POST') {
  71. options.header['Content-Type'] = 'application/x-www-form-urlencoded'
  72. }
  73. }
  74. if (options.mini) {
  75. options.url = config.miniUrl + options.url
  76. delete options.mini
  77. } else {
  78. options.url = config.baseUrl + options.url
  79. }
  80. return new Promise((resolve, reject) => {
  81. wx.request({
  82. ...options,
  83. success(res) {
  84. console.log(options.url, options.data, res)
  85. wx.hideLoading()
  86. if (res.statusCode === 200) {
  87. if (res.data.code === undefined || res.data.code === 200) {
  88. resolve(res.data)
  89. } else {
  90. if (options.url !== '/mp/wx/getuserinfo' &&
  91. res.data.code !== 1002 &&
  92. options.url.indexOf('/favorite/reward/get') == -1 && !hideToast) {
  93. wx.showToast({
  94. title: res.data.msg,
  95. icon: 'none'
  96. })
  97. }
  98. if (res.data.code === 1002) {
  99. // console.log('登陆过期正在重新登陆...')
  100. getApp().againLogin()
  101. getApp().globalData.againLogin = true
  102. } else {
  103. console.log(`${options.url} 请求出错,错误码${res.data.code}`)
  104. }
  105. reject(res)
  106. }
  107. } else {
  108. if (!hideToast) {
  109. wx.showToast({
  110. title: '网络异常,请重试',
  111. icon: 'none'
  112. })
  113. }
  114. reject(res)
  115. }
  116. },
  117. fail(res) {
  118. console.log(options.url, options.data, res)
  119. wx.hideLoading()
  120. if (!hideToast) {
  121. wx.showToast({
  122. title: '网络异常,请重试',
  123. icon: 'none'
  124. })
  125. }
  126. reject(res)
  127. }
  128. })
  129. })
  130. },
  131. getImageInfo(url) {
  132. return new Promise((resolve, reject) => {
  133. if (!url) {
  134. resolve({
  135. path: '/image/img_avatar_default.png'
  136. })
  137. } else {
  138. wx.getImageInfo({
  139. src: url,
  140. success: function (res) {
  141. resolve(res)
  142. },
  143. fail: function (res) {
  144. resolve({
  145. path: '/image/img_avatar_default.png'
  146. })
  147. }
  148. })
  149. }
  150. })
  151. }
  152. }
  153. module.exports = util