智慧保安后台管理-外网项目备份
zhongrj
2023-11-14 2d7fc00f4c1b39f45ab4eb1d42582c45d6cad921
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
/*
 *      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 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.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
 * @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){
            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;
    }
 
}