1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342 |
- /*
- * 时间格式化函数(将时间格式化为,2019年08月12日,2019-08-12,2019/08/12的形式)
- *
- *
- * pattern参数(想要什么格式的数据就传入什么格式的数据)
- * · 'yyyy-MM-dd' ---> 输出如2019-09-20
- * · 'yyyy-MM-dd hh:mm' ---> 输出如2019-09-20 08:20
- * · 'yyyy-MM-dd hh:mm:ss' ---> 输出如2019-09-20 08:20:23
- * · 'yyyy/MM/dd' ---> 输出如2019/09/20
- * · 'yyyy年MM月dd日' ---> 输出如2019年09月20日
- * · 'yyyy年MM月dd日 hh时mm分' ---> 输出如2019年09月20日 08时20分
- * · 'yyyy年MM月dd日 hh时mm分ss秒' ---> 输出如2019年09月20日 08时20分23秒
- * · 'yyyy年MM月dd日 hh时mm分ss秒 EE' ---> 输出如2019年09月20日 08时20分23秒 周二
- * · 'yyyy年MM月dd日 hh时mm分ss秒 EEE' ---> 输出如2019年09月20日 08时20分23秒 星期二
- *
- * 参考: https://www.cnblogs.com/mr-wuxiansheng/p/6296646.html
- */
- Date.prototype.pattern = function (fmt) {
- var o = {
- 'y+': this.getFullYear(),
- 'M+': this.getMonth() + 1, //月份
- 'd+': this.getDate(), //日
- 'h+': this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
- 'H+': this.getHours(), //小时
- 'm+': this.getMinutes(), //分
- 's+': this.getSeconds(), //秒
- 'q+': Math.floor((this.getMonth() + 3) / 3), //季度
- 'S': this.getMilliseconds(), //毫秒
- 'E+': this.getDay(), // 周
- };
- var week = {
- '0': '日',
- '1': '一',
- '2': '二',
- '3': '三',
- '4': '四',
- '5': '五',
- '6': '六'
- };
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
- }
- if (/(E+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[
- this.getDay() + '']);
- }
- for (var k in o) {
- if (new RegExp('(' + k + ')').test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k])
- .length)));
- }
- }
- return fmt;
- }
- var pTools = {
- // 解决ios返回不刷新页面的问题
- iosBackRefresh: function () {
- var isPageHide = false;
- window.addEventListener('pageshow', function () {
- if (isPageHide) {
- location.reload();
- }
- });
- window.addEventListener('pagehide', function () {
- isPageHide = true;
- });
- },
- // 传入你要获取的参数的名字
- getParam: function (name) {
- var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
- var r = window.location.search.substr(1).match(reg); //获取url中'?'符后的字符串并正则匹配
- var context = '';
- if (r != null) context = r[2];
- // 释放变量
- reg = null;
- r = null;
- return context == null || context == '' || context == 'undefined' ? '' : context;
- },
- // 数组去重
- unique: function (arr) {
- if (!Array.isArray(arr)) {
- console.log('type error!')
- return
- }
- var array = [];
- for (var i = 0; i < arr.length; i++) {
- if (array.indexOf(arr[i]) === -1) {
- array.push(arr[i])
- }
- }
- return array;
- },
- // 数组对象根据某一个值进行冒泡排序
- // arr 数组
- // value 字符串
- bSort: function (arr, value) {
- var len = arr.length;
- for (var i = 0; i < len - 1; i++) {
- for (var j = 0; j < len - 1 - i; j++) {
- // 相邻元素两两对比,元素交换,大的元素交换到后面
- if (arr[j][value] * 10000000 > arr[j + 1][value] * 10000000) {
- var temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- return arr;
- },
- // 通过userAgent获取用户手机操作系统类型
- androidOrIOS: function () {
- var u = navigator.userAgent.toLowerCase();
- var app = navigator.appVersion;
- var agent = null;
- if (/iphone|ipod|ipad|ios/.test(u)) {
- agent = 'ios'
- } else {
- agent = 'android'
- }
- return agent
- },
- // 获取随机字符串
- // 不传参数则获取长度不固定的字符串
- getRandomString: function (len) {
- var randomString = '';
- if (len) {
- /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
- var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
- var maxPos = $chars.length;
- for (i = 0; i < len; i++) {
- randomString += $chars.charAt(Math.floor(Math.random() * maxPos));
- }
- } else {
- // Math.random() 生成随机数字, eg: 0.123456
- // .toString(36) 转化成36进制 : "0.4fzyo82mvyr"
- // .substring(2) 去掉前面两位 : "yo82mvyr"
- // .slice(-8) 截取最后八位 : "yo82mvyr"
- randomString = Math.random().toString(36).substring(2)
- }
- return randomString;
- },
- // 数组去空格
- arrayRemoveSpace: function (arr) {
- if (!arr || !$.isArray(arr)) return []
- var index = arr.indexOf('')
- while (index !== -1) {
- arr.splice(index, 1)
- index = arr.indexOf('')
- }
- return arr
- },
- // 全局loading框
- jLoading: function (options) {
- // 设置options参数的默认值
- options.content = options.content || 'loading...'
- options.bgc = options.bgc || 'transparent'
- options.icon = options.icon || 'j-loading-icon'
- options.duration = options.duration || 0
- // 将其拼接成width: 5rem;的形式
- options.width = options.width ? 'width:' + options.width + ';' : ''
- // 结构
- // <div class="j-loading">
- // <div class="j-mask"></div>
- // <div class="j-toast">
- // <i class="j-toast_icon j-loading-icon"></i>
- // <p class="j-toast_content">loading</p>
- // </div>
- // </div>
- var that = this
- var bgcClassMap = {
- transparent: '',
- black: 'black'
- }
- var icon = options.iconHide ? '' : 'icon';
- var html = '<div class="j-loading"><div class="j-mask ' + bgcClassMap[options.bgc] + '"></div><div class="j-toast ' + icon + '" style="' + options.width + '"><i class="j-toast_icon ' + options.icon + '"></i><p class="j-toast_content">' + options.content + '</p></div></div>'
- var _html = $(html)
- $('body').append(_html)
- _html.fadeIn(this.loadingTransition)
- var _loading = {
- _html: _html,
- hide: function (callback) {
- this._html.fadeOut(that.loadingTransition, function () {
- this.remove()
- callback && callback()
- })
- }
- }
- if (options.duration > 0) {
- setTimeout(function () {
- _loading.hide(options.callback)
- }, options.duration)
- }
- return _loading
- },
- // FROM: https://www.jianshu.com/p/90ed8b728975
- // 比较两个对象是否相等
- // 返回true为相等,返回false为不相等
- deepCompare: function (x, y) {
- var i, l, leftChain, rightChain;
- function compare2Objects(x, y) {
- var p;
- // remember that NaN === NaN returns false
- // and isNaN(undefined) returns true
- if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') {
- return true;
- }
- // Compare primitives and functions.
- // Check if both arguments link to the same object.
- // Especially useful on the step where we compare prototypes
- if (x === y) {
- return true;
- }
- // Works in case when functions are created in constructor.
- // Comparing dates is a common scenario. Another built-ins?
- // We can even handle functions passed across iframes
- if ((typeof x === 'function' && typeof y === 'function') ||
- (x instanceof Date && y instanceof Date) ||
- (x instanceof RegExp && y instanceof RegExp) ||
- (x instanceof String && y instanceof String) ||
- (x instanceof Number && y instanceof Number)) {
- return x.toString() === y.toString();
- }
- // At last checking prototypes as good as we can
- if (!(x instanceof Object && y instanceof Object)) {
- return false;
- }
- if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {
- return false;
- }
- if (x.constructor !== y.constructor) {
- return false;
- }
- if (x.prototype !== y.prototype) {
- return false;
- }
- // Check for infinitive linking loops
- if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {
- return false;
- }
- // Quick checking of one object being a subset of another.
- // todo: cache the structure of arguments[0] for performance
- for (p in y) {
- if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
- return false;
- } else if (typeof y[p] !== typeof x[p]) {
- return false;
- }
- }
- for (p in x) {
- if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
- return false;
- } else if (typeof y[p] !== typeof x[p]) {
- return false;
- }
- switch (typeof (x[p])) {
- case 'object':
- case 'function':
- leftChain.push(x);
- rightChain.push(y);
- if (!compare2Objects(x[p], y[p])) {
- return false;
- }
- leftChain.pop();
- rightChain.pop();
- break;
- default:
- if (x[p] !== y[p]) {
- return false;
- }
- break;
- }
- }
- return true;
- }
- if (arguments.length < 1) {
- return true; //Die silently? Don't know how to handle such case, please help...
- // throw "Need two or more arguments to compare";
- }
- for (i = 1, l = arguments.length; i < l; i++) {
- leftChain = []; //Todo: this can be cached
- rightChain = [];
- if (!compare2Objects(arguments[0], arguments[i])) {
- return false;
- }
- }
- return true;
- }
- }
- // 页面逻辑部分
- var keySetDetail = new Vue({
- el: '.j-container',
- data: {
- // 限制数组最大长度
- conf: {
- maxKeyLength: 10,
- recommendTagsCount: 6
- },
- // 是编辑(查看详情)还是添加
- modeType: 'add',
- // 查询keyList请求返回的数据
- keysetReq: {},
- // 关键词数组
- keyList: [],
- // 是否显示选择区域picker
- showAreaPicker: false,
- // 是否显示选择类型picker
- showInfoTypePicker: false,
- // 新增时的关键词推荐(关键词输入框下方的,只显示一行,超出不显示)
- recommendTags: [],
- // 关键词刷新状态保存(类似前端分页)
- recListState: {
- loading: false, // 是否刷新中?
- loaded: false, // 请求是否完成
- pageNum: 1,
- pageSize: 6,
- total: 0, // 一共多少条数据
- list: [],
- listAll: [] // 后端返回的全部推荐关键词
- },
- // 当前关键词索引(如果为add状态,索引会加一)
- currentIndex: 0,
- // 有此项时。说明不是从列表页进入的
- fromPage: '',
- // 当前关键词展示详情
- currentInfo: {
- area: [],
- infotype: [],
- key: '',
- notkey: []
- },
- // 当前关键词备份,用来判断是否改变了
- currentInfoBackUp: {
- area: [],
- infotype: [],
- key: '',
- notkey: []
- },
- // 返回提示标志,为true则返回时不弹出提示
- iDoNotNeedConfirmed: true,
- // 输入节流定时器id
- timer: 0,
- // 省份原始数据
- provinceListMap: {
- '#': ['全国'],
- A: ['安徽', '澳门'],
- B: ['北京'],
- C: ['重庆'],
- F: ['福建'],
- G: ['广东', '广西', '贵州', '甘肃'],
- H: ['河北', '湖北', '黑龙江', '海南', '河南', '湖南'],
- J: ['吉林', '江苏', '江西'],
- L: ['辽宁'],
- N: ['内蒙古', '宁夏'],
- Q: ['青海'],
- S: ['山西', '陕西', '上海', '山东', '四川'],
- T: ['天津', '台湾'],
- X: ['西藏', '新疆', '香港'],
- Y: ['云南'],
- Z: ['浙江']
- },
- // indexBar数据
- indexList: [],
- indexListMap: {},
- // 信息类型数据
- infoTypeList: [
- {
- title: '拟建项目',
- value: '拟建',
- desc: '采集建筑工程、信息化等类项目在招标前由发改委等部门审批的信息,并向用户提供“拟建项目预告”功能。'
- },
- {
- title: '招标预告',
- value: '预告',
- desc: '在正式招标之前发布的公告信息,主要有采购计划、项目预告、采购预告、招标文件预公示、招标方式公示等信息'
- },
- {
- title: '招标公告',
- value: '招标',
- desc: '包括公开招标、邀请招标、询价采购、竞争性谈判、单一来源、公开竞价、电子反拍、变更公告等公告信息'
- },
- {
- title: '招标结果',
- value: '结果',
- desc: '包括中标公示、成交公告、废标公告、流标公告等'
- },
- {
- title: '其他信息',
- value: '其它',
- desc: '包括合同公告、验收公告、违规处理等'
- },
- ],
- infoTypeMapArr: [],
- noSpaceDialog: {
- // 触发提示的次数
- count: 0
- }
- },
- watch: {
- // 禁止输入空格
- 'currentInfo.key': function (newVal, oldVal) {
- var spaceReg = /\s+/g
- var hasSpace = spaceReg.test(newVal)
- if (hasSpace) {
- this.noSpaceDialog.count++
- if (this.noSpaceDialog.count <= 1) {
- this.showSpaceDialog()
- }
- this.currentInfo.key = this.currentInfo.key.replace(spaceReg, '')
- }
- }
- },
- computed: {
- typeConf: function () {
- var conf = {
- add: {
- bottomButtonGroupClass: 'single',
- },
- edit: {
- bottomButtonGroupClass: '',
- }
- }
- if (this.modeType) {
- return conf[this.modeType]
- } else {
- return conf.add
- }
- },
- confirmButtonDisabled: function () {
- var s = this.currentInfo.key.replace(/\s+/g, '')
- if (s) {
- return false
- } else {
- return true
- }
- }
- },
- created: function () {
- sessionStorage.removeItem('historypushDataCache')
- var t = pTools.getParam('type')
- var i = pTools.getParam('index')
- var f = pTools.getParam('from')
- if (t) {
- this.modeType = t
- }
- if (f) {
- this.fromPage = f
- }
- // 如果为新增(add),传过来的index值将不会被使用
- if (parseInt(i) >= 0) {
- var maxIndex = this.conf.maxKeyLength - 1
- var index = parseInt(i)
- this.currentIndex = index <= maxIndex ? index : maxIndex
- }
- // 清除订阅页面缓存
- if (this.currentIndex == '0' && this.modeType == 'add') {
- this.clearHistoryPushDataCache()
- }
- var recover = this.recoverPageStateFromCache()
- if (recover) {
- this.checkUpdate()
- } else {
- this.getKeyList()
- // 初始化页面必要数据
- this.initIndexBar()
- this.initInfoType()
- }
- },
- mounted: function () {
- var _this = this
- pTools.iosBackRefresh()
- this.popStateEvent()
- this.bindEvents()
- // 解决ios从上个页面返回触发popstate的问题
- setTimeout(function () {
- _this.iDoNotNeedConfirmed = false
- }, 100)
- },
- methods: {
- showDialog: function (conf) {
- var defaultConf = {
- title: '提示',
- message: 'message',
- className: 'j-confirm-dialog',
- showConfirmButton: true,
- showCancelButton: true,
- confirmButtonColor: '#2abed1'
- }
- if (conf) {
- Object.assign(defaultConf, conf)
- }
- return this.$dialog.confirm(defaultConf)
- },
- showSpaceDialog: function () {
- this.showDialog({
- title: '',
- message: '免费订阅关键词不可输入空格键,如需添加多个关键词,请前往购买超级订阅',
- confirmButtonText: '立即购买'
- }).then(function () {
- this.toVipPage()
- }.bind(this))
- .catch(function () {})
- },
- bindEvents: function () {
- var _this = this
- // 点击链接离开时,保存数据后在跳转
- $('body').on('click', '.ext-link', function (e) {
- e.preventDefault()
- _this.cachePageState()
- $(window).off('popstate')
- location.href = e.target.href
- })
- },
- // 获取关键词数据
- getKeyList: function () {
- var _this = this
- $.ajax({
- url: '/wxkeyset/ajaxReq?t=' + new Date().getTime(),
- type: 'POST',
- data: { reqType: 'getKeyset' },
- success: function (r) {
- if (!r) {
- return pTools.jLoading({
- iconHide: true,
- content: '请求失败',
- duration: 1500
- })
- }
- _this.keysetReq = r
- // 如果不是数组,则直接返回
- if (!(r.a_key instanceof Array)) {
- return
- }
- _this.keyList = r.a_key
- // 对数据进行处理(去除空字符串)
- if (r.a_key && $.isArray(r.a_key)) {
- r.a_key.forEach(function (item) {
- for (var key in item) {
- item[key] = pTools.arrayRemoveSpace(item[key])
- }
- })
- }
- if (_this.modeType === 'add') {
- _this.getKeyRecommend()
- }
- // 如果在其他地方添加关键词到10个,则刷新此页面会变为编辑状态
- if (_this.keyList.length >= _this.conf.maxKeyLength && _this.modeType === 'add') {
- _this.modeType = 'edit'
- _this.currentIndex = _this.keyList.length - 1
- }
- // 如果是添加,或者取到的值为空,则不赋值
- if (_this.modeType === 'add' || !_this.keyList[_this.currentIndex]) return
- // 给currentInfo赋值
- for (var key in _this.keyList[_this.currentIndex]) {
- if (key === 'key' && _this.keyList[_this.currentIndex][key] instanceof Array) {
- _this.currentInfo[key] = _this.keyList[_this.currentIndex][key].join(' ')
- } else {
- _this.currentInfo[key] = _this.keyList[_this.currentIndex][key]
- }
- }
- // 备份关键词
- _this.currentInfoBackUp = JSON.parse(JSON.stringify(_this.currentInfo))
- // 整理显示数据到picker
- _this.recoverAreaDataToPicker()
- _this.recoverInfoTypeDataToPicker()
- }
- })
- },
- // 获取关键词推荐
- getKeyRecommend: function () {
- var keysArr = []
- var _this = this
- this.keyList.forEach(function (item) {
- if (item.key) {
- if (item.key instanceof Array) {
- keysArr.push(item.key.join('+'))
- } else {
- keysArr.push(item.key)
- }
- }
- })
- if (keysArr.length === 0) return
- var fetchData = {
- count: 20,
- value: pTools.unique(keysArr).join(' ')
- }
- $.ajax({
- url: '/member/getRecomKWs',
- type: 'POST',
- data: fetchData,
- success: function (r) {
- if (r && r instanceof Array && r.length !== 0) {
- _this.loadKeyRecommend(r)
- }
- }
- })
- },
- loadKeyRecommend: function (list) {
- var _this = this
- var afterFilterArr = this.filterKeyRecommend(list)
- this.recommendTags = afterFilterArr.slice(0, this.conf.recommendTagsCount)
- this.recListState.listAll = afterFilterArr
- this.recListState.count = afterFilterArr.length
- this.nextPageRec()
- // 超出隐藏判断
- this.$nextTick(function () {
- var recommendTagsDOM = _this.$refs.recommendTags
- var boxWidth = $(recommendTagsDOM).width()
- var tagsDOMArr = $(recommendTagsDOM).children('.tag')
- var showIndex = 1
- // 倒着遍历
- for (var i = tagsDOMArr.length - 1; i >= 0; i--) {
- var cDOM = $(tagsDOMArr[i])
- var posL = cDOM.position().left
- // 最后一个能完全显示的元素
- if ((posL + cDOM.innerWidth()) < boxWidth) {
- showIndex = i
- break
- }
- }
- this.recommendTags.splice(showIndex + 1)
- })
- },
- // 过滤后端返回的推荐数组
- filterKeyRecommend: function (list) {
- var arr = pTools.bSort(list, 'sim')
- // 排序后
- var afterSort = arr.reverse()
- var afterTile = []
- // 已订阅关键词数组
- var allKeyArr = []
- var allKeyArrLower = []
- // 已订阅关键词的整理 -----
- // 将所有关键词整理到一个数组中
- this.keyList.forEach(function (item) {
- if (item.key instanceof Array) {
- allKeyArr = allKeyArr.concat(item.key)
- } else {
- allKeyArr.push(item.key)
- }
- })
- // 数组中英文转小写
- allKeyArr.forEach(function (item) {
- allKeyArrLower.push(item.toLowerCase())
- })
- // 推荐数组整理
- // 平铺,将数组中的对象去掉,使用字符串平铺内容
- afterSort.forEach(function (item) {
- afterTile.push(item.word.toLowerCase())
- })
- // 去重1,当前数组中的内容去重,不精确大小写
- var afterTileLength = afterTile.length
- for (var i = afterTileLength - 1; i >= 0; i--) {
- var aIndex = afterTile.indexOf(afterTile[i].toLowerCase())
- if (aIndex !== i) {
- afterTile.splice(i, 1)
- }
- }
- // 去重2,找到已经订阅过的,进行删除
- afterTileLength = afterTile.length
- for (var j = afterTileLength - 1; j >= 0; j--) {
- var aIndex = allKeyArrLower.indexOf(afterTile[j])
- if (aIndex !== -1) {
- afterTile.splice(j, 1)
- }
- }
- return afterTile
- },
- getRecListTags: function () {
- var listAll = this.recListState.listAll
- this.recListState.total = listAll.length
- var startIndex = ((this.recListState.pageNum - 1) * this.recListState.pageSize)
- var endIndex = (this.recListState.pageNum * this.recListState.pageSize)
- return listAll.slice(startIndex, endIndex)
- },
- nextPageRec: function () {
- if (this.recListState.loading) return
- this.recListState.loading = true
- this.recListState.list = this.getRecListTags()
- setTimeout(function () {
- this.recListState.loading = false
- }.bind(this), 500)
- // 最后一页,则重置页码
- if (this.recListState.pageNum * this.recListState.pageSize >= this.recListState.total) {
- this.recListState.pageNum = 1
- } else {
- this.recListState.pageNum++
- }
- return this.recListState.list
- },
- clickTag: function (item) {
- this.currentInfo.key = item
- this.checkUpdate()
- },
- onKeyInput: function () {
- var _this = this
- clearTimeout(this.timer)
- this.timer = setTimeout(function () {
- _this.saveKeyWordsRegTip()
- }, 600)
- this.checkUpdate()
- },
- // 设置关键词详情信息
- setKeyDetail: function (type) {
- var key = this.currentInfo.key.replace(/\s+/g, '')
- if (!key) {
- return pTools.jLoading({
- iconHide: true,
- content: '请先填写关键词',
- duration: 1500
- })
- }
- switch (type) {
- case 'area': {
- this.showAreaPicker = true
- break
- }
- case 'infotype': {
- this.showInfoTypePicker = true
- break
- }
- case 'notkey': {
- // 解绑popstate,解决ios从排除词页返回后对话框闪一下又消失
- $(window).off('popstate')
- // 离开页面先缓存一下数据
- this.cachePageState()
- // 编辑状态下,跳转到其他页面,则需要手动修改url
- if (this.modeType === 'add') {
- location.href = '/wxkeyset/keyset/notkey?type=add&index=' + this.keyList.length
- } else {
- location.href = '/wxkeyset/keyset/notkey?type=edit&index=' + this.currentIndex
- }
- break
- }
- default: {
- console.log('没有定义此类型')
- break
- }
- }
- },
- // 保存关键词字符验证,是否符合要要求通过
- saveKeyWordsRegTip: function () {
- // 验证关键词是否符合规范
- // 是否有非法字符
- var key = this.currentInfo.key
- if (key.match(/[^0-9a-zA-Z\u4E00-\u9FFF\s]/g)) {
- this.showKeyWordRegErrorToast('unexpectedChar')
- return false
- }
- // 关键词过长
- var keys = key.replace(/\s+/g, ' ').split(' ')
- for (var k = 0; k < keys.length; k++) {
- if (k > 1) {
- this.showKeyWordRegErrorToast('tooMuch')
- } else if (keys[k].length > 19) {
- this.showKeyWordRegErrorToast('tooLong')
- }
- }
- return true
- },
- // 保存关键词数据请求
- saveKeyList: function (data, showToast) {
- var _this = this
- $.ajax({
- url: '/wxkeyset/ajaxReq',
- type: 'POST',
- dataType: 'json',
- traditional: true,
- data: data,
- success: function (r) {
- if (!r) {
- return pTools.jLoading({
- iconHide: true,
- content: '请求失败',
- duration: 1500
- })
- }
- if (r.flag) {
- if (showToast) {
- weui.toast('订阅成功', {
- duration: 1000,
- className: 'j-toast',
- callback: function () {
- _this.pushListHistory()
- _this.goBack()
- }
- })
- }
- } else {
- pTools.jLoading({
- iconHide: true,
- content: '订阅失败',
- duration: 1000
- })
- }
- }
- })
- },
- deleteThisKey: function (keyword, index) {
- var _this = this
- $.ajax({
- url: '/wxkeyset/ajaxReq',
- type: 'POST',
- data: {
- reqType: 'delKeysWord',
- index: index,
- keyword: keyword
- },
- success: function (r) {
- if (!r) {
- return pTools.jLoading({
- iconHide: true,
- content: '请求失败',
- duration: 1500
- })
- }
- if (r.flag) {
- weui.toast('删除成功', {
- duration: 1000,
- className: 'j-toast',
- callback: function () {
- _this.goBack()
- }
- })
- } else {
- return pTools.jLoading({
- iconHide: true,
- content: '删除失败',
- duration: 1500
- })
- }
- }
- })
- },
- // 格式化关键词的每一项
- sortOutKey: function (info, type) {
- var text = ''
- info = info[type]
- switch (type) {
- case 'area': {
- if (info) {
- if ((info instanceof Array) && info.length === 0) {
- text = '全国'
- } else {
- text = info.join('、')
- }
- } else {
- text = '全国'
- }
- break
- }
- case 'infotype': {
- if (info) {
- if ((info instanceof Array) && info.length === 0) {
- text = '全部'
- } else {
- text = info.join('、')
- }
- } else {
- text = '全部'
- }
- break
- }
- case 'notkey': {
- if (info) {
- if ((info instanceof Array) && info.length === 0) {
- text = '添加不希望接收的关键词'
- } else {
- text = info.join('、')
- }
- } else {
- text = '添加不希望接收的关键词'
- }
- break
- }
- default: {
- text = ' - '
- break
- }
- }
- return text
- },
- // 删除关键词
- delThisKeyTip: function () {
- var _this = this
- var item = this.currentInfo
- var index = this.currentIndex
- var keyword = item.key
- weui.dialog({
- title: '删除关键词',
- content: '删除后将无法恢复,确定删除?',
- className: 'j-dialog',
- isAndroid: false,
- buttons: [{
- label: '取消',
- type: 'default',
- onClick: function () { }
- }, {
- label: '确定',
- type: 'warning',
- onClick: function () {
- _this.deleteThisKey(keyword, index)
- }
- }]
- })
- },
- toVipPage: function () {
- location.href = '/front/vipsubscribe/introducePage?typeinfo=free'
- },
- // 显示关键词输入提示
- showKeyWordRegErrorToast: function (type) {
- var conf = {
- iconHide: true,
- duration: 4000,
- width: '4.4rem',
- content: ''
- }
- if (type === 'tooMuch') {
- conf.content = $('#easy-alert .too-much').html().replace(/<scr(.*)<\/scr>/, '')
- } else if (type === 'tooLong') {
- conf.content = $('#easy-alert .too-long').html().replace(/<scr(.*)<\/scr>/, '')
- } else if (type === 'unexpectedChar') {
- conf.content = $('#easy-alert .unexpected-char').html().replace(/<scr(.*)<\/scr>/, '')
- }
- pTools.jLoading(conf)
- },
- // indexedBar逻辑(城市选择逻辑)
- // indexBar数据初始化函数
- initIndexBar: function () {
- // 整理数据得到indexListMap(),同时获得indexList
- var map = {}
- for (var key in this.provinceListMap) {
- var areaArr = []
- this.indexList.push(key)
- this.provinceListMap[key].forEach(function (item) {
- areaArr.push({
- name: item,
- selected: item === '全国'
- })
- })
- map[key] = areaArr
- }
- this.indexListMap = map
- // 给map赋值
- // for (var k in map) {
- // this.$set(this.indexListMap, k, map[k])
- // }
- },
- // 城市按钮点击事件
- indexBarItemClick: function (item) {
- // 选全国
- if (item.name === '全国') {
- this.setAllAreaDisSelected(false)
- item.selected = true
- } else {
- this.indexListMap['#'][0].selected = false
- item.selected = !item.selected
- }
- // 此处判断是否全部选中
- var state = this.getAllAreaStateExceptCountryWide()
- if (state !== 0) {
- // 全国选中
- this.setAllAreaDisSelected(false)
- this.indexListMap['#'][0].selected = true
- }
- },
- // 获得所有选中的省份名字的数组
- getSelectedAreaArr: function () {
- var arr = []
- for (var key in this.indexListMap) {
- this.indexListMap[key].forEach(function (item) {
- if (item.name !== '全国' && item.selected) {
- arr.push(item.name)
- }
- })
- }
- return arr
- },
- // 所有按钮设置状态
- setAllAreaDisSelected: function (state) {
- for (var key in this.indexListMap) {
- this.indexListMap[key].forEach(function (item) {
- item.selected = state
- })
- }
- },
- // 除了全国其余所有按钮是否全选或者是否全不选
- // 1 全选 -1 全部不选 0 其他
- getAllAreaStateExceptCountryWide: function () {
- var arr = []
- for (var i = 1; i < this.indexList.length; i++) {
- this.indexListMap[this.indexList[i]].forEach(function (item) {
- // 判断select的和是否为0,为0则全不选
- // state += item.selected
- arr.push(item.selected)
- })
- }
- var selectedCount = 0
- arr.forEach(function (item) {
- if (item) selectedCount++
- })
- if (selectedCount === arr.length) {
- // 除了全国以外其他所有都被选中
- return 1
- } else if (selectedCount === 0) {
- return -1
- } else {
- return 0
- }
- },
- resetAreaAll: function () {
- this.setAllAreaDisSelected(false)
- this.indexListMap['#'][0].selected = true
- },
- areaConfirm: function () {
- this.currentInfo.area = this.getSelectedAreaArr()
- this.showAreaPicker = false
- this.checkUpdate()
- },
- recoverAreaDataToPicker: function (areaArr) {
- var _this = this
- if (!areaArr) areaArr = this.currentInfo.area
- if (!(areaArr instanceof Array) || areaArr.length === 0) return
- this.indexListMap['#'][0].selected = false
- areaArr.forEach(function (item) {
- for (var key in _this.indexListMap) {
- _this.indexListMap[key].forEach(function (iitem) {
- if (iitem.name === item) {
- iitem.selected = true
- }
- })
- }
- })
- },
- initInfoType: function () {
- var arr = [
- {
- title: '全部',
- desc: '',
- value: [],
- selected: true
- }
- ]
- this.infoTypeList.forEach(function (item) {
- arr.push({
- title: item.title,
- desc: item.desc,
- value: item.value,
- selected: false
- })
- })
- this.infoTypeMapArr = arr
- },
- infoTypePickerClick: function (item) {
- // 选全部
- if (item.title === '全部') {
- this.setAllInfoTypeDisSelected(false)
- item.selected = true
- } else {
- this.infoTypeMapArr[0].selected = false
- item.selected = !item.selected
- }
- // 此处判断是否全部选中
- // 如果全部被子项选中,则全国选中。如果全部子项不被选中,则全国选中
- // var state = this.getInfoTypeState()
- // if (state !== 0) {
- // // 全国选中
- // this.setAllInfoTypeDisSelected(false)
- // this.infoTypeMapArr[0].selected = true
- // }
- },
- // state: true表示选中全部
- setAllInfoTypeDisSelected: function (state) {
- this.infoTypeMapArr.forEach(function (item) {
- item.selected = state
- })
- },
- // 获取信息类型的选中状态
- // 1 全选 -1 全部不选 0 其他
- getInfoTypeState: function () {
- var tInfoTypeMapArr = JSON.parse(JSON.stringify(this.infoTypeMapArr))
- tInfoTypeMapArr.shift()
- var selectedCountArr = []
- var selectedCount = 0
- tInfoTypeMapArr.forEach(function (item, index) {
- selectedCountArr.push(item.selected)
- })
- selectedCountArr.forEach(function (item) {
- if (item) selectedCount++
- })
- if (selectedCount === tInfoTypeMapArr.length) {
- // 除了全部以外其他所有都被选中
- return 1
- } else if (selectedCount === 0) {
- return -1
- } else {
- return 0
- }
- },
- // 获取你选择的内容
- getInfoTypeData: function () {
- var arr = []
- for (var i = 0; i < this.infoTypeMapArr.length; i++) {
- if (this.infoTypeMapArr[i].selected) {
- if (i === 0) {
- break
- } else {
- arr.push(this.infoTypeMapArr[i].value)
- }
- }
- }
- return arr
- },
- resetInfoType: function () {
- this.setAllInfoTypeDisSelected(false)
- this.infoTypeMapArr[0].selected = true
- },
- recoverInfoTypeDataToPicker: function (infoType) {
- var _this = this
- if (!infoType) infoType = this.currentInfo.infotype
- if (!(infoType instanceof Array) || infoType.length === 0) return
- _this.infoTypeMapArr[0].selected = false
- infoType.forEach(function (item) {
- _this.infoTypeMapArr.forEach(function (iitem) {
- if (item === iitem.value) {
- iitem.selected = true
- }
- })
- })
- },
- infoTypeConfirm: function () {
- this.currentInfo.infotype = this.getInfoTypeData()
- this.showInfoTypePicker = false
- this.checkUpdate()
- },
- cachePageState: function () {
- var $data = JSON.stringify(this.$data)
- sessionStorage.setItem('free-keyset-detail', $data)
- },
- recoverPageStateFromCache: function () {
- var t = sessionStorage.getItem('free-keyset-detail')
- // 以下字段,不做恢复
- var excludeArr = ['iDoNotNeedConfirmed']
- if (!t) return false
- var state = JSON.parse(t)
- for (var key in state) {
- if (excludeArr.indexOf(key) === -1) {
- this.$data[key] = state[key]
- } else {
- // console.log(key)
- }
- }
- return true
- },
- anchorClick: function (e) {
- var index = e.target.dataset.index
- $('.van-index-anchor[data-index=' + index + ']')[0].scrollIntoView()
- $(e.target).addClass('highlight-text').siblings().removeClass('highlight-text')
- },
- onConfirm: function () {
- var t = this.currentInfo
- var data = {
- reqType: 'saveKeyWordsNew',
- index: this.currentIndex,
- area: t.area,
- infotype: t.infotype,
- notkey: t.notkey,
- keyWords: t.key
- }
- this.saveKeyList(data, true)
- },
- // 检查数据是否更新了
- checkUpdate: function () {
- var _this = this
- // 比较两个对象是否相等,不相等说明数据更新了
- var hasUpdate = !pTools.deepCompare(this.currentInfo, this.currentInfoBackUp)
- if (hasUpdate) {
- this.pushHistory()
- } else {
- if (history.state) {
- this.iDoNotNeedConfirmed = true
- history.back()
- setTimeout(function () {
- _this.iDoNotNeedConfirmed = false
- }, 10)
- }
- }
- return hasUpdate
- },
- // 添加一个锚点
- pushHistory: function () {
- var pushContent = {
- id: '1',
- title: '返回确认',
- url: '#change'
- }
- if (!history.state) {
- history.pushState(pushContent, null, pushContent.url);
- }
- },
- pushListHistory: function() {
- // 如果不是直接从订阅页面进入,则直接退出函数
- if (!this.fromPage) return
- var state = history.state
- var pushContent = {
- id: '2',
- title: '订阅关键词',
- url: '/wxkeyset/keyset/index'
- }
- // 先取消此时的#change
- if (state && state.id === '1') {
- $(window).off('popstate')
- history.go(-1)
- }
- // 将当前历史记录替换为列表页
- history.replaceState(pushContent, null, pushContent.url)
- },
- // 正常返回的判断(不会显示提示弹窗)
- goBack: function () {
- $(window).off('popstate')
- var state = history.state
- if (state && state.id === '1') {
- history.go(-2)
- } else if (state && state.id === '2') {
- location.reload()
- } else {
- history.go(-1)
- }
- },
- // 监听返回事件
- popStateEvent: function () {
- var _this = this
- $(window).on('popstate', function (e) {
- // 如果picker打开,则关闭picker
- if (_this.showAreaPicker) {
- _this.showAreaPicker = false
- _this.pushHistory()
- } else if (_this.showInfoTypePicker) {
- _this.showInfoTypePicker = false
- _this.pushHistory()
- } else {
- if (_this.iDoNotNeedConfirmed) return
- // 提示是否保存
- weui.dialog({
- content: '返回将无法保存关键词',
- className: 'j-dialog no-header',
- isAndroid: false,
- buttons: [{
- label: '不保存',
- type: 'default',
- onClick: function () {
- // 返回上一页
- _this.goBack()
- }
- }, {
- label: '继续编辑',
- type: 'primary',
- onClick: function () {
- // 取消弹框
- // 并push一条历史记录
- _this.pushHistory()
- }
- }]
- })
- }
- })
- },
- // 清除订阅页面缓存
- clearHistoryPushDataCache: function () {
- sessionStorage.removeItem('historypushDataCache')
- sessionStorage.removeItem('historypushPageIndexCache')
- sessionStorage.removeItem('historypushScrollTop')
- sessionStorage.removeItem('historypushHasNextPage')
- sessionStorage.removeItem('closeAdvert')
- }
- }
- })
|