user-center.js 6.39 KB
import {
  getBindtapData
} from '../../utils/util';

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    commonTipsCompVisible: false,
    signTipsCompVisible: false,
    giftSignTipsCompVisible: false,
    innerTitle: "",
    innerText: "",
    // innerButton: "",
    userInfo: {}, // 用户数据
    signInfo: [], // 签到数据
    signNum: 0, // 签到天数
    signPrize: {}, //签到奖励
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onShow() {
    this.hideMask();
    this.initData();
  },
  onLoad(options) {
    // app.router.push({
    //   path: "webview"
    // })
  },
  initData() {
    this.queryMember().then((result) => {
      let subscriptionOpenid = result.subscriptionOpenid;
      if (subscriptionOpenid) {
        // member表 已有openid 继续流程
        this.querySignInfo();
      } else {
        // member表无openid ,重h5授权回来的local拿,成功就继续往下走
        this.querySaveOpenid().then((result) => {
          this.queryMember().then((result) => {
            this.querySignInfo();
          })
        }).catch((err) => {
          // 如果均没有,则去h5授权
          app.router.push({
            path: "webview"
          })
        });
      }
    });
  },

  /**
   * 保存openid
   */
  querySaveOpenid() {
    return new Promise((resolve, reject) => {
      let openid = app.store.getItem("openid");
      if (openid) {
        app.post({
          toast: false,
          url: app.api.saveOpenid,
          data: {
            subscriptionOpenid: openid
          }
        }).then((result) => {
          console.log("result:", result);
          app.store.clear("openid");
          resolve();
        }).catch((err) => {
          reject();
        });
      } else {
        reject();
      }
    });
  },

  /**
   * 点击签到
   */
  onSignHandler(evt) {
    console.log("onSignHandler");
    return new Promise((resolve, reject) => {
      app.post({
        toast: false,
        url: app.api.sign,
      }).then((result) => {
        this.queryMember().then((result2) => {
          this.querySignInfo().then((result3) => {
            // 签到成功
            this.setData({
              signPrize: result,
              giftSignTipsCompVisible: true
            })
          })
        })
      }).catch((err) => {
        let {
          code
        } = err;
        // 用户未审核
        if (code == 1002) {
          this.setData({
            signTipsCompVisible: true
          })
        }
      });
    });
  },


  /**
   * 签到记录
   */
  onsignInRecordHandler(evt) {
    app.router.push({
      path: "signInRecord"
    })
  },

  /**
   * 积分详情
   */
  onIntegralDetailHandler(evt) {
    app.router.push({
      path: "integralDetail"
    })
  },

  /**
   * 我的订单
   */
  onMyOrderHandler(evt) {
    let navIndex = getBindtapData(evt, "index");
    app.router.push({
      path: "myOrder",
      query: {
        navIndex: navIndex
      }
    })
  },

  /**
   * 我的二维码
   */
  onMyQrcodeHandler(evt) {
    app.router.push({
      path: "myQrcode"
    })
  },

  /**
   * 排行榜
   */
  onRankHandler(evt) {
    app.router.push({
      path: "rank"
    })
  },

  /**
   * 地址管理
   */
  onAddressHandler(evt) {
    app.router.push({
      path: "addressManagement"
    })
  },

  /**
   * 我的消息
   */
  onMyMessageHandler(evt) {
    app.router.push({
      path: "myMessage"
    })
  },

  /**
   * 点击联系我们
   */
  onContactUsHandler(evt) {
    app.router.push({
      path: "contact"
    })
  },

  /**
   * 点击拍卖
   */
  onAuctionHandler(evt) {
    app.router.push({
      path: "auctionList"
    })
  },

  /**
   * 点击秒杀
   */
  onSeckillHandler(evt) {
    app.router.push({
      path: "seckillList"
    })
  },

  /**
   * 获取会员信息
   */
  queryMember() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.member,
        data: {}
      }).then((result) => {
        this.setData({
          userInfo: result
        })
        app.globalData.userInfo = result;
        resolve(result);
      })
    });
  },

  /**
   * 签到信息
   */
  querySignInfo() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.signInfo,
        data: {}
      }).then((result) => {
        let signNum = result.signRecord && result.signRecord.length || 0;
        console.log("signNum:", signNum);
        let resultList = result.signRecord || [];
        let signInfo = [];
        for (let index = 0; index < 7; index++) {
          signInfo.push(resultList[index] ? resultList[index] : "");
        }
        this.setData({
          signInfo: signInfo,
          signNum: signNum
        })
        console.log("signInfo:", signInfo);
        resolve();
      })
    });
  },

  /**
   * 去验证
   */
  toVipLoginHandler() {
    app.router.push({
      path: "vipLogin"
    })
  },

  /**
   * 查看审核
   */
  toVipVerifyHandler() {
    app.router.push({
      path: "vipVerify"
    })
  },

  /**
   * 检查验证修改
   */
  checkVerify() {
    let auditStatus = this.data.userInfo && this.data.userInfo.auditStatus || "";
    // console.log("@checkVerify || auditStatus:", auditStatus);
    if (auditStatus == "authorization") {
      // 已经验证 去修改页,查看审核
      this.toVipVerifyHandler();
    } else if (auditStatus == "unauthorized") {
      // 已经验证 去修改页,查看审核
      this.toVipVerifyHandler();
    } else {
      // 未验证 去编辑页,填写表单
      this.toVipLoginHandler();
    }
  },

  // 隐藏蒙层
  hideMask() {
    this.setData({
      authorizeVisible: false,
      commonTipsCompVisible: false,
      signTipsCompVisible: false,
      giftSignTipsCompVisible: false
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    console.log("name:", name)
    switch (name) {

      // 授权完毕
      case "_evt_auth_complete":
        this.initData();
        this.hideMask();
        break;

        // 通用弹窗组件按钮
      case "_evt_common_comp_button":
        this.hideMask();
        break;

        // 去验证
      case "_evt_to_verify":
        this.hideMask();
        this.toVipLoginHandler();
        break;

        // 隐藏弹窗
      case "_evt_hide_mask":
        this.hideMask();
        break;

      default:
        break;
    }
  },
})