var baseurl = './jsondatas/' /* 数据列表处理 * api 需要获取的数据json文件 * datas { ** data_type 数据返回格式 'page || list || show' 默认'page' *** 'page': { total: (总条数) last_page: (总页数) data: (数据列表) } *** 'list': list(数据列表) *** 'show': { up: (上一条数据), down: (下一条数据), info: (详情内容) } ** page 当前页 默认1 ** limit 每页显示条数 默认10 ** category_id 分类id筛选 ** id 列表数据id筛选 ** sort 排序筛选 timeasc时间正序、timedesc时间倒序、sortasc排序id正序、sortdesc排序id倒序 ** list_type all取所有数据(包含所有子级) alone只取当前分类所有数据(不包含所有子级) 默认为alone ** type 类型筛选(主要用于category.json分类筛选) goods产品 content文章 text信息 carousel轮播 image图片 ** search_name 标题筛选模糊查找 } 数据筛选条件 * callback 回调函数 */ function requestdata(api, datas, callback, async = true) { var url = baseurl + api + '.json' $.ajax({ url, async, success: function (data) { if (!datas) { callback(data) } else { var datainfo = filterdatalist(api, datas, data) // console.log(datainfo); callback(datainfo) } } }) } /** * 排序方法 */ function datasort(sort, arr) { if (sort) { arr.sort(function (a, b) { //a,b表示相邻的两个元素 //若返回值>0,数组元素将按升序排列 //若返回值<0,数组元素将按降序排列 switch (sort) { case 'sortasc': return a.sort - b.sort case 'sortdesc': return b.sort - a.sort case 'timeasc': return a.create_time - b.create_time case 'timedesc': return b.create_time - a.create_time } }) } return arr } /** * 数据列表处理 */ function filterdatalist(api, condition, data) { var data_type = condition.data_type || 'page'; var page = condition.page || 1 var limit = condition.limit || 10 var category_id = condition.category_id var id = condition.id var sort = condition.sort var type = condition.type var list_type = condition.list_type || 'alone' var search_name = condition.search_name var search_key = condition.search_key // 是否启用精确搜索 var exact_search = condition.exact_search || false var newdata = data var total = data.length var last_page = limit > -1 ? math.ceil(total / limit) : 1 var up, down var search_to_lowercase = condition.search_to_lowercase || false // 是否开启大小写转换 var search_in_intro_detail = condition.search_in_intro_detail || false // 是否开启从简介和详情中搜索 newdata.foreach(item => { if (item.create_time) { item.create_time = new date(item.create_time.replace(/-/g, '/')).gettime() } }) var is_top = condition.is_top || 0 if (is_top && is_top == '1') { newdata = newdata.filter(item => item.is_top == is_top) } if (is_top && is_top == '2') { newdata = newdata.filter(item => item.is_top == 0) } newdata = datasort(sort, newdata) if (category_id && list_type == 'alone') { newdata = newdata.filter(item => { if (api == 'category' || api == 'navigation') { return item.pid == category_id } else { // return item.category_id == category_id return item.category_id.split(',').some(item => category_id.split(',').includes(item)) } }) } if (category_id && (list_type == 'all' || list_type == 'children')) { var catelist, catelist_; var type_category = api if (api == 'category' || api == 'navigation') { type_category = type if (api == 'navigation') { type_category = 'navigation' } } requestdata('category', {type: type_category, sort: sort, limit: -1, data_type: 'list'}, function (res) { catelist = res }, false) // console.log(catelist, 'catelist') catelist_ = catelist.filter(item => { return item.id == category_id }) if (list_type == 'children' && catelist_.length > 0) { catelist_ = childtree(catelist_[0].id) } else { catelist_ = catelist_.concat(childtree(category_id)) } if (api == 'category' || api == 'navigation') { newdata = catelist_ } else { newdata = newdata.filter(item => { return catelist_.find(prop => { // return prop.id == item.category_id return item.category_id && item.category_id.split(',').some(item_ => prop.id.includes(item_)) }) }) } // 递归获取子分类 function childtree(pid) { var tree = [] var list = catelist.filter(item => { return item.pid == pid }) if (list && list.length > 0) { if (list_type == 'children') { tree = list.map(item => { item.children = childtree(item.id) return item }) } else { tree = tree.concat(list) list.foreach(item => { tree = tree.concat(childtree(item.id)) }) } } return tree } } if (type) { newdata = newdata.filter(item => item.type == type) } if (search_name) { if (search_to_lowercase) { search_name = search_name.tolowercase(); if (search_in_intro_detail) { if (exact_search) { newdata = newdata.filter(item => item.title === search_name || item.intro.tolowercase() === search_name || item.details.tolowercase() === search_name) } else { newdata = newdata.filter(item => item.title.tolowercase().includes(search_name) || item.intro.tolowercase().includes(search_name) || item.details.tolowercase().includes(search_name)) } } else { if (exact_search) { newdata = newdata.filter(item => item.title === search_name) } else { newdata = newdata.filter(item => item.title.tolowercase().includes(search_name)) } } } else { if (search_in_intro_detail) { if (exact_search) { newdata = newdata.filter(item => item.title === search_name || item.intro === search_name || item.details === search_name) } else { newdata = newdata.filter(item => item.title.includes(search_name) || item.intro.includes(search_name) || item.details.includes(search_name)) } } else { if (exact_search) { newdata = newdata.filter(item => item.title === search_name) } else { newdata = newdata.filter(item => item.title.includes(search_name)) } } } } if (search_key) { if (search_to_lowercase) { search_key = search_key.tolowercase(); newdata = newdata.filter(item => item.textb === search_key) } } if (id) { newdata = newdata.filter((item, index, arr) => { if (item.id == id) { up = (index == 0) ? null : arr[index - 1] down = (index == arr.length - 1) ? null : arr[index + 1] } return item.id == id }) switch (api) {//goods产品、content文章、product商品、text信息、 carousel轮播、 image图片 case 'content': case 'goods': case 'product': case 'text': case 'carousel': case 'image': // 浏览量记录 jquery.post('https://jzt2.china9.cn/api/readnum/addread', {id: id, type: api}, function (e) { }); break; } } total = newdata.length last_page = limit > -1 ? math.ceil(total / limit) : 1; if (limit > -1) { newdata = newdata.slice((page - 1) * limit, limit * page) } // console.log(limit, 'limit'); // console.log(newdata, 'newdata'); if (api != 'category' && api != 'navigation') { var catelists requestdata('category', {type: api, sort: sort, limit: -1, data_type: 'list'}, function (res) { catelists = res }, false) newdata.foreach(item => { if (item.category_id) { var catenew = catelists.filter(items => item.category_id.split(',').includes(items.id)) if (catenew.length > 0) { var pcate = catenew.concat(parenttree(catenew[0].pid)) pcate.reverse().foreach((item_, index) => { if (item_.pid == 0) { item_.level = 1 } else if (index > 0) { if (item_.pid == pcate[index - 1].pid) { item_.level = pcate[index - 1].level } else { item_.level = pcate[index - 1].level + 1 } } }) item.category = array.from(new set(pcate.reverse())) } } }) // 递归获取父分类 function parenttree(pid) { var tree_ = [] var list_ = catelists.filter(item => { return item.id == pid }) if (list_ && list_.length > 0) { tree_ = tree_.concat(list_) list_.foreach(item => { tree_ = tree_.concat(parenttree(item.pid)) }) } return tree_ } } if (data_type == 'page') { return { total, last_page, data: newdata } } if (data_type == 'list') { return newdata } if (data_type == 'show') { return { up, down, info: total > 0 ? newdata[0] : {} } } } /** * 日期格式转换 * @param {string} time 时间戳 * @param {string} format 日期格式 例:y-m-d h:i:s * y-m-d h:i:s 转换为2021-09-01 12:30:30 * m-d h:i:s 转换为09-01 12:30:30 * m-d h:i 转换为09-01 12:30 * y年m月d日h时i分s秒 转换为2021年09月01日12时30分30秒 */ function timestamp2string(time, format) { const datetime = new date() datetime.settime(time) if (time.tostring().length == 10) { datetime.settime(time * 1000) } const year = datetime.getfullyear() const month = datetime.getmonth() + 1 < 10 ? '0' + (datetime.getmonth() + 1) : datetime.getmonth() + 1 const date = datetime.getdate() < 10 ? '0' + datetime.getdate() : datetime.getdate() const hour = datetime.gethours() < 10 ? '0' + datetime.gethours() : datetime.gethours() const minute = datetime.getminutes() < 10 ? '0' + datetime.getminutes() : datetime.getminutes() const second = datetime.getseconds() < 10 ? '0' + datetime.getseconds() : datetime.getseconds() // 返回字符串格式 let dateinfo = '' const yindex = format.search('y') const mindex = format.search('m') const dindex = format.search('d') const hindex = format.search('h') const iindex = format.search('i') const sindex = format.search('s') dateinfo += `${str(year, yindex)}` dateinfo += `${str(month, mindex)}` dateinfo += `${str(date, dindex)}` dateinfo += `${str(hour, hindex)}` dateinfo += `${str(minute, iindex)}` dateinfo += `${str(second, sindex)}` return dateinfo function str(number, index) { if (index > -1) return `${number}${format.slice(index + 1, index + 2)}` else return '' } } //获取地址栏参数//可以是中文参数 function geturlparam(key) { // 获取参数 var url = window.location.search; // 正则筛选地址栏 var reg = new regexp("(^|&)" + key + "=([^&]*)(&|$)"); // 匹配目标参数 var result = url.substr(1).match(reg); //返回参数值 return result ? decodeuricomponent(result[2]) : null; } // 动态修改网站信息 function changewebinfo(siteinfo) { /* 修改网站标题 */ document.title = siteinfo.title /* 修改网站简介 */ var $desc = document.queryselector('meta[name="description"]'); if ($desc !== null) { $desc.content = siteinfo.description; } else { $desc = document.createelement("meta"); $desc.name = "description"; $desc.content = siteinfo.description; document.head.appendchild($desc); } /* 修改网站关键词 */ var $keywords = document.queryselector('meta[name="keywords"]'); if ($keywords !== null) { $keywords.content = siteinfo.keywords; } else { $keywords = document.createelement("meta"); $keywords.name = "keywords"; $keywords.content = siteinfo.keywords; document.head.appendchild($keywords); } /* 修改ico */ var $favicon = document.queryselector('link[rel="icon"]'); if ($favicon !== null) { $favicon.href = siteinfo.icon; } else { $favicon = document.createelement("link"); $favicon.rel = "icon"; $favicon.href = siteinfo.icon; document.head.appendchild($favicon); } } function base64() { // private property _keystr = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; // public method for encoding this.encode = function (input) { var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; input = _utf8_encode(input); while (i < input.length) { chr1 = input.charcodeat(i++); chr2 = input.charcodeat(i++); chr3 = input.charcodeat(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isnan(chr2)) { enc3 = enc4 = 64; } else if (isnan(chr3)) { enc4 = 64; } output = output + _keystr.charat(enc1) + _keystr.charat(enc2) + _keystr.charat(enc3) + _keystr.charat(enc4); } return output; } // public method for decoding this.decode = function (input) { var output = ""; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0; input = input.replace(/[^a-za-z0-9\+\/\=]/g, ""); while (i < input.length) { enc1 = _keystr.indexof(input.charat(i++)); enc2 = _keystr.indexof(input.charat(i++)); enc3 = _keystr.indexof(input.charat(i++)); enc4 = _keystr.indexof(input.charat(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output = output + string.fromcharcode(chr1); if (enc3 != 64) { output = output + string.fromcharcode(chr2); } if (enc4 != 64) { output = output + string.fromcharcode(chr3); } } output = _utf8_decode(output); return output; } // private method for utf-8 encoding _utf8_encode = function (string) { string = string.replace(/\r\n/g, "\n"); var utftext = ""; for (var n = 0; n < string.length; n++) { var c = string.charcodeat(n); if (c < 128) { utftext += string.fromcharcode(c); } else if ((c > 127) && (c < 2048)) { utftext += string.fromcharcode((c >> 6) | 192); utftext += string.fromcharcode((c & 63) | 128); } else { utftext += string.fromcharcode((c >> 12) | 224); utftext += string.fromcharcode(((c >> 6) & 63) | 128); utftext += string.fromcharcode((c & 63) | 128); } } return utftext; } // private method for utf-8 decoding _utf8_decode = function (utftext) { var string = ""; var i = 0; var c = c1 = c2 = 0; while (i < utftext.length) { c = utftext.charcodeat(i); if (c < 128) { string += string.fromcharcode(c); i++; } else if ((c > 191) && (c < 224)) { c2 = utftext.charcodeat(i + 1); string += string.fromcharcode(((c & 31) << 6) | (c2 & 63)); i += 2; } else { c2 = utftext.charcodeat(i + 1); c3 = utftext.charcodeat(i + 2); string += string.fromcharcode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); i += 3; } } return string; } } // 网站访问统计 function statistics() { var url = window.location.hostname; jquery.post('https://jzt2.china9.cn/api/statistics/submit', {'url': url}, function (e) { }); } statistics();