| | |
| | | import com.alipay.api.response.AlipaySystemOauthTokenResponse; |
| | | import com.alipay.api.response.AlipayUserInfoShareResponse; |
| | | import com.alipay.api.response.AlipayUserUserinfoShareResponse; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.account.ali.Constant.AliConstant; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.lang.reflect.Method; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | |
| | | // 登录 |
| | | AlipaySystemOauthTokenResponse response = new AliAppletTools(aliProperties).login(appletUserLogin.getCode()); |
| | | if (response.isSuccess()) { |
| | | // 获取用户信息 |
| | | // AlipayUserInfoShareResponse userInfo = new AliAppletTools(aliProperties).getUserInfo(response.getAccessToken()); |
| | | String aliUnionId = firstNotBlank(readStringProperty(response, "getUnionId"), readStringProperty(response, "getUnion_id"), |
| | | getResponseBodyValue(response, "union_id"), getResponseBodyValue(response, "unionId")); |
| | | String aliUserId = firstNotBlank(response.getUserId(), getResponseBodyValue(response, "user_id"), getResponseBodyValue(response, "userId")); |
| | | log.info("支付宝授权换取用户标识成功,openId:{},userId:{},unionId:{}", response.getOpenId(), aliUserId, aliUnionId); |
| | | AlipayUserInfoShareResponse userInfo = new AliAppletTools(aliProperties).getUserInfo(response.getAccessToken()); |
| | | log.info("支付宝用户信息接口返回:{}", JSON.toJSONString(userInfo)); |
| | | aliUserId = firstNotBlank(aliUserId, userInfo == null ? null : userInfo.getUserId(), |
| | | getResponseBodyValue(userInfo, "user_id"), getResponseBodyValue(userInfo, "userId")); |
| | | aliUnionId = firstNotBlank(aliUnionId, readStringProperty(userInfo, "getUnionId"), readStringProperty(userInfo, "getUnion_id"), |
| | | getResponseBodyValue(userInfo, "union_id"), getResponseBodyValue(userInfo, "unionId")); |
| | | if(StringUtils.isEmpty(appletUserLogin.getToken())){ |
| | | return AjaxResult.success("已拒绝授权"); |
| | | } |
| | |
| | | String phoneNumber = new AliAppletTools(aliProperties).getPhoneNumber(appletUserLogin.getToken()); |
| | | log.info("获取支付宝用户信息:{}", phoneNumber); |
| | | // 用户信息封装 |
| | | return AjaxResult.success(appUserService.aliLogin(response,phoneNumber,appletUserLogin.getInviteUserId())); |
| | | return AjaxResult.success(appUserService.aliLogin(response,phoneNumber,appletUserLogin.getInviteUserId(), |
| | | aliUserId, aliUnionId)); |
| | | } else { |
| | | String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response); |
| | | log.warn("诊断结果:{}",diagnosisUrl); |
| | |
| | | } |
| | | } |
| | | |
| | | private String getResponseBodyValue(Object response, String key) { |
| | | String body = readStringProperty(response, "getBody"); |
| | | if (StringUtils.isEmpty(body)) { |
| | | return null; |
| | | } |
| | | try { |
| | | com.alibaba.fastjson.JSONObject bodyJson = JSON.parseObject(body); |
| | | String value = bodyJson.getString(key); |
| | | if (StringUtils.isNotEmpty(value)) { |
| | | return value; |
| | | } |
| | | for (String nestedKey : bodyJson.keySet()) { |
| | | com.alibaba.fastjson.JSONObject nested = bodyJson.getJSONObject(nestedKey); |
| | | if (nested != null) { |
| | | value = nested.getString(key); |
| | | if (StringUtils.isNotEmpty(value)) { |
| | | return value; |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private String readStringProperty(Object target, String methodName) { |
| | | if (target == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | Method method = target.getClass().getMethod(methodName); |
| | | Object value = method.invoke(target); |
| | | return value == null ? null : String.valueOf(value); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private String firstNotBlank(String... values) { |
| | | if (values == null) { |
| | | return null; |
| | | } |
| | | for (String value : values) { |
| | | if (StringUtils.isNotEmpty(value)) { |
| | | return value; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | } |