package com.panzhihua.applets.api; import javax.annotation.Resource; import com.panzhihua.common.constants.Constants; import com.panzhihua.common.utlis.RSAUtils; import org.springframework.util.ObjectUtils; import org.springframework.validation.annotation.Validated; 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; import com.panzhihua.applets.model.vos.LoginRequest; import com.panzhihua.common.controller.BaseController; import com.panzhihua.common.exceptions.UnAuthenticationException; import com.panzhihua.common.model.vos.LoginReturnVO; import com.panzhihua.common.model.vos.LoginUserInfoVO; import com.panzhihua.common.model.vos.R; import com.panzhihua.common.model.vos.community.ComActVO; import com.panzhihua.common.model.vos.community.ComMngStructAreaVO; import com.panzhihua.common.model.vos.user.SysUserAgreementVO; import com.panzhihua.common.model.vos.user.UuLoginVO; import com.panzhihua.common.service.auth.TokenService; import com.panzhihua.common.service.community.CommunityService; import com.panzhihua.common.service.user.UserService; import com.panzhihua.common.validated.PutGroup; import cn.binarywang.wx.miniapp.api.WxMaQrcodeService; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; import cn.hutool.core.codec.Base64; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; import java.util.HashMap; import java.util.Map; /** * @program: springcloud_k8s_panzhihuazhihuishequ * @description: 登录 * @author: huang.hongfa weixin hhf9596 qq 959656820 * @create: 2020-11-19 16:35 **/ @Slf4j @Api(tags = {"登录模块"}) @RestController("/") public class LoginApi extends BaseController { @Resource private UserService userService; @Resource private WxMaConfiguration wxMaConfiguration; @Resource private TokenService tokenService; @Resource private CommunityService communityService; @ApiOperation(value = "登录获取token", response = LoginReturnVO.class) @PostMapping("login-token") public R loginToken(@RequestParam("openId") String openId,@RequestParam("appid")String appid) { return tokenService.loginApplets(openId,appid); } @ApiOperation(value = "登录", response = LoginReturnVO.class) @PostMapping("login") public R login(@RequestBody LoginRequest loginRequest) { String code = loginRequest.getCode(); if (ObjectUtils.isEmpty(code)) { return R.fail("缺少登录参数"); } log.info(code); WxMaService maService = wxMaConfiguration.getMaService(loginRequest.getAppid()); WxMaJscode2SessionResult sessionInfo = null; try { sessionInfo = maService.getUserService().getSessionInfo(code); } catch (Exception e) { log.error("微信登录失败【{}】", e.getMessage()); if (code.equals("22")) { sessionInfo = new WxMaJscode2SessionResult(); sessionInfo.setOpenid("88888888"); sessionInfo.setSessionKey("9999999"); } else { return R.fail("微信登录失败"); } } 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 = "无"; } userService.addOrUpdate(openid, sessionKey, unionid,this.getAppId()); return tokenService.loginApplets(openid,this.getAppId()); } @PostMapping("/loginTest") public R loginTest(@RequestBody LoginRequest loginRequest){ return tokenService.loginApplets(loginRequest.getCode(),this.getAppId()); } @ApiOperation(value = "H5登录", response = LoginReturnVO.class) @PostMapping("loginH5") public R loginH5(@RequestBody LoginRequest loginRequest) { String code = loginRequest.getCode(); if (ObjectUtils.isEmpty(code)) { return R.fail("缺少登录参数"); } log.info(code); WxMaService maService = wxMaConfiguration.getMaH5Service(); WxMaJscode2SessionResult sessionInfo = null; try { sessionInfo = maService.getUserService().getSessionInfo(code); } catch (Exception e) { log.error("微信登录失败【{}】", e.getMessage()); if (code.equals("22")) { sessionInfo = new WxMaJscode2SessionResult(); sessionInfo.setOpenid("88888888"); sessionInfo.setSessionKey("9999999"); } else { return R.fail("微信登录失败"); } } log.info("微信登录成功【{}】", JSONObject.toJSONString(sessionInfo)); log.info("loginRequest参数【{}】", JSONObject.toJSONString(loginRequest)); // 解密用户信息 // WxMaUserInfo wxUserInfo = maService.getUserService().getUserInfo(sessionInfo.getSessionKey(), // loginRequest.getEncryptedData(), loginRequest.getIv()); // log.info("wxUserInfo信息【{}】", JSONObject.toJSONString(wxUserInfo)); // if (null == wxUserInfo) { // return R.fail("获取用户信息失败"); // } // String unionId = wxUserInfo.getUnionId(); String unionId = sessionInfo.getUnionid(); //通过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(),this.getAppId()); } return r1; } @ApiOperation(value = "仁和区登录", response = LoginReturnVO.class) @PostMapping("/renHe/login") public R renHeLogin(@RequestBody LoginRequest loginRequest) { String code = loginRequest.getCode(); if (ObjectUtils.isEmpty(code)) { return R.fail("缺少登录参数"); } log.info(code); WxMaService maService = wxMaConfiguration.getMaRhService(); WxMaJscode2SessionResult sessionInfo = null; try { sessionInfo = maService.getUserService().getSessionInfo(code); } catch (Exception e) { log.error("微信登录失败【{}】", e.getMessage()); if (code.equals("22")) { sessionInfo = new WxMaJscode2SessionResult(); sessionInfo.setOpenid("88888888"); sessionInfo.setSessionKey("9999999"); } else { return R.fail("微信登录失败"); } } 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 = "无"; } userService.addOrUpdate(openid, sessionKey, unionid,this.getAppId()); return tokenService.loginApplets(openid,this.getAppId()); } @ApiOperation(value = "维护用户基本信息(昵称、性别、头像)") @PostMapping("updateUserWeiXinInfo") public R updateUserWeiXinInfo(@RequestBody LoginRequest loginRequest) { Long userId = this.getUserId(); if (userId == null) { return R.fail(401, "请先登录"); } boolean empty2 = ObjectUtils.isEmpty(userId); if (empty2) { throw new UnAuthenticationException(); } WxMaUserInfo wxUserInfo = loginRequest.getUserInfo(); if (null == wxUserInfo) { return R.fail("缺少基本信息参数"); } String avatarUrl = wxUserInfo.getAvatarUrl(); String gender = wxUserInfo.getGender(); String nickName = wxUserInfo.getNickName(); return userService.updateUserWeiXinInfo(userId, nickName, Integer.parseInt(gender), avatarUrl); } @ApiOperation(value = "维护微信用户手机号") @PostMapping("updateUserWeiXinPhone") public R updateUserWeiXinPhone(@RequestBody LoginRequest loginRequest) { String encryptedData = loginRequest.getEncryptedData(); String iv = loginRequest.getIv(); boolean empty = ObjectUtils.isEmpty(iv); boolean empty1 = ObjectUtils.isEmpty(encryptedData); if (empty || empty1) { return R.fail("微信用户参数不全"); } WxMaService maService = wxMaConfiguration.getMaService(this.getAppId()); Long userId = this.getUserId(); R r = userService.getUserInfoByUserId(userId + ""); LoginUserInfoVO loginUserInfoVO = r.getData(); WxMaPhoneNumberInfo wxMaPhoneNumberInfo = maService.getUserService().getPhoneNoInfo(loginUserInfoVO.getSessionKey(), encryptedData, iv); if (ObjectUtils.isEmpty(wxMaPhoneNumberInfo) || ObjectUtils.isEmpty(wxMaPhoneNumberInfo.getPhoneNumber())) { return R.fail("微信解析手机号失败"); } String purePhoneNumber = wxMaPhoneNumberInfo.getPurePhoneNumber(); return userService.updateUserWeiXinPhone(userId, purePhoneNumber); } @ApiOperation(value = "维护微信用户手机号") @PostMapping("updateUserWeiXinPhone2") public R updateUserWeiXinPhone2(@RequestBody LoginRequest loginRequest) { String code = loginRequest.getCode(); if (ObjectUtils.isEmpty(code)) { return R.fail("缺少登录参数"); } log.info(code); WxMaService maService = wxMaConfiguration.getMaService(this.getAppId()); WxMaJscode2SessionResult sessionInfo = null; try { sessionInfo = maService.getUserService().getSessionInfo(code); } catch (Exception e) { log.error("微信登录失败【{}】", e.getMessage()); if (code.equals("22")) { sessionInfo = new WxMaJscode2SessionResult(); sessionInfo.setOpenid("88888888"); sessionInfo.setSessionKey("9999999"); } else { return R.fail("微信登录失败"); } } log.info("微信登录成功【{}】", JSONObject.toJSONString(sessionInfo)); String encryptedData = loginRequest.getEncryptedData(); String iv = loginRequest.getIv(); boolean empty = ObjectUtils.isEmpty(iv); boolean empty1 = ObjectUtils.isEmpty(encryptedData); if (empty || empty1) { return R.fail("微信用户参数不全"); } // WxMaService maService = wxMaConfiguration.getMaService(); Long userId = this.getUserId(); // R r = userService.getUserInfoByUserId(userId + ""); // LoginUserInfoVO loginUserInfoVO = r.getData(); WxMaPhoneNumberInfo wxMaPhoneNumberInfo = maService.getUserService().getPhoneNoInfo(sessionInfo.getSessionKey(), encryptedData, iv); if (ObjectUtils.isEmpty(wxMaPhoneNumberInfo) || ObjectUtils.isEmpty(wxMaPhoneNumberInfo.getPhoneNumber())) { return R.fail("微信解析手机号失败"); } String purePhoneNumber = wxMaPhoneNumberInfo.getPurePhoneNumber(); return userService.updateUserWeiXinPhone(userId, purePhoneNumber); } @ApiOperation(value = "用户登出") @PostMapping("logout") public R updateUserWeiXinPhone() { String token = this.getToken(); boolean empty2 = ObjectUtils.isEmpty(token); if (empty2) { return R.ok(); } return tokenService.logout(token); } @ApiOperation(value = "刷新token", response = LoginReturnVO.class) @GetMapping("refreshToken") @ApiImplicitParam(name = "refreshToken", value = "登录返回的刷新token") public R refreshToken(@RequestParam("refreshToken") String refreshToken) { return tokenService.refreshToken(refreshToken); } @ApiOperation(value = "刷新token", response = LoginReturnVO.class) @PostMapping("getNewToken") public R getNewToken(@RequestBody LoginUserInfoVO loginUserInfoVO) { return tokenService.getNewToken(loginUserInfoVO); } @ApiOperation(value = "查询社区") @PostMapping("listcommunity") public R listCommunity(@RequestBody ComActVO comActVO) { return communityService.listCommunity(comActVO); } @ApiOperation(value = "查询小区", response = ComMngStructAreaVO.class) @GetMapping("listarea") @ApiImplicitParam(name = "communityId", value = "社区id", required = true) public R listArea(@RequestParam("communityId") Long communityId) { return communityService.listArea(communityId); } @ApiOperation(value = "用户绑定社区、小区") @PutMapping("putusercommunityarea") public R putUserCommunityArea(@RequestBody @Validated(PutGroup.class) LoginUserInfoVO loginUserInfoVO) { Long userId = this.getUserId(); loginUserInfoVO.setUserId(userId); return userService.putUserCommunityArea(loginUserInfoVO); } @ApiOperation(value = "用户协议和隐私政策", response = SysUserAgreementVO.class) @GetMapping("useragreement") public R userAgreement() { int type = 1; return userService.userAgreement(type,this.getAppId()); } @ApiOperation("uu洗车登录") @PostMapping("uuLogin") public R uuLogin(@RequestBody UuLoginVO uuLoginVO){ return userService.uuLogin(uuLoginVO); } @ApiOperation("uu洗车获取小程序码") @ApiImplicitParam(name = "couponId", value = "优惠券id", required = true) @GetMapping("uu/getQRCode") public R uuLogin(@RequestParam("couponId") String couponId) { try { //i小懒 WxMaQrcodeService wsCodeService = wxMaConfiguration.getMaService("wx98d62711dfbd8425").getQrcodeService(); byte[] bytes = wsCodeService.createWxaCodeUnlimitBytes("couponId=" + couponId, "pages/user/getCoupon/getCoupon", true, "release", 30, true, null, false); String res = String.format("data:image/png;base64,%s", Base64.encode(bytes)); //花城 WxMaQrcodeService uuCodeService = wxMaConfiguration.getMaService("wx118de8a734d269f0").getQrcodeService(); byte[] hcBytes = uuCodeService.createWxaCodeUnlimitBytes("couponId=" + couponId, "packageD/pages/user/getCoupon/getCoupon", true, "release", 30, true, null, false); String hcRes = String.format("data:image/png;base64,%s", Base64.encode(hcBytes)); Map retMap = new HashMap<>(); retMap.put("ixiaolan",res); retMap.put("huacheng",hcRes); return R.ok(retMap); } catch (Exception e) { log.error("generate QRCode failed:" + e.getMessage()); return R.fail("generate QRCode failed"); } } @ApiOperation("天府通办登录") @PostMapping("tfLogin") public R tfLogin(@RequestBody UuLoginVO uuLoginVO){ try { log.info("天府通办登录【{}】", JSONObject.toJSONString(uuLoginVO.getMobile())); String str=RSAUtils.decrypt(uuLoginVO.getMobile(), Constants.PRIVATE_KEY); log.info("天府通办登录【{}】", JSONObject.toJSONString(str)); uuLoginVO.setMobile(str); //测试是否能正常添加用户 } catch (Exception e) { e.printStackTrace(); return R.fail(); } userService.tfLogin(uuLoginVO); R r=tokenService.tfLogin(uuLoginVO.getMobile(),"Huacheng@123","wx118de8a734d269f0"); return r; } @ApiOperation("用户授权") @GetMapping("/accept") public R accept(){ return userService.accept(this.getUserId()); } }