WxController.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.hz.employmentsite.controller;
  2. import com.google.zxing.WriterException;
  3. import com.hz.employmentsite.filter.exception.BaseResponse;
  4. import com.hz.employmentsite.filter.exception.RespGenerstor;
  5. import com.hz.employmentsite.services.service.WechatService;
  6. import com.hz.employmentsite.util.QrCodeUtils;
  7. import lombok.extern.slf4j.Slf4j;
  8. import me.chanjar.weixin.common.error.WxErrorException;
  9. import org.apache.tomcat.util.buf.UDecoder;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.web.bind.annotation.*;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.io.ByteArrayOutputStream;
  14. import java.io.IOException;
  15. import java.io.OutputStream;
  16. import java.io.UnsupportedEncodingException;
  17. import java.net.URLDecoder;
  18. import java.util.Base64;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.UUID;
  22. @RestController
  23. @RequestMapping("/api/wx")
  24. @Slf4j
  25. public class WxController {
  26. /*@Autowired
  27. private RemoteHelper remoteHelper;
  28. @GetMapping("/getWxToken")
  29. public String getWxToken() {
  30. Map<String, String> params = new HashMap<>();
  31. params.put("grant_type","client_credential");
  32. params.put("appid","wx765e6ec59e26c288");
  33. params.put("secret","a44d8034671866e957484f5ed4e1df48");
  34. remoteHelper.getJson(params, "https://api.weixin.qq.com/cgi-bin/token",null);
  35. return "";
  36. }*/
  37. @Autowired
  38. private WechatService wechatService;
  39. @Autowired
  40. private QrCodeUtils qrCodeUtils;
  41. @GetMapping("getOAuthUrl")
  42. public BaseResponse<String> getOAuthUrl(String redirectUrl) throws UnsupportedEncodingException {
  43. String url = wechatService.getOAuthUrl(URLDecoder.decode(redirectUrl, "UTF-8"));
  44. log.info("getOAuthUrl:" + url);
  45. return RespGenerstor.success(url);
  46. }
  47. @GetMapping("getOpenId")
  48. public BaseResponse<String> getOpenId(String code) throws WxErrorException {
  49. return RespGenerstor.success(wechatService.getOpenId(code));
  50. }
  51. @PostMapping("/sentMsg")
  52. public BaseResponse<String> sentMsg() throws Exception {
  53. Map<String, String> data = new HashMap<>();
  54. data.put("thing28", "张三");
  55. data.put("phone_number34", "134556456456");
  56. data.put("thing10", "开发");
  57. return RespGenerstor.success(wechatService.sendMsg("ow5Mm61JAlqqNhZzcV9lLqTDnBCs", UUID.randomUUID().toString(), "http://www.bowintek.com/hzyz/mobile/index.html/#/jobUserInfo/index?userId=60ea0d5b-a75c-11ed-a6c5-7085c2a9999e",
  58. "-R2xeAeey7rOKN5VAuUr8cFMsJFlFp7ksN7m6H5_5VU", data));
  59. }
  60. @GetMapping("/getQRCodeUrlToBase64")
  61. public BaseResponse<String> getQRCodeUrlToBase64(@RequestParam("url") String url, @RequestParam("userId") String userId, @RequestParam(required = false) Integer margin) throws IOException, WriterException {
  62. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  63. String fullUrl = wechatService.getQrcodeUrl(URLDecoder.decode(url, "UTF-8"), userId);
  64. qrCodeUtils.getQrCode(bos, fullUrl, margin);
  65. byte[] bytes = bos.toByteArray();
  66. Base64.Encoder encoder = Base64.getEncoder();
  67. String base64String = encoder.encodeToString(bytes);
  68. bos.close();
  69. return RespGenerstor.success(base64String);
  70. }
  71. @GetMapping("qrcodeVerify")
  72. public BaseResponse<Boolean> qrcodeVerify(@RequestParam("qrCodeId") String qrCodeId, @RequestParam(required = false) String openId) {
  73. return RespGenerstor.success(wechatService.qrcodeVerify(qrCodeId, openId));
  74. }
  75. }