张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
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
package com.thhy.usercore.modules.sys.login.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thhy.general.common.BasicResult;
import com.thhy.general.common.entity.SysUserDto;
import com.thhy.usercore.modules.sys.login.entity.LoginVO;
import com.thhy.usercore.modules.sys.login.entity.MpLoginVo;
import com.thhy.usercore.modules.sys.login.service.LoginService;
import com.thhy.usercore.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("mpLogin")
public class MPController {
 
    @Autowired
    private LoginService loginService;
 
    @PostMapping
    public BasicResult login(@RequestBody SysUserDto sysUserDto){
        MpLoginVo loginVO = loginService.mpLogin(sysUserDto);
        return BasicResult.success(loginVO);
    }
 
    @Value("${wemp.appId}")
    private String appId;
 
    @Value("${wemp.secret}")
    private String secret;
 
    /**
     * 获取OPENID
     * @param jscode
     * @return
     */
    @RequestMapping(value = "getOpenId")
    public BasicResult getOpenId(String jscode){
        String result = HttpUtils.get("https://api.weixin.qq.com/sns/jscode2session?appid="+appId+"&secret="+secret+"&js_code="+jscode+"&grant_type=authorization_code");
        JSONObject json = JSON.parseObject(result);
        if(json.containsKey("errcode")){
            return BasicResult.faild("9999",json.getString("errmsg"),null);
        }
        json.remove("session_key");
        return BasicResult.success(json);
    }
}