common.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright 2005-2013 dhcc.com.cn. All rights reserved.
  3. * Support: http://www.dhcc.com.cn
  4. * License: http://www.dhcc.com.cn/license
  5. *
  6. * JavaScript - Common
  7. * Version: 3.0
  8. */
  9. var shopxx = {
  10. base: "/mango",
  11. locale: "zh_CN"
  12. };
  13. var setting = {
  14. priceScale: "2",
  15. priceRoundType: "roundHalfUp",
  16. currencySign: "¥",
  17. currencyUnit: "元",
  18. uploadImageExtension: "jpg,jpeg,bmp,gif,png",
  19. uploadFlashExtension: "swf,flv",
  20. uploadMediaExtension: "swf,flv,mp3,wav,avi,rm,rmvb",
  21. uploadFileExtension: "zip,rar,7z,doc,docx,xls,xlsx,ppt,pptx"
  22. };
  23. var messages = {
  24. "shop.message.success": "操作成功",
  25. "shop.message.error": "操作错误",
  26. "shop.dialog.ok": "确  定",
  27. "shop.dialog.cancel": "取  消",
  28. "shop.dialog.deleteConfirm": "您确定要删除吗?",
  29. "shop.dialog.clearConfirm": "您确定要清空吗?",
  30. "shop.validate.required": "必填",
  31. "shop.validate.email": "E-mail格式错误",
  32. "shop.validate.url": "网址格式错误",
  33. "shop.validate.date": "日期格式错误",
  34. "shop.validate.dateISO": "日期格式错误",
  35. "shop.validate.pointcard": "信用卡格式错误",
  36. "shop.validate.number": "只允许输入数字",
  37. "shop.validate.digits": "只允许输入零或正整数",
  38. "shop.validate.minlength": "长度不允许小于{0}",
  39. "shop.validate.maxlength": "长度不允许大于{0}",
  40. "shop.validate.rangelength": "长度必须在{0}-{1}之间",
  41. "shop.validate.min": "不允许小于{0}",
  42. "shop.validate.max": "不允许大于{0}",
  43. "shop.validate.range": "必须在{0}-{1}之间",
  44. "shop.validate.accept": "输入后缀错误",
  45. "shop.validate.equalTo": "两次输入不一致",
  46. "shop.validate.remote": "输入错误",
  47. "shop.validate.integer": "只允许输入整数",
  48. "shop.validate.positive": "只允许输入正数",
  49. "shop.validate.negative": "只允许输入负数",
  50. "shop.validate.decimal": "数值超出了允许范围",
  51. "shop.validate.pattern": "格式错误",
  52. "shop.validate.extension": "文件格式错误"
  53. };
  54. // 添加Cookie
  55. function addCookie(name, value, options) {
  56. if (arguments.length > 1 && name != null) {
  57. if (options == null) {
  58. options = {};
  59. }
  60. if (value == null) {
  61. options.expires = -1;
  62. }
  63. if (typeof options.expires == "number") {
  64. var time = options.expires;
  65. var expires = options.expires = new Date();
  66. expires.setTime(expires.getTime() + time * 1000);
  67. }
  68. document.cookie = encodeURIComponent(String(name)) + "=" + encodeURIComponent(String(value)) + (options.expires ? "; expires=" + options.expires.toUTCString() : "") + (options.path ? "; path=" + options.path : "") + (options.domain ? "; domain=" + options.domain : ""), (options.secure ? "; secure" : "");
  69. }
  70. }
  71. // 获取Cookie
  72. function getCookie(name) {
  73. if (name != null) {
  74. var value = new RegExp("(?:^|; )" + encodeURIComponent(String(name)) + "=([^;]*)").exec(document.cookie);
  75. return value ? decodeURIComponent(value[1]) : null;
  76. }
  77. }
  78. // 移除Cookie
  79. function removeCookie(name, options) {
  80. addCookie(name, null, options);
  81. }
  82. // 货币格式化
  83. function currency(value, showSign, showUnit) {
  84. if (value != null) {
  85. var price;
  86. if (setting.priceRoundType == "roundHalfUp") {
  87. price = (Math.round(value * Math.pow(10, setting.priceScale)) / Math.pow(10, setting.priceScale)).toFixed(setting.priceScale);
  88. } else if (setting.priceRoundType == "roundUp") {
  89. price = (Math.ceil(value * Math.pow(10, setting.priceScale)) / Math.pow(10, setting.priceScale)).toFixed(setting.priceScale);
  90. } else {
  91. price = (Math.floor(value * Math.pow(10, setting.priceScale)) / Math.pow(10, setting.priceScale)).toFixed(setting.priceScale);
  92. }
  93. if (showSign) {
  94. price = setting.currencySign + price;
  95. }
  96. if (showUnit) {
  97. price += setting.currencyUnit;
  98. }
  99. return price;
  100. }
  101. }
  102. // 多语言
  103. function message(code) {
  104. if (code != null) {
  105. var content = messages[code] != null ? messages[code] : code;
  106. if (arguments.length == 1) {
  107. return content;
  108. } else {
  109. if ($.isArray(arguments[1])) {
  110. $.each(arguments[1], function(i, n) {
  111. content = content.replace(new RegExp("\\{" + i + "\\}", "g"), n);
  112. });
  113. return content;
  114. } else {
  115. $.each(Array.prototype.slice.apply(arguments).slice(1), function(i, n) {
  116. content = content.replace(new RegExp("\\{" + i + "\\}", "g"), n);
  117. });
  118. return content;
  119. }
  120. }
  121. }
  122. }
  123. (function($) {
  124. var zIndex = 100;
  125. // 检测登录
  126. $.checkLogin = function() {
  127. var result = false;
  128. $.ajax({
  129. url: shopxx.base + "/login/check.jhtml",
  130. type: "GET",
  131. dataType: "json",
  132. cache: false,
  133. async: false,
  134. success: function(data) {
  135. result = data;
  136. }
  137. });
  138. return result;
  139. }
  140. // 跳转登录
  141. $.redirectLogin = function (redirectUrl, message) {
  142. var href = shopxx.base + "/login.jhtml";
  143. if (redirectUrl != null) {
  144. href += "?redirectUrl=" + encodeURIComponent(redirectUrl);
  145. }
  146. if (message != null) {
  147. $.message("warn", message);
  148. setTimeout(function() {
  149. location.href = href;
  150. }, 1000);
  151. } else {
  152. location.href = href;
  153. }
  154. }
  155. // 消息框
  156. var $message;
  157. var messageTimer;
  158. $.message = function() {
  159. var message = {};
  160. if ($.isPlainObject(arguments[0])) {
  161. message = arguments[0];
  162. } else if (typeof arguments[0] === "string" && typeof arguments[1] === "string") {
  163. message.type = arguments[0];
  164. message.content = arguments[1];
  165. } else {
  166. return false;
  167. }
  168. if (message.type == null || message.content == null) {
  169. return false;
  170. }
  171. if ($message == null) {
  172. $message = $('<div class="xxMessage"><div class="messageContent message' + message.type + 'Icon"><\/div><\/div>');
  173. if (!window.XMLHttpRequest) {
  174. $message.append('<iframe class="messageIframe"><\/iframe>');
  175. }
  176. $message.appendTo("body");
  177. }
  178. $message.children("div").removeClass("messagewarnIcon messageerrorIcon messagesuccessIcon").addClass("message" + message.type + "Icon").html(message.content);
  179. $message.css({"margin-left": - parseInt($message.outerWidth() / 2), "z-index": zIndex ++}).show();
  180. clearTimeout(messageTimer);
  181. messageTimer = setTimeout(function() {
  182. $message.hide();
  183. }, 3000);
  184. return $message;
  185. }
  186. // 令牌
  187. $(document).ajaxSend(function(event, request, settings) {
  188. if (!settings.crossDomain && settings.type != null && settings.type.toLowerCase() == "post") {
  189. var token = getCookie("token");
  190. if (token != null) {
  191. request.setRequestHeader("token", token);
  192. }
  193. }
  194. });
  195. $(document).ajaxComplete(function(event, request, settings) {
  196. var loginStatus = request.getResponseHeader("loginStatus");
  197. var tokenStatus = request.getResponseHeader("tokenStatus");
  198. if (loginStatus == "accessDenied") {
  199. $.redirectLogin(location.href, "请登录后再进行操作");
  200. } else if (tokenStatus == "accessDenied") {
  201. var token = getCookie("token");
  202. if (token != null) {
  203. $.extend(settings, {
  204. global: false,
  205. headers: {token: token}
  206. });
  207. $.ajax(settings);
  208. }
  209. }
  210. });
  211. })(jQuery);
  212. // 令牌
  213. $().ready(function() {
  214. $("form").submit(function() {
  215. var $this = $(this);
  216. if ($this.attr("method") != null && $this.attr("method").toLowerCase() == "post" && $this.find("input[name='token']").size() == 0) {
  217. var token = getCookie("token");
  218. if (token != null) {
  219. $this.append('<input type="hidden" name="token" value="' + token + '" \/>');
  220. }
  221. }
  222. });
  223. });
  224. // 验证消息
  225. if ($.validator != null) {
  226. $.extend($.validator.messages, {
  227. required: message("shop.validate.required"),
  228. email: message("shop.validate.email"),
  229. url: message("shop.validate.url"),
  230. date: message("shop.validate.date"),
  231. dateISO: message("shop.validate.dateISO"),
  232. pointcard: message("shop.validate.pointcard"),
  233. number: message("shop.validate.number"),
  234. digits: message("shop.validate.digits"),
  235. minlength: $.validator.format(message("shop.validate.minlength")),
  236. maxlength: $.validator.format(message("shop.validate.maxlength")),
  237. rangelength: $.validator.format(message("shop.validate.rangelength")),
  238. min: $.validator.format(message("shop.validate.min")),
  239. max: $.validator.format(message("shop.validate.max")),
  240. range: $.validator.format(message("shop.validate.range")),
  241. accept: message("shop.validate.accept"),
  242. equalTo: message("shop.validate.equalTo"),
  243. remote: message("shop.validate.remote"),
  244. integer: message("shop.validate.integer"),
  245. positive: message("shop.validate.positive"),
  246. negative: message("shop.validate.negative"),
  247. decimal: message("shop.validate.decimal"),
  248. pattern: message("shop.validate.pattern"),
  249. extension: message("shop.validate.extension")
  250. });
  251. $.validator.setDefaults({
  252. errorClass: "fieldError",
  253. ignore: ".ignore",
  254. ignoreTitle: true,
  255. errorPlacement: function(error, element) {
  256. var fieldSet = element.closest("span.fieldSet");
  257. if (fieldSet.size() > 0) {
  258. error.appendTo(fieldSet);
  259. } else {
  260. error.insertAfter(element);
  261. }
  262. },
  263. submitHandler: function(form) {
  264. $(form).find(":submit").prop("disabled", true);
  265. form.submit();
  266. }
  267. });
  268. }