WebWXUserController.java 4.22 KB
package com.lhcredit.project.webbusiness.controller;

import com.alibaba.fastjson.JSONObject;
import com.lhcredit.common.utils.http.HttpUtil;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.project.business.frontUser.domain.FrontUser;
import com.lhcredit.project.business.frontUser.domain.FrontUserMon;
import com.lhcredit.project.business.frontUser.service.IFrontUserService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Objects;

/**
 * 用户信息对外接口
 *
 * @author lhcredit
 * @date 2023-11-28
 */
@RestController
@RequestMapping("/web/wxUser")
@RequiredArgsConstructor
@Slf4j
public class WebWXUserController {

    @Value(value = "${wx.appId}")
    private String appId;
    @Value(value = "${wx.path}")
    private String pathUrl;
    @Value(value = "${wx.secret}")
    private String secret;

    @Autowired
    private IFrontUserService frontUserService;

    /**
     * 第一步:用户同意授权,获取code
     *
     * @param response
     */
    @RequestMapping("/oauth")
    public void oauth(HttpServletResponse response) throws IOException {

        //    项目服务器url
        String path = pathUrl +"/";
        try {
            path = URLEncoder.encode(path, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }


        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                "appid=" + appId +
                "&redirect_uri=" + path +
                "&response_type=code" +
                "&scope=snsapi_userinfo" +
                "&state=comi" +
                "#wechat_redirect";
        log.info("url===" + url);
        response.sendRedirect(url);
    }


    /**
     * 第二步:用户同意授权,页面跳转至/invoke
     *
     * @param request
     */
    @RequestMapping("/invoke")
    public AjaxResult invoke(HttpServletRequest request) {
        String code = request.getParameter("code");
        log.info("code=====" + code);
        //通过code获取ACCESS_TOKEN

        //认证服务器
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
                "appid=" + appId +
                "&secret=" + secret +
                "&code=" + code +
                "&grant_type=authorization_code";

        String object = HttpUtil.doGet(url);
        JSONObject jsonObject = JSONObject.parseObject(object);
        log.info("jsonObject====" + jsonObject);
        String access_token = jsonObject.getString("access_token");
        String openid = jsonObject.getString("openid");

        //第三步:拉取用户信息
        String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?" +
                "access_token=" + access_token +
                "&openid=" + openid +
                "&lang=zh_CN";

        String userInfo = HttpUtil.doGet(userInfoUrl);
        JSONObject userJson = JSONObject.parseObject(userInfo);
        log.info("userJson=====" + userJson);
        String openid1 = userJson.getString("openid");
        AjaxResult frontUser = frontUserService.getFrontUserByOpenId(request,openid1);
        return AjaxResult.success(frontUser);
    }
    @RequestMapping("/login")
    public AjaxResult webLogin(HttpServletRequest request, @RequestBody FrontUserMon frontUserMon){
        HttpSession session = request.getSession(true);
        Object verObj=session.getAttribute("verCode");
        if (null==verObj){
            return AjaxResult.other(4003,"验证码过期");
        }
        String verCode =verObj.toString();
        String code = frontUserMon.getCode();
        if (!verCode.equalsIgnoreCase(code)) {
            return AjaxResult.loginError("验证码错误!");
        }
        return frontUserService.webWxLogin(request, frontUserMon);
    }

}