From 85bbbd6499898d66d04d59815e6f54c3f61ed96b Mon Sep 17 00:00:00 2001 From: lccsw <805383944@qq.com> Date: Thu, 8 Dec 2022 16:13:12 +0800 Subject: [PATCH] edit-sylive-2 --- home/controllers/h5/market/sylive2/Item.php | 315 ++++++++++++++++++ home/controllers/h5/market/sylive2/Notify.php | 90 +++++ market/controllers/api/sylive/Activity.php | 6 +- market/controllers/api/sylive/Goods.php | 6 +- 4 files changed, 415 insertions(+), 2 deletions(-) create mode 100644 home/controllers/h5/market/sylive2/Item.php create mode 100644 home/controllers/h5/market/sylive2/Notify.php diff --git a/home/controllers/h5/market/sylive2/Item.php b/home/controllers/h5/market/sylive2/Item.php new file mode 100644 index 00000000..0e78ff9b --- /dev/null +++ b/home/controllers/h5/market/sylive2/Item.php @@ -0,0 +1,315 @@ +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/sylive2/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/sylive2/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']]); + $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['bizId'] && $unpay['bizId'] = $p_user['bizId']; + $p_user['levelId1'] && $unpay['levelId1'] = $p_user['levelId1']; + $p_user['levelId2'] && $unpay['levelId2'] = $p_user['levelId2']; + $p_user['levelId3'] && $unpay['levelId3'] = $p_user['levelId3']; + $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/sylive2/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/sylive2/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; + } +} diff --git a/home/controllers/h5/market/sylive2/Notify.php b/home/controllers/h5/market/sylive2/Notify.php new file mode 100644 index 00000000..ab68a83b --- /dev/null +++ b/home/controllers/h5/market/sylive2/Notify.php @@ -0,0 +1,90 @@ +load->model('market/market_sylive_activity_user_model', 'act_user_model'); + $this->load->model('market/market_sylive_order_model','order_model'); + $this->load->model('market/market_sylive_activity_model'); + $this->load->model('market/market_sylive_activity_orders_model','mdSytActivityOrders'); + $this->load->library('market/sylive_entity'); + $this->load->library('market/sylive2_entity'); + try{ + $WxPayNotifyResults = new WxPayNotifyResults(); + $input = file_get_contents("php://input"); + debug_log("[input] ". __FUNCTION__ . "# $input:" . json_encode($input,JSON_UNESCAPED_UNICODE), $this->log_file,$this->log_dir); + $result = $WxPayNotifyResults->FromXml($input); + $this->mch_id = $result['mch_id']; + $config = $this->market_sylive_activity_model->pay_config($this->mch_id); + if($config && file_exists($config)){ + require_once $config; + }else{ + require_once APPPATH."../api/third_party/WXconfig/hdy_WxPay.Config.php"; + } + //验证签名 + $config = new WxPayConfig(); + $result = WxPayNotifyResults::Init($config, $input); + $this->notify = $result->GetValues(); + debug_log("[info] ". __FUNCTION__ . "# notify:" . json_encode($this->notify,JSON_UNESCAPED_UNICODE), $this->log_file,$this->log_dir); + }catch (WxPayException $e){ + debug_log("[error] ". __FUNCTION__ . ":".$e->getMessage(), $this->log_file,$this->log_dir); + exit(); + } + } + + //商品-微信支付异步通知 + public function item(){ + try{ + $sid = $this->notify['out_trade_no']; + if($sid){ + debug_log("[start] ". __FUNCTION__ . ": out_trade_no:".$this->notify['out_trade_no'], $this->log_file,$this->log_dir); + $order = $this->order_model->get(array('sid'=>$sid)); + if(!$order){ + throw new Exception("{$sid}_订单不存在"); + } + if($order['status']){ + throw new Exception("{$sid}_订单已支付"); + } + if($this->notify['result_code'] != 'SUCCESS'){ //支付失败 + throw new Exception("支付失败,sid={$sid}"); + }else{ //支付成功 + $res = $this->order_model->update(['status'=>1,'payTime'=>date('Y-m-d H:i:s')],['id'=>$order['id']]); + if($res){ + $act_user = $this->act_user_model->get(['userId'=>$order['userId'],'activityId'=>$order['activityId']]); + $params = [ + 'a_id' => $order['activityId'], + 'uid' => $order['userId'], + 'cf_uid' => $act_user['channelId'], + 'kpi' => 'order', + 'tagId' => $order['id'], + 'jsondata'=> ['order_id'=>$order['id'],'sid'=>$sid,'mch_id'=>$this->mch_id], + 'itemId' => $order['itemId'] + ]; + $sy_res = $this->sylive2_entity->kpi_log($params); + debug_log("[info] ". __FUNCTION__ . ":私域通增加记录-".json_encode($sy_res,JSON_UNESCAPED_UNICODE), $this->log_file,$this->log_dir); + debug_log("[success] ". __FUNCTION__ . ":操作成功", $this->log_file,$this->log_dir); + } + } + } + }catch (Exception $e){ + $msg = $e->getMessage(); + debug_log("[error] ". __FUNCTION__ . ":{$msg}", $this->log_file,$this->log_dir); + } + if($sid){ + debug_log("[finish] ". __FUNCTION__ . ": out_trade_no-".$this->notify['out_trade_no'], $this->log_file,$this->log_dir); + }else{ + debug_log("[finish] ". __FUNCTION__ . ": 参数错误-".json_encode($this->notify,JSON_UNESCAPED_UNICODE), $this->log_file,$this->log_dir); + } + echo ''; + } + +} diff --git a/market/controllers/api/sylive/Activity.php b/market/controllers/api/sylive/Activity.php index 7d28861a..1cc6d523 100644 --- a/market/controllers/api/sylive/Activity.php +++ b/market/controllers/api/sylive/Activity.php @@ -132,7 +132,11 @@ class Activity extends BaseController } } $skey = $this->myencryption->base64url_encode("a_id=" . $activityId); - $url = http_host_com('home') . "/h5/market/sylive?skey=" . $skey;//活动连接 + if($activityId>=6){ + $url = http_host_com('home') . "/h5/market/sylive2?skey=" . $skey;//活动连接 + }else{ + $url = http_host_com('home') . "/h5/market/sylive?skey=" . $skey;//活动连接 + } $serviceLink = $jsondata['serviceLink'] ? $jsondata['serviceLink'] : ''; $activityStart = $v['activityStart'] != '0000-00-00 00:00:00' ? $v['activityStart'] : ''; $organizationId = $v['organizationId'] ? intval($v['organizationId']) : ''; diff --git a/market/controllers/api/sylive/Goods.php b/market/controllers/api/sylive/Goods.php index a012849e..b3f07317 100644 --- a/market/controllers/api/sylive/Goods.php +++ b/market/controllers/api/sylive/Goods.php @@ -64,7 +64,11 @@ class Goods extends BaseController $v['timeEnd'] != '0000-00-00 00:00:00' && $dateRange[] = $v['timeEnd']; $v['useStart'] != '0000-00-00 00:00:00' && $useRange[] = $v['useStart']; $v['useEnd'] != '0000-00-00 00:00:00' && $useRange[] = $v['useEnd']; - $url = http_host_com('home') . "/h5/market/sylive/item/detail?skey={$skey}";//活动连接 + if($activityId>=6){ + $url = http_host_com('home') . "/h5/market/sylive2/item/detail?skey={$skey}";//活动连接 + }else{ + $url = http_host_com('home') . "/h5/market/sylive/item/detail?skey={$skey}";//活动连接 + } $list[] = ['itemId' => $v['itemId'], 'activityId' => $v['activityId'], 'title' => $v['title'], 'banner' => $banner , 'dateRange' => $dateRange, 'useRange' => $useRange, 'descrip' => $v['descrip'], 'price' => $v['price'], 'stock' => $v['stock'] , 'timeStart' => $timeStart, 'sort' => $v['sort'], 'status' => intval($v['status']), 'createTime' => $v['createTime']