The user login interface supports various login methods, including website login, WeChat login, Google login, etc., used to verify the user's identity and return login credentials.
Points to note
- The API uses the POST method and requires data to be submitted in application/ format
- Support login on multiple platforms, including websites, WeChat, QQ, Google, etc.
- Website login requires a username and password
- WeChat login requires providing corresponding user information and credentials
- Google login requires obtaining a code through the OAuth process
- After login, the user information and login token are returned
- Supports the feature to remember login status
Request address
{域名地址}/api/login
Description:{域名地址}Replace it with your domain name, such ashttps://en.anqicms.com/api/login
Request Syntax
POST {域名地址}/api/login
Content-Type: application/json
Request parameters
| Field name | Type | Required | Description |
|---|---|---|---|
| platform | string | Is | Login method, the default value is website, optional values include: tt, swan, alipay, qq, weapp, wechat, inwechat, google, website |
| code | string | No | Temporary credentials from WeChat or Google, or a verification code |
| anonymousCode | string | No | WeChat user information, platform=wechat/weapp valid |
| avatar | string | No | WeChat user information, platform=wechat/weapp valid |
| nick_name | string | No | WeChat user information, platform=wechat/weapp valid |
| gender | string | No | WeChat user information, platform=wechat/weapp valid |
| Province | string | No | WeChat user information, platform=wechat/weapp valid |
| City | string | No | WeChat user information, platform=wechat/weapp valid |
| county | string | No | WeChat user information, platform=wechat/weapp valid |
| encryptedData | string | No | WeChat user information, platform=wechat/weapp valid |
| iv | string | No | WeChat user information, platform=wechat/weapp valid |
| signature | string | No | WeChat user information, platform=wechat/weapp valid |
| rawData | string | No | WeChat user information, platform=wechat/weapp valid |
| remember | boolean | No | Whether to remember the user, platform=website valid |
| user_name | string | No | Username, platform=website valid |
| password | string | No | Password, platform=website valid |
| captcha_id | string | No | Captcha ID, platform=website valid |
| Captcha | string | No | Graphic captcha value, platform=website valid |
Return parameters
| Field name | Type | Description |
|---|---|---|
| code | int | Error code |
| msg | string | Error description |
| data | object | result content |
data parameter
| Field name | Type | Description |
|---|---|---|
| id | int | user ID |
| parent_id | int | Parent user ID |
| user_name | string | Username |
| real_name | string | Real Name |
| avatar_url | string | User avatar address |
| string | Email address | |
| phone | string | Phone number |
| group_id | int | User group ID |
| is_retailer | int | Is a Distributor |
| balance | int | User Balance |
| Total Reward | int | Distributor Accumulated Earnings |
| Invite Code | string | User Invite Code |
| last login | int | Last login time |
| expire time | int | Expiry time |
| created_time | int | registration timestamp |
| updated_time | int | update timestamp |
| status | int | Display Status |
| token | string | Login Credentials |
Usage Example
Website Login Request Example
POST /api/login HTTP/1.1
Host: www.anqicms.com
Content-Type: application/json
{
"platform": "website",
"user_name": "admin",
"password": "123456",
"remember": false
}
WeChat Login Request Example
POST /api/login HTTP/1.1
Host: www.anqicms.com
Content-Type: application/json
{
"platform": "wechat",
"code": "wechat_code",
"avatar": "https://example.com/avatar.jpg",
"nick_name": "微信用户",
"gender": "1",
"province": "广东",
"city": "深圳",
"county": "南山区",
"encryptedData": "encrypted_data",
"iv": "iv_value",
"signature": "signature_value",
"rawData": "raw_data"
}
Google Login Request Example
POST /api/login HTTP/1.1
Host: www.anqicms.com
Content-Type: application/json
{
"platform": "google",
"state": "your-generated-state-value",
"code": "google-auth-code"
}
Google Login Full Process
- Firstly, obtain the Google login redirection URL:
GET /api/google/url?state=your-state-value HTTP/1.1
Host: www.anqicms.com
Redirect to the returned Google authorization URL; after the user completes authorization, they will be redirected to the callback address
Get the authorization code on the callback page, call the login interface:
POST /api/login HTTP/1.1
Host: www.anqicms.com
Content-Type: application/json
{
"platform": "google",
"state": "your-state-value",
"code": "authorization-code-from-google"
}
Front-end Google login implementation example
// 点击谷歌登录按钮时触发
$('#login-google-btn').click(function() {
$.get("/api/google/url").then(res => {
if (res.data) {
// 保存state值,用于后续验证
sessionStorage.setItem('state', res.data.state);
// 跳转到谷歌授权页面
window.location.href = res.data.url;
}
}, 'json');
});
// 检查URL中是否有code参数(授权回调时)
if (window.location.href.indexOf('code') > -1) {
// 获取URL参数中的code和state
var urlParams = new URLSearchParams(window.location.search);
var code = urlParams.get('code');
var state = urlParams.get('state');
// 调用登录接口完成登录
$.ajax({
type: 'POST',
url: '/api/login',
data: JSON.stringify({
platform: "google",
"state": state,
"code": code,
}),
contentType: "application/json",
dataType: 'json',
success: function (res) {
if (res.code === 0) {
console.log('Google登录成功', res);
// 登录成功后的处理
window.location.href = '/dashboard'; // 跳转到用户中心
} else {
console.error('Google登录失败', res.msg);
}
},
error: function(xhr, status, error) {
console.error('网络错误', error);
}
});
}
Response Example
{
"code": 0,
"data": {
"id": 1000,
"created_time": 1669012062,
"updated_time": 1679536756,
"parent_id": 0,
"user_name": "admin",
"real_name": "",
"avatar_url": "uploads/202211/21/14f56760596b5328.webp",
"email": "",
"phone": "",
"group_id": 2,
"status": 1,
"is_retailer": 0,
"balance": 0,
"total_reward": 0,
"invite_code": "anqicms",
"last_login": 1702912236,
"expire_time": 1997404800,
"extra": null,
"token": "eyJhbGI1NiZXJJZUwNnVCI6IjEwMDAifQ.pLmYPvSajEWrF0FaUGDciOiJIUzIzNiIsILLZBCRm8fYewv49agxlTA26DwIsInR5cCI6IkpXVCJ9.eyJ0IjoiMTcwNTz",
"group": null,
"full_avatar_url": "",
"link": ""
},
"msg": ""
}
Example of error response
{
"code": -1,
"msg": "用户名或密码错误"
}
error code
| Error code | Description |
|---|---|
| 0 | OK |
| -1 | Error, the reason is indicated in msg |
| 1001 | Not logged in |
| 1002 | Not authorized |
| 200 | API request OK |