keyWord.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. $(function(){
  2. $(".enterOne").focus(function(){
  3. $(".btnChoose").show();
  4. });
  5. $('.knowBtn').on('click',function(){
  6. $(".problemPop").hide()
  7. });
  8. $(".problem").on('click',function(){
  9. $(".problemPop").css("display",'flex');
  10. });
  11. function hasWords () {
  12. var showKeyWordLength = $(".showKeyWord ul").find('li').length;
  13. if(showKeyWordLength === 0){
  14. $(".enter.addkeyWord").show();
  15. $(".addKeyWord").hide();
  16. $(".showKeyWord").hide();
  17. } else {
  18. $(".enter.addkeyWord").hide();
  19. $(".showKeyWord").show();
  20. $(".addKeyWord").show();
  21. }
  22. }
  23. hasWords();
  24. //添加按钮
  25. $(".addKeyWord i").on('click',function(){
  26. sessionStorage.removeItem("keyWord");
  27. window.location.href="/front/wx_dataExport/keyWordInput";
  28. });
  29. // 添加keyWords检查输入框内是否有文字,如果有才能点击添加按钮
  30. $('.addkeyWord input.enterOne').on('input', function() {
  31. var buttonDOM = $(this).siblings()[1].children[0];
  32. if ($(this).val().length >= 1) {
  33. buttonDOM.style.opacity = 1;
  34. buttonDOM.removeAttribute("disabled");
  35. //
  36. $(this).next().find(".appended").prop("disabled", false);
  37. $(this).next().find(".exclude").prop("disabled", false);
  38. } else {
  39. buttonDOM.style.opacity = .5;
  40. buttonDOM.setAttribute("disabled", true);
  41. //
  42. $(this).next().find(".appended").prop("disabled", true);
  43. $(this).next().find(".exclude").prop("disabled", true);
  44. }
  45. });
  46. $('textarea').each(function(i,dom){
  47. // console.log(i,dom)
  48. dom.style.height = dom.scrollHeight +'px';
  49. });
  50. $("textarea").on("input", function() {
  51. this.style.height = 'auto';
  52. this.style.height = this.scrollHeight + "px";
  53. });
  54. // 添加 按钮的点击事件
  55. $('.addkeyWord .btn .save').on('click', function(){
  56. var keyWord = $('.addkeyWord input.enterOne').val();
  57. keyWord = keyWord.replace(/\s/g,"");
  58. if (keyWord.length > 20) {
  59. // var s = keyWord.slice(0,19);
  60. // $('.addkeyWord input.enterOne').val(s)
  61. weui.toast('关键词不能超过20字', {
  62. duration: 2000,
  63. className: 'text-overflow100',
  64. callback: function(){}
  65. });
  66. return
  67. }
  68. var keyWordHtml = "";
  69. var html = `
  70. <li>
  71. <div class="one">
  72. <div>
  73. <span>
  74. <strong> 关键词:</strong>
  75. <p class="key">${ keyWord }</p>
  76. </span>
  77. </div>
  78. <button class="editKeyWord"><i class="iconfont icon-xiugai"></i> 修改</button>
  79. </div>
  80. <div class="modify">
  81. <textarea name="" rows="1" placeholder="" maxlength="">${ keyWord}</textarea>
  82. <button class="addAdjunctWord">编辑 附加词</button>
  83. <button class="addExclusion">添加 排除词</button>
  84. <button class="deleteKey">删除</button>
  85. <button class="ascertainKey">确定</button>
  86. </div>
  87. </li>`;
  88. if(sessionStorage.keyWord!==""&&sessionStorage.keyWord!==undefined){
  89. var keys = JSON.parse(sessionStorage.keyWord);
  90. for(var i in keys){
  91. var append = keys[i].appended;
  92. var exclude = keys[i].exclude;
  93. // keys[i].keyWord = keyWord;
  94. if(keys[i].keyWord === turn){
  95. if(keyWord !== turn){
  96. keys[i].keyWord = keyWord
  97. }
  98. keyWordArr.push(keys[i]);
  99. localStorage.setItem("keyWord", JSON.stringify(keyWordArr));
  100. //
  101. if(append !== undefined && exclude === undefined){
  102. keyWordHtml += `
  103. <li>
  104. <div class="one">
  105. <div>
  106. <span>
  107. <strong> 关键词:</strong>
  108. <p class="key">${ keys[i].keyWord }</p>
  109. </span>
  110. <span>
  111. <strong> 附加词:</strong>
  112. <p class="addition">${ append }</p>
  113. </span>
  114. </div>
  115. <button class="editKeyWord"><i class="iconfont icon-xiugai"></i> 修改</button>
  116. </div>
  117. <div class="modify">
  118. <textarea name="" rows="1" placeholder="" maxlength="">${ keys[i].keyWord}</textarea>
  119. <button class="addAdjunctWord">编辑 附加词</button>
  120. <button class="addExclusion">添加 排除词</button>
  121. <button class="deleteKey">删除</button>
  122. <button class="ascertainKey">确定</button>
  123. </div>
  124. </li>`;
  125. }else if(append === undefined && exclude !== undefined){
  126. keyWordHtml += `
  127. <li>
  128. <div class="one">
  129. <div>
  130. <span>
  131. <strong> 关键词:</strong>
  132. <p class="key">${ keys[i].keyWord }</p>
  133. </span>
  134. <span>
  135. <strong> 排除词:</strong>
  136. <p class="exclusion">${ exclude }</p>
  137. </span>
  138. </div>
  139. <button class="editKeyWord"><i class="iconfont icon-xiugai"></i> 修改</button>
  140. </div>
  141. <div class="modify">
  142. <textarea name="" rows="1" placeholder="" maxlength="">${ keys[i].keyWord}</textarea>
  143. <button class="addAdjunctWord">编辑 附加词</button>
  144. <button class="addExclusion">添加 排除词</button>
  145. <button class="deleteKey">删除</button>
  146. <button class="ascertainKey">确定</button>
  147. </div>
  148. </li>`;
  149. }else if(append !== undefined && exclude !== undefined){
  150. keyWordHtml += `
  151. <li>
  152. <div class="one">
  153. <div>
  154. <span>
  155. <strong> 关键词:</strong>
  156. <p class="key">${ keys[i].keyWord }</p>
  157. </span>
  158. <span>
  159. <strong> 附加词:</strong>
  160. <p class="addition">${ append }</p>
  161. </span>
  162. <span>
  163. <strong> 排除词:</strong>
  164. <p class="exclusion">${ exclude }</p>
  165. </span>
  166. </div>
  167. <button class="editKeyWord"><i class="iconfont icon-xiugai"></i> 修改</button>
  168. </div>
  169. <div class="modify">
  170. <textarea name="" rows="1" placeholder="" maxlength="">${ keys[i].keyWord}</textarea>
  171. <button class="addAdjunctWord">编辑 附加词</button>
  172. <button class="addExclusion">添加 排除词</button>
  173. <button class="deleteKey">删除</button>
  174. <button class="ascertainKey">确定</button>
  175. </div>
  176. </li>`;
  177. }else if(append === undefined && exclude === undefined){
  178. keyWordHtml += `
  179. <li>
  180. <div class="one">
  181. <div>
  182. <span>
  183. <strong> 关键词:</strong>
  184. <p class="key">${ keys[i].keyWord }</p>
  185. </span>
  186. </div>
  187. <button class="editKeyWord"><i class="iconfont icon-xiugai"></i> 修改</button>
  188. </div>
  189. <div class="modify">
  190. <textarea name="" rows="1" placeholder="" maxlength="">${ keys[i].keyWord}</textarea>
  191. <button class="addAdjunctWord">编辑 附加词</button>
  192. <button class="addExclusion">添加 排除词</button>
  193. <button class="deleteKey">删除</button>
  194. <button class="ascertainKey">确定</button>
  195. </div>
  196. </li>`;
  197. }
  198. keyWordHtml = keyWordHtml.replace(/,/g, " ");
  199. //
  200. $('.showKeyWord > ul').prepend(keyWordHtml);
  201. // 隐藏
  202. $(".enter.addkeyWord").hide();
  203. $(".showKeyWord").show();
  204. $(".addKeyWord").show();
  205. $('.enter.addkeyWord > input').val('');
  206. var buttonDOM = $('.enter.addkeyWord .btn button')[0];
  207. buttonDOM.style.opacity = .5;
  208. buttonDOM.setAttribute("disabled", true);
  209. return
  210. }
  211. }
  212. }
  213. var Obj = {"keyWord": keyWord};
  214. keyWordArr.push(Obj);
  215. localStorage.setItem("keyWord", JSON.stringify(keyWordArr));
  216. $('.showKeyWord > ul').prepend(html);
  217. // 隐藏
  218. $(".enter.addkeyWord").hide();
  219. $(".showKeyWord").show();
  220. $(".addKeyWord").show();
  221. $('.enter.addkeyWord > input').val('');
  222. var buttonDOM = $('.enter.addkeyWord .btn button')[0];
  223. buttonDOM.style.opacity = .5;
  224. buttonDOM.setAttribute("disabled", true);
  225. });
  226. // 编辑
  227. $(".showKeyWord").on('click', '.editKeyWord',function(e){
  228. let oSpan = $(this).parent().siblings().children('textarea');
  229. let val = $(oSpan).val();
  230. var keyWord = $(this).prev().children().find(".key").text();
  231. for(var i in keyWordArr){
  232. if(keyWordArr[i].keyWord === keyWord){
  233. var str = "";
  234. var strs = "";
  235. if(keyWordArr[i].appended !== undefined){
  236. var appended = keyWordArr[i].appended.length;
  237. if(appended < 1){
  238. str = "添加 附加词";
  239. }else{
  240. str = "编辑 附加词 ("+appended+")";
  241. }
  242. }else{
  243. str = "添加 附加词";
  244. }
  245. $(this).parent().next().find(".addAdjunctWord").text(str);
  246. if(keyWordArr[i].exclude !== undefined){
  247. var exclude = keyWordArr[i].exclude.length;
  248. if(exclude < 1){
  249. strs = "添加 排除词";
  250. }else{
  251. strs = "编辑 排除词 ("+exclude+")";
  252. }
  253. }else{
  254. strs = "添加 排除词";
  255. }
  256. $(this).parent().next().find(".addExclusion").text(strs);
  257. }
  258. }
  259. $(this).parent().hide();
  260. $(this).parent().siblings().show().parent().siblings().children('.modify').hide().siblings('.one').show()
  261. $(oSpan).val('').focus().val(val);
  262. $(".addKeyWord").hide();
  263. });
  264. //
  265. //防止键盘把当前输入框给挡住
  266. $('input[type="text"],textarea').focus(function () {
  267. var target = this;
  268. setTimeout(function(){
  269. target.scrollIntoViewIfNeeded();
  270. },400);
  271. });
  272. // 编辑删除
  273. $('.showKeyWord').on('click', '.deleteKey', function(e) {
  274. var jQueryDOM = $(this).parents('li');
  275. var keyWord = $(this).parent().prev().find('.key').text();
  276. weui.confirm('确定要删除关键词?', {
  277. buttons: [{
  278. label: '取消',
  279. type: 'default',
  280. onClick: function(){
  281. }
  282. }, {
  283. label: '确定',
  284. type: 'primary',
  285. onClick: function(){
  286. keyWordArr = keyWordArr.filter((e=>{return e.keyWord!==keyWord}));
  287. localStorage.keyWord = JSON.stringify(keyWordArr);
  288. jQueryDOM.remove();
  289. sessionStorage.removeItem("keyWord");
  290. $(".addKeyWord").show();
  291. $(".btnChoose").hide();
  292. $(".appended").text("添加 附加词");
  293. $(".exclude").text("添加 排除词");
  294. //
  295. $(".appended").prop("disabled", true);
  296. $(".exclude").prop("disabled", true);
  297. //
  298. hasWords()
  299. }
  300. }]
  301. });
  302. // console.log('删除关键词:',$(this).parent().find('span').text())
  303. })
  304. // 编辑确定
  305. $('.showKeyWord').on('click', '.ascertainKey', function(e) {
  306. var $this = $(this);
  307. // var keyWord = $(this).siblings('span').text();
  308. var keyWord = $(this).siblings('textarea').val();
  309. var keyWords = $(this).parent().prev().find('.key').text();
  310. keyWord = keyWord.replace(/\s/g,"");
  311. if ($(this).parent().find("textarea").val().length < 1) {
  312. weui.toast('关键词不能为空', {
  313. duration: 2000,
  314. className: 'text-overflow100',
  315. callback: function(){
  316. }
  317. });
  318. return
  319. }else if($(this).parent().find("textarea").val().length > 20){
  320. // var s = $(this).parent().find("span").text().slice(0,19);
  321. // $(this).parent().find("span").text(s);
  322. weui.toast('关键词不能超过20字', {
  323. duration: 2000,
  324. className: 'text-overflow100',
  325. callback: function(){}
  326. });
  327. return
  328. }else{
  329. for(var i in keyWordArr){
  330. if(keyWord !== keyWords){
  331. if(keyWordArr[i].keyWord === keyWord){
  332. weui.toast('关键词重复了', {
  333. duration: 2000,
  334. className: 'text-overflow100',
  335. callback: function(){}
  336. });
  337. return
  338. }
  339. }
  340. if(keyWordArr[i].keyWord === keyWords){
  341. keyWordArr[i].keyWord = keyWord;
  342. }
  343. }
  344. localStorage.keyWord = JSON.stringify(keyWordArr);
  345. $this.parent().siblings().find('.key').text(keyWord)
  346. $this.parent().hide().siblings().show()
  347. }
  348. $(".addKeyWord").show();
  349. })
  350. //去空格方法
  351. String.prototype.trim = function(){
  352. return this.replace(/(^\s*)|(\s*$)/g, ' ');
  353. }
  354. })