From c4b85c21d8bee8fef671ec8f5e3957fd88a37f88 Mon Sep 17 00:00:00 2001
From: 刘小明 <liuxm_a@163.com>
Date: 星期四, 12 十二月 2024 16:49:29 +0800
Subject: [PATCH] 小程序登录方法demo
---
pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/WeChatService.java | 40 ++++++++++
pms-parent/pms-global/src/main/resources/application-global.yml | 3
pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/AuthController.java | 36 +++++++++
pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/QueryVo.java | 14 +++
pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/PmsWechatTraceApplication.java | 6 +
pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/util/RestTemplateUtil.java | 111 +++++++++++++++++++++++++++
pms-parent/pms-wechat-trace/src/main/resources/application.yml | 13 +-
7 files changed, 217 insertions(+), 6 deletions(-)
diff --git a/pms-parent/pms-global/src/main/resources/application-global.yml b/pms-parent/pms-global/src/main/resources/application-global.yml
index ea9cdc9..822bbb8 100644
--- a/pms-parent/pms-global/src/main/resources/application-global.yml
+++ b/pms-parent/pms-global/src/main/resources/application-global.yml
@@ -84,6 +84,9 @@
report:
webPort: 12350
idSuffix: 7
+ wechat:
+ webPort: 12351
+ idSuffix: 8
file:
idSuffix: 99
file1:
diff --git a/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/PmsWechatTraceApplication.java b/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/PmsWechatTraceApplication.java
index 59c92d9..38225fe 100644
--- a/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/PmsWechatTraceApplication.java
+++ b/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/PmsWechatTraceApplication.java
@@ -1,9 +1,15 @@
package com.dy.pmsTrace;
+import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication
+@EnableAspectJAutoProxy
+@ComponentScan(basePackages = {"com.dy.common", "com.dy.pmsGlobal", "com.dy.pmsTrace"})
+@MapperScan(basePackages={"com.dy.pmsGlobal.dao*"})
public class PmsWechatTraceApplication {
public static void main(String[] args) {
diff --git a/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/AuthController.java b/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/AuthController.java
new file mode 100644
index 0000000..6d3fb44
--- /dev/null
+++ b/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/AuthController.java
@@ -0,0 +1,36 @@
+package com.dy.pmsTrace.login;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.dy.common.webUtil.BaseResponse;
+import com.dy.common.webUtil.BaseResponseUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+@RestController
+@RequestMapping("/wechat")
+public class AuthController {
+
+ private WeChatService weChatService;
+
+ @Autowired
+ public void setWechatService(WeChatService weChatUserService){
+ this.weChatService = weChatUserService;
+ }
+
+ @PostMapping("/login")
+ public BaseResponse<Boolean> login(@RequestBody QueryVo vo) {
+ try {
+ JSONObject wxResponse = weChatService.login(vo.code);
+ if (wxResponse.containsKey("openid")) {
+ // 鎴愬姛鑾峰彇鍒颁簡鐢ㄦ埛鐨刼penid,session_key鍜屽叾浠栦俊鎭�
+ // 杩欓噷鍙互杩涜鏇村涓氬姟閫昏緫鎿嶄綔锛屼緥濡傜敓鎴愯嚜瀹氫箟token杩斿洖缁欏鎴风
+ return BaseResponseUtils.buildSuccess(wxResponse);
+ } else {
+ // 濡傛灉鏈夐敊璇俊鎭紝鐩存帴杩斿洖
+ return BaseResponseUtils.buildError(wxResponse);
+ }
+ } catch (Exception e) {
+ return BaseResponseUtils.buildError(e.getMessage());
+ }
+ }
+}
\ No newline at end of file
diff --git a/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/QueryVo.java b/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/QueryVo.java
new file mode 100644
index 0000000..6082628
--- /dev/null
+++ b/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/QueryVo.java
@@ -0,0 +1,14 @@
+package com.dy.pmsTrace.login;
+
+import com.dy.common.webUtil.QueryConditionVo;
+import lombok.*;
+
+@Data
+@EqualsAndHashCode(callSuper = false)
+@ToString(callSuper = true)
+@NoArgsConstructor
+@AllArgsConstructor
+@Builder
+public class QueryVo extends QueryConditionVo {
+ public String code;
+}
diff --git a/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/WeChatService.java b/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/WeChatService.java
new file mode 100644
index 0000000..241a885
--- /dev/null
+++ b/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/login/WeChatService.java
@@ -0,0 +1,40 @@
+package com.dy.pmsTrace.login;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.dy.pmsTrace.util.RestTemplateUtil;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+@Service
+public class WeChatService {
+
+ @Value("${wechat.appId}")
+ private String appId;
+
+ @Value("${wechat.appSecret}")
+ private String appSecret;
+
+ @Value("${wechat.loginUrl}")
+ private String loginUrl;
+
+ private final RestTemplateUtil restTemplateUtil;
+
+ public WeChatService(RestTemplateUtil restTemplateUtil) {
+ this.restTemplateUtil = restTemplateUtil;
+ }
+
+ public JSONObject login(String code) throws IOException {
+ Map<String, Object> queryParams = new HashMap<>();
+ queryParams.put("appid", appId);
+ queryParams.put("secret", appSecret);
+ queryParams.put("js_code", code);
+ queryParams.put("grant_type", "authorization_code");
+ Map<String, String> headerParams = new HashMap<>();
+ return restTemplateUtil.get(loginUrl, queryParams, headerParams);
+
+ }
+}
\ No newline at end of file
diff --git a/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/util/RestTemplateUtil.java b/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/util/RestTemplateUtil.java
new file mode 100644
index 0000000..211cde3
--- /dev/null
+++ b/pms-parent/pms-wechat-trace/src/main/java/com/dy/pmsTrace/util/RestTemplateUtil.java
@@ -0,0 +1,111 @@
+package com.dy.pmsTrace.util;
+
+import com.alibaba.fastjson2.JSONObject;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.*;
+import org.springframework.stereotype.Component;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author ZhuBaoMin
+ * @date 2024-03-06 13:47
+ * @LastEditTime 2024-03-06 13:47
+ * @Description
+ */
+
+@Component
+public class RestTemplateUtil {
+
+ @Autowired
+ private RestTemplate restTemplate;
+
+ public JSONObject get(String url, Map<String, Object> queryParams) throws IOException {
+ return get(url, queryParams, new HashMap<>(1));
+ }
+
+ public JSONObject get(String url, Map<String, Object> queryParams, Map<String, String> headerParams) throws IOException {
+ String tempUrl = setParamsByAppendUrl(queryParams, url);
+ HttpHeaders headers = new HttpHeaders();
+ headerParams.forEach(headers::add);
+ HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null, headers);
+ ResponseEntity<String> response = restTemplate.exchange(tempUrl, HttpMethod.GET, httpEntity, String.class);
+ return JSONObject.parseObject(response.getBody());
+ }
+
+ public JSONObject getHeaders(String url, Map<String, Object> queryParams, Map<String, String> headerParams) throws IOException {
+ String tempUrl = setParamsByAppendUrl(queryParams, url);
+ HttpHeaders headers = new HttpHeaders();
+ headerParams.forEach(headers::add);
+ HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null, headers);
+ ResponseEntity<String> response = restTemplate.exchange(tempUrl, HttpMethod.GET, httpEntity, String.class);
+
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("headers", response.getHeaders());
+ jsonObject.put("body", response.getBody());
+ return jsonObject;
+ }
+
+ public JSONObject get2(String url, Map<String, Object> queryParams, Map<String, String> headerParams) throws IOException {
+ String tempUrl = setParamsByPath(queryParams, url);
+ HttpHeaders headers = new HttpHeaders();
+ headerParams.forEach(headers::add);
+ HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(null, headers);
+ ResponseEntity<String> response = restTemplate.exchange(tempUrl, HttpMethod.GET, httpEntity, String.class, queryParams);
+ return JSONObject.parseObject(response.getBody());
+ }
+
+ public JSONObject post(String url, String json, Map<String, String> headerParams) {
+ HttpHeaders headers = new HttpHeaders();
+ headerParams.forEach(headers::add);
+ headers.setContentType(MediaType.APPLICATION_JSON);
+ headers.add("Accept", MediaType.APPLICATION_JSON.toString());
+ HttpEntity<String> httpEntity = new HttpEntity<>(json, headers);
+ ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
+ return JSONObject.parseObject(response.getBody());
+ }
+
+ private String setParamsByPath(Map<String, Object> queryParams, String url) {
+ // url?id={id}&name={name}
+ if (queryParams == null || queryParams.isEmpty()) {
+ return url;
+ }
+ StringBuilder sb = new StringBuilder();
+ try {
+ for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
+ sb.append("&").append(entry.getKey()).append("=").append("{").append(entry.getKey()).append("}");
+ }
+ if (!url.contains("?")) {
+ sb.deleteCharAt(0).insert(0, "?");
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return url + sb;
+ }
+
+ private String setParamsByAppendUrl(Map<String, Object> queryParams, String url) {
+ // url?id=1&name=zzc
+ if (queryParams == null || queryParams.isEmpty()) {
+ return url;
+ }
+ StringBuilder sb = new StringBuilder();
+ try {
+ for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
+ sb.append("&").append(entry.getKey()).append("=");
+ sb.append(entry.getValue());
+ }
+ if (!url.contains("?")) {
+ sb.deleteCharAt(0).insert(0, "?");
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return url + sb;
+ }
+
+}
\ No newline at end of file
diff --git a/pms-parent/pms-wechat-trace/src/main/resources/application.yml b/pms-parent/pms-wechat-trace/src/main/resources/application.yml
index 2acde49..e41a2ac 100644
--- a/pms-parent/pms-wechat-trace/src/main/resources/application.yml
+++ b/pms-parent/pms-wechat-trace/src/main/resources/application.yml
@@ -4,15 +4,16 @@
#web鏈嶅姟绔彛锛宼omcat榛樿鏄�8080
server:
- port: ${pms.base.webPort}
+ port: ${pms.wechat.webPort}
servlet:
- context-path: /base #web璁块棶涓婁笅鏂囪矾寰�
+ context-path: /wechat #web璁块棶涓婁笅鏂囪矾寰�
context-parameters:
#GenerateIdSetSuffixListener涓簲鐢紝鍙栧�艰寖鍥存槸0-99
- idSuffix: ${pms.base.idSuffix}
+ idSuffix: ${pms.webPort.idSuffix}
#ConfigListener涓簲鐢�
#configFileNames: config-global.xml,config-demo.xml
-user:
- defaultTrueRandomFalsePassword: true #true:閲囩敤榛樿瀵嗙爜锛宖alse:绯荤粺浜х敓闅忔満瀵嗙爜
- defaultPassword: "ABC123" #榛樿瀵嗙爜
\ No newline at end of file
+wechat:
+ appId: wx2e6beb0eca7c3672
+ appSecret: b8f8ce34e13d3ba2eeaeba89068d7fbe
+ loginUrl: https://api.weixin.qq.com/sns/jscode2session
\ No newline at end of file
--
Gitblit v1.8.0