<?php
require '../../conn/conn.php';
require '../../conn/function.php';
$action=$_POST["action"];
$D_domain=splitx($_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"],"/template",0);

if($action=="fav_post"){

  $type=splitx($_POST["post_id"],"_",0);
  $id=intval(splitx($_POST["post_id"],"_",1));
  switch($type){
    case "product":
      $C_type=0;
    break;
    case "news":
      $C_type=1;
    break;

    case "shop":
      $C_type=2;
      if($id==0){
        die('{"status":"0","msg":"官方自营店不支持收藏"}');
      }
    break;
    case "course":
      $C_type=3;
    break;
  }
  if(intval($_SESSION["M_id"])==0){
    die('{"status":"0","msg":"请登录会员后操作"}');

  }else{
    if(getrs("select * from sl_colletion where C_type=$C_type and C_mid=".intval($_SESSION["M_id"])." and C_cid=$id","C_id")!=""){
      mysqli_query($conn,"delete from sl_colletion where C_type=$C_type and C_mid=".intval($_SESSION["M_id"])." and C_cid=$id");
      die('{"status":"1","msg":"取消收藏"}');
    }else{
      mysqli_query($conn,"insert into sl_colletion(C_type,C_mid,C_cid) values($C_type,".intval($_SESSION["M_id"]).",$id)");
      die('{"status":"1","msg":"收藏成功"}');
    }
  }
}

if($action=="get_mpweixin_qr"){
  $genkey=gen_key(32);
  die('{"status":1,"ticket_img":"https://api.pwmqr.com/qrcode/create/?url='.urlencode(gethttp().$D_domain.'/pay/wxpay/login.php?genkey='.$genkey).'","scene_id":"'.$genkey.'"}');
}

if($action=="check_mpweixin_qr"){
  $scene_id=t($_POST["scene_id"]);

  $sql = "select * from sl_member where M_pwdcode='$scene_id'";
  $result = mysqli_query($conn, $sql);
  $row = mysqli_fetch_assoc($result);
  if (mysqli_num_rows($result) > 0) {
      $_SESSION["M_login"] = $row["M_login"];
      $_SESSION["M_pwd"] = $row["M_pwd"];
      $_SESSION["M_id"] = $row["M_id"];
      $_SESSION["openid"] = $row["M_wxid"];
      $_SESSION["M_pwdcode"] = $genkey;
      die('{"status":1}');
  } else {
      die('{"status":0}');
  }
}

if($action=="ajax_search"){
	$keyword=t($_POST["text"]);

$sql="select id,title,pic,type from(

select P_id as id,P_title as title,P_pic as pic,'productinfo' as type from sl_product where P_del=0 and P_sh=1 and (P_title like '%".$keyword."%' or P_content like '%".$keyword."%' )
	union select N_id as id,N_title as title,N_pic as pic,'newsinfo' as type from sl_news where N_del=0 and N_sh=1 and (N_title like '%".$keyword."%' or N_content like '%".$keyword."%' )
union select S_id as id,S_title as title,S_pic as pic,'news' as type from sl_nsort where S_del=0 and (S_title like '%".$keyword."%' or S_content like '%".$keyword."%' )
union select C_id as id,C_title as title,C_pic as pic,'courseinfo' as type from sl_course where C_del=0 and (C_title like '%".$keyword."%' or C_content like '%".$keyword."%' )
union select S_id as id,S_title as title,S_pic as pic,'course' as type from sl_usort where S_del=0 and (S_title like '%".$keyword."%' or S_content like '%".$keyword."%' )
union select S_id as id,S_title as title,S_pic as pic,'product' as type from sl_psort where S_del=0 and (S_title like '%".$keyword."%' or S_content like '%".$keyword."%' )
union select T_id as id,T_title as title,T_pic as pic,'text' as type from sl_text where T_del=0 and (T_title like '%".$keyword."%' or T_content like '%".$keyword."%' ))a limit 5";


	$result = mysqli_query($conn, $sql);
	$arr = array();  
	while($row = mysqli_fetch_array($result)) {
	$row["url"]="?type=".$row["type"]."&id=".$row["id"];
	$row["img"]=pic($row["pic"]);
	$count=count($row);
	  for($i=0;$i<$count;$i++){ 
	    unset($row[$i]);
	  }
    array_push($arr,$row);
} 

die(json_encode($arr));
}

if($action=="ajax_getcat_post"){
	$paged=intval($_POST["paged"]);
	$cat=intval($_POST["cat"]);

if($cat==0){
	$sql="select * from sl_product,sl_psort where S_del=0 and P_del=0 ".$M_pinfo."  and P_sort=S_id order by P_top desc,P_order,P_time desc,P_id desc limit 10";
}else{
	$sql="select * from sl_product,sl_psort where S_del=0 and P_del=0 ".$M_pinfo."  and P_sort=S_id and S_sub=".$cat." order by P_top desc,P_order,P_time desc,P_id desc limit 10";
}

$result = mysqli_query($conn,  $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {


	if($row["P_limit"]>0 and $row["P_limitlong"]>0 and $row["P_limitlong"]-(time()-strtotime($row["P_limittime"]))/3600>0){
              $P_price=$row["P_limit"];
              $l=date('Y-m-d H:i:s',strtotime('+'.$row["P_limitlong"].' hour',strtotime($row["P_limittime"])));
            }else{
              $P_price=p($row["P_price"]);
              $l="";
            }

if($row['P_mid']==0){
  $author_pic=pic($C["C_ico"]);
}else{
  $author_pic=pic(getrs("select * from sl_member where M_id=".$row['P_mid'],"M_head"));
}

$sub_title=getrs("select * from sl_psort where S_id=".$row["S_sub"]);


        $api=$api.'<div class="col-lg-1-5 col-6 col-sm-6 col-md-4 col-lg-3">
  <article id="post-10742" class="post post-grid post-10742 type-post status-publish format-standard has-post-thumbnail hentry category-uncategorized tag-65">
      <div class="entry-media">
    <div class="placeholder" style="padding-bottom: 60%;">
      <a  href="?type=productinfo&id='.$row['P_id'].'" >
        <img class="lazyload" data-src="'.pic(splitx($row['P_pic'],"|",0)).'" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" alt="'.$row['P_title'].'">  
      </a>
          </div>
      </div>
      <div class="entry-wrapper">
              <a class="grid_author_avt" href="?type=productinfo&id='.$row['P_id'].'" ><div class="grid_author_bggo avatar bg-cover" style="background-image: url('.$author_pic.');"></div> </a>
            
  <header class="entry-header">          <div class="entry-meta">
                  <span class="meta-category">

                    <a href="?type=product&id='.$row['S_sub'].'" rel="category">
                  <i class="dot"></i>'.$sub_title["S_title"].'              </a>

                          <a href="?type=product&id='.$row['S_id'].'" rel="category">
                  <i class="dot"></i>'.$row['S_title'].'              </a>

                      </span>
      </div>
    
    <h2 class="entry-title"><a href="?type=productinfo&id='.$row['P_id'].'" title="'.$row['P_title'].'" rel="bookmark">'.$row['P_title'].'</a></h2>  </header>             <div class="entry-excerpt u-text-format">'.mb_substr(strip_tags($row["P_content"]),0,200,"utf-8").'</div>
            <div class="entry-footer">
  <ul class="post-meta-box">

        <li class="meta-date">
      <time datetime="2020-11-17T14:45:47+08:00"><i class="fa fa-clock-o"></i> '.date("Y-m-d",strtotime($row["P_time"])).'</time>
    </li>
            <li class="meta-views"><span><i class="fa fa-eye"></i> '.$row['P_view'].'</span></li>
            <li class="meta-price"><span> ￥'.$P_price.'</span></li>
          
  </ul>
</div>    </div>
  </article>
</div>';
    }
} 
die($api);
}

?>