|
@@ -3,96 +3,108 @@
|
|
|
* author: wangyang
|
|
|
* date: 2015-04-11
|
|
|
*/
|
|
|
-define(['config', 'native', 'zepto'], function(config, native, $) {
|
|
|
- (function($) {
|
|
|
- $.extend($, {
|
|
|
- getScript: function(src, func) {
|
|
|
- var script = document.createElement('script');
|
|
|
- script.async = 'async';
|
|
|
- script.src = src;
|
|
|
- if (func) {
|
|
|
- script.onload = func;
|
|
|
- }
|
|
|
- document.getElementsByTagName('head')[0].appendChild(script);
|
|
|
- },
|
|
|
- //根据域名判断使用ajax还是jsonP
|
|
|
- autoAjax: function(param) {
|
|
|
- param['no_sign'] = 1;
|
|
|
- if (location.host == 'yiguanjia.me') {
|
|
|
- $.ajax(param);
|
|
|
- } else {
|
|
|
- // api接口增加版本号
|
|
|
- param.data['version'] = config.o2oVersion;
|
|
|
- var url = param.url;
|
|
|
- if (url.indexOf('?') == -1) {
|
|
|
- url += '?';
|
|
|
- }
|
|
|
- if (url.charAt(url.length - 1) != '?') {
|
|
|
- url += '&';
|
|
|
- }
|
|
|
- url += 'callback=?';
|
|
|
- if (!!param.data) {
|
|
|
- if (url.charAt(url.length - 1) != '?') {
|
|
|
- url += '';
|
|
|
- }
|
|
|
- url += '&' + $.param(param.data);
|
|
|
- }
|
|
|
- $.ajaxJSONP({
|
|
|
- url: url,
|
|
|
- success: (!!param.success) ? param.success : null,
|
|
|
- error: (!!param.error) ? param.error : null
|
|
|
- })
|
|
|
- }
|
|
|
- },
|
|
|
- //带验证的ajax
|
|
|
- signAjax: function(options) {
|
|
|
- if (config.isIOS || config.isAndroid ) {
|
|
|
- var params = options.data;
|
|
|
- var sgf = options.success;
|
|
|
+define(['config', 'native', 'zepto'], function (config, native, $) {
|
|
|
+ (function ($) {
|
|
|
+ $.extend($, {
|
|
|
+ getScript: function (src, func) {
|
|
|
+ var script = document.createElement('script');
|
|
|
+ script.async = 'async';
|
|
|
+ script.src = src;
|
|
|
+ if (func) {
|
|
|
+ script.onload = func;
|
|
|
+ }
|
|
|
+ document.getElementsByTagName('head')[0].appendChild(script);
|
|
|
+ },
|
|
|
+ //根据域名判断使用ajax还是jsonP
|
|
|
+ autoAjax: function (param) {
|
|
|
+ param['no_sign'] = 1;
|
|
|
+ if (location.host == 'yiguanjia.me') {
|
|
|
+ $.ajax(param);
|
|
|
+ } else {
|
|
|
+ // api接口增加版本号
|
|
|
+ param.data['version'] = config.o2oVersion;
|
|
|
+ var url = param.url;
|
|
|
+ if (url.indexOf('?') == -1) {
|
|
|
+ url += '?';
|
|
|
+ }
|
|
|
+ if (url.charAt(url.length - 1) != '?') {
|
|
|
+ url += '&';
|
|
|
+ }
|
|
|
+ url += 'callback=?';
|
|
|
+ if (!!param.data) {
|
|
|
+ if (url.charAt(url.length - 1) != '?') {
|
|
|
+ url += '';
|
|
|
+ }
|
|
|
+ url += '&' + $.param(param.data);
|
|
|
+ }
|
|
|
+ $.ajaxJSONP({
|
|
|
+ url: url,
|
|
|
+ success: (!!param.success) ? param.success : null,
|
|
|
+ error: (!!param.error) ? param.error : null
|
|
|
+ })
|
|
|
+ }
|
|
|
+ },
|
|
|
+ //带验证的ajax
|
|
|
+ signAjax: function (options) {
|
|
|
+ if (config.isIOS || config.isAndroid) {
|
|
|
+ var params = options.data;
|
|
|
+ var sgf = options.success;
|
|
|
|
|
|
- options.success = function(res) {
|
|
|
- sgf(res);
|
|
|
- }
|
|
|
+ options.success = function (res) {
|
|
|
+ sgf(res);
|
|
|
+ }
|
|
|
|
|
|
- if (!!!params.time) {
|
|
|
- params.time = new Date().getTime() / 1000;
|
|
|
- }
|
|
|
- sign_queue.push(options);
|
|
|
- if (sign_queue.length == 1) {
|
|
|
- native.getSign(sign_queue[0].data, sign_callback);
|
|
|
- }
|
|
|
- } else {
|
|
|
- this.autoAjax(options);
|
|
|
- }
|
|
|
- },
|
|
|
- //touch绑定事件的封装
|
|
|
- newTouch: function(el, callback, parent_dom) {
|
|
|
- var event = 'tap';
|
|
|
- if (!((window.DocumentTouch && document instanceof DocumentTouch) || 'ontouchstart' in window)) {
|
|
|
- event = 'click';
|
|
|
- }
|
|
|
- if (parent_dom !== undefined) {
|
|
|
- $(parent_dom).on(event, el, function(ev) {
|
|
|
- callback.call(this, ev);
|
|
|
- });
|
|
|
- } else {
|
|
|
- $(el).on(event, function(ev) {
|
|
|
- callback.call(this, ev);
|
|
|
- });
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
- //由于加密是异步的,递归调用队列
|
|
|
- var sign_queue = [];
|
|
|
- function sign_callback(res) {
|
|
|
- var options = sign_queue.shift();
|
|
|
- options.data = res.data;
|
|
|
- $.autoAjax(options);
|
|
|
- if (sign_queue.length > 0) {
|
|
|
- native.getSign(sign_queue[0].data, sign_callback);
|
|
|
- }
|
|
|
- }
|
|
|
- })($);
|
|
|
+ if (!!!params.time) {
|
|
|
+ params.time = new Date().getTime() / 1000;
|
|
|
+ }
|
|
|
+ sign_queue.push(options);
|
|
|
+ if (sign_queue.length == 1) {
|
|
|
+ native.getSign(sign_queue[0].data, sign_callback);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ this.autoAjax(options);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ //touch绑定事件的封装
|
|
|
+ newTouch: function (el, callback, parent_dom) {
|
|
|
+ var event = 'tap';
|
|
|
+ if (!((window.DocumentTouch && document instanceof DocumentTouch) || 'ontouchstart' in window)) {
|
|
|
+ event = 'click';
|
|
|
+ }
|
|
|
+ if (parent_dom !== undefined) {
|
|
|
+ $(parent_dom).on(event, el, function (ev) {
|
|
|
+ callback.call(this, ev);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ $(el).on(event, function (ev) {
|
|
|
+ callback.call(this, ev);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+ // 时间戳转化
|
|
|
+ timetrans: function (date) {
|
|
|
+ let date = new Date(date * 1000);//如果date为13位不需要乘1000
|
|
|
+ let Y = date.getFullYear() + '-';
|
|
|
+ let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
|
|
|
+ let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
|
|
|
+ let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
|
|
|
+ let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
|
|
|
+ let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
|
|
|
+ return Y + M + D + h + m + s;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ //由于加密是异步的,递归调用队列
|
|
|
+ var sign_queue = [];
|
|
|
|
|
|
- return $;
|
|
|
+ function sign_callback(res) {
|
|
|
+ var options = sign_queue.shift();
|
|
|
+ options.data = res.data;
|
|
|
+ $.autoAjax(options);
|
|
|
+ if (sign_queue.length > 0) {
|
|
|
+ native.getSign(sign_queue[0].data, sign_callback);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })($);
|
|
|
+
|
|
|
+ return $;
|
|
|
})
|