headerVip.js 8.33 KB
import componentsMixin from "../../../../../mixins/componentsMixin";
import { defaultMerchantId } from "../../../../../api/environment";
import { equityApi, ssoApi } from "../../../../../api/api";
Component({
    behaviors: [componentsMixin],
    properties:{
        freshVIP:{
            type:Boolean,
            observer:function(v){
                if(v){
                    this.toCheckMember()
                }
            }
        }
    },
    data: {
        memberType: 1,
        vipInfo: null,
        showAuth: false
    },
    methods: {
        toLookPackage(e) {
            const { item } = e.currentTarget.dataset
            if (!item.buyStatus) {
                this.toOpenPage(0, item, true)
                return
            }
            let temp = JSON.stringify(item)
            wx.navigateTo({
                url: "/packageEquity/pages/vipPackage/vipPackage?item=" + temp,
            });
        },
        authSuccess(e) {
            let {
                data
            } = e.detail
                /**调取后端接口储存用户头像昵称 */
            if (data) {
                ssoApi.authUserInfo({
                    ...data
                }).then(({
                    code
                }) => {
                    if (code == '0') {
                        getApp().login().then(({
                            data
                        }) => {
                            wx.$util.setStorageSync("srcm_shopmall_user_info",data);
                            this.setData({
                                userInfo: data
                            })
                        })
                    }
                })
            }
        },
        showAuthModal() {
            this.setData({
                showAuth: true
            })
        },
        toCheckPackage(e) {
            const { index, item } = e.currentTarget.dataset
            if (item.buyStatus) {
                this.toLookPackage(e)
                return
            }
            this.toOpenPage(index, item, true)
        },
        toNotePage() {
            wx.navigateTo({
                url: "/packageEquity/pages/note/note",
            });
        },
        toOpenPage(index, item, flag) {
            let str = `?memberType=${this.data.memberType}` + `&index=${flag?index:null}` + `&item=${flag?JSON.stringify(item):null}`
            wx.navigateTo({
                url: "/packageEquity/pages/open/open" + str
            });
        },
        // 检测是否已经登录
        checkLogin() {
            const sessionId = wx.$util.getSessionId();
            let flag = true;
            if (!sessionId) {
                flag = false;
                this.setData({
                    showLogin: true,
                });
            }
            return flag; //fasle 未登录  true已登录
        },
        // 获取门店会员卡信息
        getMemberCardData() {
            const merchantId = wx.$util.getStorageSync("srcm_shopmall_merchant_id");
            let newMerchantId = merchantId || defaultMerchantId;
            let para = {
                merchantId: newMerchantId,
            };
            equityApi.memberCard(para).then((res) => {
                wx.$util.setStorageSync('vip_card_info', res)
                this.setData({
                    memberInfo: res,
                });
            });
        },
        // 获取用户/门店信息
        getUserinfo() {
            let userInfo = wx.$util.getStorageSync("srcm_shopmall_user_info");
            const merchantId = wx.$util.getStorageSync("srcm_shopmall_merchant_id");
            this.setData({
                userInfo,
                merchantId
            });
        },
        // 获取用户会员信息
        toCheckMember() {
            const isLogin = this.checkLogin()
            wx.removeStorageSync('equity_info')
            if (!isLogin) {
                this.matchVipLevel(false, this.data.packageLen);
                return
            }
            equityApi.checkOpenMember().then((res) => {
                let flag = false;
                let homeName = ''
                if (res.data && res.data.validitDate) {
                    res.data.time = new Date(res.data.validitDate).format("yyyy/MM/dd");
                }
                if (res.code == 0 && res.data) {
                    flag = true;
                }
                if (res.data) {
                    // cardType:权益卡类型 0:体验卡 1:金卡 2:黑卡
                    // this.data.buyStatus = res.data.packageList && res.data.packageList.length ? true : false
                    const {cardType,packageList,goldCardClassName,cardClassName,memberName,activeType} = res.data
                    this.data.buyStatus = cardType ==2
                    const packageName = packageList.length?packageList[0].packageName:''
                    homeName = cardType==2?packageName:cardType==1?goldCardClassName:cardClassName
                    homeName = activeType==0&&flag?goldCardClassName:homeName
                }
                this.matchVipLevel(flag, this.data.packageLen);
                wx.$util.setStorageSync('equity_info', res.data)
                wx.setStorageSync('isVIP', flag)
                this.setData({
                    vipInfo: res.data,
                    homeName
                });
            });
        },
        // 新版本---根据会员身份匹配对应的值
        matchVipLevel(isOpen, len) {
            let havePackage = this.data.buyStatus;
            let obj = this.vipRule(isOpen, len, havePackage)
            this.setData(obj);
        },
        // 会员定义规则
        vipRule(isOpen, len, havePackage) {
            let showMore = len > 1
                // 没有开通会员&没有套餐列表
            const type1 = !isOpen && !len
                // 有开通会员&没有套餐列表
            const type2 = isOpen && !len
                // 没有开通会员&有套餐列表
            const type3 = !isOpen && len
                // 有开通会员&有套餐列表&没有购买过套餐
            const type4 = isOpen && !havePackage
                // 有开通会员&有套餐列表&有购买套餐
            const type6 = isOpen && havePackage
            const arr = [type1, type2, type3, type4, type6]
            let tempIndex = 1
            arr.forEach((item, index) => {
                if (item) {
                    tempIndex = index + 1
                }
            })
            tempIndex = tempIndex==5?6:tempIndex
            const obj = {
                showMore,
                memberType: tempIndex
            }
            return obj
        },
        // 05版本---获取会员套餐列表
        toGetPackage() {
            let { userInfo, merchantId } = this.data
            let newMerchantId = merchantId || defaultMerchantId;
            let para = {
                merchantId: newMerchantId,
                pageNum: 1,
                pageSize: 100,
                state: 1
            }
            if (userInfo) {
                para.accountId = userInfo.accountId
            }
            equityApi.equityPacks(para).then((res) => {
                if (res.list.length == 1) {
                    let str = ''
                    res.list[0].list.forEach((item, index) => {
                        str = str + item.couponName + `${index==res.list[0].list.length-1?'':'+'}`
                    })
                    res.list[0].desc = str
                }
                this.data.canShow = true
                wx.$util.setStorageSync('packageList', res.list)
                this.setData({
                    packageList: res.list || [],
                    packageLen: res.list.length || 0,
                }, () => {
                    this.toCheckMember()
                });
            })

        },
        componentHttp(){
            this.getUserinfo()
            this.getMemberCardData();
            this.toGetPackage();
        }
    },
    lifetimes:{
        attached(){
            this.componentHttp()
            // 添加订阅者EQUITY
            wx.$eventbus.on('EQUITY',()=>{
              this.componentHttp()
            })
        }
    }
    // pageLifetimes: {
    //     show: function() {
    //         if (!this.data.canShow) {
    //             return
    //         }
    //         this.getUserinfo()
    //         this.getMemberCardData();
    //         this.toGetPackage();
    //     }
    // }
});