昨天由於有一個項目上面須要實現h5微信受權工做。因此花了兩個小時來完成這個功能。 前端
域名和端口號的要求是由於微信公衆號配置域名以及微信服務器回調都須要域名和80端口。react
這裏同一個域名,端口適配先後端IP,經過nginx統一代理處理。nginx
服務器根路徑上傳校驗文件,否則域名配置保存不了。web
import React, { useEffect } from "react";
import { View } from "@tarojs/components";
export default () => {
useEffect(() => {
// 後端回調回來路徑格式:http://xxx.cn/#/pages/webAuthorization?bindFlag=0&openid=xxxxxxxxxxx&unionid=null&isAuth=true
var isBindFlag = false, isAuth = false, opendId = '', paramsArray = [];
/* * 省略代碼:地址判斷,參數處理 賦值給isAuth, isBindFlag, openId */
if (!isAuth) { // 未受權
window.location.href=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${'xxxxxxx'}&redirect_uri=http://xxxxx/api/auth?response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect`;
} else if (!isBindFlag) { // 未註冊
window.location.href = '#/pages/login'
} else { // 登陸
window.location.href = '#/pages/index'
}
}, []);
return (
<View> </View>
);
};
複製代碼
結束工做! 後端