| | |
| | | */ |
| | | package org.springblade.modules.auth.granter; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import lombok.AllArgsConstructor; |
| | | import me.zhyd.oauth.model.AuthCallback; |
| | | import me.zhyd.oauth.model.AuthResponse; |
| | | import me.zhyd.oauth.model.AuthUser; |
| | | import me.zhyd.oauth.request.AuthRequest; |
| | | import org.springblade.common.cache.CacheNames; |
| | | import org.springblade.common.utils.HttpClientUtils; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.redis.cache.BladeRedis; |
| | | import org.springblade.core.tool.utils.DigestUtil; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.springblade.core.tool.utils.WebUtil; |
| | | import org.springblade.core.social.props.SocialProperties; |
| | | import org.springblade.core.social.utils.SocialUtil; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.utils.*; |
| | | import org.springblade.modules.auth.enums.UserEnum; |
| | | import org.springblade.modules.auth.provider.ITokenGranter; |
| | | import org.springblade.modules.auth.provider.TokenParameter; |
| | | import org.springblade.modules.auth.utils.TokenUtil; |
| | | import org.springblade.modules.system.entity.Tenant; |
| | | import org.springblade.modules.system.entity.UserInfo; |
| | | import org.springblade.modules.system.entity.UserOauth; |
| | | import org.springblade.modules.system.service.ITenantService; |
| | | import org.springblade.modules.system.service.IUserService; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * 微信小程序 TokenGranter |
| | |
| | | |
| | | private final IUserService userService; |
| | | |
| | | //获取wxAccessToken |
| | | private final String WX_GET_ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token"; |
| | | private final String WX_GET_PHONE_NUMBER = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"; |
| | | private final String WX_APP_ID = "wx11797b813d61cac1"; |
| | | private final String WX_SECRET = "c9bbdf03f4f98e2d8fc45aebd4e41eab"; |
| | | |
| | | @Override |
| | | public UserInfo grant(TokenParameter tokenParameter) { |
| | | HttpServletRequest request = WebUtil.getRequest(); |
| | | String tenantId = tokenParameter.getArgs().getStr("tenantId"); |
| | | String username = tokenParameter.getArgs().getStr("username"); |
| | | String code = tokenParameter.getArgs().getStr("code"); |
| | | // 校验,获取手机号 |
| | | String phone = "15170720695"; |
| | | // 返回 |
| | | return userService.userInfoByWx(tenantId, phone, UserEnum.WEB); |
| | | // 开放平台授权码 |
| | | String username = request.getParameter("username"); |
| | | String phone = getPhone(username); |
| | | // 判断用户是否存在 |
| | | UserInfo userInfo = userService.getUserByPhone(phone); |
| | | if (null==userInfo){ |
| | | throw new ServiceException("用户不存在!"); |
| | | }else { |
| | | //判断是否为保安员 |
| | | //判断是否已报名 |
| | | boolean flag = userService.getUserIsApply(userInfo.getUser().getId()); |
| | | if (!flag){ |
| | | throw new ServiceException("没有报名不能进行刷题!"); |
| | | } |
| | | } |
| | | // 返回UserInfo |
| | | return userInfo; |
| | | } |
| | | |
| | | /** |
| | | * 获取手机号 |
| | | * @param code |
| | | * @return |
| | | */ |
| | | public String getPhone(String code) { |
| | | String wxAccessToken = getWxAccessToken(); |
| | | //参数 |
| | | String url = WX_GET_PHONE_NUMBER + "?access_token=" + wxAccessToken; |
| | | //向微信接口发起请求获取手机号 |
| | | RestTemplate template = new RestTemplate(); |
| | | Map<String, Object> params = new LinkedHashMap<>(); |
| | | params.put("code", code); |
| | | String result = template.postForObject(url, params, String.class); |
| | | JSONObject resultObj = (JSONObject) JSONObject.parse(result); |
| | | JSONObject phoneInfo = (JSONObject) resultObj.get("phone_info"); |
| | | String phoneNumber = phoneInfo.get("phoneNumber").toString(); |
| | | return phoneNumber; |
| | | } |
| | | |
| | | /** |
| | | * 获取 WxAccessToken |
| | | * @return |
| | | */ |
| | | public String getWxAccessToken() { |
| | | //参数 |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("grant_type", "client_credential"); |
| | | params.put("appid", WX_APP_ID); |
| | | params.put("secret", WX_SECRET); |
| | | String result = HttpClientUtils.doGet(WX_GET_ACCESS_TOKEN, params); |
| | | JSONObject jsonObject = JSON.parseObject(result); |
| | | String accessToken = jsonObject.getString("access_token"); |
| | | return accessToken; |
| | | } |
| | | |
| | | } |