环信接口/邀请注册

This commit is contained in:
kuang.yife 2023-12-16 21:07:08 +08:00
parent 265b64e9eb
commit 170fbc0688
10 changed files with 199 additions and 3 deletions

View File

@ -1,12 +1,15 @@
package com.ruoyi.web.controller.app; package com.ruoyi.web.controller.app;
import com.ruoyi.common.core.domain.Result; import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TbUser; import com.ruoyi.system.domain.TbUser;
import com.ruoyi.system.service.ITbUserService; import com.ruoyi.system.service.ITbUserService;
import com.ruoyi.web.service.HxService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@Api(tags = "App*用户信息") @Api(tags = "App*用户信息")
@ -17,18 +20,47 @@ public class TbUserAppController {
@Autowired @Autowired
private ITbUserService tbUserService; private ITbUserService tbUserService;
@Autowired
private HxService hxService;
@ResponseBody @ResponseBody
@PostMapping("/add") @PostMapping("/add")
@ApiOperation(value = "注册用户", httpMethod = "POST") @ApiOperation(value = "注册用户", httpMethod = "POST")
@Transactional(rollbackFor = Exception.class)
public Result<TbUser> addSave(@RequestBody TbUser tbUser) public Result<TbUser> addSave(@RequestBody TbUser tbUser)
{ {
tbUser.setPassword(DigestUtils.md5Hex(tbUser.getPassword())); tbUser.setPassword(DigestUtils.md5Hex(tbUser.getPassword()));
int effectiveRows = tbUserService.insertTbUser(tbUser); int effectiveRows = tbUserService.insertTbUser(tbUser);
if(effectiveRows > 0){ if(effectiveRows > 0){
// 注册环信
hxService.register(tbUser);
return Result.success(tbUser); return Result.success(tbUser);
}else { }else {
return Result.error("注册用户失败!"); return Result.error("注册用户失败!");
} }
} }
@PostMapping("/register")
@ApiOperation(value = "邀请用户", httpMethod = "POST")
@Transactional(rollbackFor = Exception.class)
public Result<TbUser> 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("邀请用户失败!");
}
}
} }

View File

@ -28,7 +28,7 @@ public class HxController {
@ResponseBody @ResponseBody
@ApiOperation(value = "通过用户id,获取环信账号密码,只需要传id即可") @ApiOperation(value = "通过用户id,获取环信账号密码,只需要传id即可")
@PostMapping("/getByUserId") @PostMapping("/getByUserId")
public Result<HxResp> getByUserId(CommonReq commonReq){ public Result<HxResp> getByUserId(@RequestBody CommonReq commonReq){
TbUser user = tbUserService.getById(commonReq.getId()); TbUser user = tbUserService.getById(commonReq.getId());
if(user == null){ if(user == null){
return Result.error("用户不存在!"); return Result.error("用户不存在!");

View File

@ -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;
}

View File

@ -0,0 +1,15 @@
package com.ruoyi.web.request;
import lombok.Data;
/**
* <p>环信注册对象</p>
* @author clunt
*/
@Data
public class HxRegister {
private String username;
private String password;
}

View File

@ -0,0 +1,18 @@
package com.ruoyi.web.service;
import com.ruoyi.system.domain.TbUser;
/**
* <p>环信接口</p>
*/
public interface HxService {
/**
* <p>注册环信</p>
* @param tbUser 用户信息
*/
void register(TbUser tbUser);
String getToken();
}

View File

@ -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<String, String> headerMap = new HashMap<>();
headerMap.put("Content-Type", "application/json");
headerMap.put("Authorization", "Bearer " + token);
// body
List<HxRegister> 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<String, String> 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;
}
}

View File

@ -8,6 +8,7 @@ import com.ruoyi.system.service.ITbUserService;
import com.ruoyi.system.service.ITbUserSingleService; import com.ruoyi.system.service.ITbUserSingleService;
import com.ruoyi.web.request.LoginReq; import com.ruoyi.web.request.LoginReq;
import com.ruoyi.web.response.LoginResp; import com.ruoyi.web.response.LoginResp;
import com.ruoyi.web.service.HxService;
import com.ruoyi.web.service.LoginService; import com.ruoyi.web.service.LoginService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils; 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.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date; import java.util.Date;
@ -34,7 +36,11 @@ public class LoginServiceImpl implements LoginService {
@Autowired @Autowired
private ITbUserSingleService tbUserSingleService; private ITbUserSingleService tbUserSingleService;
@Autowired
private HxService hxService;
@Override @Override
@Transactional(rollbackFor = Exception.class)
public LoginResp loginByPhone(LoginReq loginReq) throws Exception{ public LoginResp loginByPhone(LoginReq loginReq) throws Exception{
LoginResp resp = new LoginResp(); LoginResp resp = new LoginResp();
String alreadyCode = stringRedisTemplate.opsForValue().get(RedisConstants.SMS_CODE_PREFIX+loginReq.getMobile()); 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.setCreateTime(new Date());
tbUser.setCreateBy(DEFAULT_CREATE_BY); tbUser.setCreateBy(DEFAULT_CREATE_BY);
tbUserService.save(tbUser); int index = tbUserService.insertTbUser(tbUser);
if(index > 0){
// 注册环信
hxService.register(tbUser);
}
BeanUtils.copyProperties(tbUser, resp); BeanUtils.copyProperties(tbUser, resp);
resp.setUserId(tbUser.getId()); resp.setUserId(tbUser.getId());
}else { }else {

View File

@ -175,6 +175,7 @@ youban:
access-key: LTAI5tSVfRSePk9cfLUT5y5f access-key: LTAI5tSVfRSePk9cfLUT5y5f
secret: qlpXG6usf0zPgQQECDAlrfYoMRHxgM secret: qlpXG6usf0zPgQQECDAlrfYoMRHxgM
#阿里云OSS
aliyun: aliyun:
access-key-id: LTAI5tSVfRSePk9cfLUT5y5f access-key-id: LTAI5tSVfRSePk9cfLUT5y5f
access-key-secret: qlpXG6usf0zPgQQECDAlrfYoMRHxgM access-key-secret: qlpXG6usf0zPgQQECDAlrfYoMRHxgM
@ -182,6 +183,7 @@ aliyun:
endpoint: http://oss-cn-shenzhen.aliyuncs.com endpoint: http://oss-cn-shenzhen.aliyuncs.com
url-prefix: http://youban2023.oss-cn-shenzhen.aliyuncs.com/ url-prefix: http://youban2023.oss-cn-shenzhen.aliyuncs.com/
#Adapay第三方支付
adapay: adapay:
app-id: app_77b944f9-99f5-4e62-94c1-ce1cc1ead4bc app-id: app_77b944f9-99f5-4e62-94c1-ce1cc1ead4bc
rsa-private-key: MIICeAIBADANBgkqhkiG9w0BAQEFAASC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mZsJ5icxp4U+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 mock-api-key: api_test_e016292e-2b66-4891-8353-7e640407fe15
notify-url: http://www.quanmingtuodan.xyz:18000 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

View File

@ -38,5 +38,7 @@ public class TbUser extends BaseEntity
@ApiModelProperty(value = "用户密码,前端传明文") @ApiModelProperty(value = "用户密码,前端传明文")
private String password; private String password;
@ApiModelProperty(value = "邀请人id")
private Long registerUserId;
} }

View File

@ -9,17 +9,19 @@
<result property="mobile" column="mobile" /> <result property="mobile" column="mobile" />
<result property="password" column="password" /> <result property="password" column="password" />
<result property="createTime" column="create_time" /> <result property="createTime" column="create_time" />
<result property="registerUserId" column="register_user_id" />
<result property="updateTime" column="update_time" /> <result property="updateTime" column="update_time" />
</resultMap> </resultMap>
<sql id="selectTbUserVo"> <sql id="selectTbUserVo">
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
</sql> </sql>
<select id="selectTbUserList" parameterType="TbUser" resultMap="TbUserResult"> <select id="selectTbUserList" parameterType="TbUser" resultMap="TbUserResult">
<include refid="selectTbUserVo"/> <include refid="selectTbUserVo"/>
<where> <where>
<if test="mobile != null and mobile != ''"> and mobile = #{mobile}</if> <if test="mobile != null and mobile != ''"> and mobile = #{mobile}</if>
<if test="registerUserId != null and registerUserId != ''"> and register_user_id = #{registerUserId}</if>
<if test="password != null and password != ''"> and password = #{password}</if> <if test="password != null and password != ''"> and password = #{password}</if>
</where> </where>
</select> </select>
@ -34,12 +36,14 @@
<trim prefix="(" suffix=")" suffixOverrides=","> <trim prefix="(" suffix=")" suffixOverrides=",">
<if test="mobile != null and mobile != ''">mobile,</if> <if test="mobile != null and mobile != ''">mobile,</if>
<if test="password != null">password,</if> <if test="password != null">password,</if>
<if test="registerUserId != null">register_user_id,</if>
<if test="createTime != null">create_time,</if> <if test="createTime != null">create_time,</if>
<if test="updateTime != null">update_time,</if> <if test="updateTime != null">update_time,</if>
</trim> </trim>
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="mobile != null and mobile != ''">#{mobile},</if> <if test="mobile != null and mobile != ''">#{mobile},</if>
<if test="password != null">#{password},</if> <if test="password != null">#{password},</if>
<if test="registerUserId != null and registerUserId != ''">#{registerUserId},</if>
<if test="createTime != null">#{createTime},</if> <if test="createTime != null">#{createTime},</if>
<if test="updateTime != null">#{updateTime},</if> <if test="updateTime != null">#{updateTime},</if>
</trim> </trim>