提交 7b3e1648 编写于 作者: DCloud_JSON's avatar DCloud_JSON

新增微信web登录和文档

上级 26b690b8
......@@ -18,10 +18,7 @@
},
/* 模块配置 */
"modules" : {
"OAuth" : {},
"Push" : {
"version" : "2"
}
"OAuth" : {}
},
/* 应用发布信息 */
"distribute" : {
......
......@@ -1913,11 +1913,47 @@ exports.main = async function() {
- uni-id配置:`uni-id配置文件` --> `service` --> `univerify`,填写`appid``apiKey``apiSecret``appid`就是`manifest`里的`appid``apiKey``apiSecret`则是从上一步的web界面得来的。
## 微信登录
- APP端申请微信登录需要在**微信开放平台**申请移动应用,获得的appid和appsecret,用于微信登录。[微信开放平台](https://open.weixin.qq.com/)
- 微信 appid 申请步骤:[https://ask.dcloud.net.cn/article/208](https://ask.dcloud.net.cn/article/208)
- iOS平台微信SDK配置通用链接:[https://uniapp.dcloud.io/api/plugins/universal-links.html](https://uniapp.dcloud.io/api/plugins/universal-links.html)
- 模块配置:`manifest.json` --> `App模块配置` --> `OAuth(登录鉴权)` --> `勾选微信登录` --> 填写`appid``appsecret``ios平台通用链接`
- uni-id配置:`uni-id配置文件` --> `app-plus` --> `oauth` --> `weixin`,填写`appid``appsecret`
uni-id-pages已全面支持:app、web、小程序,三端的微信登录。
web端网页链接,如果在微信内打开,是基于`微信公众号`的auth登录;否则是基于微信开放平台创建的`网站应用`生成二维码,通过手机微信扫码登录。
`APP端微信登录``web端手机微信扫码登录`,必须开通[微信开放平台账号](https://open.weixin.qq.com/),创建`移动应用``网站应用`获得相关密钥实现。
而其他登录方式之间,如果要产生关联(比如:同一个微信号,在小程序中登录和在微信中打开网页登录,认定为同一个用户),也需要在`微信开放平台`中绑定。
iOS平台微信登录SDK需要配置通用链接,详情参考:[https://uniapp.dcloud.io/api/plugins/universal-links.html](https://uniapp.dcloud.io/api/plugins/universal-links.html)
#### 客户端配置
* APP端,打开`manifest.json` -->`App模块配置` --> `OAuth(登录鉴权)` --> `勾选微信登录` --> 填写`appid``appsecret``ios平台通用链接`
* 小程序端,打开`manifest.json` --> `微信小程序配置` --> 填写微信小程序AppID
* web端,打开`/uni_modules/uni-id-pages/config.js` --> `appid` --> `weixin``h5`节点配置微信公众号的appid,`web`节点配置微信开放平台创建的网站应用appid
#### 服务端配置
* 服务端`uni-id`的密钥信息统一在`uni-config-center`中配置,路径:`uni_modules/uni-config-center/uniCloud/cloudfunctions/common/uni-config-center/uni-id/config.json`,完整的配置信息[详情查看](uni-id-summary.md#config)
#### web端登录,授权回调域配置:
- 手机微信扫码登录
微信开放平台 -> 管理中心 -> 网站应用 -> 选择对应的应用名称,点击查看 -> 开发信息,点击修改 -> 填写授权回调域
- 基于微信公众号auth登录
登录微信公众号 -> 设置与开发 -> 公众号设置 -> 设置网页授权域名
### 本地调试web端微信登录:
回调域,必须接入外网已经备案的URL地址,不然本地没法进行调试,你可以做内网穿透,映射生成一个外网URL地址来进行回调测试。当那样比较麻烦,这里我们介绍一种基于HBuilderX本地启动一个Web Server进行调试的方法。
首先我们打开`manifest.json` --> `H5配置` --> `端口` --> 输入`80`(注意:在mac os中,非root用户是无法使用小于1024的常用端口的。这里我们需要用到`终端`,cd 到 HBuilderX安装目录(默认是Applications目录,执行`cd /Applications`),然后执行 `sudo ./HBuilderX.app/Contents/MacOS/HBuilderX` 输入开机密码即可,用新打开的HBuilderX中运行项目到浏览器,即可启用一个80端口的Web Server了);
接下来我们在本地虚拟一个dns,实现你的回调域名访问你的本地Web Server,需要修改host。
host文件路径:
Windows系统:`C:\Windows\System32\drivers\etc`
mac系统:`/etc/hosts`
在末尾添加一行 `127.0.0.1 你的域名`保持即可。
如果此时直接访问域名可以服务到你的说明已经成功了。
> 如果没有启动80端口而是81等,说明你的端口被占用了。你有两个办法1.关闭可疑程序,或直接重启电脑 2.命令行关闭占用的端口[详情查看](https://www.baidu.com/s?&wd=%E5%91%BD%E4%BB%A4%E8%A1%8C%20%E8%A7%A3%E5%86%B3%E7%AB%AF%E5%8F%A3%E8%A2%AB%E5%8D%A0%E7%94%A8)
## 苹果登录集成指南
- 模块配置:`manifest.json` --> `App模块配置` --> OAuth(登录鉴权)勾选`苹果登录`[IOS苹果授权登录参考文档](https://ask.dcloud.net.cn/article/36651)。如不发布到Appstore,不需要配置此项
......
......@@ -101,8 +101,8 @@
this.$emit('setAgree', this.isAgree)
}
},
created() {
this.needAgreements = config.agreements.scope.includes(this.scope)
created() {
this.needAgreements = (config?.agreements?.scope || []).includes(this.scope)
},
data() {
return {
......
......@@ -19,7 +19,8 @@
const db = uniCloud.database();
const usersTable = db.collection('uni-id-users')
let allServicesList = []
let allServicesList = []
export default {
computed: {
agreements() {
......@@ -41,17 +42,27 @@
]
},
agree: {
get() {
return this.$parent.agree
get() {
return this.uniParentComponent.agree
},
set(agree) {
console.log('setAgree', agree);
this.$parent.agree = agree
set(agree) {
console.log('setAgree', agree);
return this.uniParentComponent.agree = agree
}
}
},
data() {
return {
data() {
// #ifndef H5
const uniParentComponent = this.$parent;
// #endif
// #ifdef H5
const uniParentComponent = this.$parent.$parent;
// #endif
return {
//父组件对象
uniParentComponent,
servicesList: [{
"id": "username",
"text": "账号登录",
......@@ -226,33 +237,6 @@
},
async login_before(type, navigateBack = true) {
console.log(type);
// #ifdef H5
if(type == 'weixin'){
console.log('开始微信网页登录');
let redirectUrl = location.protocol +'//'+
document.domain +
(window.location.href.includes('#')?'/#':'') +
'/uni_modules/uni-id-pages/pages/login/login-withoutpwd?is_weixin_redirect=true&type=weixin'
let ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger'){
console.log('在微信公众号内');
return window.open(`https://open.weixin.qq.com/connect/oauth2/authorize?
appid=wx32b2580e30ef8555
&redirect_uri=${encodeURIComponent(redirectUrl)}
&response_type=code
&scope=snsapi_userinfo
&state=STATE&connect_redirect=1#wechat_redirect`);
}else{
console.log('非微信公众号内');
return location.href = `https://open.weixin.qq.com/connect/qrconnect?appid=wx4dcf96ab6af4c5e8
&redirect_uri=${encodeURIComponent(redirectUrl)}
&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect`
}
}
// #endif
//提示空实现
if (["qq",
"xiaomi",
......@@ -308,17 +292,48 @@
}
//判断是否需要弹出隐私协议授权框
console.log(type, this.agree);
let needAgreements = config.agreements.scope.includes('register')
console.log(type, this.agree);
let needAgreements = (config?.agreements?.scope || []).includes('register')
console.log({
needAgreements
});
if (type != 'univerify' && needAgreements && !this.agree) {
return this.$parent.$refs.agreements.popup(() => {
if (type != 'univerify' && needAgreements && !this.agree) {
let agreementsRef = this.uniParentComponent.$refs.agreements
return agreementsRef.popup(() => {
console.log(type, navigateBack);
this.login_before(type, navigateBack)
})
}
}
// #ifdef H5
if(type == 'weixin'){
console.log('开始微信网页登录');
let redirectUrl = location.protocol +'//'+
document.domain +
(window.location.href.includes('#')?'/#':'') +
'/uni_modules/uni-id-pages/pages/login/login-withoutpwd?is_weixin_redirect=true&type=weixin'
console.log('redirectUrl----',redirectUrl);
let ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger'){
console.log('在微信公众号内');
return window.open(`https://open.weixin.qq.com/connect/oauth2/authorize?
appid=${config.appid.weixin.h5}
&redirect_uri=${encodeURIComponent(redirectUrl)}
&response_type=code
&scope=snsapi_userinfo
&state=STATE&connect_redirect=1#wechat_redirect`);
}else{
console.log('非微信公众号内');
return location.href = `https://open.weixin.qq.com/connect/qrconnect?appid=${config.appid.weixin.web}
&redirect_uri=${encodeURIComponent(redirectUrl)}
&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect`
}
}
// #endif
uni.showLoading({
mask: true
})
......
......@@ -14,7 +14,7 @@ export default {
// "google",
// "alipay",
// "douyin",
// #ifdef APP
"univerify",
// #endif
......@@ -29,9 +29,17 @@ export default {
"agreements": {
"serviceUrl": "https://xxx", //用户服务协议链接
"privacyUrl": "https://xxx", //隐私政策条款链接
// 哪些场景下显示,1.注册(包括登录并注册,如:微信登录、苹果登录、短信验证码登录)、2.登录(如:用户名密码登录)
// 哪些场景下显示,1.注册(包括登录并注册,如:微信登录、苹果登录、短信验证码登录)、2.登录(如:用户名密码登录)
"scope": [
// 'register', 'login'
'register', 'login'
]
},
"appid":{
"weixin":{
// 公众号
"h5":"wx32b2580e30ef8555",
// 网站应用
"web":"wx4dcf96ab6af4c5e8"
}
}
}
......@@ -34,7 +34,7 @@ export default async function() {
google: 'google',
alipay: 'alipay',
apple: "apple"
}
}
//遍历客户端配置的登录方式,与服务端比对。并在错误时抛出错误提示
let list = loginTypes.filter(type => !supportedLoginType.includes(data[type]))
if (list.length) {
......
......@@ -99,6 +99,5 @@
}
},
"dependencies": {
"jweixin-module": "^1.6.0"
}
}
......@@ -42,9 +42,6 @@ export default {
if (/^\d+$/.test(value)) {
callback('昵称不能为纯数字')
};
if(/[\u4E00-\u9FA5\uF900-\uFA2D]{1,}/.test(value)){
callback('昵称不能包含中文')
}
return true
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册