小程序-微信登录
XML/HTML Code复制内容到剪贴板
- <button type="default" :plain="true" @click="login">
- <text class="login-btn-text gui-color-white">微信登录</text>
- </button>
JavaScript Code复制内容到剪贴板
- methods: {
- async login() {
- let userInfo = await this.mpGetUserInfo();
- // console.log("用户信息",userInfo)
- uni.login({
- provider: this.loginProvider,
- // #ifdef MP-ALIPAY
- scopes: 'auth_user', //支付宝小程序需设置授权类型
- // #endif
- success: async (res) => {
- // console.log('login success:', res);
- this.Toast({
- title: '登录成功'
- })
- // #ifdef MP-WEIXIN
- let loginRes = await this.$store.dispatch("user/login",{
- ...userInfo,
- code:res.code
- })
- console.log(loginRes)
- // #endif
- },
- fail: (err) => {
- console.log('login fail:', err);
- // 一键登录点击其他登录方式
- if (err.code == '30002') {
- uni.closeAuthView();
- this.Toast({
- title: '其他登录方式'
- })
- return;
- }
- },
- complete: () => {
- this.univerifyBtnLoading = false;
- }
- });
- },
- async mpGetUserInfo() {
- return await new Promise((resolve, reject) => {
- console.log(11)
- // 获取用户信息
- uni.getUserProfile({
- desc: "仅获取您的昵称、头像等",
- lang: "zh_CN",
- success: (result) => {
- // console.log("用户信息",result)
- const userInfo = {...result.userInfo}
- userInfo.avatar = userInfo.avatarUrl
- delete userInfo.avatarUrl
- // console.log(userInfo)
- resolve(userInfo)
- },
- fail: (error) => {
- console.log('getUserInfo fail', error);
- let content = error.errMsg;
- if (~content.indexOf('uni.login')) {
- content = '请在登录页面完成登录操作';
- }
- uni.getSetting({
- success: (res) => {
- let authStatus = res.authSetting['scope.userInfo'];
- if (!authStatus) {
- uni.showModal({
- title: '授权失败',
- content: 'Hello uni-app需要获取您的用户信息,请在设置界面打开相关权限',
- success: (res) => {
- if (res.confirm) {
- uni.openSetting()
- }
- }
- })
- } else {
- // 拒绝后的回调,这个弹窗应该关掉
- // uni.showModal({
- // title: '获取用户信息失败',
- // content: '错误原因' + content,
- // showCancel: false
- // });
- }
- }
- })
- reject(error.errMsg);
- },
- complete: () => {
- this.btnLoading = false;
- }
- });
- })
- },
- Toast(data, duration = 1000) {
- uni.showToast(Object.assign({}, data, {
- duration
- }))
- },
上一篇 uniapp小程序对接腾讯云直播
下一篇 点赞、收藏,放大缩小动画三端通用