完善了订单接口
This commit is contained in:
parent
ddecad324e
commit
6cf376c0d2
13
pom.xml
13
pom.xml
|
@ -150,6 +150,19 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- http请求工具包依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>4.5.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.62</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
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.model.entity.User;
|
||||||
|
import com.cultural.heritage.model.vo.UserVO;
|
||||||
|
import com.cultural.heritage.service.user.UserService;
|
||||||
|
import com.cultural.heritage.utils.WeChatUtil;
|
||||||
|
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.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/wx")
|
||||||
|
@Slf4j
|
||||||
|
@Tag(name = "微信登录接口")
|
||||||
|
public class WeChatController {
|
||||||
|
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/wx/login")
|
||||||
|
public BaseResponse<UserVO> 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)) {
|
||||||
|
// return ResultUtils.success(ErrorCode.PARAMS_ERROR, "签名校验失败");
|
||||||
|
}
|
||||||
|
// 5.根据返回的User实体类,判断用户是否是新用户,是的话,将用户信息存到数据库;
|
||||||
|
LambdaQueryWrapper<User> 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);
|
||||||
|
}
|
||||||
|
// return R.ok().data(user);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,7 +1,5 @@
|
||||||
package com.cultural.heritage.model.dto.coupon;
|
package com.cultural.heritage.model.dto.coupon;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
|
@ -62,6 +60,13 @@ public class CouponAddRequest implements Serializable {
|
||||||
*/
|
*/
|
||||||
private Date endTime;
|
private Date endTime;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 优惠券图片
|
||||||
|
*/
|
||||||
|
private String image;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用说明
|
* 使用说明
|
||||||
*/
|
*/
|
||||||
|
|
134
src/main/java/com/cultural/heritage/utils/HttpClientUtil.java
Normal file
134
src/main/java/com/cultural/heritage/utils/HttpClientUtil.java
Normal file
|
@ -0,0 +1,134 @@
|
||||||
|
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<String, String> 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<String, String> param) {
|
||||||
|
// 创建Httpclient对象
|
||||||
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
String resultString = "";
|
||||||
|
try {
|
||||||
|
// 创建Http Post请求
|
||||||
|
HttpPost httpPost = new HttpPost(url);
|
||||||
|
// 创建参数列表
|
||||||
|
if (param != null) {
|
||||||
|
List<NameValuePair> 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;
|
||||||
|
}
|
||||||
|
}
|
27
src/main/java/com/cultural/heritage/utils/WeChatUtil.java
Normal file
27
src/main/java/com/cultural/heritage/utils/WeChatUtil.java
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
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<String, String> 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -46,3 +46,7 @@ hwyun:
|
||||||
securityKey: djX3WNrYjRDmp5v7rOXfa25e9CHj8OXKRzQJp6Ec
|
securityKey: djX3WNrYjRDmp5v7rOXfa25e9CHj8OXKRzQJp6Ec
|
||||||
endPoint: obs.cn-north-4.myhuaweicloud.com
|
endPoint: obs.cn-north-4.myhuaweicloud.com
|
||||||
bucketName: carbon2
|
bucketName: carbon2
|
||||||
|
|
||||||
|
weixin:
|
||||||
|
appid: wx3f968a09e31d6bed
|
||||||
|
secret: 847bdda7c2b01e88d59948b9ba50ef8d
|
||||||
|
|
Loading…
Reference in New Issue
Block a user