jiangqs
2023-07-11 7109e2fefaa46caffcd36b44828f48e0f8a790ba
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
package com.ruoyi.auth.controller;
 
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.auth.service.QywxInnerService;
import com.ruoyi.auth.service.SysLoginService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
 
/**
 * 企业微信H5应用专用
 */
@RestController
@Api(value = "企业微信授权信息获取", tags = "企业微信授权信息获取")
public class QwH5Controller {
 
    @Autowired
    private QywxInnerService qywxInnerService;
 
    @Autowired
    private SysLoginService sysLoginService;
 
    @Autowired
    private TokenService tokenService;
 
    @Value("${h5.redirectUriBase}")
    private String redirectUriBase;
 
 
    /**
     * 构造网页授权链接
     * @param redirectUri
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping({"/h5/oauthUrl"})
    @ApiOperation("构造网页授权链接")
    public R<OauthUrlVo> oauthUrl(@ApiParam(value = "授权重定向地址", required = true) @RequestParam("redirectUri") String redirectUri) throws UnsupportedEncodingException {
        // 普通应用
        redirectUri = redirectUriBase + redirectUri;
        String oauthRedirectUrl =  URLEncoder.encode(redirectUri,"utf-8");
        String oauthUrl = qywxInnerService.getOauthUrl(oauthRedirectUrl);
        return R.ok(new OauthUrlVo(oauthUrl));
    }
 
 
    @GetMapping("/h5/oauthUser")
    @ApiOperation("通过code获取访问用户登录")
    public R<OauthUserVo> oauthUser(@ApiParam(value = "成员授权获取到的code", required = true) @RequestParam("code") String code) throws IOException {
        // 通过code获取访问用户敏感信息
        JSONObject result = qywxInnerService.getOauthUser(code);
        QwUserDetailDto qwUserDetail = JSONObject.parseObject(result.toJSONString(), QwUserDetailDto.class);
        //QwUserDetailDto qwUserDetail = new QwUserDetailDto();
        //qwUserDetail.setMobile("13882237106");
        //qwUserDetail.setUserid("146");
        // 1、查数据库获取人员
        QwH5LoginVo qwH5LoginVo = sysLoginService.qwH5Login(qwUserDetail);
        // 2、生成Token
       return R.ok(tokenService.createQwH5Token(qwH5LoginVo));
    }
 
    @GetMapping("/h5/getAgentConfig")
    @ApiOperation("通过code获取访问用户登录")
    public R<AgentConfigVo> getAgentConfig(@ApiParam(value = "url", required = true) String url, @ApiParam(value = "type", required = false) String type) throws IOException {
        AgentConfigVo agentConfigVo = qywxInnerService.getAgentConfig(url, type);
        return R.ok(agentConfigVo);
    }
 
 
}