1 |
- (window["webpackJsonp"]=window["webpackJsonp"]||[]).push([["chunk-8e64b284"],{"5f41":function(e,t,r){"use strict";r("6044")},6044:function(e,t,r){},9341:function(e,t,r){"use strict";r.r(t);var a=function(){var e=this,t=e._self._c;return t("div",{staticClass:"wechat-auth-container"},[t("div",{staticClass:"auth-content"},["loading"===e.authStatus?t("div",{staticClass:"auth-status"},[t("van-loading",{attrs:{type:"spinner",color:"#1989fa"}}),t("p",[e._v("正在授权中...")])],1):"error"===e.authStatus?t("div",{staticClass:"auth-status"},[t("van-icon",{attrs:{name:"cross",color:"#ee0a24",size:"48"}}),t("p",[e._v("授权失败")]),t("p",{staticClass:"error-message"},[e._v(e._s(e.errorMessage))]),t("van-button",{attrs:{type:"primary"},on:{click:e.retryAuth}},[e._v("重新授权")])],1):t("div",{staticClass:"auth-status"},[t("van-icon",{attrs:{name:"wechat",color:"#07c160",size:"48"}}),t("p",[e._v("微信授权")]),t("p",{staticClass:"auth-desc"},[e._v("请点击下方按钮进行微信授权")]),t("van-button",{attrs:{type:"primary"},on:{click:e.startAuth}},[e._v(e._s(e.Loading?"授权中...":"开始授权"))])],1)])])},n=[],o=r("c7eb"),c=r("1da1"),s=r("5530"),i=(r("d9e2"),r("99af"),r("caad"),r("e9c4"),r("d3b7"),r("ac1f"),r("2532"),r("3ca3"),r("466d"),r("5319"),r("ddb0"),r("9861"),r("88a7"),r("271a"),r("5494"),r("2f62")),u=r("b775"),d="https://pbl.cocorobo.cn/api/cocoflow/";function h(e){return Object(u["a"])({url:d+"wechat-get-user-info",method:"POST",data:e,hideloading:!1})}function l(e){return Object(u["a"])({url:d+"wechat-register",method:"POST",data:e,hideloading:!1})}var p=r("852e"),f=r.n(p),g={name:"WechatAuth",data:function(){return{authStatus:"init",userInfo:{},errorMessage:"",redirect:"",code:"",Loading:!1}},computed:{wechatConfig:function(){var e=this.$route.query.url?decodeURIComponent(this.$route.query.url):null;return console.log("redirectUri from query:",e),e&&"undefined"!==e&&f.a.set("redirectUri",e),{appId:"wx2d69589899b7ecd6",scope:"snsapi_userinfo",redirectUri:encodeURIComponent(window.location.origin+"/login"),state:"wechat_auth"}}},watch:{$route:{handler:function(e){var t,r=window.location.href,a=new URLSearchParams((null===(t=r.split("?")[1])||void 0===t?void 0:t.split("#")[0])||"");this.code=a.get("code")||e.query.code,this.state=a.get("state")||e.query.state,this.redirect=e.query.redirect||this.extractRedirectFromHash(),console.log("URL参数解析:",{url:r,code:this.code,state:this.state,redirect:this.redirect,routeQuery:e.query})},immediate:!0}},methods:Object(s["a"])(Object(s["a"])({},Object(i["b"])({login:"user/login",getInfo:"user/getInfo"})),{},{startAuth:function(){if(!this.Loading){this.authStatus="loading";var e="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".concat(this.wechatConfig.appId,"&redirect_uri=").concat(this.wechatConfig.redirectUri,"&response_type=code&scope=").concat(this.wechatConfig.scope,"&state=").concat(this.wechatConfig.state,"#wechat_redirect");window.location.href=e}},handleAuthCallback:function(){var e=this;return Object(c["a"])(Object(o["a"])().mark((function t(){var r,a,n,s,i,u;return Object(o["a"])().wrap((function(t){while(1)switch(t.prev=t.next){case 0:if(e.code){t.next=4;break}return e.authStatus="error",e.errorMessage="未获取到授权码",t.abrupt("return");case 4:return t.prev=4,e.authStatus="loading",e.Loading=!0,t.next=9,e.exchangeCodeForToken(e.code);case 9:if(r=t.sent,console.log(r),!r.openid){t.next=25;break}return e.userInfo=r,e.authStatus="success",t.next=16,l({username:r.userInfo.nickname,mail:r.openid+"@wechat.com",openid:r.openid+"-wechat"});case 16:a=t.sent,console.log(a),n=JSON.stringify({openid:r.openid+"-wechat",edu:!0}),s=new Headers,s.append("Content-Type","application/json"),i={method:"POST",headers:s,body:n,redirect:"follow",credentials:"include"},fetch("https://beta.api.cocorobo.cn/api/user",i).then((function(e){return e.text()})).then(Object(c["a"])(Object(o["a"])().mark((function t(){var r;return Object(o["a"])().wrap((function(t){while(1)switch(t.prev=t.next){case 0:return t.next=2,e.login();case 2:r=f.a.get("redirectUri"),r&&"undefined"!==r||(r=""),e.Loading=!1,console.log(r),r?window.location.href=r:e.$router.replace("/appStoreCopy");case 7:case"end":return t.stop()}}),t)})))).catch((function(t){e.Loading=!1,console.log("error",t),setTimeout((function(){e.$router.replace("/login2")}),2e3)})),t.next=27;break;case 25:throw e.Loading=!1,new Error(r.message||"授权失败");case 27:t.next=44;break;case 29:return t.prev=29,t.t0=t["catch"](4),console.error("微信授权失败:",t.t0),e.authStatus="error",e.Loading=!1,e.errorMessage=t.t0.message||"授权失败,请重试",e.$message.error(e.errorMessage),t.next=38,e.getLogin();case 38:if(u=t.sent,"1"!=u){t.next=43;break}return t.abrupt("return");case 43:e.$router.replace("/login2");case 44:case"end":return t.stop()}}),t,null,[[4,29]])})))()},exchangeCodeForToken:function(e){var t=this;return Object(c["a"])(Object(o["a"])().mark((function r(){var a;return Object(o["a"])().wrap((function(r){while(1)switch(r.prev=r.next){case 0:return r.prev=0,r.next=3,h({code:e,state:t.state});case 3:if(a=r.sent,!a||!a.data){r.next=8;break}return r.abrupt("return",a.data);case 8:if(!a||void 0===a.success){r.next=12;break}return r.abrupt("return",a);case 12:return r.abrupt("return",{success:!0,userInfo:{openid:"test_openid_"+Date.now(),nickname:"微信用户",headimgurl:"",unionid:"test_unionid_"+Date.now()}});case 13:r.next=18;break;case 15:r.prev=15,r.t0=r["catch"](0),console.error("API调用失败:",r.t0);case 18:case"end":return r.stop()}}),r,null,[[0,15]])})))()},redirectToTarget:function(){var e="/";this.redirect&&(e=this.redirect),console.log("跳转到目标页面:",e),this.$router.replace(e)},retryAuth:function(){this.authStatus="init",this.errorMessage="",this.startAuth()},isWechatBrowser:function(){var e=navigator.userAgent.toLowerCase();return-1!==e.indexOf("micromessenger")},extractRedirectFromHash:function(){var e=window.location.hash;if(e&&e.includes("redirect=")){var t=e.match(/redirect=([^&]+)/);if(t)return decodeURIComponent(t[1])}return null},getLogin:function(){var e=this;return Object(c["a"])(Object(o["a"])().mark((function t(){var r,a;return Object(o["a"])().wrap((function(t){while(1)switch(t.prev=t.next){case 0:return e.loading=!0,t.next=3,e.login();case 3:if(r=t.sent,a=f.a.get("redirectUri"),a&&"undefined"!==a||(a=""),e.Loading=!1,console.log(a),!r){t.next=11;break}return a?window.location.href=a:e.$router.replace("/appStoreCopy"),t.abrupt("return","1");case 11:return t.abrupt("return","2");case 12:case"end":return t.stop()}}),t)})))()}}),mounted:function(){var e=this;return Object(c["a"])(Object(o["a"])().mark((function t(){var r;return Object(o["a"])().wrap((function(t){while(1)switch(t.prev=t.next){case 0:if(e.isWechatBrowser()){t.next=6;break}return f.a.set("isWeChat","1"),e.$router.replace("/login2"),t.abrupt("return");case 6:f.a.set("isWeChat","2");case 7:return t.next=9,e.getLogin();case 9:if(r=t.sent,"1"!=r){t.next=12;break}return t.abrupt("return");case 12:e.code?e.handleAuthCallback():e.startAuth();case 13:case"end":return t.stop()}}),t)})))()}},w=g,b=(r("5f41"),r("2877")),v=Object(b["a"])(w,a,n,!1,null,"4dd543f0",null);t["default"]=v.exports}}]);
|