diff --git a/yshop-app/src/main/java/co/yixiang/modules/activity/rest/StoreCombinationController.java b/yshop-app/src/main/java/co/yixiang/modules/activity/rest/StoreCombinationController.java index e526db35..a9a256f3 100644 --- a/yshop-app/src/main/java/co/yixiang/modules/activity/rest/StoreCombinationController.java +++ b/yshop-app/src/main/java/co/yixiang/modules/activity/rest/StoreCombinationController.java @@ -132,7 +132,8 @@ public class StoreCombinationController { if(StrUtil.isEmpty(siteUrl)){ throw new YshopException("未配置h5地址"); } - String apiUrl = systemConfigService.getData(SystemConfigConstants.API_URL); +// String apiUrl = systemConfigService.getData(SystemConfigConstants.API_URL); + String apiUrl = "http://itxzz.top"; if(StrUtil.isEmpty(apiUrl)){ throw new YshopException("未配置api地址"); } diff --git a/yshop-app/src/main/java/co/yixiang/modules/auth/rest/AuthController.java b/yshop-app/src/main/java/co/yixiang/modules/auth/rest/AuthController.java index a64eae57..909185ac 100644 --- a/yshop-app/src/main/java/co/yixiang/modules/auth/rest/AuthController.java +++ b/yshop-app/src/main/java/co/yixiang/modules/auth/rest/AuthController.java @@ -101,6 +101,14 @@ public class AuthController { } + @PostMapping("/wxapp/loginAuth") + @ApiOperation(value = "小程序获取用户信息", notes = "小程序获取用户信息") + public ApiResult loginAuth(@Validated @RequestBody LoginParam loginParam, + HttpServletRequest request) { + YxUser yxUser = authService.loginAuth(loginParam); + return ApiResult.ok(yxUser).setMsg("获取成功"); + + } /** * 微信公众号授权 */ diff --git a/yshop-app/src/main/java/co/yixiang/modules/services/AuthService.java b/yshop-app/src/main/java/co/yixiang/modules/services/AuthService.java index 3efde3a5..d6c7269c 100644 --- a/yshop-app/src/main/java/co/yixiang/modules/services/AuthService.java +++ b/yshop-app/src/main/java/co/yixiang/modules/services/AuthService.java @@ -9,7 +9,9 @@ package co.yixiang.modules.services; import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.api.WxMaUserService; 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.util.ObjectUtil; import cn.hutool.core.util.StrUtil; @@ -28,10 +30,7 @@ import co.yixiang.modules.user.domain.YxUser; import co.yixiang.modules.user.service.YxUserService; import co.yixiang.modules.user.service.dto.WechatUserDto; import co.yixiang.modules.user.vo.OnlineUser; -import co.yixiang.utils.EncryptUtils; -import co.yixiang.utils.RedisUtils; -import co.yixiang.utils.ShopKeyUtils; -import co.yixiang.utils.StringUtils; +import co.yixiang.utils.*; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -79,7 +78,7 @@ public class AuthService { * @return long */ @Transactional(rollbackFor = Exception.class) - public YxUser wxappLogin(LoginParam loginParam) { + public YxUser loginAuth(LoginParam loginParam) { String code = loginParam.getCode(); String encryptedData = loginParam.getEncryptedData(); String iv = loginParam.getIv(); @@ -102,10 +101,10 @@ public class AuthService { openid = session.getUnionid(); } - YxUser yxUser = userService.getOne(Wrappers.lambdaQuery() - .eq(YxUser::getUsername, openid), false); + YxUser yxUser = this.userService.getOne(Wrappers.lambdaQuery() + .eq(YxUser::getUid, SecurityUtils.getUserId()), false); - if (ObjectUtil.isNull(yxUser)) { + if (ObjectUtil.isNotEmpty(yxUser)) { //过滤掉表情 @@ -116,7 +115,6 @@ public class AuthService { .avatar(wxMpUser.getAvatarUrl()) .addIp(ip) .lastIp(ip) - .userType(AppFromEnum.ROUNTINE.getValue()) .build(); //构建微信用户 @@ -134,7 +132,75 @@ public class AuthService { yxUser.setWxProfile(wechatUserDTO); - userService.save(yxUser); + this.userService.update(yxUser,Wrappers.lambdaQuery() + .eq(YxUser::getUid, SecurityUtils.getUserId())); + + } + return yxUser; + } catch (WxErrorException e) { + e.printStackTrace(); + log.error(e.getMessage()); + throw new YshopException(e.toString()); + } + } + + + /** + * 小程序登陆 + * + * @param loginParam loginParam + * @return long + */ + @Transactional(rollbackFor = Exception.class) + public YxUser wxappLogin(LoginParam loginParam) { + String code = loginParam.getCode(); + String encryptedData = loginParam.getEncryptedData(); + String iv = loginParam.getIv(); + String spread = loginParam.getSpread(); + try { + //读取redis配置 + String appId = redisUtils.getY(ShopKeyUtils.getWxAppAppId()); + String secret = redisUtils.getY(ShopKeyUtils.getWxAppSecret()); + if (StrUtil.isBlank(appId) || StrUtil.isBlank(secret)) { + throw new YshopException("请先配置小程序"); + } + WxMaService wxMaService = WxMaConfiguration.getWxMaService(); + WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code); + + WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService() + .getPhoneNoInfo(session.getSessionKey(), encryptedData, iv); + + String openid = session.getOpenid(); + //如果开启了UnionId + if (StrUtil.isNotBlank(session.getUnionid())) { + openid = session.getUnionid(); + } + + YxUser yxUser = this.userService.getOne(Wrappers.lambdaQuery() + .eq(YxUser::getPhone, phoneNoInfo.getPhoneNumber()), false); + + if (ObjectUtil.isNull(yxUser)) { + + + //过滤掉表情 + String ip = IpUtil.getRequestIp(); + yxUser = YxUser.builder() + .username(phoneNoInfo.getPhoneNumber()) + .phone(phoneNoInfo.getPhoneNumber()) + .addIp(ip) + .lastIp(ip) + .userType(AppFromEnum.ROUNTINE.getValue()) + .build(); + + //构建微信用户 + WechatUserDto wechatUserDTO = WechatUserDto.builder() + .routineOpenid(session.getOpenid()) + .unionId(session.getUnionid()) + .build(); + + yxUser.setWxProfile(wechatUserDTO); + + this.userService.save(yxUser); } else { WechatUserDto wechatUser = yxUser.getWxProfile(); @@ -147,9 +213,9 @@ public class AuthService { } yxUser.setUserType(AppFromEnum.ROUNTINE.getValue()); - userService.updateById(yxUser); + this.userService.updateById(yxUser); } - userService.setSpread(spread, yxUser.getUid()); + this.userService.setSpread(spread, yxUser.getUid()); redisUtils.set(ShopConstants.YSHOP_MINI_SESSION_KET + yxUser.getUid(), session.getSessionKey()); return yxUser; } catch (WxErrorException e) { @@ -157,8 +223,6 @@ public class AuthService { log.error(e.getMessage()); throw new YshopException(e.toString()); } - - } /** @@ -372,5 +436,4 @@ public class AuthService { return onlineUsers; } - } diff --git a/yshop-mall/src/main/java/co/yixiang/modules/product/service/impl/YxStoreProductServiceImpl.java b/yshop-mall/src/main/java/co/yixiang/modules/product/service/impl/YxStoreProductServiceImpl.java index 11a96385..009913fe 100644 --- a/yshop-mall/src/main/java/co/yixiang/modules/product/service/impl/YxStoreProductServiceImpl.java +++ b/yshop-mall/src/main/java/co/yixiang/modules/product/service/impl/YxStoreProductServiceImpl.java @@ -272,10 +272,10 @@ public class YxStoreProductServiceImpl extends BaseServiceImpl pageModel = new Page<>(productQueryParam.getPage(), productQueryParam.getLimit());