| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- // 封装微信的的request
- function request(url, data = {}, method = "GET") {
- return new Promise(function(resolve, reject) {
- wx.request({
- url,
- data,
- method,
- header: {
- 'X-Dts-Token': wx.getStorageSync('token')
- },
- success: function(res) {
- if (res.statusCode == 200) {
- if (res.data.errno == 501) {
- wx.removeStorageSync('sessionKey')
- wx.removeStorageSync('userInfo')
- wx.removeStorageSync('token')
- if (wx.hideLoading) wx.hideLoading()
- else wx.hideToast()
- wx.navigateTo({ url: '/packageLogin/login/login' })
- } else {
- resolve(res.data)
- }
- } else {
- reject(res.errMsg)
- }
- },
- fail: function(err) {
- reject(err)
- }
- })
- })
- }
- function showToast(title) {
- if (Boolean(title) === false) return
- wx.showToast({ title, icon: 'none' })
- }
- function showErrorToast(title) {
- if (Boolean(title) === false) return
- wx.showToast({ title, image: '/static/images/icon_close.png' })
- }
- function showLoad(title) {
- if (wx.showLoading) wx.showLoading({ title, mask: true }) // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
- else wx.showToast({ title, icon: 'loading', mask: true, duration: 20000 }) // 低版本采用Toast兼容处理并将时间设为20秒以免自动消失
- }
- function hideLoad() {
- if (wx.hideLoading) wx.hideLoading() // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
- else wx.hideToast()
- }
- function prePage() {
- let pages = getCurrentPages()
- return pages[pages.length - 2]
- }
- module.exports = {
- request,
- showToast,
- showErrorToast,
- showLoad,
- hideLoad,
- prePage
- }
|