昨天由于有一个项目上面须要实现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>
);
};
复制代码
结束工做! 后端