diff --git a/src/main/java/com/cultural/heritage/controller/user/WeChatController.java b/src/main/java/com/cultural/heritage/controller/user/WeChatController.java index b5886f9..d844c46 100644 --- a/src/main/java/com/cultural/heritage/controller/user/WeChatController.java +++ b/src/main/java/com/cultural/heritage/controller/user/WeChatController.java @@ -1,27 +1,17 @@ package com.cultural.heritage.controller.user; - import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.core.toolkit.Wrappers; -import com.cultural.heritage.common.BaseResponse; -import com.cultural.heritage.common.ErrorCode; -import com.cultural.heritage.common.ResultUtils; -import com.cultural.heritage.exception.BusinessException; -import com.cultural.heritage.model.entity.User; -import com.cultural.heritage.model.vo.UserVO; -import com.cultural.heritage.service.user.UserService; -import com.cultural.heritage.utils.WeChatUtil; +import com.cultural.heritage.utils.UrlUtil; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.codec.digest.DigestUtils; -import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.HashMap; +import java.util.Map; + @RestController @RequestMapping("/wx") @Slf4j @@ -29,47 +19,16 @@ import org.springframework.web.bind.annotation.RestController; public class WeChatController { - @Resource - private UserService userService; - - - - @PostMapping("/wx/login") - public BaseResponse user_login(@RequestParam(value = "code", required = false) String code, - @RequestParam(value = "rawData", required = false) String rawData, - @RequestParam(value = "signature", required = false) String signature) { - // 用户非敏感信息:rawData - // 签名:signature - JSONObject rawDataJson = JSON.parseObject(rawData); - // 1.接收小程序发送的code - // 2.开发者服务器 登录凭证校验接口 appi + appsecret + code - JSONObject SessionKeyOpenId = WeChatUtil.getSessionKeyOrOpenId(code); - // 3.接收微信接口服务 获取返回的参数 - String openid = SessionKeyOpenId.getString("openid"); - String sessionKey = SessionKeyOpenId.getString("session_key"); - - // 4.校验签名 小程序发送的签名signature与服务器端生成的签名signature2 = sha1(rawData + sessionKey) - String signature2 = DigestUtils.sha1Hex(rawData + sessionKey); - if (!signature.equals(signature2)) { - throw new BusinessException(ErrorCode.PARAMS_ERROR, "签名校验失败"); - } - // 5.根据返回的User实体类,判断用户是否是新用户,是的话,将用户信息存到数据库; - LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); - lqw.eq(User::getMiniOpenId, openid); - User user = userService.getOne(lqw); - if (user == null) { - // 用户信息入库 - String nickName = rawDataJson.getString("nickName"); - String avatarUrl = rawDataJson.getString("avatarUrl"); - user = new User(); - user.setMiniOpenId(openid); - user.setUserAvatar(avatarUrl); - user.setUserName(nickName); - userService.save(user); - } - UserVO userVO = userService.getUserVO(user); - return ResultUtils.success(userVO); + @GetMapping("/login") + public JSONObject list(String code) { + String requestUrl = "https://api.weixin.qq.com/sns/jscode2session"; + Map requestUrlParam = new HashMap<>(); + requestUrlParam.put("appid","wx3f968a09e31d6bed"); //开发者设置中的appId + requestUrlParam.put("secret", "847bdda7c2b01e88d59948b9ba50ef8d"); //开发者设置中的appSecret + requestUrlParam.put("js_code", code); //小程序调用wx.login返回的code + requestUrlParam.put("grant_type", "authorization_code");//默认参数 + JSONObject jsonObject = JSON.parseObject(UrlUtil.sendPost(requestUrl, requestUrlParam)); + return jsonObject; } - } diff --git a/src/main/java/com/cultural/heritage/model/entity/Coupon.java b/src/main/java/com/cultural/heritage/model/entity/Coupon.java index 93655e8..65684e2 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Coupon.java +++ b/src/main/java/com/cultural/heritage/model/entity/Coupon.java @@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; import java.io.Serial; import java.io.Serializable; @@ -73,14 +74,14 @@ public class Coupon implements Serializable { /** * 有效开始日期 */ - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date startTime; /** * 有效截止日期 */ - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date endTime; /** diff --git a/src/main/java/com/cultural/heritage/utils/HttpClientUtil.java b/src/main/java/com/cultural/heritage/utils/HttpClientUtil.java deleted file mode 100644 index c9ea0e0..0000000 --- a/src/main/java/com/cultural/heritage/utils/HttpClientUtil.java +++ /dev/null @@ -1,134 +0,0 @@ -package com.cultural.heritage.utils; - - -import org.apache.http.NameValuePair; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.utils.URIBuilder; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; - -import java.io.IOException; -import java.net.URI; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class HttpClientUtil { - - public static String doGet(String url, Map param) { - - // 创建Httpclient对象 - CloseableHttpClient httpclient = HttpClients.createDefault(); - - String resultString = ""; - CloseableHttpResponse response = null; - try { - // 创建uri - URIBuilder builder = new URIBuilder(url); - if (param != null) { - for (String key : param.keySet()) { - builder.addParameter(key, param.get(key)); - } - } - URI uri = builder.build(); - - // 创建http GET请求 - HttpGet httpGet = new HttpGet(uri); - - // 执行请求 - response = httpclient.execute(httpGet); - // 判断返回状态是否为200 - if (response.getStatusLine().getStatusCode() == 200) { - resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); - } - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - if (response != null) { - response.close(); - } - httpclient.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - return resultString; - } - - public static String doGet(String url) { - return doGet(url, null); - } - - public static String doPost(String url, Map param) { - // 创建Httpclient对象 - CloseableHttpClient httpClient = HttpClients.createDefault(); - CloseableHttpResponse response = null; - String resultString = ""; - try { - // 创建Http Post请求 - HttpPost httpPost = new HttpPost(url); - // 创建参数列表 - if (param != null) { - List paramList = new ArrayList<>(); - for (String key : param.keySet()) { - paramList.add(new BasicNameValuePair(key, param.get(key))); - } - // 模拟表单 - UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); - httpPost.setEntity(entity); - } - // 执行http请求 - response = httpClient.execute(httpPost); - resultString = EntityUtils.toString(response.getEntity(), "utf-8"); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - response.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - return resultString; - } - - public static String doPost(String url) { - return doPost(url, null); - } - - public static String doPostJson(String url, String json) { - // 创建Httpclient对象 - CloseableHttpClient httpClient = HttpClients.createDefault(); - CloseableHttpResponse response = null; - String resultString = ""; - try { - // 创建Http Post请求 - HttpPost httpPost = new HttpPost(url); - // 创建请求内容 - StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); - httpPost.setEntity(entity); - // 执行http请求 - response = httpClient.execute(httpPost); - resultString = EntityUtils.toString(response.getEntity(), "utf-8"); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - response.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - return resultString; - } -} diff --git a/src/main/java/com/cultural/heritage/utils/UrlUtil.java b/src/main/java/com/cultural/heritage/utils/UrlUtil.java new file mode 100644 index 0000000..cfa1f29 --- /dev/null +++ b/src/main/java/com/cultural/heritage/utils/UrlUtil.java @@ -0,0 +1,76 @@ +package com.cultural.heritage.utils; + +//请求工具类 +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.URL; +import java.net.URLConnection; +import java.util.Iterator; +import java.util.Map; + +public class UrlUtil { + /** + * 向指定 URL 发送POST方法的请求 + * + * @param url 发送请求的 URL + * @return 所代表远程资源的响应结果 + */ + public static String sendPost(String url, Map paramMap) { + PrintWriter out = null; + BufferedReader in = null; + String result = ""; + + String param = ""; + Iterator it = paramMap.keySet().iterator(); + + while(it.hasNext()) { + String key = it.next(); + param += key + "=" + paramMap.get(key) + "&"; + } + + try { + URL realUrl = new URL(url); + // 打开和URL之间的连接 + URLConnection conn = realUrl.openConnection(); + // 设置通用的请求属性 + conn.setRequestProperty("accept", "*/*"); + conn.setRequestProperty("connection", "Keep-Alive"); + conn.setRequestProperty("Accept-Charset", "utf-8"); + conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + // 发送POST请求必须设置如下两行 + conn.setDoOutput(true); + conn.setDoInput(true); + // 获取URLConnection对象对应的输出流 + out = new PrintWriter(conn.getOutputStream()); + // 发送请求参数 + out.print(param); + // flush输出流的缓冲 + out.flush(); + // 定义BufferedReader输入流来读取URL的响应 + in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); + String line; + while ((line = in.readLine()) != null) { + result += line; + } + } catch (Exception e) { + e.printStackTrace(); + } + //使用finally块来关闭输出流、输入流 + finally{ + try{ + if(out!=null){ + out.close(); + } + if(in!=null){ + in.close(); + } + } + catch(IOException ex){ + ex.printStackTrace(); + } + } + return result; + } +} diff --git a/src/main/java/com/cultural/heritage/utils/WeChatUtil.java b/src/main/java/com/cultural/heritage/utils/WeChatUtil.java deleted file mode 100644 index 30e4be5..0000000 --- a/src/main/java/com/cultural/heritage/utils/WeChatUtil.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.cultural.heritage.utils; - - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; - -import java.util.HashMap; -import java.util.Map; - -public class WeChatUtil { - public static JSONObject getSessionKeyOrOpenId(String code) { - String requestUrl = "https://api.weixin.qq.com/sns/jscode2session"; - Map requestUrlParam = new HashMap<>(); - // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN - //小程序appId - requestUrlParam.put("appid", "wx3f968a09e31d6bed"); - //小程序secret - requestUrlParam.put("secret", "847bdda7c2b01e88d59948b9ba50ef8d"); - //小程序端返回的code - requestUrlParam.put("js_code", code); - //默认参数 - requestUrlParam.put("grant_type", "authorization_code"); - //发送post请求读取调用微信接口获取openid用户唯一标识 - JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl, requestUrlParam)); - return jsonObject; - } -} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index c8187f4..f86a08a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -48,5 +48,6 @@ hwyun: bucketName: carbon2 weixin: - appid: wx3f968a09e31d6bed - secret: 847bdda7c2b01e88d59948b9ba50ef8d + mini: + appid: wx3f968a09e31d6bed + secret: 847bdda7c2b01e88d59948b9ba50ef8d