admin
2022-05-12 fa705507ba574c857b1667553737d23b1b7ff495
src/main/resources/code/service/app/src/main/java/com/ks/app/controller/client/api/UserController.java
@@ -1,15 +1,20 @@
package com.ks.app.controller.client.api;
import com.ks.app.dto.user.LoginInfoDTO;
import com.ks.app.entity.APPPlatform;
import com.ks.app.entity.user.UserInfo;
import com.ks.app.entity.user.UserLoginRecord;
import com.ks.app.entity.vip.UserVIPInfo;
import com.ks.app.exception.user.LoginException;
import com.ks.app.exception.user.UserAccountException;
import com.ks.app.service.inter.user.UserAccountService;
import com.ks.app.service.inter.user.UserExtraInfoService;
import com.ks.app.service.inter.user.UserInfoService;
import com.ks.app.service.inter.vip.VIPService;
import com.ks.app.service.manager.PushManager;
import com.ks.app.service.manager.VerifyCodeManager;
import com.ks.app.utils.ApiCodeConstant;
import com.ks.app.utils.ImageUtil;
import com.ks.app.utils.annotation.UserLogin;
import com.ks.app.vo.AcceptData;
import com.ks.app.vo.user.UserInfoVO;
@@ -18,15 +23,24 @@
import com.ks.push.pojo.DO.BPushDeviceToken;
import com.ks.push.pojo.DO.PushPlatform;
import com.ks.push.service.BDeviceTokenService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.IPUtil;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.ThreadUtil;
import org.yeshi.utils.entity.FileUploadResult;
import org.yeshi.utils.tencentcloud.COSManager;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
/**
 * @author hxh
@@ -50,13 +64,17 @@
    @Resource
    private VerifyCodeManager verifyCodeManager;
    //@Reference(version = "1.0", check = false)
    private BDeviceTokenService bDeviceTokenService;
    @Resource
    private PushManager pushManager;
    @Resource
    private UserExtraInfoService userExtraInfoService;
    @ResponseBody
    @RequestMapping("loginPhone")
    public String loginPhone(AcceptData acceptData, String phone, String vcode, String token) {
    public String loginPhone(AcceptData acceptData, String phone, String vcode, String token, HttpServletRequest request) {
        LoginInfoDTO loginInfo = new LoginInfoDTO();
        loginInfo.setIpInfo(IPUtil.getRemotIP(request) + ":" + request.getRemotePort());
        loginInfo.setLoginType(UserLoginRecord.TYPE_LOGIN_PHONE);
        if (!StringUtil.isNullOrEmpty(phone)) {
            if (StringUtil.isNullOrEmpty(vcode)) {
@@ -72,6 +90,25 @@
            return JsonUtil.loadFalseResult("信息不完整");
        }
        loginInfo.setSystem(acceptData.getSystem());
        return login(loginInfo, acceptData);
    }
    @ResponseBody
    @RequestMapping("loginWX")
    public String loginWX(AcceptData acceptData, String code, HttpServletRequest request) {
        LoginInfoDTO loginInfo = new LoginInfoDTO();
        loginInfo.setIpInfo(IPUtil.getRemotIP(request) + ":" + request.getRemotePort());
        loginInfo.setLoginType(UserLoginRecord.TYPE_LOGIN_WX);
        if (StringUtil.isNullOrEmpty(code)) {
            return JsonUtil.loadFalseResult("信息不完整");
        }
        loginInfo.setWxCode(code);
        loginInfo.setSystem(acceptData.getSystem());
        return login(loginInfo, acceptData);
    }
    private String login(LoginInfoDTO loginInfo, AcceptData acceptData) {
        try {
            UserInfo userInfo = userAccountService.login(loginInfo);
            ThreadUtil.run(new Runnable() {
@@ -79,18 +116,55 @@
                public void run() {
                    try {
                        //登录成功
                        bDeviceTokenService.bindUid(acceptData.getSystem().name(), acceptData.getUtdId(), userInfo.getId() + "");
                        pushManager.bindUid(acceptData.getSystem(), userInfo.getId(), acceptData.getPlatform() == APPPlatform.ios ? acceptData.getIdfa() : acceptData.getUtdId());
                    } catch (Exception e) {
                    }
                }
            });
            return JsonUtil.loadTrueResult(JsonUtil.getApiCommonGson().toJson(userInfo));
            return outUserInfoForLogin(userInfo);
        } catch (LoginException e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
    private String outUserInfoForLogin(UserInfo userInfo) {
        UserInfoVO vo = UserInfoVO.create(userInfo, userExtraInfoService.get(userInfo.getId()));
        //是否需要填写邀请码
        vo.setHasBoss(false);
        return JsonUtil.loadTrueResult(JsonUtil.getSimpleGson().toJson(vo));
    }
    @UserLogin(uid = "#uid")
    @ResponseBody
    @RequestMapping("bindWX")
    public String bindWX(AcceptData acceptData, Long uid, String code) {
        try {
            userAccountService.bindWX(uid, code);
        } catch (UserAccountException e) {
            e.printStackTrace();
            return JsonUtil.loadFalseResult(e.getMsg());
        }
        return JsonUtil.loadTrueResult("");
    }
    @UserLogin(uid = "#uid")
    @ResponseBody
    @RequestMapping("bindPhone")
    public String bindPhone(AcceptData acceptData, Long uid, String phone, String vcode, String token) {
        try {
            userAccountService.bindPhone(uid, phone, vcode, token);
        } catch (UserAccountException e) {
            e.printStackTrace();
            return JsonUtil.loadFalseResult(e.getMsg());
        }
        return JsonUtil.loadTrueResult("");
    }
    @UserLogin(uid = "#uid")
@@ -99,7 +173,7 @@
    public String logout(AcceptData acceptData, Long uid) {
        try {
            //解绑UID
            bDeviceTokenService.unBindUid(acceptData.getSystem().name(), acceptData.getUtdId());
            pushManager.unBind(acceptData.getSystem(), acceptData.getPlatform() == APPPlatform.ios ? acceptData.getIdfa() : acceptData.getUtdId());
        } catch (Exception e) {
            e.printStackTrace();
        }
@@ -166,32 +240,76 @@
        return JsonUtil.loadTrueResult(vo);
    }
    @UserLogin(uid = "#uid")
    @ResponseBody
    @RequestMapping("updateUserInfo")
    public String updateUserInfo(AcceptData acceptData, Long uid, String nickName, @Nullable @RequestParam("portrait") MultipartFile portrait, String portraitMD5) {
        UserInfo user = userInfoService.get(uid);
        if (user == null) {
            return JsonUtil.loadFalseResult("用户不存在");
        }
        UserInfo update = new UserInfo();
        update.setId(uid);
        if (!StringUtil.isNullOrEmpty(nickName)) {
            update.setNickName(nickName);
        }
        if (portrait != null) {
            if (StringUtil.isNullOrEmpty(portraitMD5)) {
                return JsonUtil.loadFalseResult("头像文件加密值为空");
            }
            try {
                String md5 = DigestUtils.md5Hex(portrait.getBytes());
                if (!portraitMD5.equalsIgnoreCase(md5)) {
                    return JsonUtil.loadFalseResult("头像文件加密值错误");
                }
                InputStream inputStream = portrait.getInputStream();
                String contentType = portrait.getContentType();
                String key = String.format("/imgs/portrait/%s_%s.png", uid, md5);
                FileUploadResult result = COSManager.getInstance().uploadFile(inputStream, key);
                if (result == null) {
                    return JsonUtil.loadFalseResult("上传出错");
                }
                update.setPortrait(ImageUtil.getCOSImageUrl(key));
            } catch (Exception e) {
                return JsonUtil.loadFalseResult("头像修改出错");
            }
        }
        userInfoService.update(update);
        if (portrait != null) {
            //删除原来的头像
            if (!user.getPortrait().contains("default")) {
                try {
                    COSManager.getInstance().deleteFileByKey(ImageUtil.getUrlKey(user.getPortrait()));
                } catch (Exception e) {
                }
            }
        }
        return JsonUtil.loadTrueResult("");
    }
    @ResponseBody
    @RequestMapping("uploadPushRegId")
    public String uploadPushRegId(AcceptData acceptData, Long uid, String regId) {
        BPushDeviceToken deviceToken = new BPushDeviceToken();
        deviceToken.setAppCode(acceptData.getSystem().name());
        deviceToken.setDeviceId(acceptData.getUtdId());
        deviceToken.setBuildModel(acceptData.getDeviceType());
        deviceToken.setBuildVersion(acceptData.getOsVersion());
        deviceToken.setToken(regId);
        deviceToken.setType(PushPlatform.jpush);
        if (uid != null) {
            deviceToken.setUid(uid + "");
        }
        deviceToken.setVersionCode(acceptData.getVersion());
        try {
            bDeviceTokenService.save(deviceToken);
            pushManager.saveToken(acceptData, uid, regId);
            return JsonUtil.loadTrueResult("");
        } catch (BPushDeviceTokenException e) {
            return JsonUtil.loadTrueResult(e.getMessage());
            return JsonUtil.loadFalseResult(e.getCode(), "业务出错");
        } catch (ParamsException e) {
            return JsonUtil.loadTrueResult(e.getMessage());
            return JsonUtil.loadFalseResult(e.getCode(), "参数错误");
        }
    }