<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>
关于uniapp 开发小程序登录
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...