Commit c49d36b0 authored by 谢卓城's avatar 谢卓城

新增:1.无效跳转容错机制.

parent 23278ae7
......@@ -2,7 +2,14 @@
const Request = require("./utils/request.js");
App({
onLaunch: function() {
// 处理无效页面
onPageNotFound(res) {
console.log(res);
wx.switchTab({
url: "/pages/index/index",
});
},
onLaunch: function () {
// 初始化axios
Request.initAxios();
......@@ -11,17 +18,17 @@ App({
// 登录
wx.login({
success: res => {
success: (res) => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
},
});
// 获取用户信息
wx.getSetting({
success: res => {
success: (res) => {
if (res.authSetting["scope.userInfo"]) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
success: (res) => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo;
......@@ -30,58 +37,66 @@ App({
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res);
}
}
},
});
}
}
},
});
},
saveData() {
// 取得设备信息
try {
// const res = wx.getSystemInfoSync()
const that = this
const that = this;
wx.getSystemInfo({
success(res) {
// 根据不同的手机算出状态栏的高度
let custom = null
let custom = null;
try {
custom = wx.getMenuButtonBoundingClientRect()
that.globalData.devicePixelRatio = res.devicePixelRatio
that.globalData.statusBarHeight = res.statusBarHeight
that.globalData.customBarHeight = custom.bottom + custom.top - res.statusBarHeight
that.globalData.menuBotton = custom.top - res.statusBarHeight,
that.globalData.menuRight = res.screenWidth - custom.right, // 胶囊距右方间距(方保持左、右间距一致)
that.globalData.menuHeight = custom.height // 胶囊高度(自定义内容可与胶囊高度保证一致)
that.globalData.menuBottonTop = custom.top + (custom.height - 32) / 2
custom = wx.getMenuButtonBoundingClientRect();
that.globalData.devicePixelRatio = res.devicePixelRatio;
that.globalData.statusBarHeight = res.statusBarHeight;
that.globalData.customBarHeight =
custom.bottom + custom.top - res.statusBarHeight;
(that.globalData.menuBotton = custom.top - res.statusBarHeight),
(that.globalData.menuRight = res.screenWidth - custom.right), // 胶囊距右方间距(方保持左、右间距一致)
(that.globalData.menuHeight = custom.height); // 胶囊高度(自定义内容可与胶囊高度保证一致)
that.globalData.menuBottonTop =
custom.top + (custom.height - 32) / 2;
} catch (e) {
that.globalData.devicePixelRatio = 1 //res.devicePixelRatio
that.globalData.statusBarHeight = 20 //res.statusBarHeight
that.globalData.customBarHeight = 60 //res.statusBarHeight
that.globalData.devicePixelRatio = 1; //res.devicePixelRatio
that.globalData.statusBarHeight = 20; //res.statusBarHeight
that.globalData.customBarHeight = 60; //res.statusBarHeight
}
if (res.screenHeight / res.screenWidth > 2 && res.platform == "android") {
that.globalData.isIphoneX = true
if (
res.screenHeight / res.screenWidth > 2 &&
res.platform == "android"
) {
that.globalData.isIphoneX = true;
}
// console.log('手机信息res' + res.model.search('iPhone X'))
let modelmes = res.model
if (modelmes.search('iPhone X') != -1 || modelmes.search('iPhone XR') != -1) {
that.globalData.isIphoneX = true
let modelmes = res.model;
if (
modelmes.search("iPhone X") != -1 ||
modelmes.search("iPhone XR") != -1
) {
that.globalData.isIphoneX = true;
}
console.log(res.screenHeight, res.screenWidth, res, 'screenWidth')
console.log(res.screenHeight, res.screenWidth, res, "screenWidth");
// console.log(res)
if (opts.query.channel && opts.query.channel !== '') that.globalData.channel = opts.query.channel
if (opts.query.channel && opts.query.channel !== "")
that.globalData.channel = opts.query.channel;
// console.log(that.globalData.channel)
that.globalData.uuid = util.guid()
wx.setStorageSync('uuid', that.globalData.uuid)
wx.setStorageSync('systemInfo', JSON.stringify(res))
}
})
that.globalData.uuid = util.guid();
wx.setStorageSync("uuid", that.globalData.uuid);
wx.setStorageSync("systemInfo", JSON.stringify(res));
},
});
} catch (e) {
// Do something when catch error
}
......@@ -93,8 +108,8 @@ App({
devicePixelRatio: 1,
statusBarHeight: 20,
customBarHeight: 60,
menuBotton : 0,
menuRight : 0, // 胶囊距右方间距(方保持左、右间距一致)
menuHeight: 0
}
menuBotton: 0,
menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
menuHeight: 0,
},
});
......@@ -7,63 +7,63 @@ Page({
data: {
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse("button.open-type.getUserInfo")
canIUse: wx.canIUse("button.open-type.getUserInfo"),
},
//事件处理函数
bindViewTap: function() {
bindViewTap: function () {
// 网络请求
getData()
.then(res => {
.then((res) => {
console.log("发起get请求-res", res);
})
.catch(err => {
.catch((err) => {
console.log("请求失败!" + err);
});
},
onLoad: function() {
onLoad: function () {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
hasUserInfo: true,
});
} else if (this.data.canIUse) {
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
app.userInfoReadyCallback = (res) => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
hasUserInfo: true,
});
};
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
success: (res) => {
app.globalData.userInfo = res.userInfo;
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
hasUserInfo: true,
});
}
},
});
}
},
requestAction(e) {
// 网络请求
getData()
.then(res => {
.then((res) => {
console.log("发起get请求-res", res);
})
.catch(err => {
.catch((err) => {
console.log("请求失败!" + err);
});
},
getUserInfo: function(e) {
getUserInfo: function (e) {
console.log(e);
app.globalData.userInfo = e.detail.userInfo;
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
hasUserInfo: true,
});
}
},
});
......@@ -7,28 +7,28 @@ import axios from "nmaxios";
function initAxios() {
const baseUrl = API_URL.host;
const header = {
"content-type": "application/x-www-form-urlencoded; charset=UTF-8"
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
};
const transformRequest = data => {
const transformRequest = (data) => {
return {
/// 这是使用transformRequest给请求参数加上的字段
//transformRequest: API_URL.xxxx,
...data
...data,
};
};
const transformResponse = res => ({
...res
const transformResponse = (res) => ({
...res,
// transformResponse: "这是使用transformResponse给response加上的字段"
});
const resolveWrap = res => {
const resolveWrap = (res) => {
// console.log("resolveWrap:", "这里可以统一处理resolve状态");
return res.data;
};
const rejectWrap = res => {
const rejectWrap = (res) => {
// console.log("rejectWrap:", "这里可以统一处理reject状态");
return res;
};
const validateStatus = res => {
const validateStatus = (res) => {
console.log(
"validateStatus:",
"使用validateStatus验证请求状态为2xx时才resolve"
......@@ -43,7 +43,7 @@ function initAxios() {
transformRequest,
transformResponse,
resolveWrap,
rejectWrap
rejectWrap,
});
}
......@@ -56,11 +56,11 @@ const req = {
return new Promise((resolve, reject) => {
axios
.get(opts.url)
.then(res => {
.then((res) => {
// console.log("发起get请求-res", res);
resolve(res);
})
.catch(err => {
.catch((err) => {
// console.log("发起get请求-err", err);
reject(err);
});
......@@ -76,20 +76,20 @@ const req = {
return new Promise((resolve, reject) => {
axios
.post(opts.url, opts.data)
.then(res => {
.then((res) => {
// console.log("发起post请求-res", res);
resolve(res);
})
.catch(err => {
.catch((err) => {
// console.log("发起post请求-err", err);
reject(err);
});
});
}
},
};
module.exports = {
initAxios,
axiosGet: req.get,
axiosPost: req.post
axiosPost: req.post,
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment