关于uniapp 开发小程序登录

<template>
    <view>
        <view>
            <view>
                <view class="header">
                    <image src="../../../static/image/link_01.jpg"></image>
                </view>
                <view class="content">
                    <view>申请获取以下权限</view>
                    <text>获得你的公开信息(昵称,头像、地区等)</text>
                </view>
                <button class="bottom" type="primary" open-type="getUserInfo" :withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">授权登录</button>
            </view>
        </view>
    </view>
</template>

<script>
    // 请求方法
    import {
        Get,
        
    } from '../../../unit/api.js';
    
    import {
        url
    } from '@/config/config'
    export default {
        data() {
            return {
                // 用户的id
                uid: 0,
                SessionKey: '',
                OpenId: '',
                encryptedData: null,
                iv: null,
                spread: null,
                isCanUse: uni.getStorageSync('isCanUse') || true, //默认为true
                code: null,
                token: '442424234'
            };
        },

        methods: {
        
            wxGetUserInfo() {
                uni.getSetting({
                    success: (res) => {
                        if (res.authSetting['scope.userInfo']) {
                            uni.showLoading({
                                title: '登录中...'
                            });
                        }
                    }
                })

                let _this = this;
                uni.getUserInfo({
                    provider: 'weixin',
                    success: function(infoRes) {
                        console.log(infoRes, '用户信息');
                        
                       uni.setStorageSync('nickName',infoRes.userInfo.nickName);
                        uni.setStorageSync('avatar',infoRes.userInfo.avatarUrl);
                            // uni.setStorageSync({
                            //   'nickName',userInfo.nickName})
                        _this.encryptedData = infoRes.encryptedData;
                        _this.iv = infoRes.iv;
                        _this.spread = infoRes.userInfo.gender;
                        console.log(_this.encryptedData);
                        console.log(_this.iv);
                        //2.将用户登录code传递到后台置换用户SessionKey、OpenId等信息
                        uni.request({
                            url: `${url}`,
                            data: {
                                code: _this.code,
                                spread: _this.spread,
                                iv: _this.iv,
                                encryptedData: _this.encryptedData
                            },
                            method: 'POST',
                            header: {
                                'content-type': 'application/json'
                            },
                            success: res => {
                                setTimeout(function() {
                                    uni.hideLoading();
                                }, 1000);
                                _this.token = res.data.data.token;
                                console.log(res,'登录信息');
                                uni.setStorage({
                                    key: 'storage_key',
                                    data: _this.token,
                                    success() {
                                                                    //把token 存入本地
                                        const token = uni.getStorageSync('storage_key');
                                        _this.Getuserdetails();  
                                        // 判断是否登录
                                        if (token) {
                                            uni.navigateBack({
                                                delta: 1,
                                            })
                                        }
                                    }
                                });

                            }
                        });
                    },
                });
            }, //登录

            login() {
                let _this = this;

                // 1.wx获取登录用户code
                uni.login({
                    provider: 'weixin',
                    success: function(loginRes) {
                        console.log(loginRes)
                        _this.code = loginRes.code;
                    }
                });
            },

            // 获取用户详情
            Getuserdetails() {
                // 获取用户详情
                Get("url").then(res => {
                    console.log(res);
                    this.uid = res.data.uid;
                    
                    uni.setStorageSync(
                         'uid',
                     res.data.uid
                    );
                });
            }
        },
        onUnload() {},
        onLoad() {
            //默认加载
            this.login();
        }
    };
</script>

<style lang="less">
    .header {
        margin: 90rpx 0 90rpx 50rpx;
        border-bottom: 1px solid #ccc;
        text-align: center;
        width: 650rpx;
        height: 300rpx;
        line-height: 450rpx;
    }

    .header image {
        width: 200rpx;
        height: 200rpx;
    }

    .content {
        margin-left: 50rpx;
        margin-bottom: 90rpx;
    }

    .content text {
        display: block;
        color: #9d9d9d;
        margin-top: 40rpx;
    }

    .bottom {
        border-radius: 80rpx;
        margin: 70rpx 50rpx;
        font-size: 35rpx;
    }
</style>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。