| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package com.chuanghai.ihotel.controller;
- import com.chuanghai.ihotel.anno.UserLoginCheck;
- import com.chuanghai.ihotel.aop.LoginCheckAspect;
- import com.chuanghai.ihotel.common.utils.CommonResult;
- import com.chuanghai.ihotel.config.PayConfig;
- import com.chuanghai.ihotel.dto.LoginUserDTO;
- import com.chuanghai.ihotel.entity.HotelUserEntity;
- import com.chuanghai.ihotel.service.HotelUserService;
- import com.chuanghai.ihotel.vo.LoginUserVO;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.converter.StringHttpMessageConverter;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestHeader;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.client.RestTemplate;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- /**
- * 普通用户
- *
- * @author codingliang
- * @email codingliang@gmail.com
- * @date 2022-07-27 10:02:04
- */
- @Controller
- @RequestMapping("hotelUser")
- public class HotelUserController {
- @Value("${font-end-url}")
- private String fontEndUrl;
- @Autowired
- private HotelUserService hotelUserService;
- @Autowired
- PayConfig payConfig;
- /**
- * 微校授权回调地址
- *
- * @apiNote 用户跳转到微校授权链接上,微校服务器接收用户授权后会携带wxcode回调到该接口上,该接口使用wxcode获取到用户的信息。
- * 如果用户信息获取成功,该接口会以 【fontEndUrl + /?token=】 的形式跳回到前端应用页面;
- * 如果用户信息获取失败,该接口会以 【fontEndUrl + /error/?errorMsg=获取用户信息失败】 的形式跳回到前端应用页面。
- * <strong>注意:</strong>
- * fontEndUrl + 路由地址:前端项目地址+页面路由地址,用于前端页面接收用户的信息或错误信息,该地址需要前端人员与后台开发人员协调好。
- * </p>
- * @param wxcode 微校code
- * @param state 透传数据
- * @return
- */
- @GetMapping("weixiaoAuth")
- public String weixiaoAuth(String wxcode, String state) throws UnsupportedEncodingException {
- System.out.println("微校code = " + wxcode);
- LoginUserVO loginUserVO = hotelUserService.weixiaoAuth(wxcode, state);
- if (loginUserVO != null) {
- return "redirect:" + fontEndUrl + "/?token=" + loginUserVO.getToken() + "&tokenTtl=" + loginUserVO.getTokenTtl();
- } else {
- return "redirect:" + fontEndUrl + "/#/error/?errorMsg=" + URLEncoder.encode("获取用户信息失败", "utf-8");
- }
- }
- @GetMapping("weixinAuth")
- public String weiximAuth(String code) throws UnsupportedEncodingException {
- System.out.println("微信code = " + code);
- System.out.println("appid = " + payConfig.getAppid());
- System.out.println("appkey = " + payConfig.getApiKey());
- // 通过wxcode获取openid
- String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
- "appid=" + payConfig.getAppid() +
- "&secret=" + payConfig.getApiKey() +
- "&code=" + code +
- "&grant_type=authorization_code";
- try {
- RestTemplate restTemplate = new RestTemplate();
- restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 解决回参中文乱码
- String response = restTemplate.getForObject(url, String.class);
- ObjectMapper mapper2 = new ObjectMapper();
- JsonNode jsonNode = mapper2.readTree(response);
- System.out.println("jsonNode = " + jsonNode);
- String openid = jsonNode.get("openid").toString();
- System.out.println("openid = " + openid);
- System.out.println("重定向路径=" + "redirect:"+ fontEndUrl+ "/#/" + "?openId=" + openid);
- return "redirect:"+ fontEndUrl+ "/#/" + "?openId=" + openid;
- }catch (Exception e){
- e.printStackTrace();
- return "redirect:" + fontEndUrl + "/error/?errorMsg=获取用户openId失败";
- }
- }
- /**
- * 获取用户信息
- * @param userToken 用户token
- * @return
- */
- @UserLoginCheck
- @ResponseBody
- @GetMapping("userInfo")
- public CommonResult<HotelUserEntity> getUserInfoByToken(@RequestHeader("user_token") String userToken) {
- LoginUserDTO dto = LoginCheckAspect.threadLocal.get();
- HotelUserEntity vo = hotelUserService.findByCardNumber(dto.getCardNumber());
- return CommonResult.ok().setResult(vo);
- }
- }
|