小白开发微信小程序48--退出登录页面实现

1、js文件

// pages/personal/personal.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogined: false,//登录标志,默认未登录
        userInfo: {},//用户信息对象
        useravatarUrl: "",//头像地址
        usernickName: "",//昵称
        userCode: "",//用户code  
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.startLogin();
    },

    //开始登录
    startLogin() {
        let that = this;
        const token = wx.getStorageSync('token');//获取存储的数据
        //判断token
        if (!token) {
            wx.showModal({
                title: "友情提示",
                content: "您同意授权微信登录,才可以进入个人中心",
                confirmText: "同意",
                confirmColor: "rgb(18,194,94)",
                cancelText: "拒绝",
                cancelColor: "darkorange",
                success: (res) => {
                    if (res.confirm) {
                        that.doLogin();
                    } else if (res.cancel) {
                        wx.showToast({
                            title: '您已拒绝',
                            icon: "error",
                            duration: 2000
                        })
                    }
                }
            });
        }
        else {
            const storeuserinfo = wx.getStorageSync('userInfo');
            that.setData({
                userInfo: storeuserinfo,
                isLogined: true
            });
        }
    },
    //开始登录
    doLogin() {
        let that = this;
        //1、获取用户信息:头像,昵称
        wx.getUserProfile({
            desc: 'desc',
            success: (res) => {
                //   console.log(res);
                //设置页面数据变量的值
                that.setData({
                    userInfo: res.userInfo,
                    useravatarUrl: res.userInfo.avatarUrl,
                    usernickName: res.userInfo.nickName
                });
                //存储页面信息 
                wx.setStorageSync('userInfo', res.userInfo);
                //2,获取用户的code
                wx.login({
                    success: (res) => {
                        if (res.code) {
                            //设置页面数据变量的值
                            that.setData({
                                userCode: res.code
                            });
                            //构建登录参数
                            let tempparam = {
                                useravatarUrl: that.data.useravatarUrl,
                                usernickName: that.data.usernickName,
                                userCode: that.data.userCode
                            }
                            //小程序登录
                            that.wxLogin(tempparam);
                        }
                    },
                })
            }
        })
    },

    //微信登录
    wxLogin(loginpara) {
        let that = this;
        // console.log(loginpara);
        wx.request({
            url: 'https://localhost:5601/api/WeiXin/wxUserLogin',
            //必须将Js对象转换成json字符串
            data: JSON.stringify(loginpara),
            method: "POST",
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(res) {
                //console.log(res.data);
                if (res.data != "error") {
                    that.setData({
                        isLogined: true //将登录状态改成已登录
                    });
                    //存储openid
                    wx.setStorageSync('token', res.data);
                    wx.showToast({
                        title: '登录成功!',
                        icon: "success",
                        duration: 2000
                    })
                }

            }
        })
    },
    //退出登录
    logout() {
        let that = this;
        wx.setStorageSync('token', "");
        wx.setStorageSync('userInfo', "");
        that.setData({
            isLogined: false,
            userInfo: {}
        });
        wx.showToast({
            title: '退出成功!',
            icon: "success",
            duration: 2000
        })
    },

    //收货地址
    handleaddress() {
        wx.chooseAddress({
            success: (result) => {

            }
        });
    },
    
    //联系我们
    contactus() {
        wx.navigateTo({
            url: '/pages/we/we',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})

2、效果

3、数据中记录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hqwest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值