/*
|
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
|
*
|
* Redistribution and use in source and binary forms, with or without
|
* modification, are permitted provided that the following conditions are met:
|
*
|
* Redistributions of source code must retain the above copyright notice,
|
* this list of conditions and the following disclaimer.
|
* Redistributions in binary form must reproduce the above copyright
|
* notice, this list of conditions and the following disclaimer in the
|
* documentation and/or other materials provided with the distribution.
|
* Neither the name of the dreamlu.net developer nor the names of its
|
* contributors may be used to endorse or promote products derived from
|
* this software without specific prior written permission.
|
* Author: Chill 庄骞 (smallchill@163.com)
|
*/
|
package org.springblade.modules.auth.granter;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
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.social.props.SocialProperties;
|
import org.springblade.core.social.utils.SocialUtil;
|
import org.springblade.core.tool.api.R;
|
import org.springblade.core.tool.support.Kv;
|
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.*;
|
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.*;
|
|
/**
|
* 微信小程序 TokenGranter
|
* @author zhongrj
|
*/
|
@Component
|
@AllArgsConstructor
|
public class WxTokenGranter implements ITokenGranter {
|
|
public static final String GRANT_TYPE = "wx";
|
|
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 username = request.getParameter("username");
|
String phone = getPhone(username);
|
// 判断用户是否存在
|
UserInfo userInfo = userService.getUserByPhone(phone);
|
if (null==userInfo){
|
User user = new User();
|
user.setPhone(phone);
|
UserInfo userInfo1 = buildUserInfo(user, UserEnum.WEB);
|
return userInfo1;
|
}else {
|
return userInfo;
|
}
|
// 返回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;
|
}
|
|
private UserInfo buildUserInfo(User user, UserEnum userEnum) {
|
if (ObjectUtil.isEmpty(user)) {
|
return null;
|
}
|
UserInfo userInfo = new UserInfo();
|
userInfo.setUser(user);
|
// 根据每个用户平台,建立对应的detail表,通过查询将结果集写入到detail字段
|
Kv detail = Kv.create().set("type", userEnum.getName());
|
if (userEnum == UserEnum.WEB) {
|
UserWeb userWeb = new UserWeb();
|
UserWeb query = userWeb.selectOne(Wrappers.<UserWeb>lambdaQuery().eq(UserWeb::getUserId, user.getId()));
|
if (ObjectUtil.isNotEmpty(query)) {
|
detail.set("ext", query.getUserExt());
|
}
|
} else if (userEnum == UserEnum.APP) {
|
UserApp userApp = new UserApp();
|
UserApp query = userApp.selectOne(Wrappers.<UserApp>lambdaQuery().eq(UserApp::getUserId, user.getId()));
|
if (ObjectUtil.isNotEmpty(query)) {
|
detail.set("ext", query.getUserExt());
|
}
|
} else {
|
UserOther userOther = new UserOther();
|
UserOther query = userOther.selectOne(Wrappers.<UserOther>lambdaQuery().eq(UserOther::getUserId, user.getId()));
|
if (ObjectUtil.isNotEmpty(query)) {
|
detail.set("ext", query.getUserExt());
|
}
|
}
|
userInfo.setDetail(detail);
|
return userInfo;
|
}
|
|
}
|