| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | 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; |
| | | |
| | |
| | | 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("CFeSWarQLMPyPjwmiy6AV4eB-IZcipu48V8bFLkBzTU");//订阅消息模板id |
| | | wxMssVo.setPage("pages/index/index"); |
| | | |
| | | Map<String, String> m = new HashMap<>(3); |
| | | m.put("thing1", "小程序入门课程"); |
| | | m.put("thing6", "杭州浙江大学"); |
| | | m.put("thing7", "第一章第一节"); |
| | | wxMssVo.setData(m); |
| | | ResponseEntity<String> responseEntity = |
| | | restTemplate.postForEntity(url, wxMssVo, String.class); |
| | | return responseEntity.getBody(); |
| | | } |
| | | |
| | | } |