101captain
2021-11-23 f82821e697cb59bcdf5a0b77dd8eedfadfbe77ae
springcloud_k8s_panzhihuazhihuishequ/applets/src/main/java/com/panzhihua/applets/api/LoginApi.java
@@ -2,9 +2,15 @@
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.applets.config.WxMaConfiguration;
@@ -73,16 +79,40 @@
            }
        }
        log.info("微信登录成功【{}】", JSONObject.toJSONString(sessionInfo));
        log.info("loginRequest参数【{}】", JSONObject.toJSONString(loginRequest));
        String openid = sessionInfo.getOpenid();
        String sessionKey = sessionInfo.getSessionKey();
        String unionid = sessionInfo.getUnionid();
        if (ObjectUtils.isEmpty(unionid)) {
            unionid = "无";
        // 解密用户信息
        WxMaUserInfo wxUserInfo = maService.getUserService().getUserInfo(sessionKey,
                loginRequest.getEncryptedData(), loginRequest.getIv());
        log.info("wxUserInfo信息【{}】", JSONObject.toJSONString(wxUserInfo));
        if (null == wxUserInfo) {
            return R.fail("获取用户信息失败");
        }
        userService.addOrUpdate(openid, sessionKey, unionid);
        String unionId = wxUserInfo.getUnionId();
        if (ObjectUtils.isEmpty(unionId)) {
            unionId = "无";
        }
        userService.addOrUpdate(openid, sessionKey, unionId);
        return tokenService.loginApplets(openid);
    }
    @ApiOperation(value = "H5登录", response = LoginReturnVO.class)
    @PostMapping("loginH5")
    public R loginH5(@RequestBody LoginRequest loginRequest) {
        String unionId = loginRequest.getUnionId();
        if (StringUtils.isBlank(unionId)) {
            return R.fail("缺少基本信息参数");
        }
        //通过unionId去匹配user
        R r1 = userService.getUserInfoByUnionId(unionId);
        if (R.isOk(r1)) {
            LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(JSONObject.toJSONString(r1.getData()), LoginUserInfoVO.class);
            return tokenService.loginApplets(loginUserInfoVO.getOpenid());
        }
        return r1;
    }
    @ApiOperation(value = "维护用户基本信息(昵称、性别、头像)")
    @PostMapping("updateUserWeiXinInfo")
    public R updateUserWeiXinInfo(@RequestBody LoginRequest loginRequest) {