邀请功能

master
胡锐 2 months ago
parent 2055860be5
commit 4c15dde34a
  1. 6
      cweb/src/main/java/com/hfkj/controller/ClientController.java
  2. 22
      cweb/src/main/java/com/hfkj/controller/UserController.java
  3. 2
      cweb/src/main/java/com/hfkj/controller/UserInviteCodeController.java
  4. 20
      service/src/main/java/com/hfkj/dao/BsUserInviteCodeMapper.java
  5. 14
      service/src/main/java/com/hfkj/dao/BsUserInviteCodeSqlProvider.java
  6. 16
      service/src/main/java/com/hfkj/entity/BsUserInviteCode.java
  7. 70
      service/src/main/java/com/hfkj/entity/BsUserInviteCodeExample.java
  8. 7
      service/src/main/java/com/hfkj/service/user/BsUserInviteCodeService.java
  9. 13
      service/src/main/java/com/hfkj/service/user/BsUserService.java
  10. 36
      service/src/main/java/com/hfkj/service/user/impl/BsUserInviteCodeServiceImpl.java
  11. 36
      service/src/main/java/com/hfkj/service/user/impl/BsUserServiceImpl.java

@ -53,8 +53,8 @@ public class ClientController {
) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
// 邀请码
String inviteCode = body.getString("inviteCode");
// 邀请码人id
Long inviteUseId = body.getLong("inviteUseId");
// 手机号
String phone = body.getString("phone");
// 校验手机号格式
@ -74,7 +74,7 @@ public class ClientController {
}
redisUtil.del("SMS_LOGIN_CODE:" + phone);
return ResponseMsgUtil.success(userService.login(phone, UserLoginType.SMS, new HashMap<>(), inviteCode));
return ResponseMsgUtil.success(userService.login(phone, UserLoginType.SMS, new HashMap<>(), inviteUseId));
} catch (Exception e) {
return ResponseMsgUtil.exception(e);

@ -123,6 +123,28 @@ public class UserController {
}
}
@RequestMapping(value = "/bindInviteUser", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "绑定邀请人Id")
public ResponseData bindInviteUser(@RequestBody JSONObject body) {
try {
if (body == null || body.getLong("inviteUseId") == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
if (StringUtils.isNotBlank(userSession.getUser().getPhone())) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "手机号已绑定");
}
// 绑定邀请人
userService.bindInviteUser(userSession.getUser().getId(), body.getLong("inviteUseId"));
return ResponseMsgUtil.success(userCenter.getSessionModel(UserSessionObject.class));
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/verifyUpdPhoneSmsCode", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "验证修改手机号验证码")

@ -47,7 +47,7 @@ public class UserInviteCodeController {
}
Map<String,Object> param = new HashMap<>();
// 邀请码
param.put("inviteCode", userInviteCode.getInviteCode());
param.put("inviteUserId", userInviteCode.getUserId());
// 二维码
param.put("qrCode", userInviteCode.getQrCodeImg());
return ResponseMsgUtil.success(param);

@ -39,14 +39,12 @@ public interface BsUserInviteCodeMapper extends BsUserInviteCodeMapperExt {
int deleteByPrimaryKey(Long id);
@Insert({
"insert into bs_user_invite_code (user_id, invite_code, ",
"qr_code_img, `status`, ",
"create_time, ext_1, ",
"ext_2, ext_3)",
"values (#{userId,jdbcType=BIGINT}, #{inviteCode,jdbcType=VARCHAR}, ",
"#{qrCodeImg,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, ",
"#{createTime,jdbcType=TIMESTAMP}, #{ext1,jdbcType=VARCHAR}, ",
"#{ext2,jdbcType=VARCHAR}, #{ext3,jdbcType=VARCHAR})"
"insert into bs_user_invite_code (user_id, qr_code_img, ",
"`status`, create_time, ",
"ext_1, ext_2, ext_3)",
"values (#{userId,jdbcType=BIGINT}, #{qrCodeImg,jdbcType=VARCHAR}, ",
"#{status,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, ",
"#{ext1,jdbcType=VARCHAR}, #{ext2,jdbcType=VARCHAR}, #{ext3,jdbcType=VARCHAR})"
})
@Options(useGeneratedKeys=true,keyProperty="id")
int insert(BsUserInviteCode record);
@ -59,7 +57,6 @@ public interface BsUserInviteCodeMapper extends BsUserInviteCodeMapperExt {
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
@Result(column="user_id", property="userId", jdbcType=JdbcType.BIGINT),
@Result(column="invite_code", property="inviteCode", jdbcType=JdbcType.VARCHAR),
@Result(column="qr_code_img", property="qrCodeImg", jdbcType=JdbcType.VARCHAR),
@Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
@Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
@ -71,15 +68,13 @@ public interface BsUserInviteCodeMapper extends BsUserInviteCodeMapperExt {
@Select({
"select",
"id, user_id, invite_code, qr_code_img, `status`, create_time, ext_1, ext_2, ",
"ext_3",
"id, user_id, qr_code_img, `status`, create_time, ext_1, ext_2, ext_3",
"from bs_user_invite_code",
"where id = #{id,jdbcType=BIGINT}"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
@Result(column="user_id", property="userId", jdbcType=JdbcType.BIGINT),
@Result(column="invite_code", property="inviteCode", jdbcType=JdbcType.VARCHAR),
@Result(column="qr_code_img", property="qrCodeImg", jdbcType=JdbcType.VARCHAR),
@Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
@Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
@ -101,7 +96,6 @@ public interface BsUserInviteCodeMapper extends BsUserInviteCodeMapperExt {
@Update({
"update bs_user_invite_code",
"set user_id = #{userId,jdbcType=BIGINT},",
"invite_code = #{inviteCode,jdbcType=VARCHAR},",
"qr_code_img = #{qrCodeImg,jdbcType=VARCHAR},",
"`status` = #{status,jdbcType=INTEGER},",
"create_time = #{createTime,jdbcType=TIMESTAMP},",

@ -32,10 +32,6 @@ public class BsUserInviteCodeSqlProvider {
sql.VALUES("user_id", "#{userId,jdbcType=BIGINT}");
}
if (record.getInviteCode() != null) {
sql.VALUES("invite_code", "#{inviteCode,jdbcType=VARCHAR}");
}
if (record.getQrCodeImg() != null) {
sql.VALUES("qr_code_img", "#{qrCodeImg,jdbcType=VARCHAR}");
}
@ -71,7 +67,6 @@ public class BsUserInviteCodeSqlProvider {
sql.SELECT("id");
}
sql.SELECT("user_id");
sql.SELECT("invite_code");
sql.SELECT("qr_code_img");
sql.SELECT("`status`");
sql.SELECT("create_time");
@ -103,10 +98,6 @@ public class BsUserInviteCodeSqlProvider {
sql.SET("user_id = #{record.userId,jdbcType=BIGINT}");
}
if (record.getInviteCode() != null) {
sql.SET("invite_code = #{record.inviteCode,jdbcType=VARCHAR}");
}
if (record.getQrCodeImg() != null) {
sql.SET("qr_code_img = #{record.qrCodeImg,jdbcType=VARCHAR}");
}
@ -141,7 +132,6 @@ public class BsUserInviteCodeSqlProvider {
sql.SET("id = #{record.id,jdbcType=BIGINT}");
sql.SET("user_id = #{record.userId,jdbcType=BIGINT}");
sql.SET("invite_code = #{record.inviteCode,jdbcType=VARCHAR}");
sql.SET("qr_code_img = #{record.qrCodeImg,jdbcType=VARCHAR}");
sql.SET("`status` = #{record.status,jdbcType=INTEGER}");
sql.SET("create_time = #{record.createTime,jdbcType=TIMESTAMP}");
@ -162,10 +152,6 @@ public class BsUserInviteCodeSqlProvider {
sql.SET("user_id = #{userId,jdbcType=BIGINT}");
}
if (record.getInviteCode() != null) {
sql.SET("invite_code = #{inviteCode,jdbcType=VARCHAR}");
}
if (record.getQrCodeImg() != null) {
sql.SET("qr_code_img = #{qrCodeImg,jdbcType=VARCHAR}");
}

@ -23,11 +23,6 @@ public class BsUserInviteCode implements Serializable {
*/
private Long userId;
/**
* 邀请码
*/
private String inviteCode;
/**
* 二维码地址
*/
@ -67,14 +62,6 @@ public class BsUserInviteCode implements Serializable {
this.userId = userId;
}
public String getInviteCode() {
return inviteCode;
}
public void setInviteCode(String inviteCode) {
this.inviteCode = inviteCode;
}
public String getQrCodeImg() {
return qrCodeImg;
}
@ -137,7 +124,6 @@ public class BsUserInviteCode implements Serializable {
BsUserInviteCode other = (BsUserInviteCode) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
&& (this.getInviteCode() == null ? other.getInviteCode() == null : this.getInviteCode().equals(other.getInviteCode()))
&& (this.getQrCodeImg() == null ? other.getQrCodeImg() == null : this.getQrCodeImg().equals(other.getQrCodeImg()))
&& (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus()))
&& (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
@ -152,7 +138,6 @@ public class BsUserInviteCode implements Serializable {
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
result = prime * result + ((getInviteCode() == null) ? 0 : getInviteCode().hashCode());
result = prime * result + ((getQrCodeImg() == null) ? 0 : getQrCodeImg().hashCode());
result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode());
result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
@ -170,7 +155,6 @@ public class BsUserInviteCode implements Serializable {
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", userId=").append(userId);
sb.append(", inviteCode=").append(inviteCode);
sb.append(", qrCodeImg=").append(qrCodeImg);
sb.append(", status=").append(status);
sb.append(", createTime=").append(createTime);

@ -245,76 +245,6 @@ public class BsUserInviteCodeExample {
return (Criteria) this;
}
public Criteria andInviteCodeIsNull() {
addCriterion("invite_code is null");
return (Criteria) this;
}
public Criteria andInviteCodeIsNotNull() {
addCriterion("invite_code is not null");
return (Criteria) this;
}
public Criteria andInviteCodeEqualTo(String value) {
addCriterion("invite_code =", value, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeNotEqualTo(String value) {
addCriterion("invite_code <>", value, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeGreaterThan(String value) {
addCriterion("invite_code >", value, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeGreaterThanOrEqualTo(String value) {
addCriterion("invite_code >=", value, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeLessThan(String value) {
addCriterion("invite_code <", value, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeLessThanOrEqualTo(String value) {
addCriterion("invite_code <=", value, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeLike(String value) {
addCriterion("invite_code like", value, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeNotLike(String value) {
addCriterion("invite_code not like", value, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeIn(List<String> values) {
addCriterion("invite_code in", values, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeNotIn(List<String> values) {
addCriterion("invite_code not in", values, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeBetween(String value1, String value2) {
addCriterion("invite_code between", value1, value2, "inviteCode");
return (Criteria) this;
}
public Criteria andInviteCodeNotBetween(String value1, String value2) {
addCriterion("invite_code not between", value1, value2, "inviteCode");
return (Criteria) this;
}
public Criteria andQrCodeImgIsNull() {
addCriterion("qr_code_img is null");
return (Criteria) this;

@ -26,11 +26,4 @@ public interface BsUserInviteCodeService {
*/
BsUserInviteCode getDetail(Long userId);
/**
* 查询邀请码
* @param inviteCode
* @return
*/
BsUserInviteCode getDetail(String inviteCode);
}

@ -33,6 +33,13 @@ public interface BsUserService {
*/
void updateWechatNum(Long userId, String wechatNum) throws Exception;
/**
* 绑定邀请人
* @param userId
* @param inviteUseId
*/
void bindInviteUser(Long userId, Long inviteUseId) throws Exception;
/**
* 查询详情
* @param userId
@ -51,18 +58,20 @@ public interface BsUserService {
* 用户注册
* @param phone 手机号
* @param other 其他参数
* @param inviteUseId 邀请人id
* @return
* @throws Exception
*/
BsUser register(String phone, Map<String, Object> other, String inviteCode);
BsUser register(String phone, Map<String, Object> other, Long inviteUseId);
/**
* 用户登录
* @param phone 用户手机号
* @param other 其他参数
* @param inviteUseId 邀请人id
* @return
* @throws Exception
*/
SessionObject login(String phone, UserLoginType loginType, Map<String, Object> other,String inviteCode) throws Exception;
SessionObject login(String phone, UserLoginType loginType, Map<String, Object> other,Long inviteUseId) throws Exception;
}

@ -31,29 +31,17 @@ public class BsUserInviteCodeServiceImpl implements BsUserInviteCodeService {
private CommonSysConfig commonSysConfig;
private final static String CACHE_KEY = "USER_INVITE_CODE";
private String generateInviteCode() {
// 生成8位随机数
String inviteCode = RandomUtils.number(8, true);
// 查询是否存在,存在就重新生成
if (getDetail(inviteCode) != null) {
generateInviteCode();
}
return inviteCode;
}
@Override
public BsUserInviteCode create(Long userId) throws Exception {
BsUserInviteCode data = new BsUserInviteCode();
data.setUserId(userId);
data.setInviteCode(generateInviteCode());
data.setCreateTime(new Date());
data.setStatus(1);
System.out.println(commonSysConfig.getDomain());
// 生成二维码
String fileUrl = "/userInviteCode/"+data.getInviteCode()+".png";
String fileUrl = "/userInviteCode/"+data.getUserId()+".png";
QRCodeGenerator.generateQRCodeImage(
data.getInviteCode(),
""+data.getUserId(),
260,
260,
commonSysConfig.getFilesystem() + fileUrl
@ -63,38 +51,28 @@ public class BsUserInviteCodeServiceImpl implements BsUserInviteCodeService {
data.setQrCodeImg(fileUrl);
userInviteCodeMapper.insert(data);
// 缓存
redisUtil.hset(CACHE_KEY, data.getInviteCode(), data);
redisUtil.hset(CACHE_KEY, data.getUserId()+"", data);
return data;
}
@Override
public BsUserInviteCode getDetail(Long userId) {
BsUserInviteCodeExample example = new BsUserInviteCodeExample();
example.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(0);
List<BsUserInviteCode> list = userInviteCodeMapper.selectByExample(example);
if (!list.isEmpty()) {
return list.get(0);
}
return null;
}
@Override
public BsUserInviteCode getDetail(String inviteCode) {
// 缓存
Object cache = redisUtil.hget(CACHE_KEY, inviteCode);
Object cache = redisUtil.hget(CACHE_KEY, ""+userId);
if (cache != null) {
return (BsUserInviteCode) cache;
}
BsUserInviteCodeExample example = new BsUserInviteCodeExample();
example.createCriteria().andInviteCodeEqualTo(inviteCode).andStatusNotEqualTo(0);
example.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(0);
List<BsUserInviteCode> list = userInviteCodeMapper.selectByExample(example);
if (!list.isEmpty()) {
BsUserInviteCode data = list.get(0);
// 缓存
redisUtil.hset(CACHE_KEY, data.getInviteCode(), data);
redisUtil.hset(CACHE_KEY, ""+data.getUserId(), data);
return data;
}
return null;
}
}

@ -116,6 +116,29 @@ public class BsUserServiceImpl implements BsUserService {
userCenter.save(sessionObject);
}
@Override
public void bindInviteUser(Long userId, Long inviteUseId) throws Exception {
// 查询用户
BsUser user = getUser(userId);
if (user == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到用户");
}
if (user.getInviteUserId() != null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "已绑定过邀请人");
}
if (getUser(inviteUseId) == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的邀请人Id");
}
user.setInviteUserId(inviteUseId);
editData(user);
// 更新session
UserSessionObject session = new UserSessionObject();
session.setUser(user);
SessionObject sessionObject = new SessionObject(userToken(user.getId()), session);
userCenter.save(sessionObject);
}
@Override
public BsUser getUser(Long userId) {
return userMapper.selectByPrimaryKey(userId);
@ -142,20 +165,21 @@ public class BsUserServiceImpl implements BsUserService {
@Override
@Transactional(propagation= Propagation.REQUIRED,rollbackFor= {RuntimeException.class})
public BsUser register(String phone, Map<String, Object> other, String inviteCode) {
public BsUser register(String phone, Map<String, Object> other, Long inviteUseId) {
// 查询手机号
if (getUser(phone) != null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "用户手机号已存在,请更换");
}
if (StringUtils.isNotBlank(inviteCode)) {
BsUser user = new BsUser();
if (inviteUseId != null) {
// 校验邀请码
BsUserInviteCode userInviteCode = userInviteCodeService.getDetail(inviteCode);
BsUserInviteCode userInviteCode = userInviteCodeService.getDetail(inviteUseId);
if (userInviteCode == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无效的邀请码");
}
user.setInviteUserId(userInviteCode.getUserId());
}
BsUser user = new BsUser();
user.setHeadImg(MapUtils.getString(other,"headImg"));
user.setName(
StringUtils.isNotBlank(MapUtils.getString(other,"name"))
@ -175,11 +199,11 @@ public class BsUserServiceImpl implements BsUserService {
@Override
@Transactional(propagation= Propagation.REQUIRED,rollbackFor= {RuntimeException.class})
public SessionObject login(String phone, UserLoginType loginType, Map<String, Object> other, String inviteCode) throws Exception {
public SessionObject login(String phone, UserLoginType loginType, Map<String, Object> other, Long inviteUseId) throws Exception {
// 查询用户
BsUser user = getUser(phone);
if (user == null) {
user = register(phone, other, inviteCode);
user = register(phone, other, inviteUseId);
}
// 缓存
UserSessionObject session = new UserSessionObject();

Loading…
Cancel
Save