Open fontEndEasy opened 8 years ago
'use strict'; /** * [jsonp description] jsonp 跨域请求ip * @param {[type]} url [description] * @param {Function} callback [description] * @return {[type]} [description] */ function jsonp( url, callback ) { if( typeof url === 'undefined' ){ return false; } var callbackName = 'callback'; window[ callbackName ] = function( data ) { try{ delete window[ callbackName ]; }catch( e ){}; document.body.removeChild( script ); callback( data ); }; var script = document.createElement( 'script' ); if( typeof url === 'string' ){ script.src = url + ( url.indexOf( '?' ) >= 0 ? '&' : '?' ) + 'jsonp=' + callbackName; document.body.appendChild( script ); } } /** * * ajax_post封装 * * @param { string } url 请求地址 * @param { data } data 请求参数 * @param { function } callback 回调 * @returns void * */ function ajax( url, data, callback ) { $.ajax({ type: 'post', url: url, data: data, dataType: 'json' }).done( function( response ) { if( callback ) { callback( response ); } }).fail(function( error ) { console.log( error ); console.log( XMLHttpRequest.status ); }) } /** * 获取url参数值 * * 获取url参数字段的值 * * @param { string } name 参数名称 * @returns { string } value * */ function getQuery( name ){ var reg = new RegExp( "(^|&)"+ name +"=([^&]*)(&|$)" ); var r = window.location.search.substr( 1 ).match( reg ); if ( r!==null ){ return unescape( r[ 2 ] ); }else { return null; } } /** * 选项卡切换 * * 选项卡点击切换操作 * * @param { string } trigger_element 触发元素 * @param { string } target_element 目标元素 * @param { string } on_class 高亮样式 * @returns void * */ function switchTab( trigger_element, target_element, on_class ) { $( trigger_element ).on( 'click',function(){ var $this = $( this ), index = $this.index(); $this.addClass( on_class ).siblings( trigger_element ).removeClass( on_class ); $( target_element ).eq( index ).show().siblings( target_element ).hide(); } ); } /** * getCartData 获取购物车数据 * * @param { function } callback 回调 * */ function getCartData( callback ){ var apiURL = apiRoot+'index.php?r=bazaar/cart/default/index', param = 'guid=1'; ajax( apiURL, param, function( ressponse ){ callback( ressponse ); } ); } /** * updateCartNum 更新购物车数量 * * @param { string } target 更新目标 * */ function updateCartNum( target ) { getCartData( function( ressponse ){ if( ressponse .errorcode === 0 ){ if( !ressponse.data ){ $( target ).text( 0 ); }else{ var sum_count = 0; $( ressponse.data.products ).each( function(){ sum_count += parseInt( this.quantity ); } ); $( target ).text( sum_count ); } } }); } /** * logout 退出登录 * * @param { function } callback 回调 * */ function logout( type, callback ){ var logout_apiURL = apiRoot+'index.php?r=admin/user/users/logout'; +function doLogout(){ ajax( logout_apiURL, '', function( ressponse ){ if( !ressponse.errorcode ){ if( callback ) { callback(); }else { location.href = 'login.html'; } } }); }(); } /** * 判断用户是否在线 * * @param {function} callback 回调 * */ function isOnline( callback ) { var apiURL = apiRoot+'index.php?r=admin/user/users/is-login'; ajax( apiURL, '', function( ressponse ){ if( callback ){ callback( ressponse ); } }); } /** * jquery 扩展 */ $.extend({ /* * checkNotNull 非空判断 * * @param {string} event 触发事件 * @param {string} target 判断目标 */ checkNotNull: function( event, target ){ var $target = $( target ); $target.on( event, function(){ var $this = $( this ); $this.parent().find( '.warning' ).remove(); if( $.trim( $target.val() ) === ''){ $this.parent().append( '<div class="warning tr abs hide">Can not be empty</div>' ); $this.next().removeClass( 'hide' ).addClass( 'animated shake' ); }else{ $this.next().remove(); } }); }, checkMail: function( event, target ){ var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; $( target ).on( event, function(){ var $this = $( this ), target_val = $this.val(); $this.parent().find( '.warning' ).remove(); if( !reg.test( target_val ) ){ $this.parent().append( '<div class="warning tr abs hide">Email wrong</div>' ); $this.next().removeClass( 'hide' ).addClass( 'animated shake' ); }else{ $this.next().remove(); } }); }, /* * onlyNumber 只能输入数值 * * @param { string } 触发目标 * return value 处理值 */ onlyNumber: function( target ){ return target.value = target.value.replace( /[^\d]/g, '' ).replace( /(\d{4})(?=\d)/g, "$1" ); } }); /** * getCartData 获取购物车数据 * * @param { function } callback 回调 * */ function getCartData( callback ){ var api = apiRoot+'index.php?r=bazaar/cart/default/index', param = ''; ajax( api, param, function( ressponse ){ callback( ressponse ); }); } /* * 返回顶部 * @param {string} show_target, 滚动出现目标(可为空 * @param {string} to_top, 返回顶部按钮 * @param {number} speed, 滚动速度 */ function backToTop( speed ) { var _top_ico = '<a href="#" class="back-to-top" title="Back to top"><i class="fa fa-angle-up"></i></a>'; $( '#element' ).after( _top_ico ); var $top = $( '.back-to-top' ); if ( $top !== "" ) { $( window ).scroll(function () { if ( $( this ).scrollTop() !== 0 ) { $top.show(); } else { $top.fadeOut( 200 ); } }); } $top.click(function() { $( "body, html" ).animate( { scrollTop: 0 }, speed ); return false; }); } /* * 将地址存储到服务器 * @param { string } target 接入数据目标 * @param { function } callback, 回调,可选 */ function saveAddressToService( target, callback ) { var add_apiURL = apiRoot+'index.php?r=admin/user/user-address/add-user-address', add_param = ''; ajax( add_apiURL, add_param, function( ressponse ){ if( callback ) { callback( ressponse )}; }); } /** * removeItemOfCart 删除用户购物车里的商品 * * 删除用户购物车单个商品的数据 */ function removeItemOfCart( item_id, callback ){ var api = apiRoot+'index.php?r=bazaar/cart/default/delete', param = 'id='+item_id; ajax( api, param, function( ressponse ){ if( callback ) { callback( ressponse );} }); } /** * doLogout 登出用户 * */ function doLogout( callback ){ var logout_apiURL = apiRoot+'index.php?r=admin/user/users/logout'; ajax( logout_apiURL, '', function( ressponse ){ if( !ressponse.errorcode ){ if( callback ) { callback(); } } } ); } /* * 添加头部购物车数据菜单 */ function addHeaderCart(){ getCartData(function( response ){ if( ressponse .errorcode === 0 ){ } }); }