English
This document is for App login instructions, H5 and Mini Program details
Apply to activate
the WeChat login function in the application details, fill in the information according to the page prompts, and submit for reviewFor more information, please refer to the official WeChat document Mobile Application WeChat Login Open Guide
Notice
示例代码
uni.login({
"provider": "weixin",
"onlyAuthorize": true, // 微信登录仅请求授权认证
success: function(event){
const {code} = event
//The client successfully obtains the authorized temporary ticket (code) and initiates a login request to the business server.
uni.request({
url: 'https://www.example.com/loginByWeixin', //仅为示例,并非真实接口地址。
data: {
code: event.code
},
success: (res) => {
//Get the token to complete the login
uni.setStorageSync('token',res.token)
}
});
},
fail: function (err) {
// 登录授权失败
// err.code is the error code
}
})
Copy codeRelated API documentation: uni.login, [uni.request](https://uniapp.dcloud.io/api/ request/request.html)
var weixinOauth = null;
plus.oauth.getServices(function(services) {
for (var i in services) {
var service = services[i];
// 获取微信登录对象
if (service.id == 'weixin') {
weixinOauth = service;
break;
}
}
weixinOauth.authorize( function(event){
const {code} = event
//The client successfully obtains the authorized temporary ticket (code) and initiates a login request to the business server.
uni.request({
url: 'https://www.example.com/loginByWeixin', //仅为示例,并非真实接口地址。
data: {
code: event.code
},
success: (res) => {
//Get the token to complete the login
uni.setStorageSync('token',res.token)
}
});
}, function(err) {
// 登录授权失败
// err.code is the error code
})
}, function(err) {
// Failed to get services
})
Copy codeRelated API documentation: plus.oauth.getServices, [plus.oauth.AuthService](https://www .html5plus.org/doc/en_us/oauth.html#plus.oauth.AuthService)
The appsecret parameters configured in HBuilderX will be saved in apk/ipa after being packaged in the cloud, there is a risk of parameter leakage! HBuilderX3.4.18+ no longer provides visual configuration of this parameter.
For developers with low security requirements, you can add appsecret configuration through manifest.json -> source view -> app-plus -> distribute -> sdkConfigs -> oauth -> weixin -> . You can complete the login without the verification of the business server:
weixin
uni.login({
provider: 'weixin',
success: function (loginRes) {
// login successful
uni.getUserInfo({
provider: 'weixin',
success: function(info) {
// Obtain user information successfully, info.authResult saves user information
}
})
},
fail: function (err) {
// 登录授权失败
// err.code is the error code
}
});
Copy codevar weixinOauth = null;
plus.oauth.getServices(function(services) {
for (var i in services) {
var service = services[i];
// 获取微信登录对象
if (service.id == 'weixin') {
weixinOauth = service;
break;
}
}
weixinOauth.login( function(oauth){
// 授权成功,weixinOauth.authResult 中保存授权信息
}, function(err) {
// 登录授权失败
// err.code is the error code
})
}, function(err) {
// Failed to get services
})
Copy code