//作者: 957007164@qq.com //时间: 2018-01-05 //主站:http://www.55128.cn //描述:广告相关业务操作 /** * 加载layui相关模块 */ layui.use(['element', 'form', 'laydate'], function() { var $ = layui.jquery, element = layui.element, form = layui.form, laydate = layui.laydate; //监听预览位Tab element.on('tab(advertising_preview_tab)', function(data) { //TODO 加载预览页面 //console.log(data.index) var index = data.index - 1; LoadPreviewAdvertisement(index); }); //监听广告位Tab element.on('tab(advertising_position_tab)', function(data) { var index = data.index; $('#which_advertising_position_tab').val(index); LoadAdvertisement(index); }); //日期选择器 laydate.render({ elem: '#over-time', type: 'date' }); }); $(function() { //页面加载的时候默认加载主站内容 //0-主站 1-开奖网 2-手机站 LoadAdvertisement(Common.AD_TYPE_MAIN); }); /** * 加载主站广告位 */ function LoadAdvertisement(which) { //var typeId = which; //if (which === 3 || which === 4) { // which = 0; //} Loading(true); var postData = { t: new Date().getTime(), Which: which }; mui.ajax(Common.API_HOST + '/api/v1/Advertisement/GetAdvertisementList', { data: postData, dataType: 'json', //服务器返回json格式数据 type: 'post', //HTTP请求类型 timeout: 10 * 1000, //超时时间设置为10秒 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: function(req) { if(req.Status == 200) { var list = req.Data; var evalText = doT.template($("#advertisement-tmpl-" + which).html()); $("#advertisement-content-" + which).html(evalText(list)); } else { ShowMsg(req.Message); } Loading(false); }, error: function(xhr, type, errorThrown) { //异常处理 Loading(false); ShowMsg(type, 5); } }); } /** * 加载预览广告信息 * @param {Object} which */ function LoadPreviewAdvertisement(which) { //var typeId = which; //if (which === 3 || which === 4) { // which = 0; //} Loading(true); var postData = { t: new Date().getTime(), Which: which }; mui.ajax(Common.API_HOST + '/api/v1/Advertisement/GetAdvertisementHtml', { data: postData, dataType: 'json', //服务器返回json格式数据 type: 'post', //HTTP请求类型 timeout: 10 * 1000, //超时时间设置为10秒 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: function(req) { if (req.Status == 200) { console.log(req); var html = req.Data; var str = Base64.decode(html); console.log(str); $("#advertisement-preview-content-" + which).html(str); } else { ShowMsg(req.Message); } Loading(false); }, error: function(xhr, type, errorThrown) { //异常处理 Loading(false); ShowMsg(type, 5); } }); } /** * 保存广告 */ function SaveAdvertisement() { //从session中获取token var data = layui.sessionData('sys_token_info'); var token = data._token; if(CheckInput()) { if(token == "" || token == undefined) { layer.prompt({ formType: 1, //支持0(文本)默认1(密码)2(多行文本) title: '请输入密码', maxlength: 100, //可输入文本的最大长度,默认500 }, function (value, index, elem) { //TODO 验证密码,提交数据 mui.ajax(Common.API_HOST + '/api/v1/OAuth/GetToken', { dataType: 'json', //服务器返回json格式数据 type: 'get', //HTTP请求类型 data: { appkey: Common.APP_KEY, appsecret: value }, success: function(req) { //获得服务器响应 if(req.Status == 200) { var data = req.Data; //获取到Token后存入session layui.sessionData("sys_token_info", { key: '_token', value: data.AccessToken }); DoSave(data.AccessToken); } else { ShowMsg(req.Message, 5); //服务器Token失效 if(req.Status == 403 || req.Status == 401) { layui.sessionData('sys_token_info', null); } } } }); layer.close(index); }); } else { DoSave(token); } } } /** * 保存 */ function DoSave(token) { Loading(true); var which = $('#which_advertising_position_tab').val(); var title = $("#title").val(); var href = $("#href").val(); var over_time = $("#over-time").val(); var id = $("#advertising_id").val(); var $position = $("#position-msg").attr("data-val") == "" ? $("#position").val() : $("#position-msg").attr("data-val"); var positionArr = $position.split("-"); //换算出真实位置 var column = which == Common.AD_TYPE_MOBIL ? Common.MOBIL_COLUMN : Common.PC_COLUMN; var p = (parseInt(positionArr[0]) - 1) * column + parseInt(positionArr[1]) //TODO 提交数据 var postData = { Id: id, Which: which, Title: title, Href: href, OverTime: over_time, Position: p, t: new Date().getTime(), Appkey: Common.APP_KEY, AccessToken: token, Nonce: new Date().getTime() }; mui.ajax(Common.API_HOST + '/api/v1/Advertisement/SaveAdvertisement', { data: postData, dataType: 'json', //服务器返回json格式数据 type: 'post', //HTTP请求类型 timeout: 10 * 1000, //超时时间设置为10秒 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, success: function(req) { if(req.Status == 200) { ShowMsg('保存成功!'); //清理工作 Reset(); } else { //服务器Token失效 if(req.Status == 403 || req.Status == 401) { layui.sessionData('sys_token_info', null); } ShowMsg(req.Message, 5); } //0-主站 1-开奖网 2-手机站 LoadAdvertisement(which); Loading(false); }, error: function(xhr, type, errorThrown) { //异常处理; console.log(type); Loading(false); } }); } /** * 校验表单 */ function CheckInput() { var title = $("#title").val(); var href = $("#href").val(); var over_time = $("#over-time").val(); var $position = $("#position").val() != "" ? $("#position").val() : $("#position-msg").attr("data-val"); if(title == "") { layer.tips('请输入广告标题', '#title'); $("#title").focus(); return false; } else if(href == "") { layer.tips('请输入广告地址', '#href'); $("#href").focus(); return false; } else if($position == "") { layer.tips('请选择广告位置', '.layui-form-select'); return false; } else if(over_time == "") { layer.tips('请选择广告到期时间', '#over-time'); $("#over-time").focus(); return false; } else { return true; } } /** * 添加或者编辑广告位 * @param {Object} obj */ function AddOrEditAdvertisement(obj) { var title = $(obj).attr("data-title"); var href = $(obj).attr("data-href"); var over_time = $(obj).attr("data-over-time"); var $position = $(obj).attr("data-position"); var id = $(obj).attr("data-id"); if(title == "" && href == "") { //添加 //重置控件 Reset(); //隐藏下拉框 $("#position-input-block").css("display", "none"); //添加位置信息 $("#position-msg").html(GetPositionDesc($position)); } else { //编辑 $("#title").val(title); $("#href").val(href); $("#over-time").val(over_time); $("#advertising_id").val(id); //隐藏下拉框 $("#position-input-block").css("display", "none"); //添加位置信息 $("#position-msg").html(GetPositionDesc($position)); } } /** * 重置 */ function Reset() { $("#title").val(""); $("#href").val(""); $("#over-time").val(""); $("#advertising_id").val(""); $("#position-msg").html(""); $("#position-msg").attr("data-val", ""); //显示下拉框 $("#position-input-block").css("display", "block"); } /** * 获取位置描述 * @param {Object} position */ function GetPositionDesc(position) { var which = $('#which_advertising_position_tab').val(); var column = which == Common.AD_TYPE_MOBIL ? Common.MOBIL_COLUMN : Common.PC_COLUMN; var r = Math.ceil(position / column); var t = parseInt(position % column); var c = t == 0 ? column : t; $("#position-msg").attr("data-val", r + "-" + c); return "第" + r + "排第" + c + "列"; } /** * 加载层 */ var index; function Loading(show) { if(show) { index = layer.load(1, { shade: [0.5, '#fff'] //0.1透明度的白色背景 }); } else { if(index != undefined) { layer.close(index); } } } /** * 消息提示 * @param {Object} msg * @param {Object} type */ function ShowMsg(msg, type) { layer.msg(msg, { icon: type == undefined ? 1 : type }); } (function (global) { "use strict"; var _Base64 = global.Base64; var version = "2.3.2"; var buffer; if (typeof module !== "undefined" && module.exports) { try { buffer = require("buffer").Buffer } catch (err) { } } var b64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var b64tab = function (bin) { var t = {}; for (var i = 0, l = bin.length; i < l; i++)t[bin.charAt(i)] = i; return t }(b64chars); var fromCharCode = String.fromCharCode; var cb_utob = function (c) { if (c.length < 2) { var cc = c.charCodeAt(0); return cc < 128 ? c : cc < 2048 ? fromCharCode(192 | cc >>> 6) + fromCharCode(128 | cc & 63) : fromCharCode(224 | cc >>> 12 & 15) + fromCharCode(128 | cc >>> 6 & 63) + fromCharCode(128 | cc & 63) } else { var cc = 65536 + (c.charCodeAt(0) - 55296) * 1024 + (c.charCodeAt(1) - 56320); return fromCharCode(240 | cc >>> 18 & 7) + fromCharCode(128 | cc >>> 12 & 63) + fromCharCode(128 | cc >>> 6 & 63) + fromCharCode(128 | cc & 63) } }; var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g; var utob = function (u) { return u.replace(re_utob, cb_utob) }; var cb_encode = function (ccc) { var padlen = [0, 2, 1][ccc.length % 3], ord = ccc.charCodeAt(0) << 16 | (ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8 | (ccc.length > 2 ? ccc.charCodeAt(2) : 0), chars = [b64chars.charAt(ord >>> 18), b64chars.charAt(ord >>> 12 & 63), padlen >= 2 ? "=" : b64chars.charAt(ord >>> 6 & 63), padlen >= 1 ? "=" : b64chars.charAt(ord & 63)]; return chars.join("") }; var btoa = global.btoa ? function (b) { return global.btoa(b) } : function (b) { return b.replace(/[\s\S]{1,3}/g, cb_encode) }; var _encode = buffer ? buffer.from && buffer.from !== Uint8Array.from ? function (u) { return (u.constructor === buffer.constructor ? u : buffer.from(u)).toString("base64") } : function (u) { return (u.constructor === buffer.constructor ? u : new buffer(u)).toString("base64") } : function (u) { return btoa(utob(u)) }; var encode = function (u, urisafe) { return !urisafe ? _encode(String(u)) : _encode(String(u)).replace(/[+\/]/g, function (m0) { return m0 == "+" ? "-" : "_" }).replace(/=/g, "") }; var encodeURI = function (u) { return encode(u, true) }; var re_btou = new RegExp(["[À-ß][€-¿]", "[à-ï][€-¿]{2}", "[ð-÷][€-¿]{3}"].join("|"), "g"); var cb_btou = function (cccc) { switch (cccc.length) { case 4: var cp = (7 & cccc.charCodeAt(0)) << 18 | (63 & cccc.charCodeAt(1)) << 12 | (63 & cccc.charCodeAt(2)) << 6 | 63 & cccc.charCodeAt(3), offset = cp - 65536; return fromCharCode((offset >>> 10) + 55296) + fromCharCode((offset & 1023) + 56320); case 3: return fromCharCode((15 & cccc.charCodeAt(0)) << 12 | (63 & cccc.charCodeAt(1)) << 6 | 63 & cccc.charCodeAt(2)); default: return fromCharCode((31 & cccc.charCodeAt(0)) << 6 | 63 & cccc.charCodeAt(1)) } }; var btou = function (b) { return b.replace(re_btou, cb_btou) }; var cb_decode = function (cccc) { var len = cccc.length, padlen = len % 4, n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0) | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0) | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0) | (len > 3 ? b64tab[cccc.charAt(3)] : 0), chars = [fromCharCode(n >>> 16), fromCharCode(n >>> 8 & 255), fromCharCode(n & 255)]; chars.length -= [0, 0, 2, 1][padlen]; return chars.join("") }; var atob = global.atob ? function (a) { return global.atob(a) } : function (a) { return a.replace(/[\s\S]{1,4}/g, cb_decode) }; var _decode = buffer ? buffer.from && buffer.from !== Uint8Array.from ? function (a) { return (a.constructor === buffer.constructor ? a : buffer.from(a, "base64")).toString() } : function (a) { return (a.constructor === buffer.constructor ? a : new buffer(a, "base64")).toString() } : function (a) { return btou(atob(a)) }; var decode = function (a) { return _decode(String(a).replace(/[-_]/g, function (m0) { return m0 == "-" ? "+" : "/" }).replace(/[^A-Za-z0-9\+\/]/g, "")) }; var noConflict = function () { var Base64 = global.Base64; global.Base64 = _Base64; return Base64 }; global.Base64 = { VERSION: version, atob: atob, btoa: btoa, fromBase64: decode, toBase64: encode, utob: utob, encode: encode, encodeURI: encodeURI, btou: btou, decode: decode, noConflict: noConflict }; if (typeof Object.defineProperty === "function") { var noEnum = function (v) { return { value: v, enumerable: false, writable: true, configurable: true } }; global.Base64.extendString = function () { Object.defineProperty(String.prototype, "fromBase64", noEnum(function () { return decode(this) })); Object.defineProperty(String.prototype, "toBase64", noEnum(function (urisafe) { return encode(this, urisafe) })); Object.defineProperty(String.prototype, "toBase64URI", noEnum(function () { return encode(this, true) })) } } if (global["Meteor"]) { Base64 = global.Base64 } if (typeof module !== "undefined" && module.exports) { module.exports.Base64 = global.Base64 } else if (typeof define === "function" && define.amd) { define([], function () { return global.Base64 }) } })(typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : this);