赞
踩
环境配置
"mp-weixin": {
"oauth": {
"weixin": {
"appid": "wxefc8a1dc32c07d79",
"appsecret": "185cf5a667d964a0073342d5af596d11"
}
}
//云函数wxLogin
'use strict';
const uniID = require("uni-id")
exports.main = async (event, context) => {
return await uniID.loginByWeixin({
code: event.code
})
};
<template> <view class="content"> <image class="logo" :src="userInfo.avatarUrl"></image> <view class="text-area"> <text class="nickname">{{userInfo.nickName}}</text> </view> <button type="default" open-type="getUserInfo" @getuserinfo="wxLogin">微信登录</button> </view> </template> <script> export default { data() { return { userInfo:{} } }, onLoad() { }, methods: { getCode() { return new Promise((resolve, reject) => { uni.login({ provider: "weixin", success: (e) => { resolve(e) }, fail: (err) => { reject(new Error("获取code失败")) } }) }) }, getOpenID(code) { return uniCloud.callFunction({ name: "wxLogin", data: { code } }) }, getUserProfile() { return new Promise((resolve, reject) => { uni.getUserProfile({ desc: "获取你的昵称、头像、地区及性别", success: (e) => { resolve(e) }, fail: (err) => { reject(new Error("getUserProfile失败")) } }) }) }, async wxLogin() { var that = this; this.getCode().then(e => { return this.getOpenID(e.code) }).then(e => { console.log(e); const { openid, token, tokenExpired } = e.result; uni.setStorageSync('openid', openid) uni.setStorageSync('uni_id_token', token) uni.setStorageSync('uni_id_token_expired', tokenExpired) uni.showModal({ title: "温馨提示", content: "亲,授权微信登录后才能正常使用小程序功能", success: (e) => { if (e.confirm) { this.getUserProfile().then(e => { console.log(e); this.userInfo=e.userInfo }) } } }) }).catch(err => console.log(err)) } } } </script> <style> .content { display: flex; flex-direction: column; align-items: center; justify-content: center; } .logo { height: 200rpx; width: 200rpx; margin-top: 200rpx; margin-left: auto; margin-right: auto; margin-bottom: 50rpx; } .text-area { display: flex; justify-content: center; color: #00aa7f; } </style>
总结:
uni.setStorageSync('uni_id_token', token)
uni.setStorageSync('uni_id_token_expired', tokenExpired)
<button type="default" open-type="getUserInfo" @getuserinfo="wxLogin">微信登录</button>
uni.showModal({
title: "温馨提示",
content: "亲,授权微信登录后才能正常使用小程序功能",
success: (e) => {
if (e.confirm) {
this.getUserProfile().then(e => {
console.log(e);
this.userInfo=e.userInfo
})
}
}
})
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。