提交 cd58cc54 authored 作者: 李秋林's avatar 李秋林

暂存代码

上级 e73c5435
......@@ -122,6 +122,21 @@
<version>1.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk18on -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.64</version>
</dependency>
<!-- 微信小程序 SDK 依赖 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>4.6.0</version>
</dependency>
</dependencies>
<build>
......
package com.wangxiaolu.promotion.config.weixin;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Slf4j
@Configuration
@EnableConfigurationProperties(WxMaProperties.class)
public class WxMaConfiguration {
private final WxMaProperties properties;
@Autowired
public WxMaConfiguration(WxMaProperties properties) {
this.properties = properties;
}
@Bean
public WxMaService wxMaService() {
List<WxMaProperties.Config> configs = this.properties.getConfigs();
if (configs == null) {
throw new WxRuntimeException("微信相关配置错误");
}
WxMaService maService = new WxMaServiceImpl();
maService.setMultiConfigs(
configs.stream()
.map(a -> {
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
// WxMaDefaultConfigImpl config = new WxMaRedisConfigImpl(new JedisPool());
// 使用上面的配置时,需要同时引入jedis-lock的依赖,否则会报类无法找到的异常
config.setAppid(a.getAppid());
config.setSecret(a.getSecret());
config.setToken(a.getToken());
config.setAesKey(a.getAesKey());
config.setMsgDataFormat(a.getMsgDataFormat());
return config;
}).collect(Collectors.toMap(WxMaDefaultConfigImpl::getAppid, a -> a, (o, n) -> o)));
return maService;
}
@Bean
public WxMaMessageRouter wxMaMessageRouter(WxMaService wxMaService) {
final WxMaMessageRouter router = new WxMaMessageRouter(wxMaService);
router
.rule().handler(logHandler).next()
.rule().async(false).content("订阅消息").handler(subscribeMsgHandler).end()
.rule().async(false).content("文本").handler(textHandler).end()
.rule().async(false).content("图片").handler(picHandler).end()
.rule().async(false).content("二维码").handler(qrcodeHandler).end();
return router;
}
private final WxMaMessageHandler subscribeMsgHandler = (wxMessage, context, service, sessionManager) -> {
service.getMsgService().sendSubscribeMsg(WxMaSubscribeMessage.builder()
.templateId("此处更换为自己的模板id")
.data(Lists.newArrayList(
new WxMaSubscribeMessage.MsgData("keyword1", "339208499")))
.toUser(wxMessage.getFromUser())
.build());
return null;
};
private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> {
log.info("收到消息:" + wxMessage.toString());
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson())
.toUser(wxMessage.getFromUser()).build());
return null;
};
private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) -> {
service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息")
.toUser(wxMessage.getFromUser()).build());
return null;
};
private final WxMaMessageHandler picHandler = (wxMessage, context, service, sessionManager) -> {
try {
WxMediaUploadResult uploadResult = service.getMediaService()
.uploadMedia("image", "png",
ClassLoader.getSystemResourceAsStream("tmp.png"));
service.getMsgService().sendKefuMsg(
WxMaKefuMessage
.newImageBuilder()
.mediaId(uploadResult.getMediaId())
.toUser(wxMessage.getFromUser())
.build());
} catch (WxErrorException e) {
e.printStackTrace();
}
return null;
};
private final WxMaMessageHandler qrcodeHandler = (wxMessage, context, service, sessionManager) -> {
try {
final File file = service.getQrcodeService().createQrcode("123", 430);
WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia("image", file);
service.getMsgService().sendKefuMsg(
WxMaKefuMessage
.newImageBuilder()
.mediaId(uploadResult.getMediaId())
.toUser(wxMessage.getFromUser())
.build());
} catch (WxErrorException e) {
e.printStackTrace();
}
return null;
};
}
package com.wangxiaolu.promotion.config.weixin;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Data
@ConfigurationProperties(prefix = "wx.miniapp")
public class WxMaProperties {
private List<Config> configs;
@Data
public static class Config {
/**
* 设置微信小程序的appid
*/
private String appid;
/**
* 设置微信小程序的Secret
*/
private String secret;
/**
* 设置微信小程序消息服务器配置的token
*/
private String token;
/**
* 设置微信小程序消息服务器配置的EncodingAESKey
*/
private String aesKey;
/**
* 消息格式,XML或者JSON
*/
private String msgDataFormat;
}
}
package com.wangxiaolu.promotion.controller.wechat;
import com.alibaba.fastjson.JSONObject;
import com.wangxiaolu.promotion.pojo.user.vo.WxJsUserInfoVo;
import com.wangxiaolu.promotion.service.wechat.WeChatUserCoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author : liqiulin
* @date : 2024-04-03 17
* @describe : 微信用户信息
*/
@Slf4j
@RestController
@RequestMapping("/user/wechat/core")
public class WeChatUserCoreController {
@Autowired
WeChatUserCoreService weChatUserCoreService;
/**
* 促销员注册信息
*/
@PostMapping("/temporary/enroll")
public boolean enrollUserInfo(@RequestBody @Validated WxJsUserInfoVo wxJsUserInfoVo) {
System.out.println("=============== controller-WxJsUserInfoVo ===============");
System.out.println(JSONObject.toJSONString(wxJsUserInfoVo));
weChatUserCoreService.saveWxUserInfoTemporary(wxJsUserInfoVo);
return true;
}
}
package com.wangxiaolu.promotion.controller.wechat;
import com.wangxiaolu.promotion.utils.WxMaUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author : liqiulin
* @date : 2024-04-09 13
* @describe :微信用户信息查询
*/
@Slf4j
@RestController
@RequestMapping("/user/wechat/query")
public class WeChatUserQueryController {
@Autowired
WxMaUtils wxMaUtils;
/**
* 接收wx临时登录凭证code,查询openid是否已注册
* 促销员查询
*/
@GetMapping("/temporary/openid")
public Boolean getOpenIdByWxcode(String jsCode) {
if (StringUtils.isEmpty(jsCode)){
return null;
}
String userOpenId = wxMaUtils.getWxOpenId(jsCode);
System.out.println("根据userOpenId查询用户是否存在:" + userOpenId);
return true;
}
}
package com.wangxiaolu.promotion.domain.user.dao;
/**
* @author : liqiulin
* @date : 2024-04-08 16
* @describe :微信-促销员-信息
*/
public interface WxTemporaryInfoDao {
void saveTemporaryInfo();
}
package com.wangxiaolu.promotion.domain.user.dao.impl;
import com.wangxiaolu.promotion.domain.user.dao.WxTemporaryInfoDao;
import org.springframework.stereotype.Service;
/**
* @author : liqiulin
* @date : 2024-04-08 16
* @describe :
*/
@Service
public class WxTemporaryInfoDaoImpl implements WxTemporaryInfoDao {
@Override
public void saveTemporaryInfo() {
}
}
package com.wangxiaolu.promotion.pojo.user.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
/**
* @author : liqiulin
* @date : 2024-04-08 16
* @describe :微信促销员DTO
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class WxTemporaryInfoDto {
/**
* openId
*/
String openId;
/**
* 头像
*/
String avatarUrl;
/**
* 头像云存储文件id
*/
String avatarUrlFieldId;
/**
* 姓名
*/
String name;
/**
* 手机号
*/
String phone;
/**
* 身份证号
*/
String idenNmber;
/**
* 身份证正面照
*/
String idenFrontPhotoUrl;
/**
* 身份证正面照
*/
String idenFrontPhotoFieldId;
/**
* 身份证反面照
*/
String idenReversePhotoUrl;
/**
* 身份证反面照
*/
String idenReversePhotoFieldId;
/**
* 详细地址
*/
String address;
/**
* 手机验证码
*/
String phoneCode;
}
package com.wangxiaolu.promotion.pojo.user.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotBlank;
/**
* @author : liqiulin
* @date : 2024-04-08 12
* @describe :
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class WxJsUserInfoVo {
/**
* js传过来的临时登录code
*/
// @NotBlank(message = "微信登录凭证错误")
// String jsCode;
/**
* 用户信息的加密文字
*/
// String encryptedData;
/**
* 标识
*/
// String iv;
/**
* openId
*/
@NotBlank(message = "微信登录错误")
String openId;
/**
* 头像
*/
String avatarUrl;
/**
* 头像云存储文件id
*/
String avatarUrlFieldId;
/**
* 姓名
*/
@NotBlank(message = "姓名不可为空")
String name;
/**
* 手机号
*/
@NotBlank(message = "手机号不可为空")
String phone;
/**
* 身份证号
*/
@NotBlank(message = "身份证号不可为空")
String idenNmber;
/**
* 身份证正面照
*/
@NotBlank(message = "请上传身份证正面照")
String idenFrontPhotoUrl;
/**
* 身份证正面照
*/
@NotBlank(message = "请上传身份证正面照")
String idenFrontPhotoFieldId;
/**
* 身份证反面照
*/
@NotBlank(message = "请上传身份证反面照")
String idenReversePhotoUrl;
/**
* 身份证反面照
*/
@NotBlank(message = "请上传身份证反面照")
String idenReversePhotoFieldId;
/**
* 详细地址
*/
String address;
/**
* 手机验证码
*/
@NotBlank(message = "验证码无效")
String phoneCode;
}
package com.wangxiaolu.promotion.service.wechat;
import com.wangxiaolu.promotion.pojo.user.vo.WxJsUserInfoVo;
/**
* @author : liqiulin
* @date : 2024-04-08 16
* @describe :微信信息操作
*/
public interface WeChatUserCoreService {
/**
* 保存促销员用户信息
*/
void saveWxUserInfoTemporary(WxJsUserInfoVo wxJsUserInfoVo);
}
package com.wangxiaolu.promotion.service.wechat.impl;
import com.alibaba.fastjson.JSONObject;
import com.wangxiaolu.promotion.pojo.user.dto.WxTemporaryInfoDto;
import com.wangxiaolu.promotion.pojo.user.vo.WxJsUserInfoVo;
import com.wangxiaolu.promotion.service.wechat.WeChatUserCoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
/**
* @author : liqiulin
* @date : 2024-04-08 16
* @describe :微信用户信息操作
*/
@Service
public class WeChatUserCoreServiceImpl implements WeChatUserCoreService {
/**
* 保存促销员用户信息
*/
@Override
public void saveWxUserInfoTemporary(WxJsUserInfoVo wxJsUserInfoVo) {
WxTemporaryInfoDto temporaryDto = new WxTemporaryInfoDto();
BeanUtils.copyProperties(wxJsUserInfoVo, temporaryDto);
System.out.println("=============== impl-WxTemporaryInfoDto ===============");
System.out.println(JSONObject.toJSONString(temporaryDto));
}
}
package com.wangxiaolu.promotion.utils;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
......@@ -14,14 +13,12 @@ import java.util.Map;
public class OkHttp {
public static JSONObject post(String url, Map<String, Object> params) {
String requestBody= HttpUtil.createPost(url).contentType("application/json;charset=utf-8").body(JSONObject.toJSONString(params)).execute().body();
String requestBody = HttpUtil.createPost(url).contentType("application/json;charset=utf-8").body(JSONObject.toJSONString(params)).execute().body();
JSONObject resultJson = JSONObject.parseObject(requestBody);
String returnCode = resultJson.getString("return_code");
if (!"0".equals(returnCode)){
if (!"0".equals(returnCode)) {
throw new RuntimeException("OkHttp.post请求error,详情:" + requestBody);
}
return resultJson;
}
}
package com.wangxiaolu.promotion.utils;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.stereotype.Component;
/**
* @author : liqiulin
* @date : 2024-04-08 11
* @describe : 微信工具
*/
@AllArgsConstructor
@Component
public class WxMaUtils {
private final WxMaService wxMaService;
/**
* 根据code查询openId
*/
public String getWxOpenId(String jsCode) {
WxMaJscode2SessionResult sessionInfo = null;
try {
sessionInfo = wxMaService.getUserService().getSessionInfo(jsCode);
String openid = sessionInfo.getOpenid();
return openid;
} catch (WxErrorException e) {
throw new RuntimeException();
}
}
/**
* 微信用户信息解密
* (粗糙信息)
*/
public String decrUserInfo(String jsCode, String encryptedData, String iv) {
WxMaJscode2SessionResult sessionInfo = null;
try {
sessionInfo = wxMaService.getUserService().getSessionInfo(jsCode);
String openid = sessionInfo.getOpenid();
String sessionKey = sessionInfo.getSessionKey();
WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
String phoneNumber = phoneNoInfo.getPhoneNumber();
WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
} catch (WxErrorException e) {
throw new RuntimeException(e);
}
return null;
}
}
......@@ -22,3 +22,12 @@ async:
core_pool_size: 4
name:
prefix: promotion-
wx:
miniapp:
configs:
- appid: wxac14dc7765484d7d
secret: b3315e66608a397f88e7e82b8f950bcd
token: #微信小程序消息服务器配置的token
aesKey: #微信小程序消息服务器配置的EncodingAESKey
msgDataFormat: JSON
\ No newline at end of file
package com.wangxiaolu.promotion.controller.wechat;
import com.wangxiaolu.promotion.pojo.user.vo.WxJsUserInfoVo;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author : liqiulin
* @date : 2024-04-08 16
* @describe :
*/
@SpringBootTest
@RunWith(SpringRunner.class)
class WeChatUserCoreControllerTest {
@Autowired
WeChatUserCoreController weChatUserCoreController;
@Test
void enrollUserInfo() {
WxJsUserInfoVo vo = new WxJsUserInfoVo()
.setJsCode("js传过来的临时登录code")
.setEncryptedData("用户信息的加密文字")
.setIv("标识iv")
.setOpenId("openid111")
.setAvatarUrl("头像")
.setName("姓名")
.setPhone("手机号")
.setIdenNmber("身份证号")
.setAddress("地址111")
;
weChatUserCoreController.enrollUserInfo(vo);
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论