| | |
| | | import com.alipay.api.request.AlipaySystemOauthTokenRequest; |
| | | import com.alipay.api.response.AlipaySystemOauthTokenResponse; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.member.VO.AlipayUser; |
| | | import com.ruoyi.system.api.domain.AppMiniLoginVO; |
| | | import com.ruoyi.system.api.domain.dto.AppMiniLoginDTO; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | |
| | | |
| | | @RequestMapping("/getalipayInfo") |
| | | @ApiOperation(value = "获取支付宝授权码") |
| | | public R<AlipayUser> getInfo(String auth_code) throws AlipayApiException { |
| | | public R<AppMiniLoginVO> getInfo(AppMiniLoginDTO appMiniLoginDto) { |
| | | //使用支付宝小程序的固定方法获取auth_code |
| | | if (auth_code == null || auth_code.length() == 0) { |
| | | if (appMiniLoginDto.getAuth_code()== null || appMiniLoginDto.getAuth_code().length() == 0) { |
| | | return R.fail("请求参数auth_code不能为空"); |
| | | } else { |
| | | AppMiniLoginVO user=new AppMiniLoginVO(); |
| | | //String serverUrl, String appId, String privateKey, String format,String charset, String alipayPublicKey, String signType |
| | | //实例化客户端 参数:正式环境URL,Appid,商户私钥 PKCS8格式,字符编码格式,字符格式,支付宝公钥,签名方式 |
| | | AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_BORDER_PROD,APP_ID_PROD, APP_PRIVATE_KEY, "json", "GBK", ALIPAY_PUBLIC_KEY, "RSA2"); |
| | |
| | | // 值为authorization_code时,代表用code换取 |
| | | request.setGrantType("authorization_code"); |
| | | //授权码,用户对应用授权后得到的 |
| | | request.setCode(auth_code); |
| | | request.setCode(appMiniLoginDto.getAuth_code()); |
| | | //这里使用execute方法 |
| | | AlipaySystemOauthTokenResponse response = alipayClient.execute(request); |
| | | AlipaySystemOauthTokenResponse response = null; |
| | | try { |
| | | response = alipayClient.execute(request); |
| | | } catch (AlipayApiException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | //刷新令牌,上次换取访问令牌时得到。见出参的refresh_token字段 |
| | | request.setRefreshToken(response.getAccessToken()); |
| | | //返回成功时 就将唯一标识返回 |
| | | if (response.isSuccess()) { |
| | | System.out.println("调用成功"); |
| | | //我这里只返回了一个字段给前端用 |
| | | AlipayUser user=new AlipayUser(); |
| | | user.setUserid(response.getUserId()); |
| | | String user1= response.getUserId(); |
| | | user.setZfbuserid(user1); |
| | | return R.ok(user); |
| | | } else { |
| | | return R.fail("调用失败"); |