|
@@ -1,19 +1,33 @@
|
|
|
package org.dromara.web.controller;
|
|
package org.dromara.web.controller;
|
|
|
|
|
|
|
|
|
|
+import cn.binarywang.wx.miniapp.api.WxMaService;
|
|
|
|
|
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
|
|
|
+import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
|
|
|
import cn.dev33.satoken.annotation.SaIgnore;
|
|
import cn.dev33.satoken.annotation.SaIgnore;
|
|
|
|
|
+import cn.dev33.satoken.stp.SaLoginModel;
|
|
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
|
|
+import cn.hutool.http.HttpResource;
|
|
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
import me.zhyd.oauth.model.AuthCallback;
|
|
import me.zhyd.oauth.model.AuthCallback;
|
|
|
import me.zhyd.oauth.model.AuthResponse;
|
|
import me.zhyd.oauth.model.AuthResponse;
|
|
|
import me.zhyd.oauth.model.AuthUser;
|
|
import me.zhyd.oauth.model.AuthUser;
|
|
|
import me.zhyd.oauth.request.AuthRequest;
|
|
import me.zhyd.oauth.request.AuthRequest;
|
|
|
import me.zhyd.oauth.utils.AuthStateUtils;
|
|
import me.zhyd.oauth.utils.AuthStateUtils;
|
|
|
|
|
+import me.zhyd.oauth.utils.UuidUtils;
|
|
|
import org.dromara.common.core.constant.UserConstants;
|
|
import org.dromara.common.core.constant.UserConstants;
|
|
|
import org.dromara.common.core.domain.R;
|
|
import org.dromara.common.core.domain.R;
|
|
|
import org.dromara.common.core.domain.model.LoginBody;
|
|
import org.dromara.common.core.domain.model.LoginBody;
|
|
|
|
|
+import org.dromara.common.core.domain.model.LoginUser;
|
|
|
import org.dromara.common.core.domain.model.RegisterBody;
|
|
import org.dromara.common.core.domain.model.RegisterBody;
|
|
|
import org.dromara.common.core.domain.model.SocialLoginBody;
|
|
import org.dromara.common.core.domain.model.SocialLoginBody;
|
|
|
import org.dromara.common.core.utils.*;
|
|
import org.dromara.common.core.utils.*;
|
|
@@ -26,12 +40,12 @@ import org.dromara.common.social.utils.SocialUtils;
|
|
|
import org.dromara.common.tenant.helper.TenantHelper;
|
|
import org.dromara.common.tenant.helper.TenantHelper;
|
|
|
import org.dromara.common.websocket.utils.WebSocketUtils;
|
|
import org.dromara.common.websocket.utils.WebSocketUtils;
|
|
|
import org.dromara.system.domain.SysClient;
|
|
import org.dromara.system.domain.SysClient;
|
|
|
|
|
+import org.dromara.system.domain.bo.SysSocialBo;
|
|
|
import org.dromara.system.domain.bo.SysTenantBo;
|
|
import org.dromara.system.domain.bo.SysTenantBo;
|
|
|
|
|
+import org.dromara.system.domain.vo.SysSocialVo;
|
|
|
import org.dromara.system.domain.vo.SysTenantVo;
|
|
import org.dromara.system.domain.vo.SysTenantVo;
|
|
|
-import org.dromara.system.service.ISysClientService;
|
|
|
|
|
-import org.dromara.system.service.ISysConfigService;
|
|
|
|
|
-import org.dromara.system.service.ISysSocialService;
|
|
|
|
|
-import org.dromara.system.service.ISysTenantService;
|
|
|
|
|
|
|
+import org.dromara.system.domain.vo.SysUserVo;
|
|
|
|
|
+import org.dromara.system.service.*;
|
|
|
import org.dromara.web.domain.vo.LoginTenantVo;
|
|
import org.dromara.web.domain.vo.LoginTenantVo;
|
|
|
import org.dromara.web.domain.vo.LoginVo;
|
|
import org.dromara.web.domain.vo.LoginVo;
|
|
|
import org.dromara.web.domain.vo.TenantListVo;
|
|
import org.dromara.web.domain.vo.TenantListVo;
|
|
@@ -43,6 +57,7 @@ import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
@@ -67,6 +82,10 @@ public class AuthController {
|
|
|
private final ISysClientService clientService;
|
|
private final ISysClientService clientService;
|
|
|
private final ScheduledExecutorService scheduledExecutorService;
|
|
private final ScheduledExecutorService scheduledExecutorService;
|
|
|
|
|
|
|
|
|
|
+ private final WxMaService wxMaService;
|
|
|
|
|
+ private final ISysSocialService sysSocialService;
|
|
|
|
|
+ private final ISysUserService sysUserService;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -188,6 +207,69 @@ public class AuthController {
|
|
|
return R.ok();
|
|
return R.ok();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @PostMapping("/loginByPhoneCodeAndLoginCode")
|
|
|
|
|
+ public R<LoginVo> registerByPhonecode(@RequestParam String logincode,@RequestParam String phonecode) throws WxErrorException {
|
|
|
|
|
+
|
|
|
|
|
+ AuthRequest authRequest = SocialUtils.getAuthRequest("wechat_mini_program", socialProperties);
|
|
|
|
|
+ AuthResponse<AuthUser> response = authRequest.login(AuthCallback.builder().code(logincode).build());
|
|
|
|
|
+ AuthUser authUserData = response.getData();
|
|
|
|
|
+ if (!response.ok()) {
|
|
|
|
|
+ return R.fail(response.getMsg());
|
|
|
|
|
+ }
|
|
|
|
|
+ WxMaPhoneNumberInfo phone = wxMaService.getUserService().getPhoneNumber(phonecode);
|
|
|
|
|
+ SysUserVo userVo = sysUserService.selectUserByUserName(phone.getPhoneNumber());
|
|
|
|
|
+ if(userVo==null){
|
|
|
|
|
+ //注册
|
|
|
|
|
+ RegisterBody user = new RegisterBody();
|
|
|
|
|
+ user.setUsername(phone.getPhoneNumber());
|
|
|
|
|
+ user.setPassword(UuidUtils.getUUID().substring(0,6));
|
|
|
|
|
+ user.setUserType("app_user");
|
|
|
|
|
+ user.setPhonenumber(phone.getPhoneNumber());
|
|
|
|
|
+ registerService.register(user);
|
|
|
|
|
+ userVo = sysUserService.selectUserByUserName(phone.getPhoneNumber());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //绑定微信
|
|
|
|
|
+ SysSocialBo bo = BeanUtil.toBean(authUserData, SysSocialBo.class);
|
|
|
|
|
+ String authId = authUserData.getSource() + authUserData.getUuid();
|
|
|
|
|
+ BeanUtil.copyProperties(authUserData.getToken(), bo);
|
|
|
|
|
+ bo.setUserId(userVo.getUserId());
|
|
|
|
|
+ bo.setAuthId(authId);
|
|
|
|
|
+ bo.setOpenId(authUserData.getUuid());
|
|
|
|
|
+ bo.setUserName(authUserData.getUsername());
|
|
|
|
|
+ bo.setNickName(authUserData.getNickname());
|
|
|
|
|
+ // 查询是否已经绑定用户
|
|
|
|
|
+ List<SysSocialVo> list = sysSocialService.selectByAuthId(authId);
|
|
|
|
|
+ if (CollUtil.isEmpty(list)) {
|
|
|
|
|
+ // 没有绑定用户, 新增用户信息
|
|
|
|
|
+ sysSocialService.insertByBo(bo);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 更新用户信息
|
|
|
|
|
+ bo.setId(list.get(0).getId());
|
|
|
|
|
+ sysSocialService.updateByBo(bo);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ SysClient client = clientService.queryByClientId("39769a71109d3579c97f2e007f8afdaf");
|
|
|
|
|
+ LoginUser loginUser = loginService.buildLoginUser(userVo);
|
|
|
|
|
+ loginUser.setClientKey(client.getClientKey());
|
|
|
|
|
+ loginUser.setDeviceType(client.getDeviceType());
|
|
|
|
|
+ SaLoginModel model = new SaLoginModel();
|
|
|
|
|
+ model.setDevice(client.getDeviceType());
|
|
|
|
|
+ model.setTimeout(client.getTimeout());
|
|
|
|
|
+ model.setActiveTimeout(client.getActiveTimeout());
|
|
|
|
|
+ model.setExtra(LoginHelper.CLIENT_KEY, client.getClientId());
|
|
|
|
|
+ // 生成token
|
|
|
|
|
+ LoginHelper.login(loginUser, model);
|
|
|
|
|
+
|
|
|
|
|
+ LoginVo loginVo = new LoginVo();
|
|
|
|
|
+ loginVo.setAccessToken(StpUtil.getTokenValue());
|
|
|
|
|
+ loginVo.setExpireIn(StpUtil.getTokenTimeout());
|
|
|
|
|
+ loginVo.setClientId(client.getClientId());
|
|
|
|
|
+
|
|
|
|
|
+ return R.ok(loginVo);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 登录页面租户下拉框
|
|
* 登录页面租户下拉框
|
|
|
*
|
|
*
|