|
@@ -5,29 +5,42 @@ import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.lang.Validator;
|
|
import cn.hutool.core.lang.Validator;
|
|
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.annotation.Log;
|
|
import com.cyksj.common.annotation.Log;
|
|
|
import com.cyksj.common.constant.Constant;
|
|
import com.cyksj.common.constant.Constant;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
|
|
+import com.cyksj.common.snowflake.Sequence;
|
|
|
import com.cyksj.common.util.Codec;
|
|
import com.cyksj.common.util.Codec;
|
|
|
|
|
+import com.cyksj.common.util.Jsons;
|
|
|
|
|
+import com.cyksj.config.wxlogin.WxOpenPlatYHLXLoginConfig;
|
|
|
import com.cyksj.dto.Result;
|
|
import com.cyksj.dto.Result;
|
|
|
import com.cyksj.enums.BusinessType;
|
|
import com.cyksj.enums.BusinessType;
|
|
|
import com.cyksj.enums.GatewayApiCode;
|
|
import com.cyksj.enums.GatewayApiCode;
|
|
|
import com.cyksj.enums.GatewayResponse;
|
|
import com.cyksj.enums.GatewayResponse;
|
|
|
import com.cyksj.mapper.PhoneCodeMapper;
|
|
import com.cyksj.mapper.PhoneCodeMapper;
|
|
|
import com.cyksj.mapper.manage.cms.CmsUserMapper;
|
|
import com.cyksj.mapper.manage.cms.CmsUserMapper;
|
|
|
|
|
+import com.cyksj.model.dto.AuthRedirect;
|
|
|
|
|
+import com.cyksj.model.dto.AuthToken;
|
|
|
|
|
+import com.cyksj.model.dto.CpsAuthDto;
|
|
|
|
|
+import com.cyksj.model.dto.GzhOAuth2UserInfo;
|
|
|
import com.cyksj.model.entity.CmsUser;
|
|
import com.cyksj.model.entity.CmsUser;
|
|
|
import com.cyksj.model.entity.PhoneCode;
|
|
import com.cyksj.model.entity.PhoneCode;
|
|
|
import com.cyksj.model.manage.dto.AdminInfo;
|
|
import com.cyksj.model.manage.dto.AdminInfo;
|
|
|
|
|
+import com.cyksj.model.request.CmsUserBindWxReq;
|
|
|
import com.cyksj.model.request.CmsUserReq;
|
|
import com.cyksj.model.request.CmsUserReq;
|
|
|
import com.cyksj.model.views.CmsUserVO;
|
|
import com.cyksj.model.views.CmsUserVO;
|
|
|
|
|
+import com.cyksj.redis.RedisService;
|
|
|
|
|
+import com.cyksj.service.authorization.AuthorizationService;
|
|
|
import com.cyksj.service.mange.CmsAccountService;
|
|
import com.cyksj.service.mange.CmsAccountService;
|
|
|
import com.cyksj.service.mange.business.UserBusinessRateService;
|
|
import com.cyksj.service.mange.business.UserBusinessRateService;
|
|
|
import com.cyksj.service.mange.cms.CmsUserManager;
|
|
import com.cyksj.service.mange.cms.CmsUserManager;
|
|
|
import com.cyksj.service.mange.cms.manager.service.CacheService;
|
|
import com.cyksj.service.mange.cms.manager.service.CacheService;
|
|
|
|
|
+import com.cyksj.service.wechat.WeChatService;
|
|
|
import com.ejlchina.searcher.BeanSearcher;
|
|
import com.ejlchina.searcher.BeanSearcher;
|
|
|
import com.ejlchina.searcher.SearchResult;
|
|
import com.ejlchina.searcher.SearchResult;
|
|
|
import com.ejlchina.searcher.util.MapBuilder;
|
|
import com.ejlchina.searcher.util.MapBuilder;
|
|
@@ -42,7 +55,9 @@ import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
*项目名: netflix
|
|
*项目名: netflix
|
|
@@ -71,6 +86,16 @@ public class CmsAdminUserController {
|
|
|
|
|
|
|
|
private final UserBusinessRateService userBusinessRateService;
|
|
private final UserBusinessRateService userBusinessRateService;
|
|
|
|
|
|
|
|
|
|
+ private static final Sequence SEQUENCE = new Sequence(0);
|
|
|
|
|
+
|
|
|
|
|
+ private final RedisService redisService;
|
|
|
|
|
+
|
|
|
|
|
+ private final AuthorizationService authorizationService;
|
|
|
|
|
+
|
|
|
|
|
+ private final WxOpenPlatYHLXLoginConfig wxOpenPlatYHLXLoginConfig;
|
|
|
|
|
+
|
|
|
|
|
+ private final WeChatService weChatService;
|
|
|
|
|
+
|
|
|
@PostMapping(value = "/login")
|
|
@PostMapping(value = "/login")
|
|
|
public Result<String> login(@RequestBody CmsUser adminRequest) throws Exception {
|
|
public Result<String> login(@RequestBody CmsUser adminRequest) throws Exception {
|
|
|
CmsUser admin = cmsUserMapper.selectOne(
|
|
CmsUser admin = cmsUserMapper.selectOne(
|
|
@@ -313,4 +338,110 @@ public class CmsAdminUserController {
|
|
|
SearchResult<CmsUserVO> search = beanSearcher.search(CmsUserVO.class, builder.build());
|
|
SearchResult<CmsUserVO> search = beanSearcher.search(CmsUserVO.class, builder.build());
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult(search);
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult(search);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 银河后台微信授权登录
|
|
|
|
|
+ */
|
|
|
|
|
+ @GetMapping("/wx/authLogin")
|
|
|
|
|
+ public Result<CpsAuthDto> wxAuthorize(String redirectUrl) throws Exception {
|
|
|
|
|
+ CpsAuthDto cpsAuthDto = new CpsAuthDto();
|
|
|
|
|
+ Long mappingId = SEQUENCE.nextId();
|
|
|
|
|
+ String authState = mappingId.toString();
|
|
|
|
|
+ redisService.set(authState, authState, 60 * 60 * 24L);
|
|
|
|
|
+ AuthRedirect redirect = new AuthRedirect().setMappingId(mappingId);
|
|
|
|
|
+ redirect.setUrl(redirectUrl);
|
|
|
|
|
+ String state = Codec.DoBase64.custom().setData(Jsons.toJson(redirect)).encodeAsText();
|
|
|
|
|
+ String wxAuthUrl = authorizationService.createWxAuthLoginUrl(state, wxOpenPlatYHLXLoginConfig.getAppId(), wxOpenPlatYHLXLoginConfig.getManageRedirectUri());
|
|
|
|
|
+ cpsAuthDto.setAuthUrl(wxAuthUrl);
|
|
|
|
|
+ cpsAuthDto.setAuthState(authState);
|
|
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(cpsAuthDto);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 银河后台网页微信授权后跳转
|
|
|
|
|
+ */
|
|
|
|
|
+ @GetMapping("/wx/authLogin/baseRedirect")
|
|
|
|
|
+ public Result<String> wxAuthLoginRedirect(String code, String state, HttpServletResponse response) throws Exception {
|
|
|
|
|
+ if (StrUtil.isEmpty(state)) throw BusinessRuntimeException.getInstance("授权错误,请重新授权");
|
|
|
|
|
+ String json = Codec.DoBase64.custom().setData(state).decodeAsText();
|
|
|
|
|
+ AuthRedirect re = Jsons.parseObject(json, AuthRedirect.class);
|
|
|
|
|
+ AuthToken token = weChatService.getAuthToken(code, wxOpenPlatYHLXLoginConfig.getAppId());
|
|
|
|
|
+ GzhOAuth2UserInfo userinfo = weChatService.getUserInfo(token.getAccessToken(), token.getOpenid());
|
|
|
|
|
+ String unionId = userinfo.getUnionid();
|
|
|
|
|
+ String openId = userinfo.getOpenid();
|
|
|
|
|
+ CmsUser user = Optional.ofNullable(
|
|
|
|
|
+ cmsUserMapper.selectOne(new LambdaQueryWrapper<CmsUser>().eq(CmsUser::getUnionid, unionId)
|
|
|
|
|
+ .orderByAsc(CmsUser::getId)
|
|
|
|
|
+ .last("limit 1")))
|
|
|
|
|
+ .orElseGet(() -> cmsUserMapper.selectOne(new LambdaQueryWrapper<CmsUser>().eq(CmsUser::getOpenId, openId)
|
|
|
|
|
+ .orderByAsc(CmsUser::getId)
|
|
|
|
|
+ .last("limit 1")));
|
|
|
|
|
+
|
|
|
|
|
+ String url = re.getUrl();
|
|
|
|
|
+ if (user == null){
|
|
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
|
|
+ jsonObject.put("openid", openId);
|
|
|
|
|
+ jsonObject.put("unionid", unionId);
|
|
|
|
|
+ String bindWxKey = RandomUtil.randomString(10);
|
|
|
|
|
+ redisService.set("cms_bind_wx:" + bindWxKey, jsonObject.toString(), 12 * 60L);
|
|
|
|
|
+ response.sendRedirect(url.contains("?") ? url + "&bk=" + bindWxKey : url + "?bk=" + bindWxKey);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ StpUtil.login(user.getId());
|
|
|
|
|
+ String cmsToken = StpUtil.getTokenValue();
|
|
|
|
|
+ if (StrUtil.isNotBlank(url)) {
|
|
|
|
|
+ response.sendRedirect(url.contains("?") ? url + "&t=" + cmsToken : url + "?t=" + cmsToken);
|
|
|
|
|
+ }
|
|
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 绑定微信用户
|
|
|
|
|
+ */
|
|
|
|
|
+ @PostMapping("/bind/wx")
|
|
|
|
|
+ public Result<AdminInfo> bindWx(@RequestBody CmsUserBindWxReq bindWxReq) throws Exception {
|
|
|
|
|
+ String bindWxKey = bindWxReq.getBindWxKey();
|
|
|
|
|
+ Object o = redisService.get("cms_bind_wx:" + bindWxKey);
|
|
|
|
|
+ if (o == null) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("绑定操作已失效,请重新扫码");
|
|
|
|
|
+ }
|
|
|
|
|
+ String phone = bindWxReq.getPhone();
|
|
|
|
|
+ String code = bindWxReq.getCode();
|
|
|
|
|
+ Result<AdminInfo> loginInfo = getLoginInfo(phone, code);
|
|
|
|
|
+
|
|
|
|
|
+ //绑定
|
|
|
|
|
+ CmsUser admin = cmsUserMapper.selectOne(
|
|
|
|
|
+ new QueryWrapper<CmsUser>().lambda().eq(CmsUser::getPhone, phone).last("limit 1"));
|
|
|
|
|
+ JSONObject jsonObject = Jsons.parseObject(o.toString(), JSONObject.class);
|
|
|
|
|
+ admin.setUnionid(jsonObject.get("unionid").toString());
|
|
|
|
|
+ admin.setOpenId(jsonObject.get("openid").toString());
|
|
|
|
|
+ cmsUserMapper.updateById(admin);
|
|
|
|
|
+ return loginInfo;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 银河后台whoami
|
|
|
|
|
+ */
|
|
|
|
|
+ @GetMapping("/whoami")
|
|
|
|
|
+ public Result<AdminInfo> whoami() throws Exception {
|
|
|
|
|
+ Long adminId = StpUtil.getLoginIdAsLong();
|
|
|
|
|
+ CmsUser admin = cmsUserMapper.selectById(adminId);
|
|
|
|
|
+ if (null == admin) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("该用户不存在.");
|
|
|
|
|
+ }
|
|
|
|
|
+ String token = StpUtil.getTokenValue();
|
|
|
|
|
+ admin.setPassword("");
|
|
|
|
|
+
|
|
|
|
|
+ AdminInfo info = new AdminInfo();
|
|
|
|
|
+ info.setInfo2(admin);
|
|
|
|
|
+ info.setToken(token);
|
|
|
|
|
+ //缓存用户信息
|
|
|
|
|
+ cmsUserManager.loadUserRedisCache(admin);
|
|
|
|
|
+ //用户权限
|
|
|
|
|
+ info.setPermissions(cmsUserManager.getUserPermissions(admin.getId()));
|
|
|
|
|
+ //用户角色
|
|
|
|
|
+ info.setRoles(cmsUserManager.getUserRoles(admin.getId()));
|
|
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(info);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|