cartLogic = new \app\home\logic\CartLogic();
if (session('?user')) {
$user = session('user');
$user = M('users')->where("user_id", $user['user_id'])->find();
session('user', $user); //覆盖session 中的 user
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user', $user); //存储用户信息
// 给用户计算会员价 登录前后不一样
if ($user) {
$user[discount] = (empty($user[discount])) ? 1 : $user[discount];
DB::execute("update `__PREFIX__cart` set member_goods_price = goods_price * {$user[discount]} where (user_id ={$user[user_id]} or session_id = '{$this->session_id}') and prom_type = 0");
}
}
//APP
$getylpres = getylapp_res(getMobileStoId(), '3');
if ($getylpres) {
$this->assign('ylpres', $getylpres);
}
/*--
$nologin = array(
'ajaxhtml', 'index', 'vip_qd', 'vip_bqd', 'updateoffintegral', 'coupon', 'send_coupon',
'integral_info', 'submit', 'find_pwd','coupon_pro','integral_sale',
);--*/
$nologin = array(
'integral_info', 'buy_points', 'goodsInfo',
);
if (!$this->user_id && !in_array(ACTION_NAME, $nologin)) {
$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
exit;
}
if (!in_array(ACTION_NAME, $nologin)) {
if (($this->pm_erpid && (!$user || !$user['erpvipid'])) || (empty($this->pm_erpid) && !$user || !$user['mobile'])) {
$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
exit;
}
}
}
//加载页面
public function ajaxhtml()
{
$user_id = $this->user_id;//会员ID
$qdlist = M('qd_config')->where(array('store_id' => getMobileStoId(), 'is_show' => 1))->find();//
if (empty($qdlist)) {
$this->assign('none', 1);
}
//后三条签到记录
$qdrecord1 = Db::query("select * from wxd_qd_record where store_id=" . getMobileStoId() . " and user_id=" . $user_id . " and DATEDIFF(FROM_UNIXTIME(billdate),CURDATE())<>0 order by billdate desc,id desc limit 2");
//判断今天有否签到
$today = M('qd_record')->where(" user_id=" . $user_id . " and store_id=" . getMobileStoId() . " and DATEDIFF(FROM_UNIXTIME(billdate),CURDATE())=0")->count();
$sday1 = date("m.d", strtotime("-2 day"));
$cday1 = 0;//上次获取积分
$sday2 = date("m.d", strtotime("-1 day"));
$sday3 = date("m.d", time());
if (!empty($qdrecord1)) {
$cday1 = $qdrecord1[0]['daynum'];
if (count($qdrecord1) == 2) {
$sday2 = date("m.d", $qdrecord1[0]['billdate']);
$sday1 = date("m.d", $qdrecord1[1]['billdate']);
} else {
$sday2 = date("m.d", $qdrecord1[0]['billdate']);
$sday1 = date("m.d", $qdrecord1[0]['billdate'] - 86400);
}
}
//当月签到记录
//$month=M('qd_record')->where(" user_id=".$user_id." and store_id=".getMobileStoId()." and date_format(FROM_UNIXTIME(billdate),'%Y-%m')=date_format(now(),'%Y-%m') ")->field("date_format(FROM_UNIXTIME(billdate),'%d') as day")->select();
$month = Db::query("SELECT GROUP_CONCAT(date_format(FROM_UNIXTIME(billdate),'%d') separator ',') as day FROM wxd_qd_record where user_id=" . $user_id . " and store_id=" . getMobileStoId() . " and date_format(FROM_UNIXTIME(billdate),'%Y-%m')=date_format(now(),'%Y-%m') ");
$month_1 = Db::query("SELECT GROUP_CONCAT(date_format(FROM_UNIXTIME(billdate),'%d') separator ',') as day FROM wxd_qd_record where user_id=" . $user_id . " and store_id=" . getMobileStoId() . " and date_format(FROM_UNIXTIME(billdate),'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m') ");
if (!empty($month)) {
$month = $month[0]['day'];
}
if (!empty($month_1)) {
$month_1 = $month_1[0]['day'];
}
//设置
$getcurnum = "0";
$getnextnum = "0";
$getqdnum = "0";
$getqdday = "0";
$daynum = 0;//获得积分
$ljnum = 0;
$lxnum = 0;
$lxnum1 = 0;
$getlxnum = 0;
$day = 1;
$getBQNum = 0;//补签
$max_bqday = 0;//多少天漏可补
$getbqnum = 0;//补签次数
$getybqnum = 0;//已补签次数
$getkbqnum = 0;//可补签次数
$getkbqnum1 = 0;//可补签次数
$getBQIntegral = 0;//当前补签积分
$getcurbqnum = 0;//当前补签取次数
$getBQIntegral1 = 0;//补签后积分
$list_score = 0; //页面第一排积分
$list_date = 0; //页面第一排日期
$list_score1 = 0; //签到成功排积分
$list_date1 = 0; //签到成功排日期
$qdintro = '';//签到成功说明
$curbqdaynum = '';//当前需补签天数
//明天起5天积分
$tdayc1 = 0;
$tdayc2 = 0;
$tdayc3 = 0;
$tdayc4 = 0;
$tdayc5 = 0;
$tdayc6 = 0;
$bqbutton = array(
'getbqintegral' => '',
'getbqnum2' => '',
'getbqintegral1' => '',
);
switch ($qdlist['typeid']) {
case "1":
$daynum = $qdlist['integralnum1'];
$qdintro = '恭喜您获得' . $daynum . '个积分';
$tdayc1 = $tdayc2 = $tdayc3 = $tdayc4 = $tdayc5 = $tdayc6 = $daynum;
break;
case "2":
$max_bqday = $qdlist['bqday']; //多少天后可补签
$getlxnum = $qdlist['daynum2']; //连续签到设置天数
$getbqnum = $qdlist['bqnum']; //补签次数,最高可设置3次
//date_sub(curdate(),interval 1 day)
$qdrecord = Db::query("select *,DATEDIFF(curdate(),FROM_UNIXTIME(billdate)) as xcday from wxd_qd_record where store_id=" . getMobileStoId() . " and user_id=" . $user_id . " order by billdate desc,id desc limit 1"); //获取最新一次签到记录
if ($qdrecord) {
$day = $qdrecord[0]['xcday']; //跟当天相差天数
$lxnum = $qdrecord[0]['lxnum']; //连续签到
$getkbqnum = $qdrecord[0]['bqnum']; //剩余补签次数
$getkbqnum1 = $getkbqnum;
$lxnum1 = $lxnum;
}
$score = ''; //往后5天各自对应积分(前台显示)
$star = $qdrecord[0]['lxnum'] == 0 ? $qdrecord[0]['lxnum'] + 1 : $qdrecord[0]['lxnum'];
//var_dump($star);
$k = 0;
for ($i = 1; $i < 6; $i++) {
if ($today == 1) //今天已签到
{
$k = ($qdrecord[0]['lxnum'] + $i) % $getlxnum;
} else {
$k = ($i + 1) % $getlxnum;
}
$k = $k == 0 ? $getlxnum : $k;
if ($i > 1) {
$score .= ',';
}
$score .= $qdlist['dayintegral' . $k];
}
if ($score) {
$arr = explode(",", $score);
$tdayc1 = $arr[0];
$tdayc2 = $arr[1];
$tdayc3 = $arr[2];
$tdayc4 = $arr[3];
$tdayc5 = $arr[4];
}
//当前时间和上一次签到相隔为1的话
if ($day == 1) {
if ($lxnum1 == $getlxnum) {
$lxnum = 1; //今天处于连续签到的第几天,为1表示今天是连续签到的第一天
} else {
$lxnum += 1;
}
} else {
if ($lxnum1 == $getlxnum) {
$lxnum = 1;
} else {
if ($day-1 <= $max_bqday) //前天签到昨天漏签,$day值为2
{
///可补签,$lxnum值为上一次签到的连续天数,补签天数=在此基础上加1
} else {
$lxnum = 1;
}
}
}
//可补签等于0次的话,重新计算
if ($day > 1 && $getkbqnum == 0) {
$lxnum = 1;
}
if ($day > 1 && $getbqnum > 0 && $getkbqnum > 0) {
$getcurbqnum = $getbqnum - $getkbqnum + 1;// 当前补签处于第几次补签
if ($getcurbqnum <= 0) {
$getcurbqnum = 1;
}
$getBQIntegral = $qdlist['bqintegral' . $getcurbqnum];//补签需扣的积分
$getBQIntegral1 = $qdlist['dayintegral' . ($lxnum + 1)];//补签后积分
}
//
if ($lxnum == 0) {
$lxnum = 1;
}
//今天未签到情况下,显示下面内容
$daynum = $qdlist['dayintegral' . $lxnum];
$qdintro = '恭喜您获得' . $daynum . '个积分';
if ($day == 1 || $day - 1 > $max_bqday || $getkbqnum < $day - 1) {
//不能补签(没有漏签、漏签天数超过最长补签天数、剩余补签次数小于需补签天数)
} else {
if ($day > 1) {
$getbqnum2 = $getkbqnum1 - 1;
$bqbutton = array(
'getbqintegral' => $getBQIntegral,
'getbqnum2' => $getbqnum2,
'getbqintegral1' => $getBQIntegral1,
);
for ($t = 1; $t < $day; $t++) {
if ($t > 1) {
$curbqdaynum .= ',';
}
$curbqdaynum .= date("d", strtotime("-" . $t . " day")); //需补签的日期号
}
}
//dump($curbqdaynum);
}
break;
case "3":
$ljnum = M('qd_record')->where(array('user_id' => $user_id, 'store_id' => getMobileStoId()))->order('billdate desc,id desc')->field('ljnum')->limit(1)->find();
$daynum = $qdlist['integralnum3'];
if (empty($ljnum)) {
$ljnum = 1;
} else {
$ljnum = $ljnum['ljnum'] + 1;
}
if ($ljnum == $qdlist['daynum3']) {
$ljnum = 0;
}
$qdintro = '累积签到' . $qdlist['daynum3'] . '天可获得' . $daynum . '个积分';
$tdayc1 = $tdayc2 = $tdayc3 = $tdayc4 = $tdayc5 = $tdayc6 = $daynum;
break;
}
//计算当前月份日期
$curday = date("t"); //当前月天数
$xingqi = Date("w", strtotime(Date("Y-n-1"))); //当月第一天周几 0123456——日一二……
$dayhtml = '
';
for ($k = $xingqi; $k > 0; $k--) //上个月记录
{
$kk = date("d", strtotime(date('Y-m-01', time())) - 3600 * 24 * $k);
if (strpos($month_1, (String)$kk) !== false) {
$dayhtml .= '' . $kk . '';
} else if (strpos($curbqdaynum, (String)$kk) !== false && date('d') < 10) {
$dayhtml .= '' . $kk . '';
} else {
$dayhtml .= '' . $kk . '';
}
//$dayhtml.=''.$kk.'';
}
for ($i = 1; $i <= $curday; $i++) //本月记录
{
if (strpos($month, ($i < 10 ? '0' . $i : (String)$i)) !== false) {
$dayhtml .= '' . $i . '';
} else if (strpos($curbqdaynum, ($i < 10 ? '0' . $i : (String)$i)) !== false && $i < date('d')) {
$dayhtml .= '' . $i . '';
} else {
$dayhtml .= '';
}
if (($i + $xingqi) % 7 == 0) {
$dayhtml .= '';
}
}
$getcurnum = $daynum; //可获得积分
$result = array(
'getcurnum' => $getcurnum,
'getnextnum' => $getnextnum,
'getqdnum' => $getqdnum,
'getqdday' => $getqdday,
'daynum' => $daynum,
'ljnum' => $ljnum,
'lxnum' => $lxnum,
'lxnum1' => $lxnum1,
'getlxnum' => $getlxnum,
'day' => $day,
'getBQNum' => $getBQNum,
'max_bqday' => $max_bqday,
'getbqnum' => $getbqnum,
'getybqnum' => $getybqnum,
'getkbqnum' => $getkbqnum,
'getkbqnum1' => $getkbqnum1,
'getBQIntegral' => $getBQIntegral,
'getcurbqnum' => $getcurbqnum,
'getBQIntegral' => $getBQIntegral1,
'today' => $today,
'qdintro' => $qdintro,
'sday1' => $sday1,
'sday2' => $sday2,
'sday3' => $sday3,
'cday1' => $cday1,
'tdayc1' => $tdayc1,
'tdayc2' => $tdayc2,
'tdayc3' => $tdayc3,
'tdayc4' => $tdayc4,
'tdayc5' => $tdayc5,
'tdayc6' => $tdayc6,
'score' => $score,
'curbqdaynum' => $curbqdaynum,
);
//dump($bqbutton);
$this->assign('bqbutton', $bqbutton);
$this->assign('dayhtml', $dayhtml);
$this->assign('info', $qdlist);
$this->assign('result', $result);
upload_ylp_log('签到页面');
return $this->fetch('', getMobileStoId());
}
//积分签到
public function index()
{
//dump(mktime(0, 0, 0, date('m'), date('d')-2, date('Y'))); //前一天时间戳
return $this->fetch('', getMobileStoId());
}
//签到
public function vip_qd()
{
upload_ylp_log('会员签到');
$user_id = $this->user_id;//会员ID
$store_id = getMobileStoId();
$qdlist = M('qd_config')->where(array('store_id' => $store_id, 'is_show' => 1))->find();
$daynum = 0;//获得积分
$tomdaynum = 0;//明天签到积分
$ljnum = 0;
$lxnum = 0;
$getlxnum = 0;
$getbqnum = 0;
$getconfigsm = "";//配置说明
switch ($qdlist['typeid']) {
case "1": //每日
$daynum = $qdlist['integralnum1'];
$tomdaynum = $qdlist['integralnum1'];
$getconfigsm = "签到送积分(每日签到)";
break;
case "2": //连续
$getconfigsm = "签到送积分(连续签到)";
$getlxnum = $qdlist['daynum2'];
$getbqnum = $qdlist['bqnum'];
$day = 0;
$dts_date = Db::query("select *,DATEDIFF(CURDATE(),FROM_UNIXTIME(billdate))as xcday from wxd_qd_record where store_id=" . $store_id . " and user_id=" . $user_id . " order by billdate desc,id desc limit 1");
if (!empty($dts_date)) {
$timediff = time() - $dts_date[0]['billdate'];
//$days = intval($timediff/86400);
$day = $dts_date[0]['xcday'];
$getbqnum = $dts_date[0]['bqnum'];
$lxnum = $dts_date[0]['lxnum'];
}
if ($day == 1) {
if ($lxnum >= $getlxnum) {
$lxnum = 1;
$getbqnum = $qdlist['bqnum'];
} else {
$lxnum += 1;
}
} else {
$lxnum = 1;
$getbqnum = $qdlist['bqnum'];
}
if ($lxnum == 0) {
$lxnum = 1;
$getbqnum = $qdlist['bqnum'];
}
//return day + "," + getlxnum + "," + lxnum;
//
$daynum = $qdlist['dayintegral' . $lxnum];
if ($lxnum == $getlxnum) //当前签到天数已是连续最后一天,明天重新计算
{
$tomdaynum = $qdlist['dayintegral1'];
} else if ($lxnum < $getlxnum) {
$tomdaynum = $qdlist['dayintegral' . ($lxnum + 1)];
}
break;
case "3": //累积
$getconfigsm = "签到送积分(累积签到)";
$ljnum = M('qd_record')->where(array('user_id' => $user_id, 'store_id' => getMobileStoId()))->order('billdate desc,id desc')->field('ljnum')->limit(1)->find();
if (empty($ljnum)) {
$ljnum = 1;
} else {
$ljnum = $ljnum['ljnum'] + 1;
}
if ($ljnum >= $qdlist['daynum3']) {
$getconfigsm = "签到送积分(累积签到" . $ljnum . "天)";
$ljnum = 0;
$daynum = $qdlist['integralnum3'];
}
break;
}
try {
//判断今天有否签到
$today = M('qd_record')->where(" user_id=" . $user_id . " and store_id=" . $store_id . " and DATEDIFF(FROM_UNIXTIME(billdate),CURDATE())=0")->find();
if ($today) {
return json(['status' => 2, 'msg' => '今天已经签到']); //今天已签到
} else {
$strno = "qd" . date('Ymd'). $user_id.rand(100000, 999999); //"qd".date('YmdHis').rand(100000, 999999);
mlog("签到编号:" . $strno . "当前签到积分:" . $daynum . "类型:" . $qdlist['typeid'] . "配置:" . $getconfigsm, "qdgame/" . getMobileStoId());
$data = array(
"qdbh" => $strno,
"store_id" => getMobileStoId(),
"user_id" => $user_id,
"user_nickname" =>'' ,
"user_headimg" =>'',
"daynum" => $daynum,
"ljnum" => $ljnum,
"lxnum" => $lxnum,
"bqnum" => $getbqnum,
"qd_type" => $qdlist['typeid'],
"billdate" => time(),
);
$resultint = M('qd_record')->add($data);
/*--避免重复插入-*/
//$sql= one_insert("wxd_qd_record",$data,"user_id=" . $user_id . " and store_id=" . $store_id . " and DATEDIFF(FROM_UNIXTIME(billdate),CURDATE())=0");
//$resultint=Db::execute($sql);
if ($resultint) {
//线下加积分
if ($daynum) {
$result = $this->updateoffintegral($user_id, $daynum, $getconfigsm, $strno);
if ($result == 1) {
M('qd_record')->where("id=$resultint")->save(array('integerstate' => 1));
return json(['status' => 1, 'msg' => '签到成功']);
}
} else {
return json(['status' => 1, 'msg' => '签到成功']);
}
} else {
//M("qd_record")->where("id", $resultint)->delete();
return json(['status' => -1, 'msg' => '签到失败']);
}
}
} catch (HttpResponseException $exception) {
//return json(["code" => "-1", "msg" => "生成签名错误"]);
return json(['status' => 2, 'msg' => $exception->getResponse()]);
}
}
//补签到
public function vip_bqd()
{
// upload_ylp_log('会员补签');
$user_id = $this->user_id;//会员ID
$store_id = getMobileStoId();
$daynum = I('getbqintegral1');
$curbqnum = I('getbqnum2');
$bqintegral = I('getbqintegral');
$qdlist = M('qd_config')->where(array('store_id' => $store_id, 'is_show' => 1))->find();
$ljnum = 0;
$lxnum = 0;
$getlxnum = 0;
$lastrecord = Db::query("select * from wxd_qd_record where store_id=" . $store_id . " and user_id=" . $user_id . " and billdate<".time()." order by billdate desc,id desc limit 1");
if (!empty($lastrecord)) {
$lxnum = $lastrecord[0]['lxnum'] + 1;
$strno = "qd" . date('YmdHis') . rand(100000, 999999);
$data = array(
'qdbh' => $strno,
'store_id' => getMobileStoId(),
'user_id' => $user_id,
'user_nickname' => '',
'user_headimg' => '',
'daynum' => $daynum,
'ljnum' => $ljnum,
'lxnum' => $lxnum,
'billdate' => $lastrecord[0]["billdate"] + 86400,
'isbq' => 1,
'bqnum' => $curbqnum,
'qd_type' => $qdlist['typeid'],
);
$resultint = M('qd_record')->add($data);
$strno1 = "qd" . date('YmdHis') . rand(100000, 999999);
$data = array(
'qdbh' => $strno,
'store_id' => getMobileStoId(),
'user_id' => $user_id,
'user_nickname' => '',
'user_headimg' => '',
'daynum' => -$bqintegral,
'ljnum' => $ljnum,
'lxnum' => $lxnum,
'billdate' => $lastrecord[0]["billdate"]+ 86400,
'isbq' => 1,
'bqnum' => $curbqnum,
'qd_type' => $qdlist['typeid'],
);
$resultint1 = M('qd_record')->add($data);
if ($resultint && $resultint1) {
//补签扣积分
$result = $this->updateoffintegral($user_id, '-' . $bqintegral, '补签扣积分', $strno1);
//补签加积分
if ($result == 1) {
M('qd_record')->where(array("id"=>$resultint1))->save(array('integerstate' => 1));
$result = $this->updateoffintegral($user_id, $daynum, '补签加积分', $strno);
if ($result==1) {
M('qd_record')->where(array("id" => $resultint))->save(array('integerstate' => 1));
}
return json(['status' => 1, 'msg' => '补签成功']);
} else {
//M('qd_record')->where(array("id"=>$resultint1))->delete();
return json(['status' => -1, 'msg' => '补签失败1']);
}
} else {
//M('qd_record')->where(array("id"=>$resultint1))->delete();
return json(['status' => -1, 'msg' => '补签失败']);
}
}
}
//线下增减积分
public function updateoffintegral($user_id,$bqintegral,$remark,$strno)
{
$user=$this->user;
if ($this->pm_erpid)//有账套
{
/*--
$user['api_token'] = tpCache('shop_info.api_token', $user['store_id']);
$data = ['Id' => $user['erpvipid'],
'Integral' => $bqintegral,
'Remark' => $remark,
'Number' => $strno,
];
$res = getApiData("wxd.vip.addreduce", $user['api_token'], array($data,));--*/
$res=com_update_integral($this->pm_erpid,$user,$bqintegral,$remark,$module="integral/updateoffintegral",$strno);
if(!$res){
return -1;
}
return 1;//线下已执行(预防第一次插入线下返回错误即json_encode($res)=null,二次插入失败)
}else
{
accountLog($user["user_id"],0,$bqintegral,$remark,0,$user['store_id'],$user["pay_points"]);//线上表
return 1;
}
}
//优惠券
public function coupon()
{
$user_id = $this->user_id;//会员ID
$where1="type = 1 and store_id = " . getMobileStoId() . " and (sum_num=0 or (sum_num>0 and sum_num>use_sumnum)) and unix_timestamp(now()) >= send_start_time and send_end_time >= unix_timestamp(now())";
$coupon = M('coupon')->where($where1)->order('add_time desc')->select();
$coupon_count = M('coupon')->where($where1)->count();
$list = array();
if ($coupon) {
$i = 1;
foreach ($coupon as $val) {
if ($i % $coupon_count == 1) {
$val['color'] = "red";
} else if ($i % $coupon_count == 2) {
$val['color'] = "blue";
} else {
$val['color'] = "purple";
}
$list[] = $val;
$i += 1;
}
}
$this->assign('coupon', $list);
upload_ylp_log('优惠券列表');
return $this->fetch('', getMobileStoId());
}
public function send_coupon()
{
upload_ylp_log('领取优惠券');
$cid = I("post.cid");
$user_id = $this->user_id;//会员ID
if ($this->user_id == 0) {
$this->ajaxReturn(array('status' => -1, 'msg' => '未登录 !'));
}
$getstoid = getMobileStoId();
$coupon = M("coupon")->where(array('store_id' => $getstoid, 'id' => $cid))->find();
if (empty($coupon)) {
$this->ajaxReturn(array('status' => -1, 'msg' => '领取失败,优惠券不存在!'));
}
//判断是否大于0,控制总数量
if ($coupon['sum_num']) {
$use_sumcount = M('coupon_list')->where(array('store_id' => $getstoid, 'type' => 5, 'cid' => $cid))->count();
if ($use_sumcount >= $coupon['sum_num']) {
$this->ajaxReturn(array('status' => -1, 'msg' => '领取失败,该优惠券数量已领完!'));
}
}
if ($coupon["send_end_time"] < time()) {
$this->ajaxReturn(array('status' => -1, 'msg' => '领取失败,该优惠券已过期 !'));
}
$clres = M('coupon_list')->where(array('store_id' => $getstoid, 'type' => 5, 'cid' => $cid, 'uid' => $user_id))
->order('id desc')
->select();
//经验的开关
//$check = 0;
if ($clres) {
if (count($clres) >= $coupon['everyone_num'] && $coupon['everyone_num']) {
$this->ajaxReturn(array('status' => -1, 'msg' => '您已领完该券 !'));
}
//---时间间隔---
$interval_time = $coupon['interval_time'];
if ($interval_time) {
$time1 = strtotime("+" . $interval_time . " day", $clres[0]['send_time']);
if (time() < $time1)
$this->ajaxReturn(array('status' => -1,
'msg' => '您已领取过了,请过后在领 !'));
}
if ($coupon['everyone_num']) {
// 乐观锁机制,避免手机端,和pc端同时点击,造成冲突
$rs = M("coupon_list")->where("id", $clres[0]['id'])
->where(array('type' => 5, 'cid' => $cid, 'uid' => $user_id))
->where('get_num<' . ($coupon['everyone_num'] - 1))->setInc('get_num');
if (empty($rs)) {
$this->ajaxReturn(array('status' => -1, 'msg' => '您已领完该券 !'));
}
}
}
$userinfo = $this->user;
$CashRepNo = "1005" . date("ymdHis") . get_total_millisecond();
//更新总数
$update_coupon = M('coupon')->where(" store_id=".$getstoid." and id=".$cid." and ((sum_num>0 and sum_num>use_sumnum) or (sum_num=0))")->setInc('use_sumnum');
if (empty($update_coupon))
{
$this->ajaxReturn(array('status' => -1, 'msg' => '领取失败,该优惠券数量已领完!'));
}
//有账套 end
//插入线上优惠券领取表
$coupon_list['cid'] = $cid;
$coupon_list['type'] = 5;
$coupon_list['uid'] = $user_id;
$coupon_list['send_time'] = time();
$coupon_list['code'] = $CashRepNo;
$coupon_list['store_id'] = $getstoid;
//如果是按照有效期的运算
if ($coupon["endtype"] == 1) {
$coupon_list['begintime'] = time();
if ($coupon['days']) {
$days = $coupon['days'];
$time1 = strtotime("+" . $days . " day", time());
$coupon_list['validtime'] = strtotime(date('Y-m-d 23:59:59', $time1));
} else {
$coupon_list['validtime'] = 0;
}
} else {
$coupon_list['begintime'] = $coupon["use_start_time"];
$coupon_list['validtime'] = strtotime(date('Y-m-d 23:59:59', $coupon["use_end_time"]));
}
$coupon_list['sum'] = $coupon["money"];
$coupon_list['buysum'] = $coupon["condition"];
$coupon_list['remark'] = "免费发放";
$add_couponinfo = add_coupon($coupon_list, $userinfo, $getstoid);
$add_couponinfo = json_decode($add_couponinfo, true);
if ($add_couponinfo['code'] == 0) {
$this->ajaxReturn(array('status' => -1, 'msg' => '领取失败!'));
}
//有账套
if ($this->pm_erpid) {
$tk = tpCache("shop_info", getMobileStoId());
$data = array(
'CashRepNo' => $CashRepNo,//券号
'VIPId' => $userinfo["erpvipid"],//会员id
'Sum' => $coupon["money"],//金额
'SendMan' => '免费发放',//发放类型
'Operator' => '微信商城',//来源
'BuySum' => $coupon["condition"],//满多少才能使用
'Remark' => $coupon["name"] . '【消费满' . $coupon["condition"] . '元可用】',
);
if ($coupon['useobjecttype']) {
$data['UseObjectType'] = $coupon['useobjecttype'];
$data['UseObjectID'] = $coupon['useobjectid'];
$data['UseObjectNo'] = $coupon['useobjectno'];
$data['UseObjectName'] = $coupon['useobjectname'];
}
//如果是按照有效期的运算
if ($coupon["endtype"] == 1) {
$data['BeginDate'] = date('Y-m-d', time());//使用开始日期
if ($coupon['days']) {
$days = $coupon['days'];
$time1 = strtotime("+" . $days . " day", time());
$data['ValidDate'] = date('Y-m-d', $time1);
}
} else {
if ($coupon["use_start_time"] != "") {
$data['BeginDate'] = date('Y-m-d', $coupon["use_start_time"]);//使用开始日期
}
if ($coupon["use_end_time"] != "") {
$data['ValidDate'] = date('Y-m-d', $coupon["use_end_time"]);//使用结束日期
}
}
mlog(json_encode($data), "send_coupon/" . getMobileStoId());
$erp = tpCache("shop_info.ERPId", getMobileStoId());
$re1 = getApiData_java_p("/api/erp/vip/cash/insert", $erp, $data, null, null, null, "POST");
$add_rs = [$re1, "ok"];
if (!$add_rs[0]) {
$this->ajaxReturn(array('status' => -1, 'msg' => '服务器繁忙, 请联系管理员 !'));
}
$add_qlist = json_decode(urldecode($add_rs[0]), true);
if ($add_qlist['code'] != 0) {
$this->ajaxReturn(array('status' => -1, 'msg' => '领取失败!'));
}
}
$this->ajaxReturn(array('status' => 1, 'msg' => '领取成功!'));
}
//积分购
public function buy_points(){
$id=I("stoid");
$stype =I('type/d',1);
$dwup=I("dwup/d",0); //0降 1升
if($dwup) $dwup="asc";
else $dwup="desc";
//积分兑换广告
$getopgg=M('ad')->where(array('store_id'=>$id,'pid'=>501,'enabled'=>1,'start_time'=>array('elt',time()),'end_time'=>array('egt',time())))->order('orderby asc')->find();
if ($getopgg)
{
$getopgg_img=QCLOUD_IMGURL.$getopgg['ad_code'];
$this->assign('getopgg_img',$getopgg_img);
}
if(I('ispage'))
{
$now_time = time(); //当前时间
$where="";
$where=" and f.start_time<=".$now_time;
$count = M('integral_buy')
->alias('f')
->join('goods g','g.goods_id = f.goods_id','left')
->where("g.on_time<".$now_time." and (g.down_time>".$now_time." or g.down_time=0 or g.down_time='' or g.down_time is null)")
->where(" g.store_id=".$id." and g.is_on_sale=1 and f.is_show=1 and f.is_end=0 and f.end_time>".$now_time.$where)->count();
$p=I("p/d",1);
$pagesize = 10;//C('PAGESIZE'); //每页显示数
$first = $p*$pagesize;
switch ($stype)
{
case "1":$order="f.id ".$dwup;
break;
case "2":$order="f.addmoney ".$dwup;
break;
case "3":$order="f.order_num ".$dwup;
break;
case "4":$order="f.integral ".$dwup;
break;
}
$list =M('integral_buy')
->alias('f')
->join('goods g','g.goods_id = f.goods_id','left')
->where("g.on_time<".$now_time." and (g.down_time>".$now_time." or g.down_time=0 or g.down_time='' or g.down_time is null)")
->where(" g.store_id=".$id." and g.is_on_sale=1 and f.is_show=1 and f.is_end=0 and f.end_time>".$now_time.$where)
->order($order)
->limit($first.','.$pagesize)
->select();
$list1='';
if (empty($list))
{
$jsonlist['state']="0";
$jsonlist['msg']='无数据';
}
else
{
foreach ($list as $item) {
$item["original_img"]=getimg($item['original_img'],NOIMG,0,1);
$list1[]=$item;
}
$jsonlist['state']="1";
$jsonlist['msg']='成功';
}
$jsonlist['item']=$list1;
return json($jsonlist);
}
upload_ylp_log('积分购列表');
return $this->fetch('',getMobileStoId());
}
//积分购商品详情
public function integral_fun(){
$stoid=I('stoid/d',1);
C('TOKEN_ON', true);
// $user_id=cookie('user_id');
$goodsLogic = new \app\home\logic\GoodsLogic();
$goods_id = I("get.id");
$now0=time();
$where0 = [
'end_time' => ['>=', $now0],
'start_time' => ['<=', $now0],
'goods_id' => $goods_id,
'is_end'=>0,
'is_show'=>1
];
$rs0=M('integral_buy')->where($where0)->find();
if(empty($rs0)){
$this->redirect(U('Mobile/integral/buy_points', array('id' => $goods_id, 'stoid' => getMobileStoId())));
}
$this->assign('integral_buy',$rs0);
/*--商品--*/
$goods = M('Goods')->where("goods_id", $goods_id)->where('is_on_sale', 1)
->where("on_time<".$now0." and (down_time>".$now0." or down_time=0 or down_time='' or down_time is null)")
->field('goods_id,store_id,goods_name,brand_id,nation_id,cat_id,prom_type,market_price,store_count,shop_price,sales_sum,sku,prom_id,original_img,goods_sn,goods_spec,goods_color,on_time,REPLACE(REPLACE(goods_remark, CHAR(10),""), CHAR(13),"") as goods_remark,distr_type')->find();
if (empty($goods)) {
$this->error('此商品不存在或者已下架');
}
$this->buy_page_assign($goods['distr_type']);
$is_chat=tpCache("basic.is_chat",$stoid);
$rs_server=M('storage_recharge_detail')
->where('store_id',$stoid) ->where('admin_id<>0')
->where('end_time>'.$now0)
->find();
$this->assign('is_chat', $is_chat && $rs_server);
$this->assign('stoid', $stoid);
if ($goods['brand_id']) {
$brnad = M('brand')->where("id", $goods['brand_id'])->find();
$goods['brand_name'] = $brnad['name'];
}
if ($goods['nation_id']) {
$nation = M('nation')->where("id", $goods['nation_id'])->find();
$goods['nation_name'] = $nation['name'];
}
if ($goods['cat_id']) {
$cat = M('goods_category')->where("id", $goods['cat_id'])->find();
$goods['cat_name'] = $cat['name'];
}
//$goods_images_list = M('goods_images')->where("goods_id", $goods_id)->select(); // 商品 图册
$goods_images_list = M('GoodsImages')->where("goods_id", $goods_id)->order('ismain desc')->select(); // 商品 图册
$goods_videos = M('goods_videos')->where("goods_id", $goods_id)
->order('video_sort asc')->select(); // 视频
if($goods_videos[0])
$this->assign("goods_videos", $goods_videos);
/*------商品规格--------*/
$sp1 = $goods["goods_spec"];
$cn1 = $goods["goods_color"];
$guige1="规格";
$gorder1=1;$tgg1="";
if ($sp1 == "" && $cn1 == "") {
$tgg1 = $guige1.$gorder1;
$gorder1++;
} else if ($sp1 != "" && $cn1 == "") {
$tgg1 = $sp1;
} else if ($sp1 == "" && $cn1 != "") {
$tgg1 = $cn1;
} else {
$tgg1 = $sp1."/".$cn1;
}
$goods['guige']=$tgg1;
// $spec_goods_price = M('spec_goods_price')->where("goods_id", $goods_id)->getField("key,price,store_count"); // 规格 对应 价格 库存表
M('Goods')->where("goods_id=$goods_id")->save(array('click_count' => $goods['click_count'] + 1)); //统计点击数
$commentStatistics = $goodsLogic->commentStatistics($goods_id);// 获取某个商品的评论统计
// $this->assign('spec_goods_price', json_encode($spec_goods_price,true)); // 规格 对应 价格 库存表
$goods['sales_sum'] =$rs0['buy_num'];
/*------商品规格
$sp1 = $goods["goods_spec"];
$cn1 = $goods["goods_color"];
$guige1="规格";
if ($sp1 == "" && $cn1 == "") {
$tgg1 ="" ;
} else if ($sp1 != "" && $cn1 == "") {
$tgg2 = $sp1;
} else if ($sp1 == "" && $cn1 != "") {
$tgg1 = $cn1;
} else {
$tgg1 = $sp1."/".$cn1;
}--------*/
/*----选择门店-----*/
// $res1=M("storage_category")->where('store_id',$stoid)->cache("storage_category_".$stoid,TPSHOP_CACHE_TIME)->select();
// foreach ($res1 as $k1=>$v1){
// $result=M("pick_up")->where("category_id",$v1['cat_id'])->where('store_id',$stoid)
// ->where('isstop<>1')->select();
// $pkarray=get_arr_column($result,'pickup_id');
// if(empty($result)){
// unset($res1[$k1]);
// }else {
// $res1[$k1]['list'] =$result;
// }
// }
/*--当前价格--*/
// $curpirce= $ary['curprice'];
//商品限时抢购,商品特惠,团购
if($goods['prom_type'] >0) {
//所有活动判断
//$prom=$goods['flash_sale'] = get_goods_promotion($goods['goods_id'],$this->user_id);
$prom=$goods['flash_sale'] = get_goods_promotion1($goods,$rs0,$this->user_id);
if($goods['flash_sale']['is_end']!=1 && $goods['flash_sale']['is_end']!=4) {
//当前价格
$integral = $goods['flash_sale']['integral'];
$integral_name = $goods['flash_sale']['name'];
$addmoney = $goods['flash_sale']['addmoney'];
$limitvipqty=$goods['flash_sale']['buy_limit'];
$showlimte=$goods['flash_sale']['showlimit'];
$prom_goods = M('prom_goods')->where(['id' => $goods['prom_id'], 'is_close' => 0])->find();
$this->assign('prom_goods', $prom_goods);// 商品促销
$flash_sale = M('flash_sale')->where("id", $goods['prom_id'])->find();
$this->assign('flash_sale', $flash_sale);// 抢购
$goods['discount'] = round($goods['flash_sale']['price'] / $goods['shop_price'], 2) * 10;
$this->assign('onlybuy',$goods['flash_sale']['onlybuy']); //活动商品还可以买多少件
}else{
$goods['discount'] = round($goods['shop_price'] / $goods['market_price'], 2) * 10;
$goods['prom_type']=0;
}
}else{
$goods['discount'] = round($goods['shop_price'] / $goods['market_price'], 2) * 10;
}
//获取积分
$integ=get_userpoints($this->user,getMobileStoId(),$this->pm_erpid);
if ($integ)
{
$this->assign("integ",$integ);
if($rs0['integral']<=$integ || $rs0['integral']==0 ){
$this->assign("isinteg",1);
}
}else{
if($rs0['integral']==0 ){
$this->assign("isinteg",1);
}else{
$this->assign("integ",0);
$this->assign("integerr","获取积分失败");
}
}
//当前用户收藏
$user_id = $this->user_id;
$collect = M('goods_collect')->where(array("goods_id" => $goods_id, "user_id" => $user_id))->count();
$goods_collect_count = M('goods_collect')->where(array("goods_id" => $goods_id))->count(); //商品收藏数
$this->assign('collect', $collect);
$this->assign('commentStatistics', $commentStatistics);//评论概览
$this->assign('goods_images_list', $goods_images_list);//商品缩略图
$this->assign('goods', $goods);
$this->assign('goods_collect_count', $goods_collect_count); //商品收藏人数
$this->assign('collect', $collect); //是否收藏
$this->assign('integral',$integral);
$this->assign('integral_name',$integral_name);
$this->assign('addmoney',$addmoney);
$this->assign('limitvipqty',$limitvipqty);
$this->assign('showlimte',$showlimte);
$t=time();
$prom_order= M("prom_order")
->where("store_id",getMobileStoId())
->where("start_time<=".$t)
->where("end_time>=".$t)
->where("is_close=0")
->where('is_end',0)
->order(" id desc")->limit(1)->select();
$this->assign('prom_order', $prom_order);
//商品图片
$imgurl = getImg($goods['original_img'],curHostURL().NOIMG);
$msg = $this->img_curl_url($imgurl, 2);
$shareimg = $msg['data'];
$this->assign('shareimg',$shareimg);
//二维码也用64位
$shareurl=curHostURL().'/index.php/mobile/goods/goodsInfo/stoid/'.$stoid.'/id/'.$goods_id."/first_leader/".$this->user_id;
$this->assign('shareurl',$shareurl);
$qc = new \app\home\controller\Index();
$shareurl=$qc->qr_code_64($shareurl);
$this->assign('shareurlimg',$shareurl);
}
public function integral_info(){
$this->integral_fun();
return $this->fetch('', getMobileStoId());
}
//积分提交订单
public function submit(){
return $this->fetch('', getMobileStoId());
}
//优惠券详情
public function coupon_pro()
{
$user_id = $this->user_id;//会员ID
$id = I('id', 0);
$stoid=getMobileStoId();
$coupon = M('coupon')->where("type = 1 and store_id = " . getMobileStoId() . " and (sum_num=0 or (sum_num>0 and sum_num>use_sumnum)) and unix_timestamp(now()) > send_start_time and send_end_time > unix_timestamp(now()) and id=" . $id)->select(); //and id not in (select cid from wxd_coupon_list where type = 5 and uid = '.$user_id.' and store_id = ".getMobileStoId().")
/*
$gived=M('coupon_list')
->where("type = 5 and uid = '.$user_id.' and store_id = ".getMobileStoId()." and cid=".$id)
->order('id desc')
->select(); //是否已领取*/
$list = array();
if ($coupon) {
foreach ($coupon as $val) {
if ($val['use_start_time'] && $val['use_start_time'] != "0") {
$val['use_start_time'] = date('Y-m-d', $val['use_start_time']);
} else {
$val['use_start_time'] = "";
}
$list[] = $val;
}
$coupon2=$coupon[0]; $cid=$id; $err_text=null;
$use_sumcount = M('coupon_list')->where(array('store_id' => $stoid, 'type' => 5, 'cid' => $cid))->order('id desc')->select();
if (count($use_sumcount) >= $coupon2['sum_num'] && $coupon2['sum_num']) {
$err_text='该券已领完';
}
if(!$err_text){
//如果有领取过该券的话
$aa = M("coupon_list")->where('uid', $user_id)->where('cid', $cid)->order('id desc')
->field('begintime')
->select();
if ($aa) {
$everyone_num = $coupon2['everyone_num'];
if (count($aa) >= $everyone_num && $everyone_num) $err_text = "您已完该券";
$interval_time = $coupon2['interval_time'];
if ($interval_time && !$err_text) {
$time1 = strtotime("+" . $interval_time . " day", $use_sumcount[0]['send_time']);
if (time() < $time1) {
$err_text = '您已领过该券';
}
}
}
if ($coupon2["send_end_time"] < time() && !$err_text) {
$err_text = '优惠券已过期';
}
}
if($err_text) $this->assign("err_text",$err_text);
//$userinfo = $this->user;
}
//$this->assign('isgive',$gived);
$this->assign('coupon', $list);
return $this->fetch('', getMobileStoId());
}
public function get_quan_err(){
$user_id=$this->user['user_id'];
$cid=I('cid');
$everyone_num=I("everyone_num");
$interval_time=I("interval_time");
$aa=M("coupon_list")->where('uid',$user_id)->where('cid',$cid)->order('id desc')
->field('send_time')
->select();
if(count($aa)>=$everyone_num)
return json(['msg'=>"您优惠券领取数量已达上限!"]);
$time1 = strtotime("+" . $interval_time . " day", $aa[0]['send_time']);
if(time()<$time1) {
$timediff = $time1-time();
$days = intval($timediff/86400);
$remain = $timediff-$days*86400;
$hours = intval($remain/3600);
$remain = $remain-$hours*3600;
$mins = intval($remain/60);
$secs = $remain%60;
return json(['msg'=>"每".$interval_time."天领1次,剩余".($everyone_num-count($aa))."张,于".$days."天".$hours."小时".$mins."分".$secs."秒后再领"]);
}
}
// 网址图片生成BASE64
function img_curl_url($url,$type=0,$timeout=30)
{
$msg = ['code' => 2100, 'status' => 'error', 'msg' => '未知错误!'];
$imgs = ['image/jpeg' => 'jpeg',
'image/jpg' => 'jpg',
'image/gif' => 'gif',
'image/png' => 'png',
'text/html' => 'html',
'text/plain' => 'txt',
'image/pjpeg' => 'jpg',
'image/x-png' => 'png',
'image/x-icon' => 'ico'
];
if (!stristr($url, 'http')) {
$msg['code'] = 2101;
$msg['msg'] = 'url地址不正确!';
return $msg;
}
$dir = pathinfo($url);
//var_dump($dir);
$host = $dir['dirname'];
$refer = $host . '/';
$ch = curl_init($url);
//不验证https
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_REFERER, $refer); //伪造来源地址
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回变量内容还是直接输出字符串,0输出,1返回内容
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);//在启用CURLOPT_RETURNTRANSFER的时候,返回原生的(Raw)输出
curl_setopt($ch, CURLOPT_HEADER, 0); //是否输出HEADER头信息 0否1是
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //超时时间
$data = curl_exec($ch);
//$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
//$httpContentType = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
$info = curl_getinfo($ch);
curl_close($ch);
$httpCode = intval($info['http_code']);
$httpContentType = $info['content_type'];
$httpSizeDownload = intval($info['size_download']);
if ($httpCode != '200') {
$msg['code'] = 2102;
$msg['msg'] = 'url返回内容不正确!';
return $msg;
}
if ($type > 0 && !isset($imgs[$httpContentType])) {
$msg['code'] = 2103;
$msg['msg'] = 'url资源类型未知!';
return $msg;
}
if ($httpSizeDownload < 1) {
$msg['code'] = 2104;
$msg['msg'] = '内容大小不正确!';
return $msg;
}
$msg['code'] = 200;
$msg['status'] = 'success';
$msg['msg'] = '资源获取成功';
if ($type == 0 or $httpContentType == 'text/html') $msg['data'] = $data;
$base_64 = base64_encode($data);
if ($type == 1) $msg['data'] = $base_64;
elseif ($type == 2) $msg['data'] = "data:{$httpContentType};base64,{$base_64}";
elseif ($type == 3) $msg['data'] = "
";
else $msg['msg'] = '未知返回需求!';
unset($info, $data, $base_64);
return $msg;
}
}