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

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

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