微信小程序 登录 获取授权登录

很简洁的版本了吧,记忆力奇差,先存起来忘记了自己再点开看

<template>
    <view class="container">
    // 这里面的东西不要漏
        <button type="primary" open-type="getUserInfo" bindgetuserinfo="handleUserInfo">登录</button>
    </view>
</template>

<script>
import wepy from "wepy"
import request from "../utils/request"
import authManage from "../utils/authManage"

export default class Login extends wepy.page {

    data = {
        dataSource: {
            code: "",
            encryptedData: "",
            iv: "",
            rawData: "",
            signature: ""
        }
    }

    onLoad(){
        // 登录
        wx.login({
            success: (res) => {
                if (res.code) {
                this.dataSource.code = res.code
                }
            }
        })
    }

    handleUserInfo(res){
        const {detail} = res;

        // 存储登录接口需要的参数
        this.dataSource = {
            code: this.dataSource.code,
            encryptedData: detail.encryptedData,
            iv: detail.iv,
            rawData: detail.rawData,
            signature: detail.signature
        }

        // 提交到登录接口
        request({
            url: "api/public/v1/users/wxlogin",
            data:  this.dataSource,
            method: 'POST',
            success: res => {
                const {data} = res.data;
                authManage.setToken(data.token);

                wx.navigateBack()
            }
        })
    }
}
</script>

<style lang="less">
    .container{
        display: flex;
        position: absolute;
        width:100%;
        top:0;
        bottom:0;
        align-items: center;
        padding:0 20rpx;
        box-sizing: border-box;

        button{
            width:100%;
        }
    }
</style>

猜你喜欢

转载自blog.csdn.net/Q_MUMU/article/details/85251273