liuxiaolong
2019-05-06 f99bc8c6a1d10610373738edd7d0aa0181c81d99
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
package com.cloud.user.controller;
 
//import com.cloud.common.utils.AppUserUtil;
import com.cloud.model.sys.AppUser;
import com.cloud.model.sys.WechatUserInfo;
import com.cloud.user.service.TokenService;
import com.cloud.user.service.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;
 
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
 
@Slf4j
@RestController
@RequestMapping("/data/wechat")
public class WechatController {
 
    @Autowired
    private WechatService wechatService;
 
    @Autowired
    private TokenService tokenService;
 
    /**
     * 引导到授权
     *
     * @param app
     * @param request
     * @param toUrl   授权后,跳转的页面url,注意url要转义
     * @return
     */
    @GetMapping("/{app}")
    public RedirectView toWechatAuthorize(@PathVariable String app, HttpServletRequest request,
                                          @RequestParam String toUrl) throws UnsupportedEncodingException {
        String url = wechatService.getWechatAuthorizeUrl(app, request, toUrl);
 
        return new RedirectView(url);
    }
 
    /**
     * 授权后,微信跳转到此接口
     *
     * @return
     */
    @GetMapping(value = "/{app}/back", params = {"code", "state"})
    public RedirectView wechatBack(HttpServletRequest request, @PathVariable String app, String code, String state,
                                   @RequestParam String toUrl) {
        if (StringUtils.isBlank(code)) {
            throw new IllegalArgumentException("code不能为空");
        }
 
        if (StringUtils.isBlank(state)) {
            throw new IllegalArgumentException("state不能为空");
        }
 
        WechatUserInfo wechatUserInfo = wechatService.getWechatUserInfo(app, request, code, state);
 
        toUrl = wechatService.getToUrl(toUrl, wechatUserInfo);
 
        return new RedirectView(toUrl);
    }
 
    /**
     * 微信绑定用户
     *
     * @param tempCode
     * @param openid
     */
    @PostMapping(value = "/binding-user", params = {"tempCode", "openid"})
    public void bindingUser(String tempCode, String openid) {
        AppUser appUser = tokenService.currentUser();
        if (appUser == null) {
            throw new IllegalArgumentException("非法请求");
        }
 
        log.info("绑定微信和用户:{},{},{}", appUser, openid, tempCode);
        wechatService.bindingUser(appUser, tempCode, openid);
    }
 
    /**
     * 微信登陆校验
     *
     * @param tempCode
     * @param openid
     */
    @GetMapping(value = "/login-check", params = {"tempCode", "openid"})
    public void wechatLoginCheck(String tempCode, String openid) {
        wechatService.checkAndGetWechatUserInfo(tempCode, openid);
    }
}