Home > WeChat Applet > Mini Program Development > WeChat login applet steps development tutorial

WeChat login applet steps development tutorial

高洛峰
Release: 2017-03-11 14:04:33
Original
1960 people have browsed it

This article mainly introduces the development of WeChat--the steps to log in to the mini program. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

The main purpose of this small program is to allow users to log in with WeChat user information and authorize the user information to be stored in their own database, so that every time they log in to WeChat, they will get the code The obtained openid can be used to find the relevant information of the user in the project database.

During the testing process, the code obtained by the user login is required, so WeChat development gadgets will be used~!

WeChat login applet steps development tutorial

The use of this tool must be authorized by the project

WeChat login applet steps development tutorial

The generated code is time limited

The api of the WeChat login port is as follows

Interface address: http://www.php.cn/

Request parameters:

Parameters Required instructions
appid is the unique identifier of the mini program
secret is the app secret of the mini program
js_code is the code obtained when logging in
grant_type is filled in as authorization_code

Return parameters:

Parameter Description
openid User unique identification
session_key Session key
expires_in Session validity period, in seconds, for example, 2592000 means that the session validity period is 30 days

Return instructions:


//正常返回的JSON数据包
{
   "openid": "OPENID",
   "session_key": "SESSIONKEY"
   "expires_in": 2592000
}
Copy after login


//错误时返回JSON数据包(示例为Code无效)
{
  "errcode": 40029,
  "errmsg": "invalid code"
}
Copy after login

So we can write the code as follows

The company will send the message format itself It’s all written, I just need to write the request path and request parameters.


//@Param code 用户登录微信生成的code
//@Return OAuthResult 返回包含openid和session_key和expires_in的类
public OAuthResult getOAuthResultByCode(String code) {
    String path = "/sns/jscode2session";
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("appid", WechatConstant.WECHAT_APP_ID);
    parameters.put("secret", WechatConstant.WECHAT_APP_SECRET);
    parameters.put("js_code", code);
    parameters.put("grant_type", "authorization_code");

    OAuthResult oAuthResult = messageSender.getMessageForObject(path, parameters, OAuthResult.class, false);
    if (oAuthResult.getOpenid() == null){
      throw new BusinessException(BusinessException.Code.OAUTH_PROCESS_ERROR, "OAuth异常,原因:" + oAuthResult.getErrmsg());
    }
    return oAuthResult;
  }
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Script House.

The above is the detailed content of WeChat login applet steps development tutorial. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template