派生自 projectDept/qhighschool

胡仁荣
2023-04-07 4c61d5fd5c273cadffe9f20464b5341a23f4e60f
直播
4个文件已修改
3个文件已添加
329 ■■■■■ 已修改文件
src/main/java/com/qxueyou/scc/sys/action/LoginController.java 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/qxueyou/scc/teach/subject/service/impl/SubjectService.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/qxueyou/scc/web/AuthorizeFilter.java 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/qxueyou/scc/wx/service/IWechatService.java 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/qxueyou/scc/wx/service/impl/WechatService.java 120 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java 133 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.properties 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/qxueyou/scc/sys/action/LoginController.java
@@ -1,5 +1,6 @@
package com.qxueyou.scc.sys.action;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -9,10 +10,15 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.alibaba.fastjson.JSONObject;
import com.qxueyou.scc.base.util.*;
import com.qxueyou.scc.wx.service.IWechatService;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.network.LoginType;
import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@@ -95,7 +101,39 @@
    @Autowired
    private IOrganizationService organizationService;
    public static String UUNUMBER="QXYUUNUMBER";
    /**
     * 小程序appID
     */
    @Value("${wx.appId}")
    private String appId;
    /**
     * 小程序secret
     */
    @Value("${wx.secret}")
    private String secret;
    public static String UUNUMBER="QXYUUNUMBER";
    @Autowired
    IWechatService wechatService;
    /**
     * 微信登录
     *
     * @param uid
     * @throws IOException
     */
    @PostMapping("/auth/wechat/login")
    @ApiOperation("微信登录")
    public Result wechatLogin(String code) {
        JSONObject sessionKeyOrOpenId = wechatService.getSessionKeyOrOpenId(code);
        System.out.println(sessionKeyOrOpenId);
        return new Result(true,"cg",sessionKeyOrOpenId);
//        return new Result(true,"token",securityProperties.getTokenStartWith() + token,"user",user);
    }
    @ApiOperation(value = "登入接口", notes = "")
    @ApiImplicitParams({
src/main/java/com/qxueyou/scc/teach/subject/service/impl/SubjectService.java
@@ -607,7 +607,6 @@
            }
            subject.put("percent", new BigDecimal(percentAvg));
            subject.put("progressValue", new BigDecimal(progressValue));
            subject.put("lectureParentId", map.get("lectureParentId"));
            subject.put("lectureId", map.get("lectureId"));
            subject.put("unCommitExerciseCount", exerciseInfoService.getUnCommitExerciseCount(classId, userId));
src/main/java/com/qxueyou/scc/web/AuthorizeFilter.java
@@ -95,9 +95,9 @@
        //是否合法
//        if(!licenseService.isValid()) {
//            log.debug("license invalid");
//            forbiddenAsLicense(httpResponse);
//            return;
//        }
////            forbiddenAsLicense(httpResponse);
////            return;
////        }
        if (!addClientInfo(httpRequest)) {
            log.debug("非法请求:" + httpRequest.getRequestURI());
src/main/java/com/qxueyou/scc/wx/service/IWechatService.java
New file
@@ -0,0 +1,26 @@
package com.qxueyou.scc.wx.service;
import com.alibaba.fastjson.JSONObject;
import com.qxueyou.scc.teach.student.model.StuStudent;
import java.util.Map;
public interface IWechatService {
    /**
     * 查询用户
     * @param code
     * @param type
     * @return
     *
     * @author 李岩龙
     * @date 2021年4月2日 下午3:31:05
     */
    JSONObject getSessionKeyOrOpenId(String code);
//    Result isBindWx(String userId);
//    Result getSignature(String url);
}
src/main/java/com/qxueyou/scc/wx/service/impl/WechatService.java
New file
@@ -0,0 +1,120 @@
package com.qxueyou.scc.wx.service.impl;
import com.alibaba.druid.util.HttpClientUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qxueyou.scc.base.service.ICacheService;
import com.qxueyou.scc.teach.student.model.StuStudent;
import com.qxueyou.scc.wx.service.IWechatService;
import com.qxueyou.scc.wx.utils.HttpClientUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.net.URI;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@Service
public class WechatService implements IWechatService {
//    @Autowired
//    IUserService userService;
    /**
     * 小程序appID
     */
    @Value("${wx.appId}")
    private String appId;
    /**
     * 小程序secret
     */
    @Value("${wx.secret}")
    private String secret;
    @Autowired
    private ICacheService cacheService;
    private final Logger logger = LogManager.getLogger();
    @Override
    public JSONObject getSessionKeyOrOpenId(String code) {
        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, String> requestUrlParam = new HashMap<>();
        // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
        //小程序appId
        requestUrlParam.put("appid", appId);
        //小程序secret
        requestUrlParam.put("secret", secret);
        //小程序端返回的code
        requestUrlParam.put("js_code", code);
        //默认参数
        requestUrlParam.put("grant_type", "authorization_code");
        //发送post请求读取调用微信接口获取openid用户唯一标识
        JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl, requestUrlParam));
        return jsonObject;
    }
//    private String getWechatHeadImgUrl(String unionId) {
//        WechatUserDO wxUser = this.template.findOne(Query.query(condition().and("unionId").is(unionId)), WechatUserDO.class);
//        return wxUser.getHeadImgUrl();
//    }
//    @Override
//    public Result isBindWx(String userId) {
//        UserStudentDO studentDO = read(UserStudentDO.class, userId);
//        boolean status = false;
//        if (StringUtils.isNotBlank(studentDO.getUnionId())) {
//            status = true;
//        }
//        String url = "";
//        if (status) {
//            url = getWechatHeadImgUrl(studentDO.getUnionId());
//        }
//        return new Result(true, "status", status, "url", url);
//    }
//
//    public Result getSignature(String url) {
//        try {
//            long timestamp = System.currentTimeMillis() / 1000;
//            String randomStr = RandomUtils.getRandomStr();
//            //远程调用获取ticket
////            String jsapiTicket ="";//getJsapiTicket(false);
//            RestTemplate restTemplate = new RestTemplate();
//
//            String jsapiTicket = restTemplate.getForObject("http://192.168.1.111/job/ticket", String.class);
//
//            String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket,
//                    "noncestr=" + randomStr, "timestamp=" + timestamp, "url=" + url);
//            WxJsapiSignature jsapiSignature = new WxJsapiSignature();
//            jsapiSignature.setAppId(wxProperties.getAppId());
//            jsapiSignature.setTimestamp(timestamp);
//            jsapiSignature.setNonceStr(randomStr);
//            jsapiSignature.setUrl(url);
//            jsapiSignature.setSignature(signature);
//            return new Result(true, jsapiSignature);
//        } catch (Exception e) {
//            log.error(e, e);
//            return new Result(false, "获取签名失败");
//        }
//    }
//    @Override
//    public String getOpenId(String userId) {
//        UserStudentDO user = read(UserStudentDO.class, userId);
//
//        return user.getOpenId();
//    }
}
src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java
New file
@@ -0,0 +1,133 @@
package com.qxueyou.scc.wx.utils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
    public static String doGet(String url, Map<String, String> param) {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }
    public static String doGet(String url) {
        return doGet(url, null);
    }
    public static String doPost(String url, Map<String, String> param) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建参数列表
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
                    paramList.add(new BasicNameValuePair(key, param.get(key)));
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
                httpPost.setEntity(entity);
            }
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }
    public static String doPost(String url) {
        return doPost(url, null);
    }
    public static String doPostJson(String url, String json) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }
}
src/main/resources/application.properties
@@ -93,6 +93,7 @@
# default DefaultExpiration time : 30 minutes(600 seconds)
spring.redis.default.ttl=600
wx.appId=wx953bf2ed18e7836b
wx.secret=1d8b00beaddf5c8f36d1fedc14ef7973
logging.config=classpath:log4j2.xml