修改鉴权

master
liuqingkun 2 years ago
parent 0cfe13f4e5
commit d3551233d1
  1. 1
      src/main/java/org/springblade/common/constant/DictConstant.java
  2. 60
      src/main/java/org/springblade/common/utils/AutoKeyUtil.java
  3. 2
      src/main/java/org/springblade/modules/auth/endpoint/BladeTokenEndPoint.java
  4. 2
      src/main/java/org/springblade/modules/auth/utils/TokenUtil.java
  5. 7
      src/main/java/org/springblade/modules/desk/controller/OrderController.java
  6. 2
      src/main/java/org/springblade/modules/desk/service/IOrderService.java
  7. 48
      src/main/java/org/springblade/modules/desk/service/impl/OrderServiceImpl.java
  8. 16
      src/main/java/org/springblade/modules/system/controller/DictBizController.java
  9. 96
      src/main/java/org/springblade/modules/weixin/controller/AppEbLoginController.java
  10. 11
      src/main/java/org/springblade/modules/weixin/controller/WeChatAddressController.java
  11. 8
      src/main/java/org/springblade/modules/weixin/controller/WeChatUserController.java
  12. 2
      src/main/java/org/springblade/modules/weixin/dto/BuyCount.java
  13. 2
      src/main/java/org/springblade/modules/weixin/entity/WeChatAddress.java
  14. 2
      src/main/java/org/springblade/modules/weixin/entity/WeChatPhone.java
  15. 2
      src/main/java/org/springblade/modules/weixin/entity/WeChatPhoneInfo.java
  16. 2
      src/main/java/org/springblade/modules/weixin/entity/WeChatUser.java
  17. 6
      src/main/java/org/springblade/modules/weixin/mapper/WeChatAddressMapper.java
  18. 4
      src/main/java/org/springblade/modules/weixin/mapper/WeChatAddressMapper.xml
  19. 6
      src/main/java/org/springblade/modules/weixin/mapper/WeChatUserMapper.java
  20. 4
      src/main/java/org/springblade/modules/weixin/mapper/WeChatUserMapper.xml
  21. 6
      src/main/java/org/springblade/modules/weixin/service/IWeChatAddressService.java
  22. 4
      src/main/java/org/springblade/modules/weixin/service/IWeChatUserService.java
  23. 10
      src/main/java/org/springblade/modules/weixin/service/impl/WeChatAddressServiceImpl.java
  24. 20
      src/main/java/org/springblade/modules/weixin/service/impl/WeChatUserServiceImpl.java
  25. 2
      src/main/java/org/springblade/modules/weixin/utils/HttpClientSslUtils.java
  26. 3
      src/main/java/org/springblade/modules/weixin/utils/JsonUtil.java
  27. 6
      src/main/java/org/springblade/modules/weixin/utils/WeChatUtil.java
  28. 4
      src/main/java/org/springblade/modules/weixin/utils/WxPayUtils.java
  29. 110
      src/main/java/org/springblade/weixin/controller/AppEbLoginController.java
  30. 6
      src/main/resources/application.yml

@ -49,6 +49,7 @@ public interface DictConstant {
* 商品信息
*/
String GOODS_INFO = "goods_info";
String GOODS_INFO_KEY_NAME = "goodsName";
String GOODS_INFO_KEY_PRICE = "price";
String GOODS_INFO_KEY_IMG = "imgPath";

@ -0,0 +1,60 @@
package org.springblade.common.utils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
/**
* @author liuqk
* @version 1.0
* @date 2023-08-22 14:11
*/
@Slf4j
@RequiredArgsConstructor
public class AutoKeyUtil {
RedisTemplate<String, String> redisTemplate;
public String getAutoIncreSerialNumber(String redisKey, int length) {
ValueOperations<String, String> operations = redisTemplate.opsForValue();
operations.increment(redisKey, 1L);
String value = operations.get(redisKey).toString();
String numStr = addZero(value, length);
if (getMaxNumber(length).equals(numStr)) {
log.info("the serialNumber is maxNum, reset the key {}", redisKey);
redisTemplate.delete(redisKey);
}
log.info("the serialNumber is {}", numStr);
return numStr;
}
/**
* 根据位数生成最大值
*
* @param len
* @return
*/
private String getMaxNumber(int len) {
StringBuffer rStr = new StringBuffer();
for (int i = 0; i < len; i++) {
rStr.append("9");
}
return rStr.toString();
}
// 根据位数自动补零
private String addZero(String numStr, Integer maxNum) {
int addNum = maxNum - numStr.length();
StringBuffer rStr = new StringBuffer();
for (int i = 0; i < addNum; i++) {
rStr.append("0");
}
rStr.append(numStr);
return rStr.toString();
}
}

@ -42,7 +42,7 @@ import org.springblade.modules.auth.provider.TokenGranterBuilder;
import org.springblade.modules.auth.provider.TokenParameter;
import org.springblade.modules.auth.utils.TokenUtil;
import org.springblade.modules.system.entity.UserInfo;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.entity.WeChatUser;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@ -29,7 +29,7 @@ import org.springblade.core.tool.utils.*;
import org.springblade.modules.system.entity.Tenant;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.entity.UserInfo;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.entity.WeChatUser;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@ -107,7 +107,12 @@ public class OrderController {
return R.fail("您当前有未付款的清单, 请支付.");
}
String price = DictBizCache.getValue(DictConstant.GOODS_INFO, DictConstant.GOODS_INFO_KEY_PRICE);
// 生成订单号, 年月日+4位自增
info.setOrderNo(DateUtil.format(DateUtil.now(), "yyyyMMddHH") + orderService.getAutoIncreSerialNumber());
String goodsName = DictBizCache.getValue(DictConstant.GOODS_INFO, DictConstant.GOODS_INFO_KEY_NAME);
String price = DictBizCache.getValue(DictConstant.GOODS_INFO, DictConstant.GOODS_INFO_KEY_NAME);
info.setGoodsName(goodsName);
info.setGoodsPrice(Func.toDouble(price));
info.setGoodsNum(1);

@ -28,5 +28,5 @@ import org.springblade.modules.desk.entity.Order;
* @date 2023-08-21 11:35
*/
public interface IOrderService extends BaseService<Order> {
String getAutoIncreSerialNumber();
}

@ -16,6 +16,8 @@
*/
package org.springblade.modules.desk.service.impl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.desk.entity.CustomMadeInfo;
import org.springblade.modules.desk.entity.Order;
@ -23,6 +25,8 @@ import org.springblade.modules.desk.mapper.CustomMadeInfoMapper;
import org.springblade.modules.desk.mapper.OrderMapper;
import org.springblade.modules.desk.service.ICustomMadeInfoService;
import org.springblade.modules.desk.service.IOrderService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
/**
@ -32,8 +36,52 @@ import org.springframework.stereotype.Service;
* @version 1.0
* @date 2023-08-21 11:35
*/
@Slf4j
@RequiredArgsConstructor
@Service
public class OrderServiceImpl extends BaseServiceImpl<OrderMapper, Order> implements IOrderService {
private final RedisTemplate<String, String> redisTemplate;
@Override
public String getAutoIncreSerialNumber() {
String redisKey = "order-num-auto-incre";
int length = 4;
ValueOperations<String, String> operations = redisTemplate.opsForValue();
operations.increment(redisKey, 1L);
String value = operations.get(redisKey).toString();
String numStr = addZero(value, length);
if (getMaxNumber(length).equals(numStr)) {
log.info("the serialNumber is maxNum, reset the key {}", redisKey);
redisTemplate.delete(redisKey);
}
log.info("the serialNumber is {}", numStr);
return numStr;
}
/**
* 根据位数生成最大值
*
* @param len
* @return
*/
private String getMaxNumber(int len) {
StringBuffer rStr = new StringBuffer();
for (int i = 0; i < len; i++) {
rStr.append("9");
}
return rStr.toString();
}
// 根据位数自动补零
private String addZero(String numStr, Integer maxNum) {
int addNum = maxNum - numStr.length();
StringBuffer rStr = new StringBuffer();
for (int i = 0; i < addNum; i++) {
rStr.append("0");
}
rStr.append(numStr);
return rStr.toString();
}
}

@ -256,7 +256,11 @@ public class DictBizController extends BladeController {
*/
@PostMapping("/save-goods-info")
@ApiOperation(value = "购买渠道列表", notes = "购买渠道列表")
public R saveGoodsInfo(String price, String imgPath) {
public R saveGoodsInfo(String goodsName, String price, String imgPath) {
if (Func.isEmpty(goodsName)) {
return R.fail("商品名称不能为空");
}
if (Func.isEmpty(price)) {
return R.fail("商品价格不能为空");
}
@ -265,6 +269,13 @@ public class DictBizController extends BladeController {
return R.fail("商品图片不能为空");
}
LambdaUpdateWrapper<DictBiz> updateName = new LambdaUpdateWrapper<>();
updateName
.eq(DictBiz::getCode, DictConstant.GOODS_INFO)
.eq(DictBiz::getDictKey, DictConstant.GOODS_INFO_KEY_NAME)
.set(DictBiz::getDictValue, price)
;
LambdaUpdateWrapper<DictBiz> updatePrice = new LambdaUpdateWrapper<>();
updatePrice
.eq(DictBiz::getCode, DictConstant.GOODS_INFO)
@ -278,8 +289,9 @@ public class DictBizController extends BladeController {
.eq(DictBiz::getDictKey, DictConstant.GOODS_INFO_KEY_IMG)
.set(DictBiz::getDictValue, imgPath)
;
boolean flagName = dictService.update(null, updateName);
boolean flagPrice = dictService.update(null, updatePrice);
boolean flagImgPath = dictService.update(null, updateImgPath);
return R.status(flagPrice && flagImgPath);
return R.status(flagName && flagPrice && flagImgPath);
}
}

@ -0,0 +1,96 @@
package org.springblade.modules.weixin.controller;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.auth.endpoint.BladeTokenEndPoint;
import org.springblade.modules.weixin.entity.WeChatPhone;
import org.springblade.modules.weixin.entity.WeChatPhoneInfo;
import org.springblade.modules.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.service.IWeChatUserService;
import org.springblade.modules.weixin.utils.WeChatUtil;
import org.springblade.modules.weixin.utils.WxPayUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@Api(tags = "登录-小程序")
@AllArgsConstructor
@RequestMapping("/app")
public class AppEbLoginController {
private final IWeChatUserService weChatUserService;
/**
* 获取openid
*/
@PostMapping("/login")
public R<Map> login(@RequestBody WeChatPhone weChatPhone) {
//小程序 appId = "wx432c2efe6df3b97a" appSecret = "859df8b167e74223e9237dee1b344524"
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId");
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret");
//小程序需要传来一个code
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getCode2Session(weChatPhone.getCode(), appId, appSecret);
String openid = accessTokenJson.get("openid", String.class);
System.out.println("accessTokenJson:" + accessTokenJson.toString());
Map<String, Object> map = new HashMap<>();
map.put("openid", openid);
map.put("userInfo", null);
//根据openid获取本地用户信息
WeChatUser user = weChatUserService.getOne(Wrappers.<WeChatUser>lambdaQuery().eq(WeChatUser::getOpenId, openid));
if (user != null) {
map.put("userInfo", user);
BladeTokenEndPoint point = SpringUtil.getBean(BladeTokenEndPoint.class);
}
//这里可以对用户信息进行一些操作
return R.data(map);
}
/**
* 用前端请求接口获取的code换取用户手机号
* 前端需要请求的接口https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
*
* @param weChatPhone
* @return
*/
@PostMapping("/phone")
public R getPhone(@RequestBody WeChatPhone weChatPhone) {
//小程序appId appSecret
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId");
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret");
// 1.请求微信接口服务,获取accessToken
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getAccessToken(appId, appSecret);
System.out.println("accessTokenJson:" + accessTokenJson);
String accessToken = accessTokenJson.get("access_token", String.class);
System.out.println("accessToken:" + accessToken);
// 2.请求微信接口服务,获取用户手机号信息
if (StringUtils.isBlank(accessToken)) {
throw new ServiceException("获取access_token失败");
}
cn.hutool.json.JSONObject phoneNumberJson = WeChatUtil.getPhoneNumber(weChatPhone.getCode(), accessToken);
System.out.println("phoneNumberJson:" + phoneNumberJson);
WeChatPhoneInfo phoneInfo = phoneNumberJson.get("phone_info", WeChatPhoneInfo.class);
System.out.println("phoneInfo:" + phoneInfo.toString());
return R.data(phoneInfo.getPurePhoneNumber());
}
@PostMapping("/createOrder")
public R createOrder(String openId) {
return R.data(WxPayUtils.createOrderJSApiV3(openId));
}
}

@ -1,4 +1,4 @@
package org.springblade.weixin.controller;
package org.springblade.modules.weixin.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@ -7,9 +7,9 @@ import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.weixin.entity.WeChatAddress;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.weixin.service.IWeChatAddressService;
import org.springblade.modules.weixin.entity.WeChatAddress;
import org.springblade.modules.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.service.IWeChatAddressService;
import org.springframework.web.bind.annotation.*;
@RestController
@ -21,7 +21,6 @@ public class WeChatAddressController {
@GetMapping("/list")
public R<IPage<WeChatAddress>> list(WeChatUser weChatUser, Query query) {
// return R.data(weChatAddressService.getPage(weChatUser, Condition.getPage(query)));
return R.data(weChatAddressService.page(Condition.getPage(query), Wrappers.<WeChatAddress>lambdaQuery().eq(weChatUser.getId() != null, WeChatAddress::getUserId, weChatUser.getId())));
}
@ -40,4 +39,4 @@ public class WeChatAddressController {
return R.status(weChatAddressService.deleteLogic(Func.toLongList(ids)));
}
}
}

@ -1,4 +1,4 @@
package org.springblade.weixin.controller;
package org.springblade.modules.weixin.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@ -7,8 +7,8 @@ import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.weixin.service.IWeChatUserService;
import org.springblade.modules.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.service.IWeChatUserService;
import org.springframework.web.bind.annotation.*;
@RestController
@ -52,4 +52,4 @@ public class WeChatUserController {
return R.data(weChatUserService.getOne(Wrappers.<WeChatUser>lambdaQuery().eq(WeChatUser::getOpenId, openId)));
}
}
}

@ -1,4 +1,4 @@
package org.springblade.weixin.dto;
package org.springblade.modules.weixin.dto;
import lombok.Data;

@ -1,4 +1,4 @@
package org.springblade.weixin.entity;
package org.springblade.modules.weixin.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@ -1,4 +1,4 @@
package org.springblade.weixin.entity;
package org.springblade.modules.weixin.entity;
import lombok.Data;

@ -1,4 +1,4 @@
package org.springblade.weixin.entity;
package org.springblade.modules.weixin.entity;
import lombok.Data;

@ -1,4 +1,4 @@
package org.springblade.weixin.entity;
package org.springblade.modules.weixin.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

@ -14,12 +14,12 @@
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.weixin.mapper;
package org.springblade.modules.weixin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.weixin.entity.WeChatAddress;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.entity.WeChatAddress;
import org.springblade.modules.weixin.entity.WeChatUser;
import java.util.List;

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.springblade.weixin.mapper.WeChatAddressMapper">
<mapper namespace="org.springblade.modules.weixin.mapper.WeChatAddressMapper">
<select id="getPage" resultType="org.springblade.weixin.entity.WeChatAddress">
<select id="getPage" resultType="org.springblade.modules.weixin.entity.WeChatAddress">
SELECT
ad.id,
ad.consignee,

@ -14,11 +14,11 @@
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.weixin.mapper;
package org.springblade.modules.weixin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springblade.weixin.dto.BuyCount;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.dto.BuyCount;
import org.springblade.modules.weixin.entity.WeChatUser;
import java.util.List;

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.springblade.weixin.mapper.WeChatUserMapper">
<mapper namespace="org.springblade.modules.weixin.mapper.WeChatUserMapper">
<select id="buyCount" resultType="org.springblade.weixin.dto.BuyCount">
<select id="buyCount" resultType="org.springblade.modules.weixin.dto.BuyCount">
SELECT
a.buyer_phone AS phone,
count(a.buyer_phone) AS count

@ -14,12 +14,12 @@
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.weixin.service;
package org.springblade.modules.weixin.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseService;
import org.springblade.weixin.entity.WeChatAddress;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.entity.WeChatAddress;
import org.springblade.modules.weixin.entity.WeChatUser;
/**
* 服务类

@ -14,11 +14,11 @@
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.weixin.service;
package org.springblade.modules.weixin.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseService;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.entity.WeChatUser;
/**
* 服务类

@ -14,16 +14,16 @@
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.weixin.service.impl;
package org.springblade.modules.weixin.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.weixin.entity.WeChatAddress;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.weixin.mapper.WeChatAddressMapper;
import org.springblade.weixin.service.IWeChatAddressService;
import org.springblade.modules.weixin.entity.WeChatAddress;
import org.springblade.modules.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.mapper.WeChatAddressMapper;
import org.springblade.modules.weixin.service.IWeChatAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@ -14,29 +14,19 @@
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package org.springblade.weixin.service.impl;
package org.springblade.modules.weixin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.desk.entity.Order;
import org.springblade.modules.desk.mapper.OrderMapper;
import org.springblade.modules.desk.service.IOrderService;
import org.springblade.modules.system.entity.ApiScope;
import org.springblade.modules.system.mapper.ApiScopeMapper;
import org.springblade.modules.system.service.IApiScopeService;
import org.springblade.weixin.dto.BuyCount;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.weixin.mapper.WeChatUserMapper;
import org.springblade.weixin.service.IWeChatUserService;
import org.springblade.modules.weixin.dto.BuyCount;
import org.springblade.modules.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.mapper.WeChatUserMapper;
import org.springblade.modules.weixin.service.IWeChatUserService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ -1,4 +1,4 @@
package org.springblade.weixin.utils;
package org.springblade.modules.weixin.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.NameValuePair;

@ -1,7 +1,6 @@
package org.springblade.weixin.utils;
package org.springblade.modules.weixin.utils;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;

@ -1,15 +1,11 @@
package org.springblade.weixin.utils;
package org.springblade.modules.weixin.utils;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springblade.core.http.util.HttpUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

@ -1,11 +1,10 @@
package org.springblade.weixin.utils;
package org.springblade.modules.weixin.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.*;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.commons.collections4.QueueUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
@ -14,7 +13,6 @@ import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringUtil;
import java.io.ByteArrayInputStream;
import java.io.IOException;

@ -1,110 +0,0 @@
package org.springblade.weixin.controller;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.auth.endpoint.BladeTokenEndPoint;
import org.springblade.weixin.entity.WeChatPhone;
import org.springblade.weixin.entity.WeChatPhoneInfo;
import org.springblade.weixin.entity.WeChatUser;
import org.springblade.weixin.service.IWeChatUserService;
import org.springblade.weixin.utils.WeChatUtil;
import org.springblade.weixin.utils.WxPayUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@Api(tags = "登录-小程序")
@AllArgsConstructor
@RequestMapping("/app")
public class AppEbLoginController {
private final IWeChatUserService weChatUserService;
/**
* 获取openid
*/
@PostMapping("/login")
public R<Map> login(@RequestBody WeChatPhone weChatPhone) {
//小程序 appId = "wx432c2efe6df3b97a" appSecret = "859df8b167e74223e9237dee1b344524"
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId");
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret");
//小程序需要传来一个code
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getCode2Session(weChatPhone.getCode(), appId, appSecret);
String openid = accessTokenJson.get("openid", String.class);
System.out.println("accessTokenJson:" + accessTokenJson.toString());
Map<String, Object> map = new HashMap<>();
map.put("openid", openid);
map.put("userInfo", null);
//根据openid获取本地用户信息
WeChatUser user = weChatUserService.getOne(Wrappers.<WeChatUser>lambdaQuery().eq(WeChatUser::getOpenId, openid));
if (user != null) {
map.put("userInfo", user);
BladeTokenEndPoint point = SpringUtil.getBean(BladeTokenEndPoint.class);
// Kv admin = point.token("000000", "admin", "21232f297a57a5a743894a0e4a801fc3", "", "");
}
//获得响应的数据
// Map<String,Object> responseBody = map;
//if (responseBody.get("errcode")!=null&&responseBody.get("errcode").equals(40029)){
// return CommonResult.failed("code 无效");
// }
// 解密用户信息
// String encryptedData = request.get("encryptedData");
// String iv = request.get("iv");
// String sessionKey = (String) responseBody.get("session_key");
// Map<String, Object> userInfo = getDecryptedUserInfo(encryptedData, sessionKey, iv);
//这里可以对用户信息进行一些操作
return R.data(map);
}
/**
* 用前端请求接口获取的code换取用户手机号
* 前端需要请求的接口https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
*
* @param weChatPhone
* @return
*/
@PostMapping("/phone")
public R getPhone(@RequestBody WeChatPhone weChatPhone) {
//小程序appId appSecret
// String appId = "wx432c2efe6df3b97a";
// String appSecret = "859df8b167e74223e9237dee1b344524";
String appId = DictCache.getValue(DictEnum.WECHAT_APP, "appId");
String appSecret = DictCache.getValue(DictEnum.WECHAT_APP, "appSecret");
// 1.请求微信接口服务,获取accessToken
cn.hutool.json.JSONObject accessTokenJson = WeChatUtil.getAccessToken(appId, appSecret);
System.out.println("accessTokenJson:" + accessTokenJson);
String accessToken = accessTokenJson.get("access_token", String.class);
System.out.println("accessToken:" + accessToken);
// 2.请求微信接口服务,获取用户手机号信息
if (StringUtils.isBlank(accessToken)) {
throw new ServiceException("获取access_token失败");
}
cn.hutool.json.JSONObject phoneNumberJson = WeChatUtil.getPhoneNumber(weChatPhone.getCode(), accessToken);
System.out.println("phoneNumberJson:" + phoneNumberJson);
WeChatPhoneInfo phoneInfo = phoneNumberJson.get("phone_info", WeChatPhoneInfo.class);
System.out.println("phoneInfo:" + phoneInfo.toString());
return R.data(phoneInfo.getPurePhoneNumber());
}
@PostMapping("/createOrder")
public R createOrder(String openId) {
return R.data(WxPayUtils.createOrderJSApiV3(openId));
}
}

@ -183,12 +183,14 @@ blade:
secure:
#接口放行
skip-url:
- /blade-resource/**
- /blade-system/**
#- /blade-resource/**
#- /blade-system/**
- /blade-desk/custom-made-info/**
- /blade-system/dict-biz/get-logistics-companies
- /blade-system/dict-biz/get-purchase-channel
- /blade-system/dict-biz/get-goods-info
- /app/login
- /weChatUser/save
#授权认证配置
auth:
- method: ALL

Loading…
Cancel
Save