parent
0cfe13f4e5
commit
d3551233d1
30 changed files with 275 additions and 179 deletions
@ -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(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -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.dto; |
package org.springblade.modules.weixin.dto; |
||||||
|
|
||||||
import lombok.Data; |
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 com.baomidou.mybatisplus.annotation.TableName; |
||||||
import lombok.Data; |
import lombok.Data; |
||||||
@ -1,4 +1,4 @@ |
|||||||
package org.springblade.weixin.entity; |
package org.springblade.modules.weixin.entity; |
||||||
|
|
||||||
import lombok.Data; |
import lombok.Data; |
||||||
|
|
||||||
@ -1,4 +1,4 @@ |
|||||||
package org.springblade.weixin.entity; |
package org.springblade.modules.weixin.entity; |
||||||
|
|
||||||
import lombok.Data; |
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.TableField; |
||||||
import com.baomidou.mybatisplus.annotation.TableName; |
import com.baomidou.mybatisplus.annotation.TableName; |
||||||
@ -1,8 +1,8 @@ |
|||||||
<?xml version="1.0" encoding="UTF-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"> |
<!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 |
SELECT |
||||||
ad.id, |
ad.id, |
||||||
ad.consignee, |
ad.consignee, |
||||||
@ -1,8 +1,8 @@ |
|||||||
<?xml version="1.0" encoding="UTF-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"> |
<!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 |
SELECT |
||||||
a.buyer_phone AS phone, |
a.buyer_phone AS phone, |
||||||
count(a.buyer_phone) AS count |
count(a.buyer_phone) AS count |
||||||
@ -1,4 +1,4 @@ |
|||||||
package org.springblade.weixin.utils; |
package org.springblade.modules.weixin.utils; |
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j; |
import lombok.extern.slf4j.Slf4j; |
||||||
import org.apache.http.NameValuePair; |
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.annotation.JsonInclude; |
||||||
import com.fasterxml.jackson.core.JsonProcessingException; |
import com.fasterxml.jackson.core.JsonProcessingException; |
||||||
import com.fasterxml.jackson.databind.DeserializationFeature; |
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.JSONObject; |
||||||
import cn.hutool.json.JSONUtil; |
import cn.hutool.json.JSONUtil; |
||||||
import com.alibaba.fastjson.TypeReference; |
import com.alibaba.fastjson.TypeReference; |
||||||
import com.fasterxml.jackson.databind.ObjectMapper; |
import com.fasterxml.jackson.databind.ObjectMapper; |
||||||
import org.apache.commons.codec.binary.Base64; |
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.impl.client.CloseableHttpClient; |
||||||
import org.apache.http.util.EntityUtils; |
|
||||||
import org.springblade.core.http.util.HttpUtil; |
import org.springblade.core.http.util.HttpUtil; |
||||||
import org.springframework.http.ResponseEntity; |
import org.springframework.http.ResponseEntity; |
||||||
import org.springframework.web.client.RestTemplate; |
import org.springframework.web.client.RestTemplate; |
||||||
@ -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)); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
Loading…
Reference in new issue