ecshop套模板常用函数整理

公共:

[code lang=”php”]

/**
* 通过goodsid,brandid 获取月售数量
* $type :goods 商店brand
* $date 成交天数 -1 为所有
*/
function yueshou($id,$type=’goods’,$date=30){
if($date>0) $alltime = " and oi.add_time>UNIX_TIMESTAMP()-86400*".$date;
if($type==’goods’){
$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);
}else{
$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);
}

return intval($row);
}

/**
* 获取收藏数量
* @param undefined $id
* @param undefined $type goods,brand
*
*/
function get_shoucan_num($id,$type=’goods’){
if($type=="goods"){
return $GLOBALS[‘db’]->getOneCached("SELECT COUNT(*) as cnum FROM " . $GLOBALS[‘ecs’]->table(‘collect_goods’) . " where goods_id =’$id’ ");
}else{
return $GLOBALS[‘db’]->getOneCached("SELECT COUNT(*) as cnum FROM " . $GLOBALS[‘ecs’]->table(‘collect_goods_sj’) . " where brand_id =’$id’ ");
}
}

/**
* goodid 获取 评论星级 ,评论总数
* 2014 12 10 shanmao.me
* 返回 评论总数,星级,星级html
*/
function good_id_re_commavg($goods_id){
$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’");
$row[‘crank’] = ceil($row[‘crank’]) == 0 ? 5 : ceil($row[‘crank’]);
for($i=1;$i<=5;$i++){
if($i<=$row[‘crank’])
$row[‘cstar’] .= ‘<em class="select"></em>’;
else
$row[‘cstar’] .= ‘<em></em>’;
}
return $row;
}

/**
* 获取评论总数 看是否需要用到上面的! good_id_re_commavg
* @param undefined $id
* @param undefined $type
*
*/

function get_comment_count($id,$type=0){ //活得商品评论个数 $type =0 商品 =1 文章
$count = $GLOBALS[‘db’]->getOne(‘SELECT COUNT(*) FROM ‘ .$GLOBALS[‘ecs’]->table(‘comment’).
" WHERE id_value = ‘$id’ AND comment_type = ‘$type’ AND status = 1 AND parent_id = 0");
return $count;
}

//正则匹配手机格式
function is_moblie($mobile){
$chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|1[0-9]{2}[0-9]{8}$/";
if (preg_match($chars, $mobile)){
return true;
}else{
return false;
}
}

/**
* 获得指定地址
*
* @access public
* @param int country 国家的编号
* @return array
*/
function a_get_regions($region = 0, $region2 = 0,$region3 = 0)
{
$sql = ‘SELECT region_id, region_name FROM ‘ . $GLOBALS[‘ecs’]->table(‘region’) .
" WHERE region_id = ‘$region’ or region_id = ‘$region2’ or region_id = ‘$region3’ ";
$data = $GLOBALS[‘db’]->getAllCached($sql);
$region_name =array();
if($data){
foreach ($data as $v){
$region_name[] = $v[‘region_name’];
}
}
//$region_name = implode(‘ ‘, $region_name);
return $region_name;
}

/**
* 验证手机号码是否正确 2015-5-7 shanmao.me
* @param undefined $mob
* @param undefined $code
*
*/
function checkmobcode($mob,$code){
if(!is_moblie($mob)) return ‘手机号码不正确’;
$sql = "select time from aoy_mob_code where mob = ".$mob." and code=’".$code."’ order by id desc limit 1";
$btime = $GLOBALS[‘db’]->getOne($sql);
if($btime && $btime+600 >gmtime()){ //10分钟内
return ‘success’;
}else{
return ‘验证码不正确或已超时’;
}
}

/**
*
* 发送短信验证码
* 2015-4-21 山猫
* 返回成功状态: 发送成功
* */
function send_mobcode($mob,$code){
if(!is_moblie($mob)) return ‘手机号码不正确’;
$sql = "select time from ecs_mob_code where mob = ".$mob." and status=1 order by id desc limit 1";
$btime = $GLOBALS[‘db’]->getOne($sql);
if($btime+60 >gmtime()){
return ‘1分钟内一个手机号只能发送一次’;
}else{
$data[‘mob’]=$mob;
$data[‘code’]=$code;
$data[‘ip’]=real_ip();
$data[‘ip2’]=ip2long($data[‘ip’]);
$data[‘time’]=gmtime();
$data[‘status’]=0;//默认不成功
if(1){
$url = ‘http://106.dxton.com/webservice/sms.asmx/Submit?account=ssss&password=xxxxxx&mobile=’.$mob.’&content=’."您的验证码是:【".$code."】。请不要把验证码泄露给其他人。如非本人操作,可不用理会!";

$res = file_get_contents($url);
//var_dump($res);
$ref = json_decode(json_encode((array) simplexml_load_string($res)), true);
if($ref[‘result’]==100){
$data[‘status’]=1;
}
$re = $GLOBALS[‘db’]->autoExecute(‘aoy_mob_code’,$data);
//
return $ref[‘message’];

}
}

}
[/code]

自定义php:

[code lang=”php”]
header("Content-Type:text/html;charset=utf-8");
define(‘IN_ECS’, true);

require(dirname(__FILE__) . ‘/includes/init.php’);
include_once(ROOT_PATH . ‘includes/lib_order.php’);
$_LANG[‘no_basic_goods’] = ‘对不起,您希望将该商品做为配件购买,可是购物车中还没有该商品的基本件。’;
$_LANG[‘not_on_sale’] = ‘对不起,该商品已经下架。’;
$_LANG[‘cannt_alone_sale’] = ‘对不起,该商品不能单独销售。’;
$_LANG[‘shortage’] = "对不起,该商品已经库存不足暂停销售。";

$act =$_GET[‘type’];
switch($act){
case "getcode"://获取手机验证码
$code = rand(111111,999999);
$re = send_mobcode($_POST[‘mob’],$code);
if($re==’发送成功’){
exit(‘1’);
}else{
if($re){
exit($re);
}else{
exit(‘发送失败,请重新发送’);
}

}
break;
case "checkcode":
$re = checkmobcode($_POST[‘mob’],intval($_POST[‘code’]));
if($re==’success’){
if($_POST[‘pass’]){//修改密码
$uinfo = $GLOBALS[‘db’]->getRow("select * from ".$GLOBALS[‘ecs’]->table(‘users’)." where mobile_phone = ".$_POST[‘mob’]);
if($uinfo){
$data[‘password’] = $uinfo[‘ec_salt’]?md5(md5($_POST[‘pass’]).$uinfo[‘ec_salt’]):md5($_POST[‘pass’]);
$GLOBALS[‘db’]->autoExecute($GLOBALS[‘ecs’]->table(‘users’), $data, ‘UPDATE’,’user_id=’.$uinfo[‘user_id’]);
}else{
exit("没有找到用户信息");
}
}
exit(‘1’);
}else{
exit($re);
}
break;
case "addcart":
$goodid = addslashes($_POST[‘goodid’]);
$num = addslashes($_POST[‘num’]);
$num = $num? $num:1;
if(addto_cart($goodid,$num)){
$result[‘info’]=’添加成功’;
$result[‘num’] =$num ;
$result[‘error’]=0;
}else{
$info = $err->last_message();
$result[‘info’] =$info[0] ;
$result[‘error’] = $err->error_no;
//var_dump($result);
}
die(json_encode($result));
break;

default:

echo ‘err action’;
break;
}

[/code]

前台好用的js

[code lang=”js”]

function addToCart(goodid,num){// add by shanmao.me

$.post("ajax.php?type=addcart",{goodid:goodid,num:num},function(data){

alertmsg(data.info);

var carnum=$("#carnum").text();

$("#carnum").text(Number(carnum)+Number(data.num));

},’json’);

}

function addToCartbytj(tid){//鎺ㄨ崘鏉ョ殑鐐硅禐

$.post("ajax.php?type=tjaddtocart",{tid:tid},function(data){

alertmsg(data.msg);

if(data.code==1){

reloadpage(2,’flow.php?step=checkout’);

//window.location.href=’flow.php?step=checkout’;

}

},’json’);

}

function collect(goodsId,type,vthis) // type = sj 是商家 2015-7-24

{

$.get(‘user.php?act=collect&id=’ + goodsId+’&type=’+type,function(data){
if(data.error==0){
$(vthis).addClass("select");
}
alertmsg(data.message);

},’json’);

}

function reloadpage(t,url){

if(!t)t=1;

t=t*1000;

if(url){

setTimeout("window.location.href=’"+url+"’",t);

}else{

setTimeout("window.location.reload()",t);

}

}

function alertmsg(msg){

alert(msg);
return false;
$("#alertmsg").html(msg);

$(".dhcg-box").show();

}

$(".cell-num-left").click(function(){
var cellNum = $(this).parent().find(‘input’).val();
cellNum–;
if (cellNum<=0) {
cellNum=0;
};
changegoodsnum(cellNum)
var recid = $(this).attr(‘recid’);
if(recid) changegwcnum(recid,cellNum);
$(this).parent().find(‘input’).val(cellNum)
})
$(".cell-num-right").click(function(){
var cellNum = $(this).parent().find(‘input’).val();
cellNum++;
changegoodsnum(cellNum)
var recid = $(this).attr(‘recid’);
if(recid) changegwcnum(recid,cellNum)
$(this).parent().find(‘input’).val(cellNum)
})

function changegoodsnum(num){ //改变商品数量 改变价格。
var goodsid = $("#goodsid").val();
if(!goodsid) return false;
$.get("goods.php?act=price&id="+goodsid+"&attr=&number="+num+"&",function(data){
$("#zongjia").html(data.result);
},’json’)
}

function changegwcnum(recid,num){//改变购物车内商品价格
if(!recid) return false;
var arr = {};
arr[‘goods_number[‘+recid+’]’]=num;
$.post("flow.php?step=update_cart&ptype=ajax",arr,function(data){
if(data.content==1){
window.location.reload();
}else{
alert(data.content);
}
},’json’)
}
[/code]