• uniapp微信登陆


    async getuserinfoh5appwx() {
    var _this = this
    var weixinService = null;
    // 参考:http://www.html5plus.org/doc/zh_cn/oauth.html#plus.oauth.getServices
    plus.oauth.getServices(
    async (services) => {
    if (services && services.length) {
    for (var i = 0, len = services.length; i < len; i++) {
    if (services[i].id === ‘weixin’) {
    weixinService = services[i]
    break
    }
    }
    if (!weixinService) {
    console.log(‘没有微信登录授权服务’)
    return
    }

    							//参考: http://www.html5plus.org/doc/zh_cn/oauth.html#plus.oauth.AuthService.authorize
    							// weixinService.authorize(
    							await weixinService.login(
    								async (event) => {
    										uni.showToast({
    											title: '微信登陆成功' + JSON.stringify(event.userInfo),
    											icon: 'none'
    										})
    										await weixinService.getUserInfo(function(e) {
    											plus.nativeUI.alert("获取用户信息成功:" + JSON.stringify(
    												weixinService.userInfo));
    											 uni.request({
    												url: Api.BASEURI + Api.index.appLogin,
    												method: 'POST',
    												header: {
    													'content-type': 'application/x-www-form-urlencoded'
    												},
    												data: {
    													sex: weixinService.userInfo.sex,
    													city: weixinService.userInfo.country + '-' +
    														weixinService.userInfo
    														.province + '-' + weixinService.userInfo
    														.city,
    													source: 1,
    													unionid: weixinService.userInfo.unionid,
    													openid: weixinService.userInfo.openid,
    													nickname: weixinService.userInfo.nickname,
    													headimgurl: weixinService.userInfo.headimgurl
    												},
    												success: res => {
    													plus.nativeUI.alert("接口回调:" + JSON.stringify(res));
    													plus.nativeUI.alert("接口回调res.data:" + JSON.stringify(res.data));
    													// 登录成功 记录会员信息到本地
    													if (res) {
    														console.log(res);
    														// that.login(res.data.data);
    														uni.setStorageSync('userInfos',
    															res.data
    															.data.userInfo);
    														uni.setStorageSync('token', res
    															.data.data
    															.tokenHead + res.data
    															.data.token);
    
    														uni.switchTab({
    															url: '/pages/index/index'
    														});
    													} else {
    														uni.showToast({
    															title: JSON
    																.stringify(res
    																	.data),
    															icon: 'none'
    														});
    													}
    												},
    												fail: e => {
    													console.log(JSON.stringify(e));
    												}
    											});
    										}, function(e) {
    											plus.nativeUI.alert("获取用户信息失败: " + JSON.stringify(e));
    										});
    									},
    									function(error) {
    										console.error('authorize fail:' + JSON.stringify(error))
    									}, {
    										scope: 'snsapi_userinfo',
    										state: 'authorize',
    										appid: Api.WXAPPID,
    										appsecret: Api.appsecret
    									}
    							)
    						} else {
    							console.log('无可用的登录授权服务')
    						}
    					},
    					function(error) {
    						console.error('getServices fail:' + JSON.stringify(error))
    					}
    			)
    		}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
  • 相关阅读:
    使用预约小程序app有什么方便之处
    Vue3之uni-app中注册全局属性案例
    SpringCloud系列(13)--Eureka服务名称修改和服务IP显示
    计算机视觉——飞桨深度学习实战-深度学习网络模型
    hal开发之hidl/aidl支持的绑定式直通式详细讲解
    [附源码]计算机毕业设计JAVA医院门诊信息管理系统
    黑马全套Java教程(十一)
    将轨迹显示到卫星地图上
    JAVA中继承的实现
    c++可变参数模板
  • 原文地址:https://blog.csdn.net/ssorth/article/details/126266561