jinpengyong
2023-10-31 69790994b403a61e92a20cef7451b7f087b50ad2
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
package com.moral.api.controller;
 
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
import java.util.HashMap;
import java.util.Map;
 
import com.moral.api.service.UserService;
 
import com.moral.api.utils.HttpClientUtil;
import com.moral.api.vo.WxMssVo;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
 
 
@Slf4j
@Api(tags = {"小程序用户管理"})
@RestController
@RequestMapping("/AppUser")
public class AppUserController {
 
    @Autowired
    private UserService userService;
 
    @ApiOperation(value = "登陆信息")
    @PostMapping("logins")
    public ResultMessage login(@RequestBody Map<String, Object> parameters) {
        if (!(parameters.containsKey("account") && parameters.containsKey("password"))) {
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        }
        Map<String, Object> result = userService.loginSmallRoutine(parameters);
        if (!result.containsKey("token")) {
            return ResultMessage.fail((int) result.get("code"), (String) result.get("msg"));
        }
        return ResultMessage.ok(result);
    }
 
 
    @GetMapping("/wx/login")
    @ApiOperation(value = "小程序登陆")
    public ResultMessage userLogin(@RequestParam(value = "code") String code
                        ) {
        Map<String, Object> result = userService.wxLogin(code);
        return ResultMessage.ok(result);
    }
 
    @GetMapping("/wx/exit")
    @ApiOperation(value = "小程序退出")
    public ResultMessage updateUserId(@RequestParam @ApiParam(value = "userId",name = "用户主键") Integer userId) {
        userService.updateUserId(userId);
        return ResultMessage.ok();
    }
 
    @GetMapping("pushOneUser")
    @ApiOperation(value = "小程序推送消息")
    public  ResultMessage pushOneUser(String openid){
        String body = push("oOCWi6yfVapaK25Jnkk7jKSbMLyw");
        return ResultMessage.ok(body);
    }
 
 
    public String push(String openid) {
        RestTemplate restTemplate = new RestTemplate();
        //这里简单起见我们每次都获取最新的access_token(时间开发中,应该在access_token快过期时再重新获取)
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + HttpClientUtil.getAccessToken();
        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户的openid(要发送给那个用户,通常这里应该动态传进来的)
        wxMssVo.setTemplate_id("sClgB85aEG1hgaiXpnJBopo5LJX6r91DUsyikOmUgv8");//订阅消息模板id
        wxMssVo.setPage("pages/index/index");
 
        Map<String, String> m = new HashMap<>(3);
        m.put("time2", "2023-10-23");
        m.put("thing3", "七星");
        m.put("thing7", "第一章第一节");
        m.put("character_string8", "第一章第一节");
        m.put("phrase10", "第一章第一节");
        wxMssVo.setData(m);
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        return responseEntity.getBody();
    }
 
}