(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.yncms = factory()); }(window, function () { 'use strict'; var util = { /** * 模板解析 * @param {[type]} html [description] * @param {[type]} options [description] * @return {[type]} [description] */ // var template = 'my skills:' + // '<%if(this.showskills) {%>' + // '<%for(var index in this.skills) {%>' + // '<%this.skills[index]%>' + // '<%}%>' + // '<%} else {%>' + // '

none

' + // '<%}%>'; rendertemplate: function (html, options) { var re = /<%([^%>]+)?%>/g, reexp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g, code = 'var r=[];\n', cursor = 0; var add = function (line, js) { js ? (code += line.match(reexp) ? line + '\n' : 'r.push(' + line + ');\n') : (code += line !== '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : ''); return add; }; var match; while (match = re.exec(html)) { add(html.slice(cursor, match.index))(match[1], true); cursor = match.index + match[0].length; } add(html.substr(cursor, html.length - cursor)); code += 'return r.join("");'; return new function(code.replace(/[\r\t\n]/g, '')).apply(options); }, /** * json转换为query参数 * @param {object} json 参数对象 */ jsontoqueryparam: function (json) { var param = []; for (var key in json) { param.push(encodeuricomponent(key) + '=' + encodeuricomponent(json[key])); } return param.join('&'); }, /** * 字符串截取 * @param {string} str * @param {number} length */ intercept: function (str, length) { return str.length < length ? str : str.slice(0, length) + "..."; }, /** * 列表截取 * @param {object} srclist 数据列表 * @param {number} start 截取开始位置 * @param {number} length 截取结束位置 */ cliplist: function (srclist, start, length) { return start >= 0 ? srclist.slice(start, start + length) : srclist.slice(start); }, /** * * @param {*} fmt * @param {*} date */ dateformat: function (fmt, date) { var date = new date(date * 1000); var ret; var opt = { "y+": date.getfullyear().tostring(), // 年 "m+": (date.getmonth() + 1).tostring().padstart(2, '0'), // 月 "d+": date.getdate().tostring().padstart(2, '0'), // 日 "h+": date.gethours().tostring(), // 时 "m+": date.getminutes().tostring(), // 分 "s+": date.getseconds().tostring() // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 }; for (var k in opt) { ret = new regexp("(" + k + ")").exec(fmt); if (ret) { fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padstart(ret[1].length, "0"))) }; }; return fmt; } }; function yncms(option) { this.option = option; this.util = util; } // 开启ie8 ajax跨域 $.support.cors = true; // jquery高于1.5.0版本,返回的是deferred对象,可以进行链式操作。 yncms.prototype = { /** * 配置 * @param {object} option 初始对象 */ setconfig: function (option) { this.option = option; }, /** * 搜索 * @param {string} keywords 关键词 */ search: function (keyword, param) { var path = this.option.host + '/home/info/search?' + this.util.jsontoqueryparam(param); return $.post(path, { keyword: keyword }); }, /** * 通过栏目id获取栏目信息 * @param {number} $columnid 栏目id */ getinfobycolumnid: function (columnid, param) { var path = this.option.host + '/home/column/' + columnid + '/info'; console.log(columnid) return $.get(path, param); }, /** * 通过父栏目id获取所有子栏目 * @param {number} $id 栏目id */ getcolumnbypid: function (id) { var path = this.option.host + '/home/column/' + id; return $.get(path); }, /** * 通过pid获取网站的栏目 * @param {number} param 查询参数 */ getchildrencolumnbypid(param) { var path = this.option.host + '/home/column'; return $.get(path, { website_id: param.website_id, pid: param.pid }); }, /** * 通过网站id获取网站信息 * @param {number} $id 网站id */ getwebsitebyid: function (id) { var path = this.option.host + '/home/website/' + id; return $.get(path); }, /** * 通过信息模型和id获取点击量 * @param {string} model 信息所属模型 * @param {number} id 信息id */ getview: function (model, id) { var path = this.option.host + '/home/info/view'; return $.get(path, { model: model, id: id }); } }; return yncms; }));