背景概述: 第二个和第三个view根据条件判断显示,第一个view根据条件判断是否在任一页面显示。
<view class="root">
<view wx:if="{{(hasRecord || length > 0) && apiFinished }}" class="navigate-wrapper">
***
</view>
<view wx:if="{{hasAuth&& apiFinished}}">
***
</view>
<view wx:if="{{!hasAuth&& apiFinished}}" class="wrapper">
***
</view>
</view>
const { jvyxOptometryAuth, jvyxOptometryScanCode } = require("./services");
const { getPersonalEyeByPin } = require("../optometry/service-optometry");
const {
noLogin,
getPtKey,
getPtPin,
shareAppMessageConfig,
} = require("../../../utils/util");
Page({
/**
* 页面的初始数据
*/
data: {
storeName: "",
bizNumber: "",
isLogin: false,
login_pin: "",
pt_key: "",
apiFinished: false,
hasAuth: false,
hasRecord: false,
length: 0,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
options.q =
"https://jdminiwx.jd.com?storeId=25372208&Id=1234567887654321234565";
let { q } = options;
console.log(options, q);
let params = q.split("?")[1].split("&");
let paramsObj = {};
for (let i of params) {
paramsObj[i.split("=")[0]] = i.split("=")[1];
}
if (noLogin()) {
let returnPage = "/pages/optometryService/showKey/showKey";
wx.redirectTo({
url: `/pages/login/index/index?returnPage=${encodeURIComponent(
returnPage
)}&q=${encodeURIComponent(q)}`,
});
} else {
wx.showLoading({
title: "加载中",
});
jvyxOptometryAuth({})
.then((res) => {
let { hasAuth, hasRecord } = res.data;
hasAuth = true;
this.setData({
hasAuth,
hasRecord,
});
// 值为真则发送两个请求
if (hasAuth) {
return Promise.all([
jvyxOptometryScanCode({ storeId: paramsObj["storeId"] }),
getPersonalEyeByPin({}),
]);
} else {
return getPersonalEyeByPin({});
}
})
.then((res) => {
// promise.all返回的结果为数组,判断返回结果
if (Object.prototype.toString.call(res) === "[object Array]") {
let [resScanCode, resEyeByPin] = res;
const { bizNumber, storeName } = resScanCode.data;
const length = resEyeByPin.data.length;
this.setData({
apiFinished: true,
bizNumber,
storeName,
length,
});
} else {
const { data } = res;
this.setData({
apiFinished: true,
length: data.length,
});
}
wx.hideLoading();
})
.catch((err) => {
this.setData({
apiFinished: true,
});
wx.hideLoading();
});
}
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
return shareAppMessageConfig();
},
toOptometry: function () {
wx.navigateTo({
***
});
},
});
背景:后来改了需求,要求第一个界面navigate-wrapper必须显示,第二个界面判断条件
<view class="root">
<view wx:if="{{(hasAuth || (hasRecord || length > 0)) && apiFinished}}" class="navigate-wrapper">
***
</view>
<view wx:if="{{hasAuth&& apiFinished}}">
***
</view>
<view wx:if="{{!hasAuth&& apiFinished}}" class="wrapper">
***
</view>
</view>
const { jvyxOptometryAuth, jvyxOptometryScanCode } = require("./services");
const { getPersonalEyeByPin } = require("../optometry/service-optometry");
const {
noLogin,
getPtKey,
getPtPin,
shareAppMessageConfig,
} = require("../../../utils/util");
Page({
/**
* 页面的初始数据
*/
data: {
storeName: "",
bizNumber: "",
isLogin: false,
login_pin: "",
pt_key: "",
apiFinished: false,
hasAuth: false,
hasRecord: false,
length: 0,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
options.q =
"https://jdminiwx.jd.com?storeId=25372208&Id=1234567887654321234565";
let { q } = options;
console.log(options, q);
let params = q.split("?")[1].split("&");
let paramsObj = {};
for (let i of params) {
paramsObj[i.split("=")[0]] = i.split("=")[1];
}
if (noLogin()) {
let returnPage = "/pages/optometryService/showKey/showKey";
wx.redirectTo({
url: `/pages/login/index/index?returnPage=${encodeURIComponent(
returnPage
)}&q=${encodeURIComponent(q)}`,
});
} else {
wx.showLoading({
title: "加载中",
});
jvyxOptometryAuth({})
.then((res) => {
let { hasAuth, hasRecord } = res.data;
this.setData({
hasAuth,
hasRecord,
});
if (hasAuth) {
return jvyxOptometryScanCode({ storeId: paramsObj["storeId"] });
} else {
return getPersonalEyeByPin({});
}
})
.then((res) => {
// 上面第一个请求返回的res.data是对象,第二个请求则为array
if (Object.prototype.toString.call(res.data) !== "[object Array]") {
const { bizNumber, storeName } = res.data;
this.setData({
apiFinished: true,
bizNumber,
storeName,
});
} else {
const length = res.data.length;
this.setData({
apiFinished: true,
length,
});
}
wx.hideLoading();
})
.catch((err) => {
this.setData({
apiFinished: true,
});
wx.hideLoading();
});
}
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
return shareAppMessageConfig();
},
toOptometry: function () {
wx.navigateTo({
***
});
},
});