手机调试模式和预览显示不同
自己封装了wx.request,用到了es6-Promise
var Promise = require( './promise.js' ) var env = 'test' ; function serverConfig(env) { if (!env) { throw new Error( '不合法的环境参数' ); } return { test: '...' , }[env]; } function fetch(ops) { console.log(Promise) console.log(ops,1) wx.showLoading({ title: '加载中...' , }) return new Promise((resolve, reject) => { wx.request({ url: `${serverConfig(env)}${ops.url}`, data: ops.data, header: { 'content-type' : 'application/json' , }, method: ops.method || 'GET' , success: function (res) { if (res.statusCode !== 200 || !res.data.success) { ops.error && ops.error(res); // 处理如果请求失败,要做的操作 reject(res.data); } else { resolve(res.data); } }, fail: function (res) { ops.error && ops.error(res); // 处理如果请求失败,要做的操作 reject({ errorMessage: '请求失败请重试' }) }, complete: function (){ wx.hideLoading() } }) }). catch ((error) => { // 502会直接触发这里 wx.showModal({ showCancel: false , content: error.errorMessage || '请求失败请重试' }) }); } module.exports = { fetch } |
又封装了login
//app.js var Promise = require( './utils/promise.js' ) App({ store: { code: '' , openid: '' , userInfo: {} }, init: function () { let _that = this ; return new Promise((resolve, reject) => { wx.login({ success: function (res) { if (res.code) { _that.store.code = res.code var appid = 'wx3c8ed9700a6eb9e4' ; var secret = 'fd693d4689807e6c3f6b30bad8995ab6' wx.request({ url: `https: //api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${res.code}&grant_type=authorization_code`, header: { 'content-type' : 'application/json' }, success: function (data){ _that.store.openid = data.data.openid resolve(_that.store) } }) } }, fail: function (res) { reject(Object.assign(res, { errorMessage: '用户登录失败' })) } }) }).then(data => new Promise((resolve, reject) => { wx.getUserInfo({ success: function (res) { _that.store.userInfo = res.userInfo resolve(_that.store) }, fail: function (res) { reject(Object.assign(res, { errorMessage: '获取用户信息失败' })) } }) })). catch ((error) => { let errorMessage = error.errorMessage || '授权失败' ; wx.showModal({ showCancel: false , content: errorMessage }) }) } }) |
在小程序进入的第一个页面里调用
onShow: function (res) { var _that = this ; app.init().then((res)=>{ console.log(res) utils.fetch({ url: '/user/initData' , method: 'POST' , data: { openid: res.openid }, success: function (res){ _that.setData({ userInfo: res.userInfo, isLogn: res.userInfo.isRegister, isSign: res.userInfo.isSign, goodsList: res.productLists }) } }) }); // this.setData({ // modelShow: res.show // }) } |
手机调试模式,开发者工具里一切都正常,先出现loading,报错之后出现模态框
手机预览和体验版本里wx.loading都出不来
安卓和ios都这样
ios版本11.1.2,微信基础库1.7.1
这是什么情况,求大神解答