Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use uni-app to obtain WeChat Mini Programs openid in Java backend

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how the Java backend uses uni-app to obtain WeChat Mini Programs openid. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Preface

This is a pure front-end (uniapp) to get the openid: https://my.oschina.net/u/4284277/blog/3168782

But there is a problem with this, that is, Mini Program cannot get openid after it is officially launched, so update the one obtained by Java below. I hope it will be helpful to you.

First, introduce openid

When developing Wechat, a user needs authorization to use Mini Program, so openid is needed to bind this user. Openid is the unique user identity of Wechat users under the official account appid (the openid obtained is different if the appid is different). It can be used to permanently mark a user and is also a required parameter for Wechat JSAPI payment.

1. Why use openid?

Openid refers to the unique identity (such as your ID card) of the authorized user in a certain Mini Program.

two。 What's the problem if you don't use it?

The user data is saved to the database on the first authorization, and then the user cleans up the cache, so we don't know if the user has been authorized on the second authorization. The user will save a new copy of the data into the database. This is illogical, because if the user has bought something and authorizes it again, it will be invisible and will be like a new number. Of course, some designs have their own recording methods, such as the need to register and log in.

3. How to obtain openid?

Need to use the interface specified by wx

2. Achieve 1. Uniapp//@author uniapp//@author uni.login ({success: res = > {/ / code value (expired in 5 minutes) console.info (res.code) Uni.request ({/ / change to your own service address url:' http://192.168.1.4:10010/wx/getOpenid/'+res.code, method:'GET', success: (res) = > {/ / you will get the openid here, but it is usually used directly in the backend, so you don't need to get the frontend. I just want to make a demonstration. Console.info (res);}})}); 2. Pom dependency

The dependency of HttpClient and the dependence of json transformation

Com.alibaba fastjson 1.2.54 org.apache.httpcomponents httpclient 4.5.103. Java API / * * @ author * Wechat controller * / @ RestController@RequestMapping ("/ wx") public class WChat {/ / Mini Program appid, need to be changed to real private final static String APPID = "wx3599fdagf87366c9"; / / Mini Program secret, need to change to real private final static String SECRET = "1a5567978djhs875ss8s2397er57jce" / * * obtain openid * @ param code * / @ GetMapping ("/ getOpenid/ {code}") public String getOpenid (@ PathVariable String code) throws IOException {/ / wx interface path String url = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code&" +" appid= "+ APPID +" & via appid & secret & code Secret= "+ SECRET +" & js_code= "+ code / send request CloseableHttpClient httpclient = HttpClients.createDefault () using HttpClient; / / send Get request HttpGet request = new HttpGet (url); request.addHeader ("Content-Type", "application/json"); / / get response CloseableHttpResponse response = httpclient.execute (request); / / get response body HttpEntity httpEntity = response.getEntity () / / use the tool to convert String result = EntityUtils.toString (httpEntity, "UTF-8"); / / convert to string JSONObject jsonObject = JSONObject.parseObject (result); System.out.println (jsonObject); / / all the content String openid = jsonObject.get ("openid"). ToString (); System.out.println (openid); / / obtained openid return openid }} this is the end of sharing about how the Java backend uses uni-app to obtain WeChat Mini Programs openid. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report