Compare commits
4 Commits
4c393e1ac1
...
ef722e04b1
Author | SHA1 | Date |
---|---|---|
胡锐 | ef722e04b1 | 3 weeks ago |
胡锐 | 2c881b7615 | 3 weeks ago |
胡锐 | d40df80773 | 3 weeks ago |
胡锐 | 973571eb35 | 3 weeks ago |
@ -0,0 +1,118 @@ |
|||||||
|
package com.hfkj.controller; |
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject; |
||||||
|
import com.alipay.api.request.AlipaySystemOauthTokenRequest; |
||||||
|
import com.alipay.api.request.AlipayUserInfoShareRequest; |
||||||
|
import com.alipay.api.response.AlipaySystemOauthTokenResponse; |
||||||
|
import com.alipay.api.response.AlipayUserInfoShareResponse; |
||||||
|
import com.hfkj.common.alipay.AlipayUtils; |
||||||
|
import com.hfkj.common.exception.ErrorCode; |
||||||
|
import com.hfkj.common.exception.ErrorHelp; |
||||||
|
import com.hfkj.common.exception.SysCode; |
||||||
|
import com.hfkj.common.security.UserCenter; |
||||||
|
import com.hfkj.common.utils.ResponseMsgUtil; |
||||||
|
import com.hfkj.entity.BsUserPlatformAuthorize; |
||||||
|
import com.hfkj.model.ResponseData; |
||||||
|
import com.hfkj.model.SecUserSessionObject; |
||||||
|
import com.hfkj.model.UserSessionObject; |
||||||
|
import com.hfkj.service.user.BsUserGradeService; |
||||||
|
import com.hfkj.service.user.BsUserParentRelService; |
||||||
|
import com.hfkj.service.user.BsUserPlatformAuthorizeService; |
||||||
|
import com.hfkj.service.user.BsUserService; |
||||||
|
import com.hfkj.sysenum.user.UserAuthorizePlatformEnum; |
||||||
|
import io.swagger.annotations.Api; |
||||||
|
import io.swagger.annotations.ApiOperation; |
||||||
|
import org.apache.commons.lang3.StringUtils; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
import org.springframework.stereotype.Controller; |
||||||
|
import org.springframework.web.bind.annotation.*; |
||||||
|
|
||||||
|
import javax.annotation.Resource; |
||||||
|
|
||||||
|
/** |
||||||
|
* @className: CmsController |
||||||
|
* @author: HuRui |
||||||
|
* @date: 2024/9/24 |
||||||
|
**/ |
||||||
|
@Controller |
||||||
|
@RequestMapping(value = "/userAuth") |
||||||
|
@Api(value = "用户授权管理") |
||||||
|
public class UserAuthController { |
||||||
|
private static Logger log = LoggerFactory.getLogger(UserAuthController.class); |
||||||
|
@Resource |
||||||
|
private BsUserPlatformAuthorizeService userPlatformAuthorizeService; |
||||||
|
@Resource |
||||||
|
private UserCenter userCenter; |
||||||
|
|
||||||
|
@RequestMapping(value="/alipay",method = RequestMethod.POST) |
||||||
|
@ResponseBody |
||||||
|
@ApiOperation(value = "支付宝授权") |
||||||
|
public ResponseData alipay(@RequestBody JSONObject body) { |
||||||
|
try { |
||||||
|
UserSessionObject session = userCenter.getSessionModel(UserSessionObject.class); |
||||||
|
if (body == null || StringUtils.isBlank(body.getString("code"))) { |
||||||
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); |
||||||
|
} |
||||||
|
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest(); |
||||||
|
request.setGrantType("authorization_code"); |
||||||
|
request.setCode(body.getString("code")); |
||||||
|
AlipaySystemOauthTokenResponse response = AlipayUtils.initClient().execute(request); |
||||||
|
if(response.isSuccess()) { |
||||||
|
AlipayUserInfoShareRequest alipayUserInfoShareRequest = new AlipayUserInfoShareRequest(); |
||||||
|
AlipayUserInfoShareResponse alipayUserInfoShareResponse = AlipayUtils.initClient().execute(alipayUserInfoShareRequest,response.getAccessToken()); |
||||||
|
if(alipayUserInfoShareResponse.isSuccess()) { |
||||||
|
// 授权信息
|
||||||
|
BsUserPlatformAuthorize userPlatformAuthorize = userPlatformAuthorizeService.getUserAuth(session.getUser().getId(), UserAuthorizePlatformEnum.type1); |
||||||
|
if (userPlatformAuthorize == null) { |
||||||
|
userPlatformAuthorize = new BsUserPlatformAuthorize(); |
||||||
|
} |
||||||
|
userPlatformAuthorize.setUserId(session.getUser().getId()); |
||||||
|
userPlatformAuthorize.setPlatformCode(UserAuthorizePlatformEnum.type1.getType()); |
||||||
|
userPlatformAuthorize.setPlatformName(UserAuthorizePlatformEnum.type1.getName()); |
||||||
|
userPlatformAuthorize.setOpenId(alipayUserInfoShareResponse.getOpenId()); |
||||||
|
userPlatformAuthorize.setAvatar(alipayUserInfoShareResponse.getAvatar()); |
||||||
|
userPlatformAuthorize.setNickName(alipayUserInfoShareResponse.getNickName()); |
||||||
|
userPlatformAuthorizeService.edit(userPlatformAuthorize); |
||||||
|
return ResponseMsgUtil.success(userPlatformAuthorize); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "授权失败"); |
||||||
|
|
||||||
|
} catch (Exception e) { |
||||||
|
return ResponseMsgUtil.exception(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@RequestMapping(value="/getUserAuthDetail",method = RequestMethod.GET) |
||||||
|
@ResponseBody |
||||||
|
@ApiOperation(value = "支付宝授权详情") |
||||||
|
public ResponseData getUserAuthDetail(@RequestParam(value = "platform" , required = true) Integer platform) { |
||||||
|
try { |
||||||
|
UserSessionObject session = userCenter.getSessionModel(UserSessionObject.class); |
||||||
|
UserAuthorizePlatformEnum platformEnum = UserAuthorizePlatformEnum.getDataByType(platform); |
||||||
|
if (platformEnum == null) { |
||||||
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知平台"); |
||||||
|
} |
||||||
|
return ResponseMsgUtil.success( userPlatformAuthorizeService.getUserAuth(session.getUser().getId(), platformEnum)); |
||||||
|
|
||||||
|
} catch (Exception e) { |
||||||
|
return ResponseMsgUtil.exception(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@RequestMapping(value="/getUserAuthList",method = RequestMethod.GET) |
||||||
|
@ResponseBody |
||||||
|
@ApiOperation(value = "用户授权列表") |
||||||
|
public ResponseData getUserAuthList() { |
||||||
|
try { |
||||||
|
UserSessionObject session = userCenter.getSessionModel(UserSessionObject.class); |
||||||
|
return ResponseMsgUtil.success( userPlatformAuthorizeService.getUserAuth(session.getUser().getId())); |
||||||
|
|
||||||
|
} catch (Exception e) { |
||||||
|
return ResponseMsgUtil.exception(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue