| | |
| | | package com.ruoyi.auction.service.impl; |
| | | |
| | | import cn.binarywang.wx.miniapp.api.WxMaService; |
| | | import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alipay.api.AlipayApiException; |
| | | import com.alipay.api.AlipayClient; |
| | | import com.alipay.api.DefaultAlipayClient; |
| | | 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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | |
| | | import com.ruoyi.auction.service.IAuctionVideoService; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.core.utils.page.PageDTO; |
| | | import com.ruoyi.auction.util.HttpUtils; |
| | | import com.ruoyi.system.api.constants.SecurityConstant; |
| | | import com.ruoyi.system.api.domain.AppMiniLoginVO; |
| | | import com.ruoyi.system.api.domain.Member; |
| | | import com.ruoyi.system.api.domain.SysUser; |
| | | import com.ruoyi.system.api.domain.dto.AppMiniLoginDTO; |
| | | import com.ruoyi.system.api.feignClient.SysUserClient; |
| | | import javax.annotation.Resource; |
| | | |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import static com.ruoyi.member.service.impl.MemberServiceImpl.getAccessTokenByWX; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Resource |
| | | private SysUserClient sysUserClient; |
| | | |
| | | @Resource |
| | | private WxMaService wxMaService; |
| | | |
| | | |
| | | //微信 |
| | | private static final String ACCESS_TOKEN_HOST = "https://api.weixin.qq.com/cgi-bin/token"; |
| | | |
| | | private static final String WX_APPID = "wxb7f0ea286fc4e535"; |
| | | |
| | | private static final String WX_SECRET = "852a2512a6ab559cafc68bae5d4160ac"; |
| | | |
| | | |
| | | //支付寶 |
| | | /** |
| | | * Alipay客户端 |
| | | */ |
| | | private AlipayClient alipayClient; |
| | | |
| | | /**支付宝网关*/ |
| | | /** |
| | | * 沙箱的 |
| | | */ |
| | | private static final String ALIPAY_BORDER_DEV = "https://openapi-sandbox.dl.alipaydev.com/gateway.do"; |
| | | /** |
| | | * 个人用户的 |
| | | */ |
| | | private static final String ALIPAY_BORDER_PROD = "https://openapi.alipay.com/gateway.do"; |
| | | /** |
| | | * appID |
| | | **/ |
| | | private static final String APP_ID_PROD = "2021003196653501"; |
| | | /** |
| | | * 私钥 |
| | | */ |
| | | private static final String APP_PRIVATE_KEY = ""; |
| | | /** |
| | | * 支付宝公钥 |
| | | */ |
| | | private static final String ALIPAY_PUBLIC_KEY = ""; |
| | | |
| | | @Override |
| | | public PageDTO<AuctionSalesroom> getAuctionBidRecordList(AuctionSalesroomGoodsDTO ationSalesroomGoodsDTO) { |
| | |
| | | AuctionSalesroom auctionBidRecord=iAuctionSalesroomService.getOne(wrapper); |
| | | return auctionBidRecord; |
| | | } |
| | | |
| | | @Override |
| | | public AppMiniLoginVO actionMiniLogin(AppMiniLoginDTO appMiniLoginDto) { |
| | | try { |
| | | AppMiniLoginVO appMiniLoginVo = new AppMiniLoginVO(); |
| | | if (appMiniLoginDto.getType()==1){ |
| | | if (appMiniLoginDto.getAuth_code()== null || appMiniLoginDto.getAuth_code().length() == 0) { |
| | | } 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"); |
| | | AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest(); |
| | | // 值为authorization_code时,代表用code换取 |
| | | request.setGrantType("authorization_code"); |
| | | //授权码,用户对应用授权后得到的 |
| | | request.setCode(appMiniLoginDto.getAuth_code()); |
| | | //这里使用execute方法 |
| | | 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("调用成功"); |
| | | //我这里只返回了一个字段给前端用 |
| | | String user1= response.getUserId(); |
| | | appMiniLoginVo.setZfbuserid(response.getUserId()); |
| | | AlipayUserInfoShareResponse alipayUserInfoShareResponse=getAliUserInfo(response.getAccessToken()); |
| | | appMiniLoginVo.setPhone(alipayUserInfoShareResponse.getPhone()); |
| | | R<SysUser> sysUserR = sysUserClient.queryUserByPhone(alipayUserInfoShareResponse.getPhone()); |
| | | SysUser data = sysUserR.getData(); |
| | | appMiniLoginVo.setSysUser(data); |
| | | } |
| | | } |
| | | }else{ |
| | | WxMaJscode2SessionResult session = null; |
| | | String unionid; |
| | | String openid; |
| | | String sessionKey = null; |
| | | //获取session |
| | | session = wxMaService.getUserService().getSessionInfo(appMiniLoginDto.getCode()); |
| | | if (session != null && StringUtils.isNotBlank(session.getOpenid())) { |
| | | unionid = session.getUnionid(); |
| | | openid = session.getOpenid(); |
| | | sessionKey = session.getSessionKey(); |
| | | |
| | | |
| | | String responseAccessToken = getAccessTokenByWX(); |
| | | |
| | | JSONObject jsonAccessToken = JSONObject.parseObject(responseAccessToken); |
| | | String accessToken = jsonAccessToken.getString("access_token"); |
| | | String errmsg = jsonAccessToken.getString("errmsg"); |
| | | Long expiresIn = jsonAccessToken.getLong("expires_in"); |
| | | if (StringUtils.isBlank(accessToken)) { |
| | | throw new ServiceException(errmsg); |
| | | } |
| | | String responseUserPhoneNumber = getMobileByWX(accessToken, appMiniLoginDto.getCode()); |
| | | JSONObject jsonUserPhoneNumber = JSONObject.parseObject(responseUserPhoneNumber); |
| | | |
| | | |
| | | String phoneInfo = jsonUserPhoneNumber.getString("phone_info"); |
| | | JSONObject jsonUserPhoneInfo = JSONObject.parseObject(phoneInfo); |
| | | String mobile = jsonUserPhoneInfo.getString("purePhoneNumber"); |
| | | //获取用户 |
| | | R<SysUser> sysUserR = sysUserClient.queryUserByPhone(mobile); |
| | | SysUser data = sysUserR.getData(); |
| | | appMiniLoginVo.setSysUser(data); |
| | | |
| | | appMiniLoginVo.setMiniOpenid(openid); |
| | | appMiniLoginVo.setWxUnionid(unionid); |
| | | appMiniLoginVo.setPhone(mobile); |
| | | appMiniLoginVo.setSessionKey(sessionKey); |
| | | } |
| | | } |
| | | return appMiniLoginVo; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private AlipayUserInfoShareResponse getAliUserInfo (String accessToken) throws Exception { |
| | | |
| | | AlipayClient alipayClient = new DefaultAlipayClient(ALIPAY_BORDER_PROD,APP_ID_PROD, APP_PRIVATE_KEY, "json", "GBK", ALIPAY_PUBLIC_KEY, "RSA2"); |
| | | AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest(); |
| | | AlipayUserInfoShareResponse response = alipayClient.execute(request, accessToken); |
| | | if(response.isSuccess()){ |
| | | System.out.println("获取会员信息 - 调用成功"); |
| | | return response; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static String getAccessTokenByWX() throws Exception { |
| | | String host = ACCESS_TOKEN_HOST + "?appid=" + WX_APPID + "&secret=" + WX_SECRET + "&grant_type=client_credential"; |
| | | Map<String, String> headers = new HashMap<>(8); |
| | | HttpResponse response = HttpUtils.doGet(host, "", "GET", headers, null); |
| | | return EntityUtils.toString(response.getEntity()); |
| | | } |
| | | |
| | | public static String getMobileByWX(String accessToken, String code) throws Exception { |
| | | Map<String, String> headers = new HashMap<>(16); |
| | | headers.put("Content-Type", "application/json"); |
| | | headers.put("Accept", "application/json"); |
| | | Map<String, String> querys = new HashMap<>(16); |
| | | Map<String, String> bodys = new HashMap<>(16); |
| | | querys.put("access_token", accessToken); |
| | | bodys.put("code", code); |
| | | String body = JSONObject.toJSONString(bodys); |
| | | String host = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"; |
| | | HttpResponse response = HttpUtils.doPost(host, "", "POST", headers, querys, body); |
| | | return EntityUtils.toString(response.getEntity()); |
| | | } |
| | | } |