From 4c61d5fd5c273cadffe9f20464b5341a23f4e60f Mon Sep 17 00:00:00 2001 From: 胡仁荣 <897853850@qq.com> Date: 星期五, 07 四月 2023 14:50:26 +0800 Subject: [PATCH] 直播 --- src/main/resources/application.properties | 3 src/main/java/com/qxueyou/scc/web/AuthorizeFilter.java | 6 src/main/java/com/qxueyou/scc/sys/action/LoginController.java | 40 ++++++ src/main/java/com/qxueyou/scc/teach/subject/service/impl/SubjectService.java | 1 src/main/java/com/qxueyou/scc/wx/service/IWechatService.java | 26 ++++ src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java | 133 ++++++++++++++++++++++ src/main/java/com/qxueyou/scc/wx/service/impl/WechatService.java | 120 ++++++++++++++++++++ 7 files changed, 323 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/qxueyou/scc/sys/action/LoginController.java b/src/main/java/com/qxueyou/scc/sys/action/LoginController.java index e0d5693..506ddd9 100644 --- a/src/main/java/com/qxueyou/scc/sys/action/LoginController.java +++ b/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"; + /** + * 灏忕▼搴廰ppID + */ + @Value("${wx.appId}") + private String appId; + + + /** + * 灏忕▼搴弒ecret + */ + @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({ diff --git a/src/main/java/com/qxueyou/scc/teach/subject/service/impl/SubjectService.java b/src/main/java/com/qxueyou/scc/teach/subject/service/impl/SubjectService.java index 9d9036f..4627751 100644 --- a/src/main/java/com/qxueyou/scc/teach/subject/service/impl/SubjectService.java +++ b/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)); diff --git a/src/main/java/com/qxueyou/scc/web/AuthorizeFilter.java b/src/main/java/com/qxueyou/scc/web/AuthorizeFilter.java index 9cb8070..8f8931d 100644 --- a/src/main/java/com/qxueyou/scc/web/AuthorizeFilter.java +++ b/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()); diff --git a/src/main/java/com/qxueyou/scc/wx/service/IWechatService.java b/src/main/java/com/qxueyou/scc/wx/service/IWechatService.java new file mode 100644 index 0000000..2cab94b --- /dev/null +++ b/src/main/java/com/qxueyou/scc/wx/service/IWechatService.java @@ -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); + +} diff --git a/src/main/java/com/qxueyou/scc/wx/service/impl/WechatService.java b/src/main/java/com/qxueyou/scc/wx/service/impl/WechatService.java new file mode 100644 index 0000000..dd7bb86 --- /dev/null +++ b/src/main/java/com/qxueyou/scc/wx/service/impl/WechatService.java @@ -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; + + /** + * 灏忕▼搴廰ppID + */ + @Value("${wx.appId}") + private String appId; + + + /** + * 灏忕▼搴弒ecret + */ + @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 + //灏忕▼搴廰ppId + requestUrlParam.put("appid", appId); + //灏忕▼搴弒ecret + requestUrlParam.put("secret", secret); + //灏忕▼搴忕杩斿洖鐨刢ode + requestUrlParam.put("js_code", code); + //榛樿鍙傛暟 + requestUrlParam.put("grant_type", "authorization_code"); + //鍙戦�乸ost璇锋眰璇诲彇璋冪敤寰俊鎺ュ彛鑾峰彇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(); +// } + + +} diff --git a/src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java b/src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java new file mode 100644 index 0000000..faab644 --- /dev/null +++ b/src/main/java/com/qxueyou/scc/wx/utils/HttpClientUtil.java @@ -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; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 2718c45..1ec895b 100644 --- a/src/main/resources/application.properties +++ b/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 -- Gitblit v1.8.0