core.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. define(['jquery', 'foxui', 'tpl'], function ($, FoxUI, tpl) {
  2. window.FoxUI = FoxUI;
  3. var defaults = {baseUrl: '', siteUrl: '', staticUrl: '../addons/ewei_shopv2/static/'};
  4. var core = {options: {}};
  5. core.init = function (options) {
  6. this.options = $.extend({}, defaults, options || {})
  7. };
  8. core.toQueryPair = function (key, value) {
  9. if (typeof value == 'undefined') {
  10. return key
  11. }
  12. return key + '=' + encodeURIComponent(value === null ? '' : String(value))
  13. };
  14. core.number_format = function (number, fix) {
  15. var fix = arguments[1] ? arguments[1] : 2;
  16. var fh = ',';
  17. var jg = 3;
  18. var str = '';
  19. number = number.toFixed(fix);
  20. zsw = number.split('.')[0];
  21. xsw = number.split('.')[1];
  22. zswarr = zsw.split('');
  23. for (var i = 1; i <= zswarr.length; i++) {
  24. str = zswarr[zswarr.length - i] + str;
  25. if (i % jg == 0) {
  26. str = fh + str;
  27. }
  28. }
  29. str = (zsw.length % jg == 0) ? str.substr(1) : str;
  30. zsw = str + '.' + xsw;
  31. return zsw
  32. };
  33. core.toQueryString = function (obj) {
  34. var ret = [];
  35. for (var key in obj) {
  36. key = encodeURIComponent(key);
  37. var values = obj[key];
  38. if (values && values.constructor == Array) {
  39. var queryValues = [];
  40. for (var i = 0, len = values.length, value; i < len; i++) {
  41. value = values[i];
  42. queryValues.push(this.toQueryPair(key, value))
  43. }
  44. ret = ret.concat(queryValues)
  45. } else {
  46. ret.push(this.toQueryPair(key, values))
  47. }
  48. }
  49. return ret.join('&')
  50. };
  51. core.getUrl = function (routes, params, full) {
  52. routes = routes.replace(/\//ig, ".");
  53. var url = this.options.baseUrl.replace('ROUTES', routes);
  54. if (params) {
  55. if (typeof(params) == 'object') {
  56. url += "&" + this.toQueryString(params)
  57. } else if (typeof(params) == 'string') {
  58. url += "&" + params
  59. }
  60. }
  61. return full ? this.options.siteUrl + 'app/' + url : url
  62. };
  63. core.json = function (routes, args, callback, hasloading, ispost) {
  64. var url = ispost ? this.getUrl(routes) : this.getUrl(routes, args);
  65. var op = {
  66. url: url, type: ispost ? 'post' : 'get', dataType: 'json', cache: false, beforeSend: function () {
  67. if (hasloading) {
  68. FoxUI.loader.show('mini')
  69. }
  70. }, error: function (a) {
  71. /*alert(JSON.stringify(a));*/
  72. if (hasloading) {
  73. FoxUI.loader.hide()
  74. }
  75. }
  76. };
  77. if (args && ispost) {
  78. op.data = args
  79. }
  80. if (callback) {
  81. op.success = function (data) {
  82. if (hasloading) {
  83. FoxUI.loader.hide()
  84. }
  85. callback(data)
  86. }
  87. }
  88. $.ajax(op)
  89. };
  90. core.post = function (routes, args, callback, hasloading) {
  91. this.json(routes, args, callback, hasloading, true)
  92. };
  93. core.html = function (routes, args, callback, hasloading, async) {
  94. if (typeof async === undefined) {
  95. async = true
  96. }
  97. var op = {
  98. url: this.getUrl(routes, args),
  99. type: 'get',
  100. cache: false,
  101. dataType: 'html',
  102. async: async,
  103. beforeSend: function () {
  104. if (hasloading) {
  105. FoxUI.loader.show('mini')
  106. }
  107. },
  108. error: function () {
  109. core.removeLoading();
  110. if (hasloading) {
  111. FoxUI.loader.hide()
  112. }
  113. }
  114. };
  115. if (callback) {
  116. op.success = function (html) {
  117. if (hasloading) {
  118. FoxUI.loader.hide()
  119. }
  120. callback(html)
  121. }
  122. }
  123. $.ajax(op)
  124. };
  125. core.tpl = function (containerid, templateid, data, append) {
  126. if (typeof append === undefined) {
  127. append = false
  128. }
  129. var html = tpl(templateid, data);
  130. if (append) {
  131. $(containerid).append(html)
  132. } else {
  133. $(containerid).html(html)
  134. }
  135. setTimeout(function () {
  136. $(containerid).closest('.fui-content').lazyload('render')
  137. }, 10)
  138. };
  139. core.getNumber = function (str) {
  140. str = $.trim(str);
  141. if (str == '') {
  142. return 0
  143. }
  144. return parseFloat(str.replace(',', ''))
  145. };
  146. core.showIframe = function (url) {
  147. var if_w = "100%";
  148. var if_h = $(document.body).height();
  149. $("<iframe width='" + if_w + "' height='" + if_h + "' id='mainFrame' name='mainFrame' style='position:absolute;z-index:4;' frameborder='no' marginheight='0' marginwidth='0' ></iframe>").prependTo('body');
  150. var st = document.documentElement.scrollTop || document.body.scrollTop;
  151. var sl = document.documentElement.scrollLeft || document.body.scrollLeft;
  152. var ch = document.documentElement.clientHeight;
  153. var cw = document.documentElement.clientWidth;
  154. var objH = $("#mainFrame").height();
  155. var objW = $("#mainFrame").width();
  156. var objT = Number(st) + (Number(ch) - Number(objH)) / 2;
  157. var objL = Number(sl) + (Number(cw) - Number(objW)) / 2;
  158. $("#mainFrame").css('left', objL);
  159. $("#mainFrame").css('top', objT);
  160. $("#mainFrame").attr("src", url)
  161. };
  162. core.getDistanceByLnglat = function (lng1, lat1, lng2, lat2) {
  163. function rad(d) {
  164. return d * Math.PI / 180.0
  165. }
  166. var rad1 = rad(lat1), rad2 = rad(lat2);
  167. var a = rad1 - rad2, b = rad(lng1) - rad(lng2);
  168. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));
  169. s = s * 6378137.0;
  170. s = Math.round(s * 10000) / 10000000;
  171. return s
  172. };
  173. core.showImages = function(imgClass){
  174. var ua = navigator.userAgent.toLowerCase();
  175. var isWX = ua.match(/MicroMessenger/i) == "micromessenger";
  176. var z = [];
  177. $(imgClass).each(function() {
  178. var img = $(this).attr("data-lazy");
  179. z.push(img?img:$(this).attr("src"));
  180. });
  181. var current;
  182. if (isWX) {
  183. $(imgClass).unbind('click').click(function(e) {
  184. e.preventDefault();
  185. var startingIndex = $(imgClass).index($(e.currentTarget));
  186. var current = null;
  187. $(imgClass).each(function(B, A) {
  188. if (B === startingIndex) {
  189. current = $(A).attr("data-lazy")?$(A).attr("data-lazy"):$(A).attr("src");
  190. }
  191. });
  192. WeixinJSBridge.invoke("imagePreview", {
  193. current: current,
  194. urls: z
  195. });
  196. });
  197. }
  198. };
  199. core.ish5app = function () {
  200. var userAgent = navigator.userAgent;
  201. if (userAgent.indexOf('CK 2.0') > -1){
  202. return true;
  203. }
  204. return false;
  205. };
  206. core.isWeixin = function () {
  207. var ua = navigator.userAgent.toLowerCase();
  208. var isWX = ua.match(/MicroMessenger/i) == "micromessenger";
  209. return isWX;
  210. };
  211. window.core = core;
  212. return core
  213. });