HotelUserController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.chuanghai.ihotel.controller;
  2. import com.chuanghai.ihotel.anno.UserLoginCheck;
  3. import com.chuanghai.ihotel.aop.LoginCheckAspect;
  4. import com.chuanghai.ihotel.common.utils.CommonResult;
  5. import com.chuanghai.ihotel.config.PayConfig;
  6. import com.chuanghai.ihotel.dto.LoginUserDTO;
  7. import com.chuanghai.ihotel.entity.HotelUserEntity;
  8. import com.chuanghai.ihotel.service.HotelUserService;
  9. import com.chuanghai.ihotel.vo.LoginUserVO;
  10. import com.fasterxml.jackson.databind.JsonNode;
  11. import com.fasterxml.jackson.databind.ObjectMapper;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.http.converter.StringHttpMessageConverter;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.web.bind.annotation.GetMapping;
  18. import org.springframework.web.bind.annotation.RequestHeader;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.ResponseBody;
  21. import org.springframework.web.client.RestTemplate;
  22. import java.io.UnsupportedEncodingException;
  23. import java.net.URLEncoder;
  24. import java.nio.charset.StandardCharsets;
  25. /**
  26. * 普通用户
  27. *
  28. * @author codingliang
  29. * @email codingliang@gmail.com
  30. * @date 2022-07-27 10:02:04
  31. */
  32. @Controller
  33. @RequestMapping("hotelUser")
  34. public class HotelUserController {
  35. @Value("${font-end-url}")
  36. private String fontEndUrl;
  37. @Autowired
  38. private HotelUserService hotelUserService;
  39. @Autowired
  40. PayConfig payConfig;
  41. /**
  42. * 微校授权回调地址
  43. *
  44. * @apiNote 用户跳转到微校授权链接上,微校服务器接收用户授权后会携带wxcode回调到该接口上,该接口使用wxcode获取到用户的信息。
  45. * 如果用户信息获取成功,该接口会以 【fontEndUrl + /?token=】 的形式跳回到前端应用页面;
  46. * 如果用户信息获取失败,该接口会以 【fontEndUrl + /error/?errorMsg=获取用户信息失败】 的形式跳回到前端应用页面。
  47. * <strong>注意:</strong>
  48. * fontEndUrl + 路由地址:前端项目地址+页面路由地址,用于前端页面接收用户的信息或错误信息,该地址需要前端人员与后台开发人员协调好。
  49. * </p>
  50. * @param wxcode 微校code
  51. * @param state 透传数据
  52. * @return
  53. */
  54. @GetMapping("weixiaoAuth")
  55. public String weixiaoAuth(String wxcode, String state) throws UnsupportedEncodingException {
  56. System.out.println("微校code = " + wxcode);
  57. LoginUserVO loginUserVO = hotelUserService.weixiaoAuth(wxcode, state);
  58. if (loginUserVO != null) {
  59. return "redirect:" + fontEndUrl + "/?token=" + loginUserVO.getToken() + "&tokenTtl=" + loginUserVO.getTokenTtl();
  60. } else {
  61. return "redirect:" + fontEndUrl + "/#/error/?errorMsg=" + URLEncoder.encode("获取用户信息失败", "utf-8");
  62. }
  63. }
  64. @GetMapping("weixinAuth")
  65. public String weiximAuth(String code) throws UnsupportedEncodingException {
  66. System.out.println("微信code = " + code);
  67. System.out.println("appid = " + payConfig.getAppid());
  68. System.out.println("appkey = " + payConfig.getApiKey());
  69. // 通过wxcode获取openid
  70. String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
  71. "appid=" + payConfig.getAppid() +
  72. "&secret=" + payConfig.getApiKey() +
  73. "&code=" + code +
  74. "&grant_type=authorization_code";
  75. try {
  76. RestTemplate restTemplate = new RestTemplate();
  77. restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 解决回参中文乱码
  78. String response = restTemplate.getForObject(url, String.class);
  79. ObjectMapper mapper2 = new ObjectMapper();
  80. JsonNode jsonNode = mapper2.readTree(response);
  81. System.out.println("jsonNode = " + jsonNode);
  82. String openid = jsonNode.get("openid").toString();
  83. System.out.println("openid = " + openid);
  84. System.out.println("重定向路径=" + "redirect:"+ fontEndUrl+ "/#/" + "?openId=" + openid);
  85. return "redirect:"+ fontEndUrl+ "/#/" + "?openId=" + openid;
  86. }catch (Exception e){
  87. e.printStackTrace();
  88. return "redirect:" + fontEndUrl + "/error/?errorMsg=获取用户openId失败";
  89. }
  90. }
  91. /**
  92. * 获取用户信息
  93. * @param userToken 用户token
  94. * @return
  95. */
  96. @UserLoginCheck
  97. @ResponseBody
  98. @GetMapping("userInfo")
  99. public CommonResult<HotelUserEntity> getUserInfoByToken(@RequestHeader("user_token") String userToken) {
  100. LoginUserDTO dto = LoginCheckAspect.threadLocal.get();
  101. HotelUserEntity vo = hotelUserService.findByCardNumber(dto.getCardNumber());
  102. return CommonResult.ok().setResult(vo);
  103. }
  104. }