package com.everyday.word.controller.client.user; import com.everyday.word.dto.QQUserInfo; import com.everyday.word.entity.config.SystemConfigKeyEnum; import com.everyday.word.entity.user.User; import com.everyday.word.entity.user.UserAuth; import com.everyday.word.factory.user.UserFactory; import com.everyday.word.service.inter.SMSService; import com.everyday.word.service.inter.config.SystemConfigService; import com.everyday.word.service.inter.user.UserService; import com.everyday.word.utils.AliyunOneKeyLoginUtil; import com.everyday.word.vo.AcceptData; import com.everyday.word.vo.user.UserInfoVO; import com.google.gson.Gson; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.yeshi.utils.JsonUtil; import org.yeshi.utils.StringUtil; import org.yeshi.utils.entity.wx.WeiXinUser; import org.yeshi.utils.wx.WXAppLoginUtil; import javax.annotation.Resource; import java.io.IOException; import java.util.List; /** * @author hxh * @title: UserController * @description: * @date 2025/2/7 15:59 */ @Controller @RequestMapping("user") public class UserController { @Resource private SMSService smsService; @Resource private UserService userService; @Resource private SystemConfigService systemConfigService; private final Gson gson = JsonUtil.getSimpleGson(); @ResponseBody @RequestMapping("uploadPortrait") public String uploadPortrait(AcceptData acceptData, MultipartFile portrait) { if (acceptData.getUid() == null) { return JsonUtil.loadFalseResult("用户未登录"); } if (portrait == null) { return JsonUtil.loadFalseResult("上传文件不能为空"); } try { userService.uploadPortrait(portrait, acceptData.getUid()); return JsonUtil.loadTrueResult(""); } catch (IOException e) { return JsonUtil.loadFalseResult("头像上传失败"); } } @ResponseBody @RequestMapping("getUserInfo") public String getUserInfo(AcceptData acceptData) { if (acceptData.getUid() == null) { return JsonUtil.loadFalseResult("用户未登录"); } User user = userService.selectUser(acceptData.getUid()); if (user == null) { return JsonUtil.loadFalseResult("尚未获取到用户信息"); } List authList = userService.listUserAuth(acceptData.getUid()); UserInfoVO vo = UserFactory.createVO(user, authList); return JsonUtil.loadTrueResult(gson.toJson(vo)); } /** * @return java.lang.String * @author hxh * @description 绑定电话 * @date 14:37 2025/2/13 * @param: acceptData * @param: phone * @param: vcode * @param: token **/ @ResponseBody @RequestMapping("bindPhone") public String bindPhone(AcceptData acceptData, String phone, String vcode, String token) { if (acceptData.getUid() == null) { return JsonUtil.loadFalseResult("用户未登录"); } if (!StringUtil.isNullOrEmpty(token)) { if (StringUtil.isNullOrEmpty(phone)) { return JsonUtil.loadFalseResult("电话号码不能为空"); } if (StringUtil.isNullOrEmpty(vcode)) { return JsonUtil.loadFalseResult("验证码不能为空"); } // 验证验证码 String cacheCode = smsService.getVerifyCode(acceptData.getSystem(), phone, 1); if (!vcode.equalsIgnoreCase(cacheCode)) { return JsonUtil.loadFalseResult("验证码错误"); } } else { phone = AliyunOneKeyLoginUtil.getMobile(token, ""); if (StringUtil.isNullOrEmpty(phone)) { return JsonUtil.loadFalseResult("手机号获取失败"); } } userService.bindPhone(acceptData.getUid(), phone); return JsonUtil.loadTrueResult(""); } @ResponseBody @RequestMapping("bindWX") public String bindWX(AcceptData acceptData, String code) { if (acceptData.getUid() == null) { return JsonUtil.loadFalseResult("用户未登录"); } String wxAppId = systemConfigService.getValueCache(SystemConfigKeyEnum.wxAppId, acceptData.getSystem()); String wxAppSecret = systemConfigService.getValueCache(SystemConfigKeyEnum.wxAppSecret, acceptData.getSystem()); if (StringUtil.isNullOrEmpty(wxAppId) || StringUtil.isNullOrEmpty(wxAppSecret)) { return JsonUtil.loadFalseResult("获取配置信息失败"); } WeiXinUser weiXinUser = WXAppLoginUtil.getWeiXinUser(code, wxAppId, wxAppSecret); if (weiXinUser == null) { return JsonUtil.loadFalseResult("获取微信授权信息失败"); } userService.bindWechat(acceptData.getUid(), weiXinUser); return JsonUtil.loadTrueResult(""); } @ResponseBody @RequestMapping("bindQQ") public String bindQQ(AcceptData acceptData, QQUserInfo qqUserInfo) { if (acceptData.getUid() == null) { return JsonUtil.loadFalseResult("用户未登录"); } if (StringUtil.isNullOrEmpty(qqUserInfo.getOpenid())) { return JsonUtil.loadFalseResult("获取QQ授权信息失败"); } userService.bindQQ(acceptData.getUid(), qqUserInfo); return JsonUtil.loadTrueResult(""); } }