Login Interface

User login interface, supporting multiple login methods, including website login, WeChat login, Google login, etc., used to verify user identity and return login credentials.

Precautions

  • This interface 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 the corresponding user information and credentials
  • Google login requires obtaining the code through the OAuth process
  • After login, the user information and login token are returned
  • Supports the feature of remembering the login status

Request address

{域名地址}/api/login

Description:{域名地址}Need to replace it with your domain address, 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 Yes Login method, default value is website, optional values include: tt, swan, alipay, qq, weapp, wechat, inwechat, google, website
code string No WeChat, Google's temporary token, or verification code
anonymousCode string No WeChat user information, platform=wechat/weapp is valid
avatar string No WeChat user information, platform=wechat/weapp is valid
nick_name string No WeChat user information, platform=wechat/weapp is valid
gender string No WeChat user information, platform=wechat/weapp is valid
province string No WeChat user information, platform=wechat/weapp is valid
city string No WeChat user information, platform=wechat/weapp is valid
county string No WeChat user information, platform=wechat/weapp is valid
encryptedData string No WeChat user information, platform=wechat/weapp is valid
iv string No WeChat user information, platform=wechat/weapp is valid
signature string No WeChat user information, platform=wechat/weapp is valid
rawData string No WeChat user information, platform=wechat/weapp is valid
remember boolean No Whether to remember the user, platform=website is effective
user_name string No Username, platform=website is effective
password string No password, platform=website valid
captcha_id string No Image verification code ID, platform=website valid
captcha string No Graphic captcha value, platform=website valid

Return parameters

Field Name Type Description
code integer Error code
message string Error Description
data object Result Content

data Parameter

Field Name Type Description
id integer User ID
parent_id integer Parent User ID
user_name string Username
Real Name string True Name
avatar_url string User avatar address
email string Email address
phone string Phone number
group_id integer User group ID
is_retailer integer Is the retailer
balance integer User Balance
Total Reward integer Distributor Cumulative Earnings
Invite Code string User invitation code
Last login integer Time of last login
Expire time integer Expiration time
created_time integer registration timestamp
updated_time integer update timestamp
status integer Show Status
token string Login credentials

Example Usage

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

  1. First, obtain the Google login redirection URL:
GET /api/google/url?state=your-state-value HTTP/1.1
Host: www.anqicms.com
  1. Redirect to the returned Google authorization URL, after the user completes the authorization, they will be redirected to the callback address

  2. Get the authorization code on the callback page and 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": ""
}

Error response example

{
  "code": -1,
  "msg": "用户名或密码错误"
}

Error Code

Error code Description
0 OK
-1 Error, the reason is indicated in msg
1001 Not logged in
1002 Unauthorized
200 API request OK