wlzboy
2025-11-13 51b9df7d9e907506ce565fd47a7aa4661a4139ea
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
package com.ruoyi.web.controller.wechat;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.config.WechatConfig;
import com.ruoyi.common.utils.WechatUtils;
 
/**
 * 微信接口Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/wechat")
public class WechatController extends BaseController {
    
    @Autowired
    private WechatConfig wechatConfig;
    
    /**
     * 获取微信AccessToken
     */
    @GetMapping("/accessToken")
    public AjaxResult getAccessToken() {
        try {
            String accessToken = WechatUtils.getAccessToken(
                wechatConfig.getAppId(), 
                wechatConfig.getAppSecret()
            );
            if (accessToken == null || accessToken.isEmpty()) {
                return error("获取微信AccessToken失败");
            }
            return success(accessToken);
        } catch (Exception e) {
            return error("获取微信AccessToken异常:" + e.getMessage());
        }
    }
}