微信小程序复杂条件判断按钮显示隐藏

背景概述: 第二个和第三个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({
      ***    
      });
  },
});

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值