Files
liche/home/controllers/h5/market/sylive/Item.php
T
xiaoyu 6a9316dd64 0507
2025-05-07 12:39:07 +08:00

325 lines
14 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once 'Common.php';
class Item extends Wx{
private $item_banner = ['https://qs.liche.cn/web/images/project/H5-ShiYu/goods.jpg'];
public function __construct(){
parent::__construct();
$this->load->model('market/market_sylive_items_model');
$this->load->model('market/market_sylive_activity_model');
$this->load->model('market/market_sylive_order_model');
$this->load->model('market/market_sylive_activity_bizinfo_model','msa_bizinfo_model');
$this->load->library('market/sylive_entity');
}
public function detail(){
$skey = $this->input->get('skey');
$param = $this->myencryption->base64url_decode($skey);
$a_id = intval($param['a_id']);//活动id
$itemId = intval($param['itemId']);//商品id
$row = $this->market_sylive_items_model->get(['itemId'=>$itemId,'activityId'=>$a_id]);
$act = $this->market_sylive_activity_model->get(['activityId'=>$a_id]);
if(!$row){
throw new Hd_exception('参数错误',400);
}
$imgs = json_decode($row['imgs'],true);
if($imgs['banner']){
$banner = [];
foreach ($imgs['banner'] as $item) {
$banner[] = build_qiniu_image_url($item);
}
}else{
$banner = $this->item_banner;
}
$e_time = time() < strtotime($row['timeEnd']) ? strtotime($row['timeEnd']) - time() : 0;
$s_time = time() < strtotime($row['timeStart']) ? strtotime($row['timeStart']) - time() : 0;
$is_pay = $this->market_sylive_order_model->count(['itemId'=>$itemId,'activityId'=>$a_id,'status'=>1,'userId'=>$this->uid,'type'=>0]);
if($is_pay){
$url = http_host_com('home').'/h5/market/sylive/ucenter?skey='.$skey;
redirect($url);exit;
}
$validity = '';
if($row['useStart']!='0000-00-00 00:00:00' || $row['useEnd']!='0000-00-00 00:00:00'){
$useTimeStart = date('Y-m-d',strtotime($row['useStart']));
$useTimeEnd = date('Y-m-d',strtotime($row['useEnd']));
$validity = "有效期 {$useTimeStart} - {$useTimeEnd}";
}
$title = $row['title'] ? $row['title'] : '';
$stock = $row['stock'];
$introduction = $row['descrip'] ? $row['descrip'] : '';
$price = $row['price'] ? $row['price'] : '';
$need_shop = 0;
//是否需要选择门店 时隐藏
// $need_shop = 1;
// $ac_user = $this->act_user_model->get(['id'=>$this->act_uid]);
// if($ac_user['channelId']){
// $channel_user = $this->act_user_model->get(['userId'=>$ac_user['channelId'],'activityId'=>$a_id]);
// if($channel_user['bizId'] && $channel_user['organizationId']){
// $need_shop = 0;
// }
// }
$biz_arr = $city_arr = $province_arr = [];
// if($need_shop){
// $province_arr = $this->msa_bizinfo_model->select(['status'=>0,'activityId'=>4],'',0,0,'distinct provinceName as name');
// }
$user = $this->user_model->get(['userId'=>$this->uid],'unionid,nickname,headimg');
$userid = $user['unionid'] ? $user['unionid'] : $this->uid;
$ts = time()*1000;
$sign = md5($this->secretkey.$userid.$this->secretkey.$ts);
$live_url = "https://live.haodian.cn/watch/{$act['channelId']}?userid={$userid}&ts={$ts}&sign={$sign}";
$info = [
'itemId' => $row['itemId'],
'title' => $title,
's_time' => $s_time,
'e_time' => $e_time,
'price' => $price,
'content' => $introduction,
'banner' => $banner,
'stock' => $stock,
'skey' => $skey,
'validity' => $validity,
'introTitle' => '权益说明',
'need_shop' => $need_shop,
'live_url' => $live_url,
'a_id' => $a_id,
'ifAddress' => $row['ifAddress'],
'protocolTitle' => $act['protocolTitle']
];
$this->data['province_arr'] = $province_arr;
$this->data['city_arr'] = $city_arr;
$this->data['biz_arr'] = $biz_arr;
$this->data['info'] = $info;
$this->data['_title'] = $row['title'];
//微信分享
$wx_info = $this->share_info($row);
$this->data['sign_package'] = $wx_info['sign_package'];
$this->data['share'] = $wx_info['share'];
$this->show_view('h5/market/sylive/item/detail');
}
//支付
public function post_pay(){
$redis = load_cache('redis');
$redis_obj = $redis->redis();
$skey = $this->input->post('skey');
$name = $this->input->post('name');
$mobile = $this->input->post('telPhone');
$code = $this->input->post('code');
$province = $this->input->post('province');
$city = $this->input->post('city');
$biz = $this->input->post('biz');
$itemId = $this->input->post('itemId');
$param = $this->myencryption->base64url_decode($skey);
$a_id = intval($param['a_id']);//活动id
$row = $this->market_sylive_items_model->get(['itemId'=>$itemId]);
if(!$row){
$this->show_json('',400,'参数错误');
}
if(time()< strtotime($row['timeStart'])){
$this->show_json('',400,'尚未开始');
}
if(time() > strtotime($row['timeEnd'])){
$this->show_json('',400,'活动已结束');
}
$mb_key = "SYLIVE_MEMBERS_ITEM_ID_{$itemId}"; //集合缓存key
$code_key = "sylive_item_code_{$this->uid}_{$mobile}";//验证码
if(!mobile_valid($mobile)){
$this->show_json('',400,'请输入正确手机号');
}
if(!$code || ($redis->get($code_key)!=$code)){
$this->show_json('',400,'请输入正确验证码');
}
//判断是否已支付
$o_row = $this->market_sylive_order_model->get(['itemId'=>$itemId,'activityId'=>$a_id,'status'=>1,'userId'=>$this->uid,'type'=>0]);
if($o_row){
$this->show_json('',400,'已支付,请勿重复支付');
}
$pay_price = $row['price'];
if($pay_price<=0){
$this->show_json('',400,'无需支付');
}
if($row['stock']<=0){
$this->show_json('',400,'已售罄');
}
$user = $this->user_model->get(['userId'=>$this->uid]);
$act_user = $this->act_user_model->get(['userId'=>$this->uid,'activityId'=>$a_id]);
$p_user = [];
$act_user['channelId'] && $p_user = $this->act_user_model->get(['userId'=>$act_user['channelId']]);
if(!$p_user){
if($user['teamId']){ //团长
$t_level_id = $this->sylive_entity->get_team_level($user['teamId']);
$p_user = $t_level_id<3 ? $act_user : [];
}
if($user['organizationId']){ //顾问
$group_id = $this->sylive_entity->get_level($user['organizationId']);
$p_user = $group_id<4 ? $act_user : 0;
}
}
$unpay = $this->market_sylive_order_model->get(['itemId'=>$itemId,'activityId'=>$a_id,'status'=>0,'userId'=>$this->uid,'expireTime>'=>time(),'type'=>0]);
if(!$unpay){
$mb_count = $redis_obj->sCard($mb_key); //获取集合中用户数量
if($mb_count && $mb_count>$row['stock']+10){
$mb = $redis_obj->sMembers($mb_key);
$this->show_json('',400,'已售罄:'.json_encode($mb,JSON_UNESCAPED_UNICODE));
}
try {
$province && $jsondata = ['biz'=>"{$province}-{$city}-{$biz}"];
$this->market_sylive_order_model->db->trans_begin();
//限购一件
$u_row = $this->market_sylive_order_model->get(['itemId'=>$itemId,'activityId'=>$a_id,'status>='=>0,'userId'=>$this->uid,'expireTime>'=>time(),]);
if($u_row){
throw new Exception('限购一件');
}
$this->load->helper('order');
$unpay = [
'sid' => create_order_no('350200','market'),
'activityId' => $a_id,
'userId' => $this->uid,
'itemId' => $itemId,
'itemTitle' => $row['title'],
'itemPrice' => $pay_price,
'totalPrice' => $pay_price,
'expireTime' => time()+5*60,//订单过期时间
'createTime' => date('Y-m-d H:i:s')
];
$name && $unpay['uname'] = $name;
$mobile && $unpay['mobile'] = $mobile;
$p_user['teamId'] && $unpay['cfrom'] = 1;
$p_user['areaId'] && $unpay['areaId'] = $p_user['areaId'];
$p_user['bizId'] && $unpay['bizId'] = $p_user['bizId'];
$p_user['userId'] && $unpay['cfUserId'] = $p_user['userId'];
$jsondata && $unpay['jsondata'] = json_encode($jsondata,JSON_UNESCAPED_UNICODE);
$oid = $this->market_sylive_order_model->add($unpay);
if(!is_numeric($oid)){
$this->market_sylive_order_model->db->trans_rollback();
throw new Exception('创建订单失败');
}
$unpay['id'] = $oid;
$this->market_sylive_items_model->update(['stock = stock-1' =>null],['itemId'=>$itemId,'stock>'=>0]);
$res = $this->market_sylive_items_model->db->affected_rows();
if(!$res){
throw new Exception('已售罄');
}
$this->market_sylive_order_model->db->trans_commit();
$redis_obj->sRem($mb_key,$this->uid); //删除集合
}catch (Exception $e){
$redis_obj->sRem($mb_key,$this->uid); //删除集合
$this->market_sylive_order_model->db->trans_rollback();
$this->show_json('',400,$e->getMessage());
}
}else{
$jsondata = json_decode($unpay['jsondata'],true);
$update = ['uname'=>$name,'mobile'=>$mobile];
if($province){
$jsondata['biz'] = "{$province}-{$city}-{$biz}";
$update['jsondata'] = json_encode($jsondata,JSON_UNESCAPED_UNICODE);
}
$this->market_sylive_order_model->update($update,['id'=>$unpay['id']]);
}
$notify_url = http_host_com('home').'/h5/market/sylive/notify/item';
$other_data = [];
$act = $this->market_sylive_activity_model->get(['activityId'=>$row['activityId']],'mchId');
$pay_config = $this->market_sylive_activity_model->pay_config($act['mchId']);
$pay_config && $other_data['pay_config'] = $pay_config;
$result = $this->unorder($unpay['sid'],$unpay['totalPrice'],$user['openid'],$unpay['itemTitle'],$notify_url,$unpay['expireTime'],$other_data);
if(!$result['code']){
$this->show_json('',400,$result['msg']);
}
//删除缓存
$redis->delete($code_key);
$data = [
'wxjson' => $result['data'],
'oid' => $unpay['id'],
];
$this->show_json($data,200,'操作成功');
}
//获取验证码
public function get_code(){
$mobile = $this->input->post('mobile');
if(!mobile_valid($mobile)){
$this->show_json('',400,'请输入正确的手机号码');
}
//验证是否黑名单用户
// if(!$this->ck_mobile($mobile)){
// $this->show_json('',400,'本次活动仅限受邀客户参加');
// }
$redis = &load_cache('redis');
$key = "sylive_item_code_{$this->uid}_{$mobile}";
$code = $redis->get($key);
if(!$code){
$this->load->helper('string');
$code = random_string('numeric', 4);
$redis->save($key, $code, 60*5);
}
$content = "【好店云】您的验证码为: {$code},五分钟之内有效,请勿泄露于他人,!";
b2m_send_sms($mobile,$content);
$this->show_json('',200, '验证码已发送');
}
public function biz_info(){
$type = $this->input->get('type');
$p_name = $this->input->get('p_name');
$c_name = $this->input->get('c_name');
$where = [
'status'=>0,
'activityId'=>4,
];
if($type=='city'){
$where['provinceName'] = $p_name;
$lists = $this->msa_bizinfo_model->select($where,'',0,0,'distinct cityName as name');
}elseif ($type=='biz'){
$where['provinceName'] = $p_name;
$where['cityName'] = $c_name;
$lists = $this->msa_bizinfo_model->select($where,'',0,0,'bizName as name');
}else{
$lists = $this->msa_bizinfo_model->select($where,'',0,0,'distinct provinceName as name');
}
$data = [
'lists' => $lists
];
$this->show_json($data,200);
}
public function agreement(){
$a_id = $this->input->get('a_id');
$act = $this->market_sylive_activity_model->get(['activityId'=>$a_id]);
$info = [
'title' => $act['protocolTitle'],
'content' => $act['protocol']
];
$this->data['info'] = $info;
$this->show_view('h5/market/sylive/item/agreement');
}
//验证手机号可以购买
private function ck_mobile($mobile){
$file_path = FCPATH.'sylive_blacklist.txt';
$ckey = 'SYLIVE_BLACK_MOBLIE_LIST';
$redis = &load_cache('redis');
$mobile_list = $redis->get($ckey);
if(!$mobile_list){
$mobile_list = [];
if (file_exists($file_path)) {
$content_arr = file($file_path);
foreach ($content_arr as $item) {
$mobile_list[] = trim($item);
}
}
$redis->save($ckey,$mobile_list,2*24*60*60);
}
if(in_array($mobile,$mobile_list)){
return false;
}
//判断是否购买
if($this->market_sylive_order_model->count(['activityId'=>3,'mobile'=>$mobile])){
return false;
}
return true;
}
}