合并微信小程序和支付宝小程序登录入口

This commit is contained in:
谭凯凯
2024-09-24 09:53:14 +08:00
committed by Tankaikai
parent 05efa142e9
commit 3ee4ea9870
5 changed files with 148 additions and 92 deletions

View File

@@ -4,6 +4,9 @@ import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.chaozhanggui.system.cashierservice.alipayUtil.AlipayUtil;
import com.chaozhanggui.system.cashierservice.auth.AuthSource;
import com.chaozhanggui.system.cashierservice.dao.TbMerchantAccountMapper;
import com.chaozhanggui.system.cashierservice.entity.TbMerchantAccount;
import com.chaozhanggui.system.cashierservice.entity.TbUserInfo;
@@ -16,10 +19,13 @@ import com.chaozhanggui.system.cashierservice.service.LoginService;
import com.chaozhanggui.system.cashierservice.service.OnlineUserService;
import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
import com.chaozhanggui.system.cashierservice.sign.Result;
import com.chaozhanggui.system.cashierservice.util.*;
import com.chaozhanggui.system.cashierservice.util.IpUtil;
import com.chaozhanggui.system.cashierservice.util.MD5Utils;
import com.chaozhanggui.system.cashierservice.util.StringUtil;
import com.chaozhanggui.system.cashierservice.util.TokenUtil;
import com.chaozhanggui.system.cashierservice.wxUtil.WechatUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -60,28 +66,27 @@ public class LoginContoller {
@Autowired
RedisUtil redisUtil;
@Resource
AlipayUtil alipayUtil;
@RequestMapping("/wx/business/login")
public Result wxBusinessLogin(@RequestParam(value = "code", required = false) String code,
@RequestParam(value = "shopId", required = false) String shopId
) {
public Result wxBusinessLogin(@RequestParam(value = "code", required = false) String code, @RequestParam(value = "shopId", required = false) String shopId) {
JSONObject SessionKeyOpenId = WechatUtil.getSessionKeyOrOpenId(code, businessAppId, businessSecrete);
String openid = SessionKeyOpenId.getString("openid");
if(Objects.isNull(openid)){
if (Objects.isNull(openid)) {
return Result.fail("获取微信id失败");
}
return loginService.wxBusinessLogin(openid,shopId);
return loginService.wxBusinessLogin(openid, shopId);
}
@GetMapping("/wx/business/openId")
public Result getOpenId(
@RequestParam String code
) {
public Result getOpenId(@RequestParam String code) {
JSONObject SessionKeyOpenId = WechatUtil.getSessionKeyOrOpenId(code, customAppId, customSecrete);
String openid = SessionKeyOpenId.getString("openid");
if(Objects.isNull(openid)){
if (Objects.isNull(openid)) {
return Result.fail("获取微信id失败");
}
@@ -95,48 +100,41 @@ public class LoginContoller {
* @param map
* @return
*/
@RequestMapping("/wx/custom/login")
public Result wxCustomLogin(HttpServletRequest request, @RequestBody Map<String, String> map) {
@RequestMapping("/auth/custom/login")
@SneakyThrows
public Result authCustomLogin(HttpServletRequest request, @RequestBody Map<String, String> map) {
if (ObjectUtil.isNull(map) || ObjectUtil.isEmpty(map) || !map.containsKey("code") || ObjectUtil.isEmpty(map.get("code"))) {
Result.fail("code不能为空");
}
String code = map.get("code").toString();
String rawData = map.get("rawData");
// String signature = map.get("signature");
// String encryptedData = map.get("encryptedData");
// String ivStr = map.get("iv");
// String phone = map.get("phone");
// 用户非敏感信息:rawData
// 签名signature
JSONObject rawDataJson = JSON.parseObject(rawData);
// 1.接收小程序发送的code
// 2.开发者服务器 登录凭证校验接口 appi + appsecret + code
JSONObject SessionKeyOpenId = WechatUtil.getSessionKeyOrOpenId(code, customAppId, customSecrete);
// 3.接收微信接口服务 获取返回的参数
String openid = SessionKeyOpenId.getString("openid");
// String sessionKey = SessionKeyOpenId.getString("session_key");
// 4.校验签名 小程序发送的签名signature与服务器端生成的签名signature2 = sha1(rawData + sessionKey)
// String signature2 = DigestUtils.sha1Hex(rawData + sessionKey);
// if (!signature.equals(signature2)) {
// return Result.fail("签名校验失败");
// }
// String phone = "";
// try{
// String data = WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr);
// if (ObjectUtil.isNotEmpty(data) && JSONObject.parseObject(data).containsKey("phoneNumber")) {
// }// phone =JSONObject.parseObject(data).get("phoneNumber").toString();
// }catch (Exception e){
// log.info("登录传参:获取手机号失败{}",e.getMessage());
// }
String nickName = rawDataJson.getString("nickName");
String avatarUrl = rawDataJson.getString("avatarUrl");
try {
return loginService.wxCustomLogin(openid, avatarUrl, nickName, "", IpUtil.getIpAddr(request));
} catch (Exception e) {
e.printStackTrace();
// 三方登录来源 wechat、alipay
String source = map.getOrDefault("source",AuthSource.WECHAT.getValue());
String code = map.get("code");
if(AuthSource.WECHAT.getValue().equals(source)){
String rawData = map.get("rawData");
// 签名signature
JSONObject rawDataJson = JSON.parseObject(rawData);
// 1.接收小程序发送的code
// 2.开发者服务器 登录凭证校验接口 appi + appsecret + code
JSONObject SessionKeyOpenId = WechatUtil.getSessionKeyOrOpenId(code, customAppId, customSecrete);
// 3.接收微信接口服务 获取返回的参数
String openid = SessionKeyOpenId.getString("openid");
String nickName = rawDataJson.getString("nickName");
String avatarUrl = rawDataJson.getString("avatarUrl");
try {
return loginService.wxCustomLogin(openid, avatarUrl, nickName, "", IpUtil.getIpAddr(request));
} catch (Exception e) {
e.printStackTrace();
}
}else if(AuthSource.ALIPAY.getValue().equals(source)){
try {
String openId = alipayUtil.getOpenId(code);
return loginService.alipayCustomLogin(openId);
}catch (AlipayApiException e){
return Result.fail("登录失败:"+e.getErrMsg());
}catch (Exception e){
e.printStackTrace();
}
}
return Result.fail("登录失败");
}
@@ -179,7 +177,7 @@ public class LoginContoller {
// return Result.fail("获取手机号失败,请重试!");
// }
@RequestMapping("getPhoneNumber")
public Result getPhoneNumber(@RequestHeader String openId,@RequestBody Map<String, String> map) {
public Result getPhoneNumber(@RequestHeader String openId, @RequestBody Map<String, String> map) {
if (ObjectUtil.isNull(map) || ObjectUtil.isEmpty(map) || !map.containsKey("code") || ObjectUtil.isEmpty(map.get("code"))) {
Result.fail("code不能为空");
@@ -201,14 +199,14 @@ public class LoginContoller {
try {
if (ObjectUtil.isNotEmpty(data) && JSONObject.parseObject(data).containsKey("phoneNumber")) {
// if (!map.containsKey("shopId") || ObjectUtil.isEmpty(map.get("shopId"))) {
return Result.success(CodeEnum.SUCCESS, JSONObject.parseObject(data).get("phoneNumber"));
return Result.success(CodeEnum.SUCCESS, JSONObject.parseObject(data).get("phoneNumber"));
// }
// log.info("登录传参 获取手机号成功 sessionKey:{}\n encryptedData:{} \nivStr:{} \n data:{},",sessionKey,encryptedData,ivStr,JSONObject.parseObject(data).get("phoneNumber"));
// return loginService.upPhone(openId,JSONObject.parseObject(data).get("phoneNumber").toString(),map.get("shopId").toString());
}
} catch (Exception e){
} catch (Exception e) {
// e.printStackTrace();
log.info("登录传参 获取手机号失败 sessionKey:{}\n encryptedData:{} \nivStr:{} \n data:{},",sessionKey,encryptedData,ivStr,data);
log.info("登录传参 获取手机号失败 sessionKey:{}\n encryptedData:{} \nivStr:{} \n data:{},", sessionKey, encryptedData, ivStr, data);
}
return Result.fail("获取手机号失败,请重试!");
}
@@ -254,10 +252,8 @@ public class LoginContoller {
* @return
*/
@GetMapping("createCardNo")
public Result createCardNo(@RequestHeader("openId") String openId, @RequestHeader("token") String token, @RequestHeader("id") String id,
@RequestParam("shopId") String shopId
) {
return loginService.createCardNo(id, openId,shopId);
public Result createCardNo(@RequestHeader("openId") String openId, @RequestHeader("token") String token, @RequestHeader("id") String id, @RequestParam("shopId") String shopId) {
return loginService.createCardNo(id, openId, shopId);
}
@GetMapping("/userInfo")
@@ -267,6 +263,7 @@ public class LoginContoller {
/**
* 更新用户信息
*
* @param token
* @param userInfo
* @return
@@ -281,16 +278,16 @@ public class LoginContoller {
}
@PostMapping(value = "/upPass")
public Result upPass(@RequestHeader String token,@RequestBody UserPassDto passVo){
public Result upPass(@RequestHeader String token, @RequestBody UserPassDto passVo) {
String userId = TokenUtil.parseParamFromToken(token).getString("userId");
String newPass = MD5Utils.MD5Encode(passVo.getNewPass(), "utf-8");
if (ObjectUtil.isNull(passVo.getCode())) {
String oldPass = MD5Utils.MD5Encode(passVo.getOldPass(), "utf-8");
return loginService.upPass(userId,oldPass, newPass);
return loginService.upPass(userId, oldPass, newPass);
} else {
boolean tf = loginService.validate(passVo.getCode(), passVo.getPhone());
if (tf) {
TbUserInfo userInfo=new TbUserInfo();
TbUserInfo userInfo = new TbUserInfo();
userInfo.setId(Integer.valueOf(userId));
userInfo.setPassword(newPass);
return loginService.upUserInfo(userInfo);
@@ -301,16 +298,16 @@ public class LoginContoller {
}
@PostMapping(value = "modityPass")
public Result modityPass(@RequestHeader String token,@RequestBody UserPassDto passVo){
public Result modityPass(@RequestHeader String token, @RequestBody UserPassDto passVo) {
String userId = TokenUtil.parseParamFromToken(token).getString("userId");
String newPass = MD5Utils.MD5Encode(passVo.getNewPass(), "utf-8");
if (ObjectUtil.isNull(passVo.getCode())) {
String oldPass = MD5Utils.MD5Encode(passVo.getOldPass(), "utf-8");
return loginService.upPass(userId,oldPass, newPass);
return loginService.upPass(userId, oldPass, newPass);
} else {
boolean tf = loginService.validate(passVo.getCode(), passVo.getPhone());
if (tf) {
TbUserInfo userInfo=new TbUserInfo();
TbUserInfo userInfo = new TbUserInfo();
userInfo.setId(Integer.valueOf(userId));
userInfo.setPassword(newPass);
return loginService.upUserInfo(userInfo);
@@ -359,12 +356,12 @@ public class LoginContoller {
}
//验证密码
String mdPasswordString = MD5Utils.MD5Encode(authUserDto.getPassword(), "utf-8");
return loginService.appLogin(authUserDto.getUsername(),openid, mdPasswordString);
return loginService.appLogin(authUserDto.getUsername(), openid, mdPasswordString);
} else {
// tf = true;
tf = loginService.validate(authUserDto.getCode(), authUserDto.getUsername());
if (tf) {
return loginService.appLogin(authUserDto.getUsername(),openid, null);
return loginService.appLogin(authUserDto.getUsername(), openid, null);
} else {
return Result.fail("验证码输入有误");
}
@@ -393,29 +390,31 @@ public class LoginContoller {
/**
* 重置资金密码
*
* @param token
* @param map
* @return
*/
@RequestMapping("resetPwd")
public Result resetPwd(@RequestHeader String token,@RequestBody Map<String, Object> map){
public Result resetPwd(@RequestHeader String token, @RequestBody Map<String, Object> map) {
String userId = TokenUtil.parseParamFromToken(token).getString("userId");
return loginService.resetPwd(userId,map);
return loginService.resetPwd(userId, map);
}
/**
* 修改密码
*
* @param token
* @param map
* @return
*/
@RequestMapping("mpdifyPwd")
public Result mpdifyPwd(@RequestHeader String token,@RequestBody Map<String, Object> map){
public Result mpdifyPwd(@RequestHeader String token, @RequestBody Map<String, Object> map) {
String userId = TokenUtil.parseParamFromToken(token).getString("userId");
return loginService.modifyPwd(userId,map);
return loginService.modifyPwd(userId, map);
}