parent
02373d1178
commit
973571eb35
@ -0,0 +1,91 @@ |
||||
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.RequestBody; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestMethod; |
||||
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
||||
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); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue