微信绑定实现

This commit is contained in:
张松 2025-03-27 11:24:34 +08:00
parent 7caaf008e9
commit 49ae38630d
4 changed files with 51 additions and 0 deletions

View File

@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sqx.common.annotation.Debounce;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.dto.BindWxDTO;
import com.sqx.modules.app.dto.LoginDTO;
import com.sqx.modules.app.dto.RegisterDTO;
import com.sqx.modules.app.dto.UserInviteDTO;
@ -168,6 +170,14 @@ public class AppLoginController {
return userService.register(registerDTO);
}
@Login
@PostMapping("/bindWx")
@ResponseBody
@Debounce(interval = 2500, value = "#code")
public Result bindWx(@RequestBody BindWxDTO bindWxDTO, @RequestHeader Long userId) {
return userService.bindWx(bindWxDTO, userId);
}
@PostMapping("/bindWxOpenPhone")
@ApiOperation("微信公众号绑定手机号")
public Result bindWxOpenPhone(Long userId,String phone,String msg){

View File

@ -0,0 +1,14 @@
package com.sqx.modules.app.dto;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
/**
* @author Administrator
*/
@Data
public class BindWxDTO {
@NotEmpty(message = "code不能为空")
private String code;
}

View File

@ -238,4 +238,6 @@ public interface UserService extends IService<UserEntity> {
Result removeUserBlack(Long userId, Integer status);
Map<String, Object> queryPayAndExtractInfo();
Result bindWx(BindWxDTO bindWxDTO, Long userId);
}

View File

@ -80,6 +80,7 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import weixin.popular.api.SnsAPI;
import weixin.popular.bean.sns.SnsToken;
import weixin.popular.util.JsonUtil;
import javax.servlet.http.HttpServletRequest;
@ -1824,4 +1825,28 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements
return result;
}
@Override
public Result bindWx(BindWxDTO bindWxDTO, Long userId) {
//微信appid
CommonInfo one = commonInfoService.findOne(5);
//微信秘钥
CommonInfo two = commonInfoService.findOne(21);
SnsToken snsToken = SnsAPI.oauth2AccessToken(one.getValue(), two.getValue(), bindWxDTO.getCode());
String openid = snsToken.getOpenid();
int count = count(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getWxOpenId, openid));
if (count > 0) {
return Result.error("该微信已被其他用户绑定");
}
UserEntity userEntity = getOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUserId, userId));
if (StringUtils.isNotBlank(userEntity.getWxOpenId())) {
return Result.error("当前用户已绑定微信");
}
userEntity.setWxOpenId(openid);
userEntity.setUpdateTime(DateUtil.now());
update(userEntity, new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUserId, userId));
return Result.success();
}
}