智慧保安后台管理-外网项目备份
钟日健
2026-06-01 62eb499b0c969f246d3245d1429a97da4de1ce28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 *      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;
    }
 
}