新鲜的空气需要闻 發表於 2019-8-22 22:27:00

uni-app关于小程序及app端第三方微信登陆问题

<p>1.第一次做第三方微信登陆,所以在这方面话太多时间了,主要是在获取code的时候感觉头痛,uni-app没有说明如何获取code,后来在网上搜索诸多信息后终于解决了问题</p>
<p>uni-app在app端第三方微信登陆时要获取code再传给后端,后端根据code向微信发送登陆请求:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifdef APP-PLUS</span>
<span style="color: rgba(0, 0, 0, 1)">      login() {
            var it</span>=<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
            vargetAppid </span>=<span style="color: rgba(0, 0, 0, 1)"> plus.runtime.appid;
            console.log(</span>'app端登陆'<span style="color: rgba(0, 0, 0, 1)">)
            uni.login({
            provider: </span>'weixin'<span style="color: rgba(0, 0, 0, 1)">,
            success: function (loginRes) {
                console.log(JSON.stringify(loginRes.authResult));
                uni.showModal({
                  content: JSON.stringify(loginRes.authResult),
                  cancelText: </span>"我再想想"<span style="color: rgba(0, 0, 0, 1)">,
                  cancelColor: </span>"#999"<span style="color: rgba(0, 0, 0, 1)">,
                  confirmText: </span>"转让"<span style="color: rgba(0, 0, 0, 1)">,
                  confirmColor: </span>"#DEC17C"<span style="color: rgba(0, 0, 0, 1)">,
                  success: function(res) {
                        
                  }
                })
            }
            });
            var weixinService </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 128, 0, 1)">//</span> <span style="color: rgba(0, 128, 0, 1); text-decoration: underline">http://www.html5plus.org/doc/zh_cn/oauth.html</span><span style="color: rgba(0, 128, 0, 1)">#plus.oauth.getServices</span>
<span style="color: rgba(0, 0, 0, 1)">            plus.oauth.getServices(function(services) {
                console.log(</span>312589340656548<span style="color: rgba(0, 0, 0, 1)">)
                console.log(services)
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (services &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> services.length) {
                  </span><span style="color: rgba(0, 0, 255, 1)">for</span> (var i = 0, len = services.length; i &lt; len; i++<span style="color: rgba(0, 0, 0, 1)">) {
                        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (services.id === 'weixin'<span style="color: rgba(0, 0, 0, 1)">) {
                            weixinService </span>=<span style="color: rgba(0, 0, 0, 1)"> services;
                            console.log(</span>'授权对象'<span style="color: rgba(0, 0, 0, 1)">)
                            console.log(weixinService)
                            </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                        }
                  }
                  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">weixinService) {
                        console.log(</span>'没有微信登录授权服务'<span style="color: rgba(0, 0, 0, 1)">);
                        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
                  }
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span> <span style="color: rgba(0, 128, 0, 1); text-decoration: underline">http://www.html5plus.org/doc/zh_cn/oauth.html</span><span style="color: rgba(0, 128, 0, 1)">#plus.oauth.AuthService.authorize</span>
<span style="color: rgba(0, 0, 0, 1)"><span style="background-color: rgba(255, 102, 0, 1)">                  weixinService.authorize(function(event) {//此处获取code的关键</span>
                        console.log(event)
                        console.log(event.code,</span>'这次是真的授权后返回的code'<span style="color: rgba(0, 0, 0, 1)">)
                        it.weixinCode </span>= event.code; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">用户换取 access_token 的 code
                        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> it.requestLogin();</span>
                        let data=<span style="color: rgba(0, 0, 0, 1)">{code: it.weixinCode};
                        it.$api.user.login.getWeiXinCode(data).then(function(userInfo) {
                            console.log(</span>'是否走到这里'<span style="color: rgba(0, 0, 0, 1)">)
                              console.log(</span>'第三方登录授权'<span style="color: rgba(0, 0, 0, 1)">,it.weixinCode)
                              console.log(</span>'第三方登录授权1111'<span style="color: rgba(0, 0, 0, 1)">,userInfo)
                              it.$store.commit(</span>'isLogged', <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
                              it.$store.commit(</span>'user'<span style="color: rgba(0, 0, 0, 1)">, {});
                              it.$store.commit(</span>'user'<span style="color: rgba(0, 0, 0, 1)">, userInfo.data);
                              uni.navigateBack()
                            }).</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(res =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                            console.log(res)      
                              });
                        
                  }, function(error) {
                        console.error(</span>'authorize fail:' +<span style="color: rgba(0, 0, 0, 1)"> JSON.stringify(error));
                  }, {
                        </span><span style="color: rgba(0, 128, 0, 1)">//</span> <span style="color: rgba(0, 128, 0, 1); text-decoration: underline">http://www.html5plus.org/doc/zh_cn/oauth.html</span><span style="color: rgba(0, 128, 0, 1)">#plus.oauth.AuthOptions</span>
                        appid: getAppid, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开放平台的应用标识。暂时填个假的充数,仅做演示。</span>
<span style="color: rgba(0, 0, 0, 1)">                  });
                } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                  console.log(</span>'无可用的登录授权服务'<span style="color: rgba(0, 0, 0, 1)">);
                }
            }, function(error) {
                console.error(</span>'getServices fail:' +<span style="color: rgba(0, 0, 0, 1)"> JSON.stringify(error));
            });
      },
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #endif</span></pre>
</div>
<p>2. 微信小程序登陆方式 :</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #ifdef MP-WEIXIN</span>
<span style="color: rgba(0, 0, 0, 1)">      getUserInfo(v) {
            let vm </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
            uni.login({
                provider: </span>'weixin'<span style="color: rgba(0, 0, 0, 1)">,
                success: function(loginRes) {
                  uni.getUserInfo({
                        success: function(res) {
                            vm.$api.user.login.weChatAppletLogin(loginRes.code,res.encryptedData,res.iv)
                              .then(function(userInfo) {
                                    vm.$store.commit(</span>'isLogged', <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
                                    vm.$store.commit(</span>'user'<span style="color: rgba(0, 0, 0, 1)">, {});
                                    vm.$store.commit(</span>'user'<span style="color: rgba(0, 0, 0, 1)">, userInfo.data);
                                    uni.navigateBack()
                              }).</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(res =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                              });
                        },
                        fail: function(res) {
                            vm.$store.commit(</span>'isLogged', <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
                        }
                  });
                },
                fail: function(res) {
                }
            });
      }
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> #endif</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/Dark-fire-liehuo/p/11397257.html
頁: [1]
查看完整版本: uni-app关于小程序及app端第三方微信登陆问题