util.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // 封装微信的的request
  2. function request(url, data = {}, method = "GET") {
  3. return new Promise(function(resolve, reject) {
  4. wx.request({
  5. url,
  6. data,
  7. method,
  8. header: {
  9. 'X-Dts-Token': wx.getStorageSync('token')
  10. },
  11. success: function(res) {
  12. if (res.statusCode == 200) {
  13. if (res.data.errno == 501) {
  14. wx.removeStorageSync('sessionKey')
  15. wx.removeStorageSync('userInfo')
  16. wx.removeStorageSync('token')
  17. if (wx.hideLoading) wx.hideLoading()
  18. else wx.hideToast()
  19. wx.navigateTo({ url: '/packageLogin/login/login' })
  20. } else {
  21. resolve(res.data)
  22. }
  23. } else {
  24. reject(res.errMsg)
  25. }
  26. },
  27. fail: function(err) {
  28. reject(err)
  29. }
  30. })
  31. })
  32. }
  33. function showToast(title) {
  34. if (Boolean(title) === false) return
  35. wx.showToast({ title, icon: 'none' })
  36. }
  37. function showErrorToast(title) {
  38. if (Boolean(title) === false) return
  39. wx.showToast({ title, image: '/static/images/icon_close.png' })
  40. }
  41. function showLoad(title) {
  42. if (wx.showLoading) wx.showLoading({ title, mask: true }) // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
  43. else wx.showToast({ title, icon: 'loading', mask: true, duration: 20000 }) // 低版本采用Toast兼容处理并将时间设为20秒以免自动消失
  44. }
  45. function hideLoad() {
  46. if (wx.hideLoading) wx.hideLoading() // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
  47. else wx.hideToast()
  48. }
  49. function prePage() {
  50. let pages = getCurrentPages()
  51. return pages[pages.length - 2]
  52. }
  53. module.exports = {
  54. request,
  55. showToast,
  56. showErrorToast,
  57. showLoad,
  58. hideLoad,
  59. prePage
  60. }