ソースを参照

Merge branch 'develop' of gitadmin/tuoheng_telecomumale into release

tags/v1.0.0^2
chengwang 1年前
コミット
919b3141a8
12個のファイルの変更333行の追加8行の削除
  1. +11
    -5
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/controller/WxController.java
  2. +11
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/dao/UserAuthorizeMapper.java
  3. +41
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/entity/UserAuthorize.java
  4. +36
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/entity/wx/WxMessageRequest.java
  5. +8
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/entity/wx/request/PubMessageRequest.java
  6. +39
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/enums/UserAuthorizeEnum.java
  7. +1
    -1
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/param/WxSendMessageParam.java
  8. +11
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/IUserAuthorizeService.java
  9. +14
    -1
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/IWxService.java
  10. +20
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/impl/UserAuthorizeServiceImpl.java
  11. +131
    -1
      tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/impl/WxServiceImpl.java
  12. +10
    -0
      tuoheng-service/tuoheng-miniprogram/src/main/resources/mapper/UserAuthorizeMapper.xml

+ 11
- 5
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/controller/WxController.java ファイルの表示

@@ -1,13 +1,11 @@
package com.tuoheng.miniprogram.controller;

import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.miniprogram.entity.wx.WxMessageRequest;
import com.tuoheng.miniprogram.service.IWxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

/**
* @Author ChengWang
@@ -28,7 +26,15 @@ public class WxController {
return iWxService.openid(code);
}

//获取access_code值
@PostMapping("/send/message")
public JsonResult sendMessage(@RequestBody WxMessageRequest wxMessageRequest){
log.info("进入发送消息接口,WxMessageRequest={}",wxMessageRequest);
return iWxService.sendMessage(wxMessageRequest);
}






}

+ 11
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/dao/UserAuthorizeMapper.java ファイルの表示

@@ -0,0 +1,11 @@
package com.tuoheng.miniprogram.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tuoheng.miniprogram.entity.UserAuthorize;

/**
* @Author ChengWang
* @Date 2023/6/26
*/
public interface UserAuthorizeMapper extends BaseMapper<UserAuthorize> {
}

+ 41
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/entity/UserAuthorize.java ファイルの表示

@@ -0,0 +1,41 @@
package com.tuoheng.miniprogram.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.tuoheng.common.core.common.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
* @Author ChengWang
* @Date 2022/11/16
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("th_user_authorize")
public class UserAuthorize extends BaseEntity {
private static final long serialVersionUID = 1L;

/**
*用户id
*/
private String userId;

/**
* 用户的openId
*/
private String openId;

/**
* 唯一标识
*/
private String unionId;

/**
* accessToken值(先不存)
*/
private String accessToken;

}

+ 36
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/entity/wx/WxMessageRequest.java ファイルの表示

@@ -0,0 +1,36 @@
package com.tuoheng.miniprogram.entity.wx;

import lombok.Data;

/**
* @Author ChengWang
* @Date 2023/6/26
*/
@Data
public class WxMessageRequest {

/**
* 任务id
*/
private String inspectionId;

/**
* 问题id
*/
private String questionId;

/**
* 上报时间
*/
private String createTime;

/**
*问题标题
*/
private String InspectionName;

/**
* 问题描述
*/
private String questionDesc;
}

+ 8
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/entity/wx/request/PubMessageRequest.java ファイルの表示

@@ -0,0 +1,8 @@
package com.tuoheng.miniprogram.entity.wx.request;

/**
* @Author ChengWang
* @Date 2023/6/26
*/
public class PubMessageRequest {
}

+ 39
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/enums/UserAuthorizeEnum.java ファイルの表示

@@ -0,0 +1,39 @@
package com.tuoheng.miniprogram.enums;

public enum UserAuthorizeEnum {

UNAUTHORIZED(10001, "用户未授权"),
OPEN_ID_IS_NULL(10002, "openId为空");

/**
* 码
*/
private Integer code;

/**
* 描述
*/
private String msg;

UserAuthorizeEnum(Integer code, String msg){
this.code = code;
this.msg = msg;
}

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

}

+ 1
- 1
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/param/WxSendMessageParam.java ファイルの表示

@@ -15,7 +15,7 @@ public class WxSendMessageParam {
/**
* 接收者openId
*/
private String openId;
private String touser;

/**
* 订阅模板id

+ 11
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/IUserAuthorizeService.java ファイルの表示

@@ -0,0 +1,11 @@
package com.tuoheng.miniprogram.service;

import com.tuoheng.common.core.common.IBaseService;
import com.tuoheng.miniprogram.entity.UserAuthorize;

/**
* @Author ChengWang
* @Date 2023/6/26
*/
public interface IUserAuthorizeService extends IBaseService<UserAuthorize> {
}

+ 14
- 1
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/IWxService.java ファイルの表示

@@ -1,6 +1,7 @@
package com.tuoheng.miniprogram.service;

import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.miniprogram.entity.wx.WxMessageRequest;

/**
* @Author ChengWang
@@ -8,6 +9,18 @@ import com.tuoheng.common.core.utils.JsonResult;
*/
public interface IWxService {

//根据code获取openId
/**
* 根据code获取openId
* @param code
* @return
*/
JsonResult openid(String code);

/**
* 发送消息
* @param wxMessageRequest
* @return
*/
JsonResult sendMessage(WxMessageRequest wxMessageRequest);

}

+ 20
- 0
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/impl/UserAuthorizeServiceImpl.java ファイルの表示

@@ -0,0 +1,20 @@
package com.tuoheng.miniprogram.service.impl;

import com.tuoheng.common.core.common.BaseServiceImpl;
import com.tuoheng.miniprogram.dao.RoleMapper;
import com.tuoheng.miniprogram.dao.UserAuthorizeMapper;
import com.tuoheng.miniprogram.entity.Role;
import com.tuoheng.miniprogram.entity.UserAuthorize;
import com.tuoheng.miniprogram.service.IRoleService;
import com.tuoheng.miniprogram.service.IUserAuthorizeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
* @Author ChengWang
* @Date 2022/12/16
*/
@Service
@Slf4j
public class UserAuthorizeServiceImpl extends BaseServiceImpl<UserAuthorizeMapper, UserAuthorize> implements IUserAuthorizeService {
}

+ 131
- 1
tuoheng-service/tuoheng-miniprogram/src/main/java/com/tuoheng/miniprogram/service/impl/WxServiceImpl.java ファイルの表示

@@ -1,13 +1,33 @@
package com.tuoheng.miniprogram.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import com.tuoheng.miniprogram.dao.UserAuthorizeMapper;
import com.tuoheng.miniprogram.entity.UserAuthorize;
import com.tuoheng.miniprogram.entity.dto.TemplateMinDto;
import com.tuoheng.miniprogram.entity.wx.WxMessageRequest;
import com.tuoheng.miniprogram.enums.MarkEnum;
import com.tuoheng.miniprogram.enums.UserAuthorizeEnum;
import com.tuoheng.miniprogram.param.WxSendMessageParam;
import com.tuoheng.miniprogram.service.IWxService;
import com.tuoheng.miniprogram.utils.CurrentUserUtil;
import com.tuoheng.miniprogram.utils.GetOpenIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @Author ChengWang
@@ -17,17 +37,127 @@ import org.springframework.stereotype.Service;
@Slf4j
public class WxServiceImpl implements IWxService {

@Autowired
private UserAuthorizeMapper userAuthorizeMapper;

private static String accessToken;

private static long expiresTime;


/**
* 根据code获取openId
* @param code
* @return
*/
@Override
public JsonResult openid(String code) {

String openidResult = GetOpenIdUtil.getopenid(code, CommonConfig.appId, CommonConfig.appSecret);
String openid = JSONObject.parseObject(openidResult).getString("openid");
String unionid = JSONObject.parseObject(openidResult).getString("unionid");
UserAuthorize userAuthorize = new UserAuthorize();
if(StringUtils.isNotEmpty(openid)){
return JsonResult.success(openid);
//查询出来openId数据入库
userAuthorize.setOpenId(openid);
}else {
log.error("获取openid失败:",openidResult);
return JsonResult.error("获取openid失败");
}
if(StringUtils.isNotEmpty(unionid)){
userAuthorize.setUnionId(unionid);
}
userAuthorize.setCreateTime(DateUtils.now());
if(StringUtils.isNotEmpty(CurrentUserUtil.getUserId())){
userAuthorize.setCreateUser(CurrentUserUtil.getUserId());
}
int result = userAuthorizeMapper.insert(userAuthorize);
if(result<=0){
return JsonResult.error("授权数据入库失败");
}
return JsonResult.success(openid);
}

/**
* 获取access_code值
* @return
*/
public String getAccessToken(){
//判断accessToken是否已经过期,如果国企需要重新获取
if(accessToken == null || expiresTime <System.currentTimeMillis()){
RestTemplate restTemplate = new RestTemplate();
Map<String,String> params = new HashMap<>(2);
params.put("APPID",CommonConfig.appId);
params.put("APPSECRET",CommonConfig.appSecret);
ResponseEntity<String> responseEntity = restTemplate.getForEntity("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
String body = responseEntity.getBody();
JSONObject object = JSON.parseObject(body);
Integer errcode = object.getInteger("errcode");
if(errcode != null && errcode != 0){
String errmsg = object.getString("errmsg");
log.info("请求accessToken失败,返回码:" + errcode + "错误信息:" + errmsg);
throw new RuntimeException("请求获取accessToken失败");
}
//缓存accessToken
accessToken = object.getString("access_token");
//设置accessToken的失效时间
Long expires_in = object.getLong("expires_in");
//失效时间 = 当前时间+有效期(提前一分钟,也可以不提前)
expiresTime = System.currentTimeMillis() + (expires_in - 60) * 1000;
}

return accessToken;
}

/**
* 发送消息
* @param wxMessageRequest
* @return
*/
@Override
public JsonResult sendMessage(WxMessageRequest wxMessageRequest) {
//获取当前登录用户id
String userId = CurrentUserUtil.getUserId();
boolean isSend =false;
//获取当前登录openId
UserAuthorize userAuthorize = userAuthorizeMapper.selectOne(Wrappers.<UserAuthorize>lambdaQuery()
.eq(UserAuthorize::getUserId, userId)
.eq(UserAuthorize::getMark, MarkEnum.VALID.getCode()));
if(ObjectUtil.isNull(userAuthorize)){
return JsonResult.success(UserAuthorizeEnum.UNAUTHORIZED.getCode(),UserAuthorizeEnum.UNAUTHORIZED.getMsg());
}
if(StringUtils.isEmpty(userAuthorize.getOpenId())){
return JsonResult.success(UserAuthorizeEnum.OPEN_ID_IS_NULL.getCode(),UserAuthorizeEnum.OPEN_ID_IS_NULL.getMsg());
}
String openId = userAuthorize.getOpenId();
RestTemplate restTemplate = new RestTemplate();
//发送消息订阅
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();
WxSendMessageParam param = new WxSendMessageParam();
param.setPage("/package_question/pages/taskList/taskList");
param.setTouser(openId);
param.setTemplate_id(CommonConfig.templateId);
Map<String, TemplateMinDto> data = new HashMap<>(3);
//消息模板{{time12.DATA}} {{thing4.DATA}} {{thing13.DATA}} 时间格式一定是这样的2022-01-13 02:31:36
ResponseEntity<String> responseEntity = null;
try {
data.put("time12",new TemplateMinDto(wxMessageRequest.getCreateTime()));
data.put("thing4",new TemplateMinDto(wxMessageRequest.getInspectionName()));
data.put("thing13",new TemplateMinDto(wxMessageRequest.getQuestionDesc()));
param.setData(data);

responseEntity = restTemplate.postForEntity(url, param, String.class);
String messageBody = responseEntity.getBody();
JSONObject jsonObject = JSON.parseObject(messageBody);
if(Objects.nonNull(jsonObject)){
log.info("openId:" + openId + "发送消息返回内容:" + jsonObject.toJSONString());
}
isSend = true;
} catch (RestClientException e) {
log.info("openId:"+ openId +"发送消息异常报错:"+e);
e.printStackTrace();
}

return JsonResult.success(isSend);
}
}

+ 10
- 0
tuoheng-service/tuoheng-miniprogram/src/main/resources/mapper/UserAuthorizeMapper.xml ファイルの表示

@@ -0,0 +1,10 @@
<?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="com.tuoheng.miniprogram.dao.UserAuthorizeMapper">




</mapper>

読み込み中…
キャンセル
保存