keyWord.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. var reloadFunc = function(){
  2. //去空格方法
  3. // String.prototype.trim = function () {
  4. // return this.replace(/(^\s*)|(\s*$)/g, ' ');
  5. // }
  6. // 疑问解答相关操作
  7. $('.knowBtn').on('click', function () {
  8. $(".problemPop").hide()
  9. })
  10. $(".problem").on('click', function () {
  11. $(".problemPop").css("display", 'flex');
  12. })
  13. $(".enterOne").focus(function () {
  14. $(".btnChoose").show();
  15. })
  16. // 输入框自适应高度
  17. $('textarea').each(function(i,dom){
  18. dom.style.height = dom.scrollHeight +'px';
  19. })
  20. $("textarea").on("input", function() {
  21. this.style.height = 'auto';
  22. this.style.height = this.scrollHeight + "px";
  23. })
  24. // 添加keyWords检查输入框内是否有文字,如果有才能点击添加按钮
  25. $('.content .addkeyWord input.enterOne').on('input', function () {
  26. var buttonDOM = $(this).siblings()[1].children[1]
  27. if ($.trim($(this).val()).length >= 1) {
  28. buttonDOM.style.opacity = 1
  29. buttonDOM.removeAttribute("disabled")
  30. } else {
  31. buttonDOM.style.opacity = .5
  32. buttonDOM.setAttribute("disabled", true)
  33. }
  34. })
  35. // 添加 确认按钮的点击事件
  36. $('.content .addkeyWord .btn button:nth-child(2)').on('click', function () {
  37. setTimeout(function(){
  38. var keyWord = $.trim($('.addkeyWord input.enterOne').val());
  39. if(keyWord==""){
  40. return
  41. }
  42. if(kws_arr[keyWord]!=undefined){
  43. weui.toast('您设置的关键词已存在,请调整后再添加。', {
  44. duration: 2000,
  45. className: 'custom-toast',
  46. callback: function () { console.log('close') }
  47. });
  48. return
  49. }
  50. if(kws_name.length > 20){
  51. weui.toast('关键词不能超过20字', {
  52. duration: 2000,
  53. className: 'custom-toast',
  54. callback: function () { console.log('close') }
  55. });
  56. }
  57. kws_name = keyWord;
  58. kws_index = $(".showKeyWord li").length;
  59. if(kws_index==0){
  60. //分类首次添加关键词需重新获取分类索引
  61. classify_index = a_items.length;
  62. }
  63. var _index = 0;
  64. if($(".showKeyWord li").length>0){
  65. _index = Number($(".showKeyWord li:first").find(".editKeyWord").attr("dataIndex")) + Number(1);
  66. }
  67. classify_name = $.trim($('.classify-r .classify-detail').text());
  68. //保存关键词
  69. kws_count = parseInt(kws_count) + 1;
  70. if(kws_count>300){
  71. weui.toast('您设置的关键词已超出最高上限,请调整后再添加。', {
  72. duration: 2000,
  73. className: 'custom-toast',
  74. callback: function () { console.log('close') }
  75. });
  76. }else if (saveData("SK")){
  77. // 设置状态 首次添加关键词
  78. if(a_items.length==0){
  79. setEmptyHistory();
  80. }
  81. //
  82. doSessionData("",_index);
  83. $(".add-keyword-container .addNewKeyword i").show();
  84. //点击保存关键词,查看缓存中存的附加词 和排除词。
  85. $(".kws_count").text(kws_count);
  86. var html = `<li><div class="one"><div>
  87. <span>
  88. <strong> 关键词:</strong>
  89. <p class="key">${keyWord}</p>
  90. </span>`
  91. if (addition_kws.length>0){
  92. html +='<span>'
  93. +'<strong> 附加词:</strong>'
  94. +'<p class="addition">'+addition_kws.join(" ")+'</p>'
  95. +'</span>'
  96. }
  97. if (not_kws.length>0){
  98. html +='<span>'
  99. +'<strong> 排除词:</strong>'
  100. +'<p class="notkey">'+not_kws.join(" ")+'</p>'
  101. +'</span>'
  102. }
  103. html +=`</div>
  104. <button class="editKeyWord" dataIndex=`+_index+`><i class="iconfont icon-xiugai"></i> 修改</button>
  105. </div>
  106. <div class="modify">
  107. <textarea name="" rows="1" placeholder="" maxlength="20">${keyWord}</textarea>`
  108. if (addition_kws.length>0){
  109. html +=`<button class="addAdjunctWord" onClick="toappendkey(this)">编辑 附加词<i>(`+addition_kws.length+`)</i></button>`
  110. }else{
  111. html +=`<button class="addAdjunctWord" onClick="toappendkey(this)">添加 附加词</button>`
  112. }
  113. if (not_kws.length>0){
  114. html +=`<button class="addExclusion" onClick="tonotkey(this)">编辑 排除词<i>(`+not_kws.length+`)</i></button>`
  115. }else{
  116. html +=`<button class="addExclusion" onClick="tonotkey(this)">添加 排除词</button>`
  117. }
  118. html +=`<button class="deleteKey">删除</button><button class="ascertainKey" dataIndex=`+_index+` onclick="saveK(this)">确定</button></div></li>`
  119. $('.showKeyWord > ul').prepend(html);
  120. // 添加完成隐藏输入框
  121. $(".enter.addkeyWord").hide();
  122. // 还原状态
  123. $('.enter.addkeyWord > input').val('')
  124. //把kws_name,kws_index,addition_kws,not_kws初始化
  125. kws_index = 0;
  126. kws_name = "";
  127. addition_kws = [];
  128. not_kws = [];
  129. //
  130. var buttonDOM = $('.enter.addkeyWord .btn button')[1]
  131. buttonDOM.style.opacity = .5
  132. buttonDOM.setAttribute("disabled", true)
  133. //保存关键词 查看关键词数量 未分类 超过20 100提示
  134. if(classify_name.indexOf("未分类")>-1){
  135. if((kws_tips==1&&$(".showKeyWord li").length>=20)||(kws_tips==20&&$(".showKeyWord li").length>=100)){
  136. //未分类 20个关键词提示,100个关键词提示
  137. showMeg();
  138. var param = {};
  139. param.classify_index = classify_index;
  140. if(kws_tips==1){
  141. param.kws_tips = 20;
  142. }else if(kws_tips==20){
  143. param.kws_tips = 100;
  144. }
  145. $.post("/subscribepay/afterPay/updateUserTips",param,function(r){
  146. if(r.flag){
  147. kws_tips = param.kws_tips;
  148. $(".classify").attr("tips",kws_tips);
  149. }
  150. })
  151. }
  152. }
  153. }
  154. },150)
  155. })
  156. // 编辑
  157. $(".showKeyWord").on('click', '.editKeyWord', function (e) {
  158. kws_name = $.trim($(this).parent().find('.key').text());
  159. //当前关键词数组的位置 是数组的总长度-当前所在的位置-1 数组正序索引
  160. kws_index = $('.showKeyWord li').length - $(this).parent().parent("li").index() -1;
  161. if($(this).prev('div').find(".addition").text()!=""){
  162. addition_kws = $(this).prev('div').find(".addition").text().split(" ")
  163. }else{
  164. addition_kws = []
  165. }
  166. if ($(this).prev('div').find(".notkey").text()!=""){
  167. not_kws = $(this).prev('div').find(".notkey").text().split(" ")
  168. }else{
  169. not_kws = []
  170. }
  171. $('.enter.addkeyWord').hide()
  172. let oSpan = $(this).parent().siblings().children('textarea');
  173. let val = $(oSpan).val()
  174. $(this).parent().hide()
  175. $(this).parent().siblings().show().parent().siblings().children('.modify').hide().siblings('.one').show()
  176. $(oSpan).val('').focus().val(val)
  177. $(".add-keyword-container .addNewKeyword i").hide()
  178. })
  179. // 编辑删除
  180. $('.showKeyWord').on('click', '.deleteKey', function (e) {
  181. var _index = Number($(this).next(".ascertainKey").attr("dataindex"));
  182. var jQueryDOM = $(this).parents('li')
  183. weui.confirm('确定要删除关键词?', {
  184. buttons: [{
  185. label: '取消',
  186. type: 'default',
  187. onClick: function () { console.log('不删了') }
  188. }, {
  189. label: '确定',
  190. type: 'primary',
  191. onClick: function () {
  192. if(saveData('DK')){
  193. kws_count -= 1;
  194. $(".kws_count").text(kws_count);
  195. jQueryDOM.remove();
  196. if($(".showKeyWord ul").find('li').length==0){
  197. $(".enter.addkeyWord").find(".btnChoose button").eq(0).html('添加 附加词');
  198. $(".enter.addkeyWord").find(".btnChoose button").eq(1).html('添加 附加词');
  199. $(".enter.addkeyWord").find("input").val("");
  200. }else{
  201. $(".add-keyword-container .addNewKeyword i").show();
  202. }
  203. hasWords();
  204. doSessionData('DK',_index);
  205. //把kws_name,kws_index,addition_kws,not_kws初始化
  206. kws_index = 0;
  207. kws_name = "";
  208. addition_kws = [];
  209. not_kws = [];
  210. }else{
  211. weui.toast('无法删除', {
  212. duration: 2000,
  213. className: 'custom-toast',
  214. callback: function () { console.log('close') }
  215. });
  216. }
  217. }
  218. }]
  219. });
  220. console.log('删除关键词:', $(this).parent().find('textarea').val())
  221. })
  222. // 编辑确定
  223. // $('.showKeyWord').on('click', '.ascertainKey', function (e) {
  224. // var $this = $(this)
  225. // var keyWord = $(this).siblings('textarea').val()
  226. // if(keyWord.length > 20){
  227. // weui.toast('关键词不能超过20字', {
  228. // duration: 2000,
  229. // className: 'custom-toast',
  230. // callback: function () { console.log('close') }
  231. // });
  232. // }else{
  233. // $this.parent().siblings().find('.key').text(keyWord)
  234. // $this.parent().hide().siblings().show()
  235. // $('.addKeyWord').show()
  236. // }
  237. // })
  238. //防止键盘把当前输入框给挡住
  239. var u = navigator.userAgent;
  240. var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
  241. var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  242. if(isAndroid){
  243. //防止键盘把当前输入框给挡住
  244. window.addEventListener('resize', function () {
  245.    if (document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'TEXTAREA') {
  246.    window.setTimeout(function () {
  247.           document.activeElement.scrollIntoViewIfNeeded();
  248. }, 0);
  249. }
  250. })
  251. }
  252. (/iphone|ipod|ipad/i.test(navigator.appVersion)) && document.addEventListener('blur', (e) => {
  253. // 这里加了个类型判断,因为a等元素也会触发blur事件
  254. ['input', 'textarea'].includes(e.target.localName) && document.body.scrollIntoView(false)
  255. }, true)
  256. // 显示关键词分类弹框
  257. $('.classify-r').on('click', function(){
  258. modalOne();
  259. })
  260. //
  261. $('.classify-edit-pop .classify-keyword').bind('input propertychange', function() {
  262. var s = $('input.classify-keyword').val()
  263. // 去空格
  264. s = s.trim()
  265. if (s.length === 0) {
  266. $('.weui-dialog__ft .dialog__btn_confirm').addClass("opacity6");
  267. return
  268. }
  269. $('.weui-dialog__ft .dialog__btn_confirm').removeClass("opacity6");
  270. })
  271. // 关键词分类 - 确定按钮点击事件
  272. $('.classify-edit-pop .dialog__btn_confirm').on('click', function() {
  273. classify_name = $.trim($('input.classify-keyword').val());
  274. // 去空格
  275. classify_name = classify_name.trim();
  276. if (classify_name.length === 0) {
  277. return
  278. }
  279. //分类名称是否已存在
  280. if($.inArray(classify_name, classify_arr)>-1){
  281. weui.toast('此分类名称已存在', {
  282. duration: 2000,
  283. className: 'custom-toast',
  284. callback: function () { console.log('close') }
  285. });
  286. return
  287. }
  288. if(classify_name.length > 20){
  289. classify_name = classify_name.substring(0,20);
  290. }
  291. $(this).removeClass("opacity6");
  292. $('.classify-r .classify-detail').text(classify_name);
  293. // 关闭弹框后要重置input内容
  294. $('.classify-edit-pop').hide();
  295. $('input.classify-keyword').val('');
  296. if($(".showKeyWord li").length>0){
  297. saveData('SC')
  298. a_items[classify_index]["s_item"] = classify_name;
  299. }
  300. })
  301. // 关键词分类 - 取消按钮点击事件
  302. $('.classify-edit-pop .dialog__btn_cancel').on('click', function() {
  303. $('.classify-edit-pop').hide()
  304. $('input.classify-keyword').val('')
  305. })
  306. }