当前位置:首页 > CMS教程 > ecshop > 列表

ecshop模板常用函数整理总结

发布:smiling 来源: PHP粉丝网  添加日期:2015-12-07 13:35:11 浏览: 评论:0 

ecshop系统有许多常用的模块函数了,这里我们整理了一些非常实用的函数给各位参考,有兴趣进来看看小编整理的ecshop模板常用函数了,希望例子能够对大家有用.

  1. /** 
  2. * 通过goodsid,brandid 获取月售数量 
  3. * $type :goods 商店brand 
  4. * $date 成交天数  -1 为所有 
  5. */ 
  6. function yueshou($id,$type='goods',$date=30){ 
  7.     if($date>0) $alltime = " and oi.add_time>UNIX_TIMESTAMP()-86400*".$date; 
  8.     if($type=='goods'){ 
  9.             $row = $GLOBALS['db']->getOneCached("SELECT sum(og.goods_number) as gnum FROM " . $GLOBALS['ecs']->table('order_goods') . " as og left join " . $GLOBALS['ecs']->table('order_info') . " as oi ON oi.order_id=og.order_id  where og.goods_id ='$id'".$alltime); 
  10.     }else{ 
  11.             $row = $GLOBALS['db']->getOneCached("SELECT sum(og.goods_number) as gnum FROM " . $GLOBALS['ecs']->table('order_goods') . " as og left join " . $GLOBALS['ecs']->table('order_info') . " as oi ON oi.order_id=og.order_id  where og.brandid ='$id'".$alltime); 
  12.     } 
  13.  
  14.     return intval($row); 
  15. } 
  16.  
  17.  
  18.  
  19.  
  20. /** 
  21. * 获取收藏数量 
  22. * @param undefined $id 
  23. * @param undefined $type  goods,brand 
  24. *  
  25. */ 
  26. function get_shoucan_num($id,$type='goods'){ 
  27. if($type=="goods"){ 
  28. return  $GLOBALS['db']->getOneCached("SELECT COUNT(*) as cnum FROM " . $GLOBALS['ecs']->table('collect_goods') . " where  goods_id ='$id' "); 
  29. }else{ 
  30. return  $GLOBALS['db']->getOneCached("SELECT COUNT(*) as cnum FROM " . $GLOBALS['ecs']->table('collect_goods_sj') . " where  brand_id ='$id' ");   
  31. } 
  32. } 
  33.  
  34.  
  35.  
  36.  
  37.  
  38. /** 
  39. * goodid 获取 评论星级 ,评论总数 
  40. * 2014 12 10 shanmao.me 
  41. * 返回 评论总数,星级,星级html 
  42. */ 
  43. function good_id_re_commavg($goods_id){ 
  44.     $row = $GLOBALS['db']->getRowCached("SELECT COUNT(*) as cnum,IFNULL(AVG(comment_rank), 0) AS crank FROM " . $GLOBALS['ecs']->table('comment') . " where comment_type=0 and id_value ='$goods_id'"); 
  45.      $row['crank']  = ceil($row['crank']) == 0 ? 5 : ceil($row['crank']); 
  46.      for($i=1;$i<=5;$i++){ 
  47.         if($i<=$row['crank']) 
  48.         $row['cstar'] .= '<em class="select"></em>'; 
  49.         else 
  50.         $row['cstar'] .= '<em></em>'; 
  51.      }    
  52.     return $row; 
  53. } 
  54.  
  55. /**    
  56. * 获取评论总数  看是否需要用到上面的!  good_id_re_commavg 
  57. * @param undefined $id 
  58. * @param undefined $type 
  59. *  
  60. */ 
  61.  
  62. function  get_comment_count($id,$type=0){  //活得商品评论个数 $type =0 商品 =1 文章 
  63.     $count = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM ' .$GLOBALS['ecs']->table('comment'). 
  64.            " WHERE id_value = '$id' AND comment_type = '$type' AND status = 1 AND parent_id = 0"); 
  65.            return $count; 
  66. } 
  67.  
  68.  
  69. //正则匹配手机格式  
  70. function is_moblie($mobile){ 
  71.     $chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|1[0-9]{2}[0-9]{8}$/"; 
  72.     if (preg_match($chars, $mobile)){ 
  73.         return true; 
  74.     }else{ 
  75.         return false; 
  76.     } 
  77. } 
  78.  
  79. /** 
  80.  * 获得指定地址 
  81.  * 
  82.  * @access      public 
  83.  * @param       int     country    国家的编号 
  84.  * @return      array 
  85.  */ 
  86. function a_get_regions($region = 0, $region2 = 0,$region3 = 0) 
  87. { 
  88.     $sql = 'SELECT region_id, region_name FROM ' . $GLOBALS['ecs']->table('region') . 
  89.     " WHERE  region_id = '$region' or  region_id = '$region2' or  region_id = '$region3' "; 
  90.     $data = $GLOBALS['db']->getAllCached($sql); 
  91.     $region_name =array(); 
  92.     if($data){ 
  93.         foreach ($data as $v){ 
  94.             $region_name[] = $v['region_name']; 
  95.         } 
  96.     } 
  97.     //$region_name = implode(' ', $region_name); 
  98.     return $region_name; 
  99. } 
  100.  
  101. /** 
  102.  * 验证手机号码是否正确  2015-5-7 shanmao.me 
  103.  * @param undefined $mob 
  104.  * @param undefined $code 
  105.  * 
  106.  */ 
  107. function checkmobcode($mob,$code){ 
  108.     if(!is_moblie($mob)) return '手机号码不正确'; 
  109.     $sql = "select time from aoy_mob_code where mob = ".$mob." and  code='".$code."' order by id desc limit 1"; 
  110.     $btime = $GLOBALS['db']->getOne($sql); 
  111.     if($btime && $btime+600 >gmtime()){ //10分钟内 
  112.         return 'success'; 
  113.     }else{ 
  114.         return '验证码不正确或已超时'; 
  115.     } 
  116. } 
  117.  
  118. /** 
  119.  * 
  120.  * 发送短信验证码 
  121.  * 2015-4-21 山猫 
  122.  * 返回成功状态:  发送成功 
  123.  * */ 
  124. function send_mobcode($mob,$code){ 
  125.     if(!is_moblie($mob)) return '手机号码不正确'; 
  126.     $sql = "select time from ecs_mob_code where mob = ".$mob." and status=1 order by id desc limit 1"; 
  127.     $btime = $GLOBALS['db']->getOne($sql); 
  128.     if($btime+60 >gmtime()){ 
  129.         return '1分钟内一个手机号只能发送一次'; 
  130.     }else{ 
  131.         $data['mob']=$mob; 
  132.         $data['code']=$code; 
  133.         $data['ip']=real_ip(); 
  134.         $data['ip2']=ip2long($data['ip']); 
  135.         $data['time']=gmtime(); 
  136.         $data['status']=0;//默认不成功 
  137.         if(1){ 
  138.             $url = 'http://106.dxton.com/webservice/sms.asmx/Submit?account=ssss&password=xxxxxx&mobile='.$mob.'&content='."您的验证码是:【".$code."】。请不要把验证码泄露给其他人。如非本人操作,可不用理会!"; 
  139.                   
  140.             $res  = file_get_contents($url); 
  141.             //var_dump($res); 
  142.             $ref = json_decode(json_encode((array) simplexml_load_string($res)), true); //phpfensi.com 
  143.             if($ref['result']==100){ 
  144.                 $data['status']=1; 
  145.             } 
  146.             $re = $GLOBALS['db']->autoExecute('aoy_mob_code',$data); 
  147.             // 
  148.             return  $ref['message']; 
  149.                   
  150.         } 
  151.     } 
  152.  
  153. } 

自定义php:

  1. header("Content-Type:text/html;charset=utf-8"); 
  2. define('IN_ECS', true); 
  3.  
  4. require(dirname(__FILE__) . '/includes/init.php'); 
  5. include_once(ROOT_PATH . 'includes/lib_order.php'); 
  6.     $_LANG['no_basic_goods'] = '对不起,您希望将该商品做为配件购买,可是购物车中还没有该商品的基本件。'; 
  7.         $_LANG['not_on_sale'] = '对不起,该商品已经下架。'; 
  8.         $_LANG['cannt_alone_sale'] = '对不起,该商品不能单独销售。'; 
  9.         $_LANG['shortage'] = "对不起,该商品已经库存不足暂停销售。"; 
  10.           
  11.           
  12. $act =$_GET['type']; 
  13. switch($act){ 
  14.     case "getcode"://获取手机验证码 
  15.     $code = rand(111111,999999);     
  16.     $re = send_mobcode($_POST['mob'],$code); 
  17.     if($re=='发送成功'){ 
  18.         exit('1'); 
  19.     }else{ 
  20.         if($re){ 
  21.             exit($re); 
  22.         }else{ 
  23.             exit('发送失败,请重新发送'); 
  24.         } 
  25.           
  26.     } 
  27.     break; 
  28.     case "checkcode": 
  29.     $re = checkmobcode($_POST['mob'],intval($_POST['code'])); 
  30.     if($re=='success'){ 
  31.     if($_POST['pass']){//修改密码 
  32.     $uinfo = $GLOBALS['db']->getRow("select * from ".$GLOBALS['ecs']->table('users')." where mobile_phone = ".$_POST['mob']); 
  33.     if($uinfo){  
  34.     $data['password'] = $uinfo['ec_salt']?md5(md5($_POST['pass']).$uinfo['ec_salt']):md5($_POST['pass']); 
  35.      $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('users'), $data, 'UPDATE','user_id='.$uinfo['user_id']);  
  36.     }else{ 
  37.         exit("没有找到用户信息"); 
  38.     } 
  39.     } 
  40.         exit('1'); 
  41.     }else{ 
  42.         exit($re); 
  43.     } 
  44.     break; 
  45.     case "addcart": 
  46.         $goodid = addslashes($_POST['goodid']); 
  47.         $num = addslashes($_POST['num']); 
  48.         $num = $num? $num:1;     
  49.         if(addto_cart($goodid,$num)){ 
  50.         $result['info']='添加成功'; 
  51.         $result['num']  =$num ; 
  52.         $result['error']=0;  
  53.         }else{ //phpfensi.com 
  54.             $info = $err->last_message();             
  55.             $result['info']  =$info[0] ; 
  56.             $result['error']    = $err->error_no; 
  57.             //var_dump($result); 
  58.         } 
  59.     die(json_encode($result)); 
  60.     break; 
  61.  
  62.     default: 
  63.  
  64.     echo 'err action'; 
  65.     break; 
  66. } 

前台好用的js:

  1. function addToCart(goodid,num){// add by shanmao.me 
  2.  
  3.     $.post("ajax.php?type=addcart",{goodid:goodid,num:num},function(data){ 
  4.  
  5.             alertmsg(data.info); 
  6.  
  7.             var carnum=$("#carnum").text(); 
  8.  
  9.             $("#carnum").text(Number(carnum)+Number(data.num)); 
  10.  
  11.             },'json'); 
  12.  
  13.     } 
  14.  
  15. function addToCartbytj(tid){//?ㄨ???ョ???硅? 
  16.  
  17.     $.post("ajax.php?type=tjaddtocart",{tid:tid},function(data){ 
  18.  
  19.             alertmsg(data.msg); 
  20.  
  21.             if(data.code==1){ 
  22.  
  23.                 reloadpage(2,'flow.php?step=checkout'); 
  24.  
  25.             //window.location.href='flow.php?step=checkout'; 
  26.  
  27.             }    
  28.  
  29.             },'json'); 
  30.  
  31.     }    
  32.  
  33. function collect(goodsId,type,vthis)  // type = sj  是商家  2015-7-24 
  34.  
  35. { 
  36.  
  37.     $.get('user.php?act=collect&id=' + goodsId+'&type='+type,function(data){ 
  38.         if(data.error==0){ 
  39.             $(vthis).addClass("select"); 
  40.             } 
  41.         alertmsg(data.message); 
  42.  
  43.     },'json'); 
  44.  
  45. } 
  46.  
  47.  
  48. function reloadpage(t,url){ 
  49.  
  50.     if(!t)t=1; 
  51.  
  52.     t=t*1000; 
  53.  
  54.     if(url){ 
  55.  
  56.     setTimeout("window.location.href='"+url+"'",t);  
  57.  
  58.         }else{ 
  59.  
  60.     setTimeout("window.location.reload()",t); 
  61.  
  62.     } 
  63.  
  64.     } 
  65.  
  66.  
  67.  
  68. function alertmsg(msg){ 
  69.       
  70.     alert(msg); 
  71.     return false; 
  72.     $("#alertmsg").html(msg); 
  73.  
  74.     $(".dhcg-box").show(); 
  75.  
  76.     } 
  77.  
  78.     $(".cell-num-left").click(function(){ 
  79.         var cellNum = $(this).parent().find('input').val(); 
  80.         cellNum--; 
  81.         if (cellNum<=0) { 
  82.             cellNum=0; 
  83.         }; 
  84.         changegoodsnum(cellNum) 
  85.         var recid = $(this).attr('recid'); 
  86.         if(recid) changegwcnum(recid,cellNum); 
  87.         $(this).parent().find('input').val(cellNum) 
  88.     }) 
  89.     $(".cell-num-right").click(function(){ 
  90.         var cellNum = $(this).parent().find('input').val(); 
  91.         cellNum++; 
  92.         changegoodsnum(cellNum) 
  93.         var recid = $(this).attr('recid'); 
  94.         if(recid) changegwcnum(recid,cellNum) 
  95.         $(this).parent().find('input').val(cellNum) 
  96.     }) 
  97.  
  98. function changegoodsnum(num){ //改变商品数量 改变价格。 
  99.     var goodsid = $("#goodsid").val(); 
  100.     if(!goodsid) return false; 
  101.     $.get("goods.php?act=price&id="+goodsid+"&attr=&number="+num+"&",function(data){ 
  102.             $("#zongjia").html(data.result);                                                                           
  103.     },'json') 
  104.     } 
  105.       
  106. function changegwcnum(recid,num){//改变购物车内商品价格 
  107.         if(!recid) return false; 
  108.         var arr = {}; 
  109.         arr['goods_number['+recid+']']=num; 
  110.         $.post("flow.php?step=update_cart&ptype=ajax",arr,function(data){ 
  111.                     if(data.content==1){ 
  112.                         window.location.reload(); 
  113.                         }else{ 
  114.                         alert(data.content);     
  115.                             }                               
  116.         },'json') 
  117.     }

Tags: ecshop模板函数 ecshop常用函数

分享到: