do.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //作者: 957007164@qq.com
  2. //时间: 2018-01-05
  3. //主站:http://www.55128.cn
  4. //描述:广告相关业务操作
  5. /**
  6. * 加载layui相关模块
  7. */
  8. layui.use(['element', 'form', 'laydate'], function() {
  9. var $ = layui.jquery,
  10. element = layui.element,
  11. form = layui.form,
  12. laydate = layui.laydate;
  13. //监听预览位Tab
  14. element.on('tab(advertising_preview_tab)', function(data) {
  15. //TODO 加载预览页面
  16. //console.log(data.index)
  17. var index = data.index - 1;
  18. LoadPreviewAdvertisement(index);
  19. });
  20. //监听广告位Tab
  21. element.on('tab(advertising_position_tab)', function(data) {
  22. var index = data.index;
  23. $('#which_advertising_position_tab').val(index);
  24. LoadAdvertisement(index);
  25. });
  26. //日期选择器
  27. laydate.render({
  28. elem: '#over-time',
  29. type: 'date'
  30. });
  31. });
  32. $(function() {
  33. //页面加载的时候默认加载主站内容
  34. //0-主站 1-开奖网 2-手机站
  35. LoadAdvertisement(Common.AD_TYPE_MAIN);
  36. });
  37. /**
  38. * 加载主站广告位
  39. */
  40. function LoadAdvertisement(which) {
  41. //var typeId = which;
  42. //if (which === 3 || which === 4) {
  43. // which = 0;
  44. //}
  45. Loading(true);
  46. var postData = {
  47. t: new Date().getTime(),
  48. Which: which
  49. };
  50. mui.ajax(Common.API_HOST + '/api/v1/Advertisement/GetAdvertisementList', {
  51. data: postData,
  52. dataType: 'json', //服务器返回json格式数据
  53. type: 'post', //HTTP请求类型
  54. timeout: 10 * 1000, //超时时间设置为10秒
  55. headers: {
  56. 'Content-Type': 'application/x-www-form-urlencoded'
  57. },
  58. success: function(req) {
  59. if(req.Status == 200) {
  60. var list = req.Data;
  61. var evalText = doT.template($("#advertisement-tmpl-" + which).html());
  62. $("#advertisement-content-" + which).html(evalText(list));
  63. } else {
  64. ShowMsg(req.Message);
  65. }
  66. Loading(false);
  67. },
  68. error: function(xhr, type, errorThrown) {
  69. //异常处理
  70. Loading(false);
  71. ShowMsg(type, 5);
  72. }
  73. });
  74. }
  75. /**
  76. * 加载预览广告信息
  77. * @param {Object} which
  78. */
  79. function LoadPreviewAdvertisement(which) {
  80. //var typeId = which;
  81. //if (which === 3 || which === 4) {
  82. // which = 0;
  83. //}
  84. Loading(true);
  85. var postData = {
  86. t: new Date().getTime(),
  87. Which: which
  88. };
  89. mui.ajax(Common.API_HOST + '/api/v1/Advertisement/GetAdvertisementHtml', {
  90. data: postData,
  91. dataType: 'json', //服务器返回json格式数据
  92. type: 'post', //HTTP请求类型
  93. timeout: 10 * 1000, //超时时间设置为10秒
  94. headers: {
  95. 'Content-Type': 'application/x-www-form-urlencoded'
  96. },
  97. success: function(req) {
  98. if (req.Status == 200) {
  99. console.log(req);
  100. var html = req.Data;
  101. var str = Base64.decode(html);
  102. console.log(str);
  103. $("#advertisement-preview-content-" + which).html(str);
  104. } else {
  105. ShowMsg(req.Message);
  106. }
  107. Loading(false);
  108. },
  109. error: function(xhr, type, errorThrown) {
  110. //异常处理
  111. Loading(false);
  112. ShowMsg(type, 5);
  113. }
  114. });
  115. }
  116. /**
  117. * 保存广告
  118. */
  119. function SaveAdvertisement() {
  120. //从session中获取token
  121. var data = layui.sessionData('sys_token_info');
  122. var token = data._token;
  123. if(CheckInput()) {
  124. if(token == "" || token == undefined) {
  125. layer.prompt({
  126. formType: 1, //支持0(文本)默认1(密码)2(多行文本)
  127. title: '请输入密码',
  128. maxlength: 100, //可输入文本的最大长度,默认500
  129. }, function (value, index, elem) {
  130. //TODO 验证密码,提交数据
  131. mui.ajax(Common.API_HOST + '/api/v1/OAuth/GetToken', {
  132. dataType: 'json', //服务器返回json格式数据
  133. type: 'get', //HTTP请求类型
  134. data: {
  135. appkey: Common.APP_KEY,
  136. appsecret: value
  137. },
  138. success: function(req) {
  139. //获得服务器响应
  140. if(req.Status == 200) {
  141. var data = req.Data;
  142. //获取到Token后存入session
  143. layui.sessionData("sys_token_info", {
  144. key: '_token',
  145. value: data.AccessToken
  146. });
  147. DoSave(data.AccessToken);
  148. } else {
  149. ShowMsg(req.Message, 5);
  150. //服务器Token失效
  151. if(req.Status == 403 || req.Status == 401) {
  152. layui.sessionData('sys_token_info', null);
  153. }
  154. }
  155. }
  156. });
  157. layer.close(index);
  158. });
  159. } else {
  160. DoSave(token);
  161. }
  162. }
  163. }
  164. /**
  165. * 保存
  166. */
  167. function DoSave(token) {
  168. Loading(true);
  169. var which = $('#which_advertising_position_tab').val();
  170. var title = $("#title").val();
  171. var href = $("#href").val();
  172. var over_time = $("#over-time").val();
  173. var id = $("#advertising_id").val();
  174. var $position = $("#position-msg").attr("data-val") == "" ? $("#position").val() : $("#position-msg").attr("data-val");
  175. var positionArr = $position.split("-");
  176. //换算出真实位置
  177. var column = which == Common.AD_TYPE_MOBIL ? Common.MOBIL_COLUMN : Common.PC_COLUMN;
  178. var p = (parseInt(positionArr[0]) - 1) * column + parseInt(positionArr[1])
  179. //TODO 提交数据
  180. var postData = {
  181. Id: id,
  182. Which: which,
  183. Title: title,
  184. Href: href,
  185. OverTime: over_time,
  186. Position: p,
  187. t: new Date().getTime(),
  188. Appkey: Common.APP_KEY,
  189. AccessToken: token,
  190. Nonce: new Date().getTime()
  191. };
  192. mui.ajax(Common.API_HOST + '/api/v1/Advertisement/SaveAdvertisement', {
  193. data: postData,
  194. dataType: 'json', //服务器返回json格式数据
  195. type: 'post', //HTTP请求类型
  196. timeout: 10 * 1000, //超时时间设置为10秒
  197. headers: {
  198. 'Content-Type': 'application/x-www-form-urlencoded'
  199. },
  200. success: function(req) {
  201. if(req.Status == 200) {
  202. ShowMsg('保存成功!');
  203. //清理工作
  204. Reset();
  205. } else {
  206. //服务器Token失效
  207. if(req.Status == 403 || req.Status == 401) {
  208. layui.sessionData('sys_token_info', null);
  209. }
  210. ShowMsg(req.Message, 5);
  211. }
  212. //0-主站 1-开奖网 2-手机站
  213. LoadAdvertisement(which);
  214. Loading(false);
  215. },
  216. error: function(xhr, type, errorThrown) {
  217. //异常处理;
  218. console.log(type);
  219. Loading(false);
  220. }
  221. });
  222. }
  223. /**
  224. * 校验表单
  225. */
  226. function CheckInput() {
  227. var title = $("#title").val();
  228. var href = $("#href").val();
  229. var over_time = $("#over-time").val();
  230. var $position = $("#position").val() != "" ? $("#position").val() : $("#position-msg").attr("data-val");
  231. if(title == "") {
  232. layer.tips('请输入广告标题', '#title');
  233. $("#title").focus();
  234. return false;
  235. } else if(href == "") {
  236. layer.tips('请输入广告地址', '#href');
  237. $("#href").focus();
  238. return false;
  239. } else if($position == "") {
  240. layer.tips('请选择广告位置', '.layui-form-select');
  241. return false;
  242. } else if(over_time == "") {
  243. layer.tips('请选择广告到期时间', '#over-time');
  244. $("#over-time").focus();
  245. return false;
  246. } else {
  247. return true;
  248. }
  249. }
  250. /**
  251. * 添加或者编辑广告位
  252. * @param {Object} obj
  253. */
  254. function AddOrEditAdvertisement(obj) {
  255. var title = $(obj).attr("data-title");
  256. var href = $(obj).attr("data-href");
  257. var over_time = $(obj).attr("data-over-time");
  258. var $position = $(obj).attr("data-position");
  259. var id = $(obj).attr("data-id");
  260. if(title == "" && href == "") { //添加
  261. //重置控件
  262. Reset();
  263. //隐藏下拉框
  264. $("#position-input-block").css("display", "none");
  265. //添加位置信息
  266. $("#position-msg").html(GetPositionDesc($position));
  267. } else { //编辑
  268. $("#title").val(title);
  269. $("#href").val(href);
  270. $("#over-time").val(over_time);
  271. $("#advertising_id").val(id);
  272. //隐藏下拉框
  273. $("#position-input-block").css("display", "none");
  274. //添加位置信息
  275. $("#position-msg").html(GetPositionDesc($position));
  276. }
  277. }
  278. /**
  279. * 重置
  280. */
  281. function Reset() {
  282. $("#title").val("");
  283. $("#href").val("");
  284. $("#over-time").val("");
  285. $("#advertising_id").val("");
  286. $("#position-msg").html("");
  287. $("#position-msg").attr("data-val", "");
  288. //显示下拉框
  289. $("#position-input-block").css("display", "block");
  290. }
  291. /**
  292. * 获取位置描述
  293. * @param {Object} position
  294. */
  295. function GetPositionDesc(position) {
  296. var which = $('#which_advertising_position_tab').val();
  297. var column = which == Common.AD_TYPE_MOBIL ? Common.MOBIL_COLUMN : Common.PC_COLUMN;
  298. var r = Math.ceil(position / column);
  299. var t = parseInt(position % column);
  300. var c = t == 0 ? column : t;
  301. $("#position-msg").attr("data-val", r + "-" + c);
  302. return "第" + r + "排第" + c + "列";
  303. }
  304. /**
  305. * 加载层
  306. */
  307. var index;
  308. function Loading(show) {
  309. if(show) {
  310. index = layer.load(1, {
  311. shade: [0.5, '#fff'] //0.1透明度的白色背景
  312. });
  313. } else {
  314. if(index != undefined) {
  315. layer.close(index);
  316. }
  317. }
  318. }
  319. /**
  320. * 消息提示
  321. * @param {Object} msg
  322. * @param {Object} type
  323. */
  324. function ShowMsg(msg, type) {
  325. layer.msg(msg, {
  326. icon: type == undefined ? 1 : type
  327. });
  328. }
  329. (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);