在uni-app中进行第三方登录(微信,qq)

 1 login_weixin() {
 2                 var vm = this;
 3                 uni.getProvider({
 4                     service: 'oauth',
 5                     success: function (res) {
 6                         console.log(res.provider) //qq,xiaomi,sinaweibo,weixin
 7                         vm.log.log1 = res.provider
 8                         if (~res.provider.indexOf('weixin')) {
 9                             uni.login({
10                                 provider: 'weixin', 
11                                 success: function (loginRes) {
12                                     vm.$HTTP({
13                                       method: 'GET',
14                                       baseURL:'https://api.weixin.qq.com/sns/userinfo',
15                                       url: '',
16                                       data: {
17                                           openid:loginRes.authResult.openid,
18                                           access_token:loginRes.authResult.access_token
19                                       },
20                                       load:true
21                                     }).then((data) =>{
22                                       uni.setStorage({
23                                         key: 'userInfo',
24                                         data:{
25                                             name:data.nickname,
26                                             picUrl:data.headimgurl
27                                         }
28                                       });
29                                       uni.switchTab({
30                                         url: "../../tabBar/me/me"
31                                       })
32                                     }, (err) => {
33                                       console.log(err)
34                                     });
35                                 }
36                             });
37                         }
38                     }
39                 });
40             },
41             login_qq() {
42                 var vm = this;
43                 uni.getProvider({
44                     service: 'oauth',
45                     success: function (res) {
46                         console.log(res.provider) //qq,xiaomi,sinaweibo,weixin
47                         vm.log.log1 = res.provider
48                         if (~res.provider.indexOf('qq')) {
49                             uni.login({
50                                 provider: 'qq', 
51                                 success: function (loginRes) {
52                                     vm.$HTTP({
53                                       method: 'GET',
54                                       baseURL:'https://graph.qq.com/user/get_user_info',
55                                       url: '',
56                                       data: {
57                                           openid:loginRes.authResult.openid,
58                                           access_token:loginRes.authResult.access_token,
59                                           appid:'101832674'
60                                       },
61                                       load:true
62                                     }).then((data) =>{
63                                       console.log(data)
64                                       uni.setStorage({
65                                         key: 'userInfo',
66                                         data:{
67                                             name:data.nickname,
68                                             picUrl:data.figureurl_qq
69                                         }
70                                       });
71                                       uni.switchTab({
72                                         url: "../../tabBar/me/me"
73                                       })
74                                     }, (err) => {
75                                       console.log(err)
76                                     });
77                                 }
78                             });
79                         }
80                     }
81                 });
82             }

 

posted @ 2020-08-04 11:36  我有头盔  阅读(3706)  评论(0编辑  收藏  举报