<style lang="scss">
    /* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
    @import "uview-ui/index.scss";
</style>
<script>
    export default {
        onLaunch: function () {
            console.log('App Launch')
        },
        onShow: function () {
            console.log('App Show 小程序显示了+++++++++++++++++++++++')
            let that = this;
            wx.login({
                success(re) {
                    that.$api.wxLogin({code: re.code}).then(res => {
                        console.log('++++++++++登录返回了++++++++++++++++', res)
                        uni.setStorageSync('userInfo', res.data.data.userInfo)
                        uni.setStorageSync('accessToken', res.data.data.access_token)
                        let isNew = res.data.data.isNew
                        uni.removeStorageSync('parent_member_id');


                        //用户点击用户分享进入小程序，进行绑定操作
                        let shareUserId = uni.getStorageSync('shareUserId')
                        if (shareUserId) {
                            that.$api.scanCode({
                                openId: res.data.data.userInfo.otherId,
                                memberId: shareUserId
                            }).then(res => {
                                console.log('用户分享用户绑定成功', res)
                                uni.removeStorageSync('shareUserId');
                            })
                        }


                        //用户扫码拓客端进入小程序，携带的参数，进行绑定操作
                        let expandParam = uni.getStorageSync('expandParam')
                        if (expandParam) {


                            console.log('获取到的拓客端参数',expandParam)
                            if (new Date().getTime() <= expandParam.timestamp){
                                if (isNew){
                                  //登录成功，且是新用户，触发首页的自定义事件
                                  uni.$emit("loginSuccess")
                                    //用户扫码拓客端进入小程序
                                    // that.$api.userBindMember(
                                    //     {
                                    //         ...expandParam,
                                    //         memberId: res.data.data.userInfo.id
                                    //     }
                                    // ).then(res1 => {
                                    //     console.log('拓客端推广用户绑定成功！')
                                    //
                                    //     that.$api.addRecordForAttach({
                                    //         ...expandParam,
                                    //         isNew:1,
                                    //         isBindSuccess:1,
                                    //     }).then(res=>{
                                    //         console.log('+++++++++新用户，扫码推广成功记录！++++++++++++++++')
                                    //     })
                                    //
                                    //     uni.removeStorageSync('expandParam');
                                    // }).catch(err=>{
                                    //
                                    //     that.$api.addRecordForAttach({
                                    //         ...expandParam,
                                    //         isNew:1,
                                    //         isBindSuccess:0,
                                    //     }).then(res=>{
                                    //         console.log('+++++++++新用户，扫码推广失败记录！++++++++++++++++')
                                    //     })
                                    //
                                    //     uni.removeStorageSync('expandParam');
                                    // })
                                }else {

                                    that.$api.addRecordForAttach({
                                        ...expandParam,
                                        isNew:0,
                                        isBindSuccess:0,
                                    }).then(res=>{
                                        console.log('+++++++++老用户，扫码推广失败记录！++++++++++++++++')
                                    })

                                    uni.removeStorageSync('expandParam');
                                }

                             }else {
                                uni.showModal({
                                    title: '温馨提示',
                                    content: ' 当前二维码已过期，请重新扫码',
                                    showCancel:false,
                                    success: function (res) {
                                        if (res.confirm) {
                                            console.log('用户点击确定');
                                        } else if (res.cancel) {
                                            console.log('用户点击取消');
                                        }
                                    }
                                });
                                uni.removeStorageSync('expandParam');
                            }

                        }

                        //用户扫码技师端进入小程序，携带的参数。 暂无操作
                        let promotionParam = uni.getStorageSync('promotionParam')


                    })
                },
            })


        },
        onHide: function () {
            console.log('App Hide')
        }
    }
</script>


