From 170fbc0688580a55f0f93b8a0dd5b9789851297c Mon Sep 17 00:00:00 2001 From: "kuang.yife" Date: Sat, 16 Dec 2023 21:07:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=AF=E4=BF=A1=E6=8E=A5=E5=8F=A3/=E9=82=80?= =?UTF-8?q?=E8=AF=B7=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/app/TbUserAppController.java | 32 +++++++ .../web/controller/tool/HxController.java | 2 +- .../com/ruoyi/web/core/config/HxConfig.java | 18 ++++ .../com/ruoyi/web/request/HxRegister.java | 15 ++++ .../java/com/ruoyi/web/service/HxService.java | 18 ++++ .../ruoyi/web/service/impl/HxServiceImpl.java | 88 +++++++++++++++++++ .../web/service/impl/LoginServiceImpl.java | 12 ++- .../src/main/resources/application.yml | 9 ++ .../java/com/ruoyi/system/domain/TbUser.java | 2 + .../resources/mapper/system/TbUserMapper.xml | 6 +- 10 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/core/config/HxConfig.java create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/request/HxRegister.java create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/service/HxService.java create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/service/impl/HxServiceImpl.java diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/TbUserAppController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/TbUserAppController.java index 6c5a5260..01a115fd 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/TbUserAppController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/TbUserAppController.java @@ -1,12 +1,15 @@ package com.ruoyi.web.controller.app; import com.ruoyi.common.core.domain.Result; +import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.TbUser; import com.ruoyi.system.service.ITbUserService; +import com.ruoyi.web.service.HxService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; @Api(tags = "App*用户信息") @@ -17,18 +20,47 @@ public class TbUserAppController { @Autowired private ITbUserService tbUserService; + @Autowired + private HxService hxService; + @ResponseBody @PostMapping("/add") @ApiOperation(value = "注册用户", httpMethod = "POST") + @Transactional(rollbackFor = Exception.class) public Result addSave(@RequestBody TbUser tbUser) { tbUser.setPassword(DigestUtils.md5Hex(tbUser.getPassword())); int effectiveRows = tbUserService.insertTbUser(tbUser); if(effectiveRows > 0){ + // 注册环信 + hxService.register(tbUser); return Result.success(tbUser); }else { return Result.error("注册用户失败!"); } } + @PostMapping("/register") + @ApiOperation(value = "邀请用户", httpMethod = "POST") + @Transactional(rollbackFor = Exception.class) + public Result register(@RequestBody TbUser tbUser) + { + TbUser registerUser = tbUserService.lambdaQuery().eq(TbUser::getMobile, tbUser.getMobile()).one(); + if(registerUser != null ){ + return Result.error("用户已注册,请直接登陆!"); + } + if(StringUtils.isEmpty(tbUser.getPassword())){ + tbUser.setPassword("123456"); + } + tbUser.setPassword(DigestUtils.md5Hex(tbUser.getPassword())); + int effectiveRows = tbUserService.insertTbUser(tbUser); + if(effectiveRows > 0){ + // 注册环信 + hxService.register(tbUser); + return Result.success(tbUser); + }else { + return Result.error("邀请用户失败!"); + } + } + } diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/HxController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/HxController.java index 5a9eb9d8..cd801796 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/HxController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/HxController.java @@ -28,7 +28,7 @@ public class HxController { @ResponseBody @ApiOperation(value = "通过用户id,获取环信账号密码,只需要传id即可") @PostMapping("/getByUserId") - public Result getByUserId(CommonReq commonReq){ + public Result getByUserId(@RequestBody CommonReq commonReq){ TbUser user = tbUserService.getById(commonReq.getId()); if(user == null){ return Result.error("用户不存在!"); diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/HxConfig.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/HxConfig.java new file mode 100644 index 00000000..04b1fcb9 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/HxConfig.java @@ -0,0 +1,18 @@ +package com.ruoyi.web.core.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Data +@Configuration +@ConfigurationProperties(prefix = "hx") +public class HxConfig { + + private String url; + private String orgName; + private String appName; + private String clientId; + private String clientSecret; + +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/request/HxRegister.java b/ruoyi-admin/src/main/java/com/ruoyi/web/request/HxRegister.java new file mode 100644 index 00000000..5c072a9a --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/request/HxRegister.java @@ -0,0 +1,15 @@ +package com.ruoyi.web.request; + +import lombok.Data; + +/** + *

环信注册对象

+ * @author clunt + */ +@Data +public class HxRegister { + + private String username; + private String password; + +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/service/HxService.java b/ruoyi-admin/src/main/java/com/ruoyi/web/service/HxService.java new file mode 100644 index 00000000..c820f3fa --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/service/HxService.java @@ -0,0 +1,18 @@ +package com.ruoyi.web.service; + +import com.ruoyi.system.domain.TbUser; + +/** + *

环信接口

+ */ +public interface HxService { + + /** + *

注册环信

+ * @param tbUser 用户信息 + */ + void register(TbUser tbUser); + + String getToken(); + +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/service/impl/HxServiceImpl.java b/ruoyi-admin/src/main/java/com/ruoyi/web/service/impl/HxServiceImpl.java new file mode 100644 index 00000000..522a342f --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/service/impl/HxServiceImpl.java @@ -0,0 +1,88 @@ +package com.ruoyi.web.service.impl; + +import cn.hutool.http.HttpUtil; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.system.domain.TbUser; +import com.ruoyi.web.core.config.HxConfig; +import com.ruoyi.web.request.HxRegister; +import com.ruoyi.web.service.HxService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.codec.digest.DigestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Service +public class HxServiceImpl implements HxService { + + private final static String HX_TOKEN_KEY = "hx_token"; + + private static final String HX_END_STRING = "_pingban_youban"; + + @Autowired + private HxConfig hxConfig; + + @Autowired + private StringRedisTemplate redisTemplate; + + @Override + public void register(TbUser tbUser) { + String token = getToken(); + String registerUrl = hxConfig.getUrl() + hxConfig.getOrgName() + "/" + hxConfig.getAppName() + "/users"; + // header + Map headerMap = new HashMap<>(); + headerMap.put("Content-Type", "application/json"); + headerMap.put("Authorization", "Bearer " + token); + // body + List registers = new ArrayList<>(); + HxRegister register = new HxRegister(); + register.setPassword(DigestUtils.md5Hex(tbUser.getId() + HX_END_STRING)); + register.setUsername(String.valueOf(tbUser.getId())); + registers.add(register); + + log.info("注册环信入参:{}", JSONArray.toJSON(registers)); + String result = HttpUtil.createPost(registerUrl).addHeaders(headerMap).body(JSONArray.toJSONString(registers)).execute().body(); + log.info("注册环信返回结果:{}", result); + } + + @Override + public String getToken() { + // 未失效 + String token = redisTemplate.opsForValue().get(HX_TOKEN_KEY); + if(StringUtils.isNotEmpty(token)){ + return token; + } + // 失效重新获取 + String tokenUrl = hxConfig.getUrl() + hxConfig.getOrgName() + "/" + hxConfig.getAppName() + "/token"; + JSONObject tokenJson = new JSONObject(); + tokenJson.put("grant_type", "client_credentials"); + tokenJson.put("client_id", hxConfig.getClientId()); + tokenJson.put("client_secret", hxConfig.getClientSecret()); + log.info("请求地址:{}, 请求内容:{}", tokenUrl, tokenJson); + // headerMap + Map headerMap = new HashMap<>(); + headerMap.put("Content-Type", "application/json"); + String result = HttpUtil.createPost(tokenUrl).addHeaders(headerMap).body(JSONObject.toJSONString(tokenJson)).execute().body(); + log.info("返回内容:{}", result); + if(StringUtils.isNotEmpty(result)){ + JSONObject resultJson = JSONObject.parseObject(result); + String newToken = resultJson.getString("access_token"); + Long expireTime = resultJson.getLong("expires_in") - 3600L; + // 获取到token + if(StringUtils.isNotEmpty(newToken)){ + redisTemplate.opsForValue().set(HX_TOKEN_KEY, newToken, expireTime, TimeUnit.SECONDS); + return newToken; + } + } + return null; + } +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/service/impl/LoginServiceImpl.java b/ruoyi-admin/src/main/java/com/ruoyi/web/service/impl/LoginServiceImpl.java index fdc48ac0..60db3333 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/service/impl/LoginServiceImpl.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/service/impl/LoginServiceImpl.java @@ -8,6 +8,7 @@ import com.ruoyi.system.service.ITbUserService; import com.ruoyi.system.service.ITbUserSingleService; import com.ruoyi.web.request.LoginReq; import com.ruoyi.web.response.LoginResp; +import com.ruoyi.web.service.HxService; import com.ruoyi.web.service.LoginService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; @@ -15,6 +16,7 @@ import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.Date; @@ -34,7 +36,11 @@ public class LoginServiceImpl implements LoginService { @Autowired private ITbUserSingleService tbUserSingleService; + @Autowired + private HxService hxService; + @Override + @Transactional(rollbackFor = Exception.class) public LoginResp loginByPhone(LoginReq loginReq) throws Exception{ LoginResp resp = new LoginResp(); String alreadyCode = stringRedisTemplate.opsForValue().get(RedisConstants.SMS_CODE_PREFIX+loginReq.getMobile()); @@ -57,7 +63,11 @@ public class LoginServiceImpl implements LoginService { } tbUser.setCreateTime(new Date()); tbUser.setCreateBy(DEFAULT_CREATE_BY); - tbUserService.save(tbUser); + int index = tbUserService.insertTbUser(tbUser); + if(index > 0){ + // 注册环信 + hxService.register(tbUser); + } BeanUtils.copyProperties(tbUser, resp); resp.setUserId(tbUser.getId()); }else { diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 42dbba68..ae21cda0 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -175,6 +175,7 @@ youban: access-key: LTAI5tSVfRSePk9cfLUT5y5f secret: qlpXG6usf0zPgQQECDAlrfYoMRHxgM +#阿里云OSS aliyun: access-key-id: LTAI5tSVfRSePk9cfLUT5y5f access-key-secret: qlpXG6usf0zPgQQECDAlrfYoMRHxgM @@ -182,6 +183,7 @@ aliyun: endpoint: http://oss-cn-shenzhen.aliyuncs.com url-prefix: http://youban2023.oss-cn-shenzhen.aliyuncs.com/ +#Adapay第三方支付 adapay: app-id: app_77b944f9-99f5-4e62-94c1-ce1cc1ead4bc rsa-private-key: MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALGI62YjX8UbBiupWPLYeck9TMNZVSLE09M8ZoSfe7yC3ZHO0S3Vq+q9aRsk9hOiOxiTA1tMtp5+5MvZyVK7mIC8FkTbSk3r4sJ+WtPkogOgFJ2xSljS4fu2wIVPYeGyOuWbB6npzJ121vFE91uhNIbK4K2QfJUqMYnWwYCCqyQHAgMBAAECgYBRUmqhyqpf21UkQtpfwxFmQRIcmZsJ5icxp4U+Ut+XJkrgM2BWIn4xdLnkmTWvIKz5QL5U3/r29yFOz2AM6amc6nHUuf9ArFqMFZ/2FgXu1y7UGU2cKq+ZZ2afyuG3EuxTycxxXEw/pekO94mSvjuGLxX/XzS+zb4Uzb9/J3xKwQJBAOk+pzsfdUR9dK1hrDZkgVmvq9tEi9yn0rteT5UW66lVOcypa3k9eZ6cTCv4CGJpl1wktr0l6pLi3JU27VNfZ28CQQDC2uW8pl9B9RxJR+s4QGaiySI0J0AmmRbPPol9kCegSPpg+Tcq9bYGNEI9dPy+EvkqejZLixPEU2V9IUSRigDpAkEAppls46sNnPUrUOhyFIvnZIM48q5cZCivOcwcdfZgL5xDY68jp/7EDwm+0q0geALJ7TQAHsylZ3OJcT9BdwqvGwJBAK+GuETEKjMkNaLdoko92TbysFkCsosShLWTxA7T+J4unz0Twlp0lM/p63GpHLOsK7/T720Fj3zfEyExAq+H/WECQQDc9MLEjO3CuEZ8PBPtC3krKglBK3XzXjqelb95bI+6YrI7I65V5sPYWYJBiOONczuWaD+SzEWMq2pIIustO2zW @@ -189,3 +191,10 @@ adapay: mock-api-key: api_test_e016292e-2b66-4891-8353-7e640407fe15 notify-url: http://www.quanmingtuodan.xyz:18000 +#环信聊天 +hx: + url: http://a1.easemob.com/ + app-name: youban + org-name: 1198231130162318 + client-id: YXA6vEylcIavRGCeXZVRkgq3eA + client-secret: YXA6i-kpSfafuseuVLMDdLLtOSgT8eQ diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/TbUser.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/TbUser.java index f85e74a5..16791c49 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/TbUser.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/TbUser.java @@ -38,5 +38,7 @@ public class TbUser extends BaseEntity @ApiModelProperty(value = "用户密码,前端传明文") private String password; + @ApiModelProperty(value = "邀请人id") + private Long registerUserId; } diff --git a/ruoyi-system/src/main/resources/mapper/system/TbUserMapper.xml b/ruoyi-system/src/main/resources/mapper/system/TbUserMapper.xml index 25799c19..f47f6d4b 100644 --- a/ruoyi-system/src/main/resources/mapper/system/TbUserMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/TbUserMapper.xml @@ -9,17 +9,19 @@ + - select id, mobile, password, create_time, update_time from tb_user + select id, mobile, password, create_time, update_time,register_user_id from tb_user @@ -34,12 +36,14 @@ mobile, password, + register_user_id, create_time, update_time, #{mobile}, #{password}, + #{registerUserId}, #{createTime}, #{updateTime},