Java项目经验-小程序2
后台管理系统,若依框架
Java实现http请求
get请求
CloseableHttpClient client = HttpClients.createDefault(); HttpGet request = new HttpGet(url); request.setConfig(requestConfig);
CloseableHttpResponse response = client.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if(statusCode == HttpStatus.SC_OK){ HttpEntity entity = response.getEntity(); String responseContent = EntityUtils.toString(entity); JSONObject jsonObj = new JSONObject(responseContent); return jsonObj.get("access_token").toString();
post请求
CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); StringEntity requestEntity = new StringEntity(JSONObject.toJSONString(requestParam)); requestEntity.setContentEncoding("utf-8"); requestEntity.setContentType("json"); httpPost.setEntity(requestEntity); httpPost.setConfig(requestConfig);
CloseableHttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if(statusCode == HttpStatus.SC_OK){ HttpEntity responseEntity = response.getEntity(); return EntityUtils.toString(responseEntity);
建立http请求就能在代码里调用三方接口,从而对返回值操作
比如,微信开发文档,可以免费生成小程序码,需要配置信息,有兴趣可自行了解。
关于实现常见应用上的评论功能,一级评论,二级评论,要父id和根id,可以回复的评论,一般根据时间或者点赞数,抖音除外,抖音的排序规则太奇怪了
删除评论之后,怎么做到回复的指向还存在的,如果删除的存在不展示,那是怎么做分页的。这个需要以后研究了。
评论和弹幕都要做敏感词处理
//获取输入的String类型评论 String words = wxCourseDiscuss.getDiscuss(); log.info("获取当前提交的评论为:"+words); //调用工具类对评论进行过滤 List flag = SensitiveWordUtil.checkTxt(words); log.info("获取当前提交的评论是否含有敏感词:"+flag); if(flag.size()>0){ //处理敏感字符
words = SensitiveWordUtil.filterTxt(words); log.info("敏感词过滤后的评论为:"+words); }
其中的工具类
package com.dcone.wechat.utils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.compress.utils.IOUtils; import org.springframework.core.io.ClassPathResource; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.util.*; @Slf4j public class SensitiveWordUtil { private static final String fileName = "CensorWords.txt"; /** * 敏感词集合 */ public static HashMap sensitiveWordMap; /** * 初始化敏感词库,构建DFA算法模型 */ public static void initContext() { HashSet<String> set = new HashSet<String>(); try { byte[] bytes; ClassPathResource classPathResource = new ClassPathResource(fileName); //获取文件流 InputStream keyStream = classPathResource.getInputStream(); bytes = IOUtils.toByteArray(keyStream); keyStream.read(bytes); keyStream.close(); ByteArrayInputStream certBis = new ByteArrayInputStream(bytes); InputStreamReader input = new InputStreamReader(certBis); BufferedReader bf = new BufferedReader(input); String line = null; StringBuilder sb = new StringBuilder(); while((line=bf.readLine()) != null){ set.add(line); } initSensitiveWordMap(set); } catch (Exception e) { log.error("<<<<<<解析敏感词文件报错!"); e.printStackTrace(); } } /** * 初始化敏感词库,构建DFA算法模型 * @param sensitiveWordSet 敏感词库 */ private static void initSensitiveWordMap(Set<String> sensitiveWordSet) { //初始化敏感词容器,减少扩容操作 sensitiveWordMap = new HashMap<String,String>(sensitiveWordSet.size()); Map<Object,Object> temp; Map<Object,Object> newWorMap; //遍历sensitiveWordSet for(String key:sensitiveWordSet) { temp = sensitiveWordMap; for (int i = 0; i < key.length(); i++) { //转换成char型 char keyChar = key.charAt(i); //库中获取关键字 Object wordMap = temp.get(keyChar); //如果存在该key,直接赋值,用于下一个循环获取 if (wordMap != null) { temp = (Map) wordMap; } else { //不存在则,则构建一个map,同时将isEnd设置为0,因为他不是最后一个 newWorMap = new HashMap<>(); //不是最后一个 newWorMap.put("isEnd", "0"); temp.put(keyChar, newWorMap); temp = newWorMap; } //最后一个 if (i == key.length() - 1) temp.put("isEnd", "1"); } } } /** * 判断文字是否包含敏感字符文本 * 若包含返回true,否则返回false */ public static boolean contains(String txt) { boolean flag = false; for (int i = 0; i < txt.length(); i++) { //判断是否包含敏感字符 int matchFlag = checkSensitiveWord(txt, i); //大于0存在,返回true if (matchFlag > 0) { flag = true; } } return flag; } /** * 检查文字中是否包含敏感字符,检查规则如下: * @param txt * @param beginIndex * @return 如果存在,则返回敏感词字符的长度,不存在返回0 */ private static int checkSensitiveWord(String txt, int beginIndex) { //敏感词结束标识位:用于敏感词只有1位的情况 boolean flag = false; //匹配标识数默认为0 int matchFlag = 0; char word; Map nowMap = sensitiveWordMap; for (int i = beginIndex; i < txt.length(); i++) { word = txt.charAt(i); //获取指定key nowMap = (Map) nowMap.get(word); //存在,则判断是否为最后一个 if (nowMap != null) { //找到相应key,匹配标识+1 matchFlag++; //如果为最后一个匹配规则,结束循环,返回匹配标识数 if ("1".equals(nowMap.get("isEnd"))) { //结束标志位为true flag = true; } } else {//不存在,直接返回 break; } } //长度必须大于等于1,为词 if (matchFlag < 2 || !flag) { matchFlag = 0; } return matchFlag; } /** * 获取文字中的敏感词 * txt文字 */ public static List getSensitiveWord(String txt) { List sensitiveWordList = new ArrayList(); for (int i = 0; i < txt.length(); i++) { //判断是否包含敏感字符 int length = checkSensitiveWord(txt, i); //存在,加入list中 if (length > 0) { sensitiveWordList.add(txt.substring(i, i + length)); //减1的原因,是因为for会自增 i = i + length - 1; } } return sensitiveWordList; } /** * context是要校验的内容。返回结果是list,为空说明没有敏感词 * @param context * @return */ public static List checkTxt(String context) { initContext(); //包含敏感词返回所有敏感词数据 return getSensitiveWord(context); } public static String filterTxt(String context){ initContext(); //过滤掉所有敏感数据,用“*”代替 List<String> res = getSensitiveWord(context); for(String word:res){ context = context.replace(word,"**"); } return context; } }
其中txt文件即为敏感词库,这里不能上传。。
订单逻辑