package cn.mb.cloud.auth.security.social.handler; import cn.mb.cloud.auth.security.entity.User; import cn.mb.cloud.auth.security.service.IUserService; import cn.mb.cloud.common.api.vo.UserVO; import cn.mb.cloud.common.core.exception.ValidateCodeException; import cn.mb.cloud.common.core.util.WebUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.aliyun.api.gateway.demo.Client; import com.aliyun.api.gateway.demo.Request; import com.aliyun.api.gateway.demo.Response; import com.aliyun.api.gateway.demo.constant.Constants; import com.aliyun.api.gateway.demo.constant.ContentType; import com.aliyun.api.gateway.demo.constant.HttpHeader; import com.aliyun.api.gateway.demo.constant.HttpSchema; import com.aliyun.api.gateway.demo.enums.Method; import com.aliyun.api.gateway.demo.util.MessageDigestUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.AllArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; import java.util.UUID; @Slf4j @Component("OT") @AllArgsConstructor public class OneTouchLoginHandler extends AbstractLoginHandler { private final IUserService userService; /** * 友盟苹果key */ //private final String iOSKey = "5fe2e2181283d52b40b31b2f"; private final String iOSKey = "600a7d656a2a470e8f86e6ef"; /** * 友盟安卓key */ private final String androidKey = "5e09c1a50cafb2153b000f33"; /** * 阿里云认证服务Key */ private final String aliyunKey = "24600523"; /** * 阿里云认证服务Secret */ private final String aliyunSecret = "710e0107b022277a6bf4deafe9d6d971"; @Override public String identify(String code) { return code; } @Override @SneakyThrows public UserVO info(String identify, String threeAvatar, String threeName) { // 获取设备类型 0安卓 1苹果 String deviceType = WebUtils.getRequest().getHeader("DeviceType"); if (StringUtils.isEmpty(deviceType)) { log.info("未获取到设备类型"); throw new ValidateCodeException("未获取到设备类型"); } String mobile = null; JSONObject result = postString(identify, deviceType); if (result.containsKey("success")) { // 判断是否成功 if (result.getBoolean("success")) { JSONObject data = result.getJSONObject("data"); mobile = data.getString("mobile"); } else { log.info(result.getString("message")); throw new ValidateCodeException(result.getString("message")); } } User user = null; user = userService.getOne(Wrappers.query().lambda(). eq(User::getUsername, mobile). eq(User::getDelFlag, 0)); if (user == null) { /* log.info("一键登录未绑定:{}", identify); throw new ValidateCodeException("用户还未注册");*/ //注册 user = userService.getOne(Wrappers.query().lambda() .eq(User::getUsername, mobile) .eq(User::getDelFlag, 0) ); } UserVO userVO = new UserVO(); BeanUtils.copyProperties(user, userVO); //登录更新设备号,设备类型,版本号 return userVO; } public JSONObject postString(String token, String deviceType) throws Exception { String appKey = null; if (deviceType.equals("0")) { appKey = androidKey; } else { appKey = iOSKey; } //请求path String path = "/api/v1/mobile/info"; //Body内容 String body = "{\n" + "\t\"token\":\"" + token + "\"\n" + "}"; Map headers = new HashMap(); //(必填)根据期望的Response内容类型设置 headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "application/json"); //(可选)Body MD5,服务端会校验Body内容是否被篡改,建议Body非Form表单时添加此Header headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(body)); //(POST/PUT请求必选)请求Body内容格式 headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT); headers.put("X-Ca-Nonce", UUID.randomUUID().toString()); Request request = new Request(Method.POST_STRING, HttpSchema.HTTPS + "verify5.market.alicloudapi.com", path, aliyunKey, aliyunSecret, Constants.DEFAULT_TIMEOUT); request.setHeaders(headers); // request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX); //请求的query Map querys = new HashMap(); querys.put("appkey", appKey); request.setQuerys(querys); request.setStringBody(body); //调用服务端 Response response = Client.execute(request); String responseBody = response.getBody(); log.info("一键登录返回日志:{}", JSON.toJSONString(responseBody)); return JSONObject.parseObject(responseBody); } }