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 javax.validation.Valid; import com.alibaba.fastjson.JSON; import com.moral.api.entity.Allocation; import com.moral.api.pojo.query.app.AppAllocationPushUserCond; import com.moral.api.service.UserService; import com.moral.api.utils.WechatUtils; 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 parameters) { if (!(parameters.containsKey("account") && parameters.containsKey("password"))) { return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); } Map 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 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(); } @PostMapping("pushOneUser") @ApiOperation(value = "小程序推送消息") public ResultMessage pushOneUser(@Valid @RequestBody Allocation appAllocationPushUserCond){ userService.pushOneUser(appAllocationPushUserCond); // String body = push("oOCWi6-_hnzSvrMT8HX5D7Dz7tEA"); return ResultMessage.ok(); } 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=" + WechatUtils.getAccessToken(); // String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + HttpClientUtil.getAccessToken(); // String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" + HttpClientUtil.getAccessToken(); // String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + HttpClientUtil.getAccessToken(); //小程序客服 // String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + HttpClientUtil.getAccessToken(); //拼接推送的模版 WxMssVo wxMssVo = new WxMssVo(); wxMssVo.setTouser(openid);//用户的openid(要发送给那个用户,通常这里应该动态传进来的) wxMssVo.setTemplate_id("YNqUZ1MgMvwY3G-NENVbcmIBR5dUotSdnwcz96CWrho");//订阅消息模板id wxMssVo.setLang("zh_CN"); wxMssVo.setMiniprogramState("formal"); // wxMssVo.setPage("pages/index/index"); Map m = new HashMap<>(); HashMap map1 = new HashMap<>(); HashMap map2 = new HashMap<>(); HashMap map3 = new HashMap<>(); HashMap map4 = new HashMap<>(); map1.put("value","欧阳仑2"); map2.put("value","ouyanglun"); map3.put("value","巧克力1"); map4.put("value","2023-10-16"); m.put("thing18", map1); m.put("character_string1", map2); m.put("thing2", map3); m.put("date4", map4); wxMssVo.setData(JSON.toJSON(m)); ResponseEntity responseEntity = restTemplate.postForEntity(url, wxMssVo, String.class); return responseEntity.getBody(); } }