keyWord.js 12 KB

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