添加微信相关代码
parent
818cf08000
commit
58a79d1f2e
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.baiye.constant;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2019/2/1
|
||||
*/
|
||||
public interface WeChatRequestConstants {
|
||||
|
||||
/**
|
||||
* 获取二维码
|
||||
*/
|
||||
String GET_QR_CODE = "/wechat/v1/ios";
|
||||
|
||||
/**
|
||||
* 退出微信
|
||||
*/
|
||||
String WE_CHAT_LOGOUT = "/wechat/v1/batchLogout";
|
||||
|
||||
/**
|
||||
* 发送文本消息
|
||||
*/
|
||||
String WE_CHAT_SEND_TXT = "/wechat/v1/sendtext";
|
||||
|
||||
/**
|
||||
* 发送图片
|
||||
*/
|
||||
String WE_CHAT_SEND_PICTURE = "/wechat/v1/sendurlpic";
|
||||
/**
|
||||
* 发送gif
|
||||
*/
|
||||
String WE_CHAT_SEND_GIF = "/wechat/v1/sendGif";
|
||||
|
||||
/**
|
||||
* 发送视频
|
||||
*/
|
||||
String WE_CHAT_SEND_VIDEO = "/wechat/v1/sendCdnVideoMsg";
|
||||
|
||||
/**
|
||||
* 获取好友信息
|
||||
*/
|
||||
String WE_CHAT_FRIEND_INFORMATION = "/wechat/v1/friendinfo";
|
||||
|
||||
|
||||
/**
|
||||
* 添加好友
|
||||
*/
|
||||
String WE_CHAT_ADD_FRIEND = "/wechat/v1/addfriends";
|
||||
|
||||
|
||||
/**
|
||||
* 在线状态检测
|
||||
*/
|
||||
String WE_CHAT_STATUS = " /wechat/v1/wecatstatus";
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baiye.http;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/6/23
|
||||
*/
|
||||
@Data
|
||||
public class WeChatResponse<T extends Serializable> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 6763956796736587193L;
|
||||
|
||||
private T data;
|
||||
|
||||
private String msg;
|
||||
|
||||
private Integer status;
|
||||
|
||||
private Boolean success;
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baiye.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/6/24
|
||||
*/
|
||||
@Data
|
||||
public class WeChatQrCodeDTO implements Serializable {
|
||||
private static final long serialVersionUID = 5388401222056549488L;
|
||||
|
||||
private String wechat;
|
||||
|
||||
private String uuid;
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.baiye.config.properties;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/6/24
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "wechat")
|
||||
public class WeChatProperties {
|
||||
|
||||
@ApiModelProperty("请求地址")
|
||||
private String gatewayHost;
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.baiye.modules.system.domain;
|
||||
|
||||
import com.baiye.model.base.BaseEntity;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/6/23
|
||||
*/
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Table(name="tb_wechat_user")
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
public class WeChatUser extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = -4755997517585953316L;
|
||||
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
@ApiModelProperty(value = "id")
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.baiye.modules.system.repository;
|
||||
|
||||
import com.baiye.modules.system.domain.WeChatUser;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-22
|
||||
*/
|
||||
public interface WeChatRepository extends JpaRepository<WeChatUser, Long>, JpaSpecificationExecutor<WeChatUser> {
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baiye.modules.system.rest;
|
||||
|
||||
import com.baiye.http.WeChatResponse;
|
||||
import com.baiye.modules.system.service.WeChatUserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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 Enzo
|
||||
* @date : 2022/6/24
|
||||
*/
|
||||
@RestController
|
||||
@Api(tags = "微信回调")
|
||||
@RequestMapping("/wechat")
|
||||
@RequiredArgsConstructor
|
||||
public class WeChatCallbackController {
|
||||
|
||||
private final WeChatUserService weChatUserService;
|
||||
|
||||
/**
|
||||
* 请求三方微信操作回调
|
||||
* @param weChatResponse
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/callback")
|
||||
public ResponseEntity<String> frontRcvResponse(@RequestBody WeChatResponse weChatResponse) {
|
||||
weChatUserService.analyticalData(weChatResponse);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baiye.modules.system.rest;
|
||||
|
||||
import com.baiye.http.CommonResponse;
|
||||
import com.baiye.modules.system.service.WeChatUserService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/6/23
|
||||
*/
|
||||
|
||||
@RestController
|
||||
@Api(tags = "微信相关接口")
|
||||
@RequestMapping("/api/wechat")
|
||||
@RequiredArgsConstructor
|
||||
public class WechatController {
|
||||
|
||||
private final WeChatUserService weChatUserService;
|
||||
|
||||
@ApiOperation("获取二维码")
|
||||
@PostMapping("/getQrCode")
|
||||
public CommonResponse<String> read(String wechatId) {
|
||||
return CommonResponse.createBySuccess
|
||||
(weChatUserService.getQrCode(wechatId));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baiye.modules.system.service;
|
||||
|
||||
import com.baiye.http.WeChatResponse;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/6/23
|
||||
*/
|
||||
public interface WeChatUserService {
|
||||
|
||||
/**
|
||||
* 获取二维码地址
|
||||
* @param wechatId
|
||||
* @return
|
||||
*/
|
||||
String getQrCode(String wechatId);
|
||||
|
||||
/**
|
||||
* 处理微信消息请求
|
||||
* @param weChatResponse
|
||||
*/
|
||||
|
||||
void analyticalData(WeChatResponse weChatResponse);
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.baiye.modules.system.service.impl;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baiye.config.properties.WeChatProperties;
|
||||
import com.baiye.constant.WeChatRequestConstants;
|
||||
import com.baiye.exception.BadRequestException;
|
||||
import com.baiye.http.WeChatResponse;
|
||||
import com.baiye.model.enums.ResponseCode;
|
||||
import com.baiye.modules.system.repository.WeChatRepository;
|
||||
import com.baiye.modules.system.service.WeChatUserService;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/6/23
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class WeChatUserServiceImpl implements WeChatUserService {
|
||||
|
||||
private final WeChatRepository weChatRepository;
|
||||
|
||||
|
||||
private final WeChatProperties weChatProperties;
|
||||
|
||||
@Override
|
||||
public String getQrCode(String wechatId) {
|
||||
Map<String, Object> getQrCodeMap =
|
||||
ImmutableMap.of("uuid", IdUtil.randomUUID());
|
||||
if (StringUtils.isNotBlank(wechatId)){
|
||||
getQrCodeMap.put("wechat", wechatId);
|
||||
}
|
||||
String getQrCodeResult = HttpUtil.get
|
||||
(weChatProperties.getGatewayHost().
|
||||
concat(WeChatRequestConstants.GET_QR_CODE), getQrCodeMap);
|
||||
if (getQrCodeResult.contains(ResponseCode.SUCCESS.getDesc())) {
|
||||
WeChatResponse weChatResponse =
|
||||
JSONUtil.toBean(getQrCodeResult, WeChatResponse.class);
|
||||
return weChatResponse.getMsg();
|
||||
}
|
||||
throw new BadRequestException(ResponseCode.FAILED_GET_QR_CODE.getDesc());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void analyticalData(WeChatResponse weChatResponse) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue