canyin-project/ybcy/controllers/channelApi/ShopController.php
2024-11-01 16:07:54 +08:00

339 lines
17 KiB
PHP

<?php
namespace app\controllers\channelApi;
use Illuminate\Support\Facades\DB;
use Yii;
use app\models\common\Message;
use app\models\common\Order;
use app\models\common\Config;
use app\models\common\Member;
use app\models\common\Window;
use app\models\common\Store;
use app\models\common\Power;
class ShopController extends CommonController{
public $enableCsrfValidation = false;
//商家列表
public function actionStoreList()
{
$result=axios_request();
$typeId=$result['typeId'];
$lat = $result['lat'] ?: '30.504803';
$lng = $result['lng'] ?: '114.04992';
$page=$result['page']?:1;
$size=$result['size']?:10;
$table=(new \yii\db\Query())
->select(['a.id', 'a.name','a.icon','a.storeTel','a.lat','a.lng','a.address','b.name as typeName','ROUND(6378.137 * 2 * ASIN(SQRT(POW(SIN(('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(('.$lng.' * PI() / 180 - lng * PI() / 180) / 2),2))) * 1000) AS distance'])
->from('{{%ybwm_store}} as a')
->join('LEFT JOIN', '{{%ybwm_core_category}} as b', 'b.id = a.typeId')
->where('a.uniacid=:uniacid AND a.deleteAt=0 AND a.display=1',[':uniacid'=>$this->wqData['uniacid']]);
if($typeId){
$table->andwhere('a.typeId=:typeId',['typeId'=>$typeId]);
}
$storeSet=Config::getSystemSet('storeSet',$this->wqData['uniacid']);
$showType=$storeSet['showType']?:1;
if($showType==1){
if($result['zoneId']){
$table->andwhere('a.zoneId=:zoneId',[':zoneId'=>$result['zoneId']]);
}
if($result['cityId']){
$table->andwhere('a.cityId=:cityId',[':cityId'=>$result['cityId']]);
}
}elseif($storeSet['distance']>0){
$table->andwhere('ROUND(6378.137 * 2 * ASIN(SQRT(POW(SIN(('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(('.$lng.' * PI() / 180 - lng * PI() / 180) / 2),2))) * 1000)<='.$storeSet['distance']*1000);
}
if($result['storeId']){
$table->andwhere('a.id !='.$result['storeId']);
}
$table->offset(($page - 1) * $size)->limit($size);
$res=$table->orderby(["distance"=>SORT_ASC])->all();
if($page==1){
$firstStore=(new \yii\db\Query())
->select(['a.id', 'a.storeTel','a.name','a.icon','a.lat','a.lng','a.address','b.name as typeName','ROUND(6378.137 * 2 * ASIN(SQRT(POW(SIN(('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(('.$lng.' * PI() / 180 - lng * PI() / 180) / 2),2))) * 1000) AS distance'])
->from('{{%ybwm_store}} as a')
->join('LEFT JOIN', '{{%ybwm_core_category}} as b', 'b.id = a.typeId')
->where('a.id=:id',[':id'=>$result['storeId']])
->one();
if($firstStore){
array_unshift($res,$firstStore);
}
}
for($i=0;$i<count($res);$i++){
$takeOutSet=Config::getStoreSet('takeOutSet',$res[$i]['id']);
$res[$i]['timeArr']=$takeOutSet['timeArr'];
$res[$i]['timeType']=$takeOutSet['timeType']?:1;
$res[$i]['distributionSupport']=$takeOutSet['distributionSupport'];
$res[$i]['storeOpen']=Store::getStoreOutBusiness($res[$i]['id']);
}
return $this->result(1,'成功',$res);
}
//搜索商家
public function actionSearchStore(){
$result=axios_request();
$table=(new \yii\db\Query())
->select(['a.id', 'a.name','a.address','b.name as cityName'])
->from('{{%ybwm_store}} as a')
->join('LEFT JOIN', '{{%ybwm_core_district}} as b', 'b.id = a.cityId')
->where('a.uniacid=:uniacid AND a.deleteAt=0 AND a.display=1',[':uniacid'=>$this->wqData['uniacid']]);
if($result['keyword']){
$table->andWhere(['or',['like','a.address',$result['keyword']],['like','a.name',$result['keyword']]]);
}
$store=$table->all();
return $this->result(1,'成功',$store);
}
//查询最近商家
public function actionLatelyStore()
{
$result=axios_request();
$lat = $result['lat'] ?: '30.504803';
$lng = $result['lng'] ?: '114.04992';
$power=Power::getPower($this->wqData['uniacid']);
$storeModel=$power['shopModel']?:2;
if($storeModel==1){
$res=(new \yii\db\Query())
->select(['id', 'name','icon','ROUND(6378.137 * 2 * ASIN(SQRT(POW(SIN(('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(('.$lng.' * PI() / 180 - lng * PI() / 180) / 2),2))) * 1000) AS distance'])
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND deleteAt=0 AND isMain=1',[':uniacid'=>$this->wqData['uniacid']])->one();
}else{
$storeSet=Config::getSystemSet('storeSet',$this->wqData['uniacid']);
$enterStore=$storeSet['enterStore']?:1;
$user=(new \yii\db\Query())
->select(['browseStoreId'])
->from('{{%ybwm_member}}')
->where('id=:id',[':id'=>$this->wqData['userId']])->one();
if($result['storeId'] or ($enterStore==1 AND $user['browseStoreId'])){
$result['storeId']=$result['storeId']?:$user['browseStoreId'];
$table=(new \yii\db\Query())
->select(['display','id','icon', 'name','ROUND(6378.137 * 2 * ASIN(SQRT(POW(SIN(('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(('.$lng.' * PI() / 180 - lng * PI() / 180) / 2),2))) * 1000) AS distance'])
->from('{{%ybwm_store}}')
->where('id=:id',[':id'=>$result['storeId']]);
$res=$table->one();
if($res['display']==2){
$table=(new \yii\db\Query())
->select(['id','icon', 'name','ROUND(6378.137 * 2 * ASIN(SQRT(POW(SIN(('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(('.$lng.' * PI() / 180 - lng * PI() / 180) / 2),2))) * 1000) AS distance'])
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND deleteAt=0 AND display=1',[':uniacid'=>$this->wqData['uniacid']]);
$res=$table->orderby(["SQRT(POWER(" . $result['lat'] . " - lat, 2) + POWER(" . $result['lng'] . " - lng,2))"=>SORT_ASC])->one();
}
}else{
$table=(new \yii\db\Query())
->select(['id','icon', 'name','ROUND(6378.137 * 2 * ASIN(SQRT(POW(SIN(('.$lat.' * PI() / 180 - lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(lat * PI() / 180) * POW(SIN(('.$lng.' * PI() / 180 - lng * PI() / 180) / 2),2))) * 1000) AS distance'])
->from('{{%ybwm_store}}')
->where('uniacid=:uniacid AND deleteAt=0 AND display=1',[':uniacid'=>$this->wqData['uniacid']]);
$res=$table->orderby(["SQRT(POWER(" . $result['lat'] . " - lat, 2) + POWER(" . $result['lng'] . " - lng,2))"=>SORT_ASC])->one();
}
}
$res['notice']= Config::getStoreSet('takeOutSet',$res['id'])['notic']?:'';
Yii::$app->db->createCommand()->update('{{%ybwm_member}}', ['browseStoreId'=>$res['id']],['id'=>$this->wqData['userId']])->execute();
return $this->result(1,'成功',$res);
}
//查看收藏
public function actionCollectionStoreList(){
$result=axios_request();
$page=$result['page']?:1;
$size=$result['size']?:10;
$collection=(new \yii\db\Query())
->select('collectionId')
->from('{{%ybwm_user_collection}}')
->where('userId=:userId AND state=1 AND type=1',[':userId'=>$this->wqData['userId']])->all();
$collectionId=array_column($collection,'collectionId')?:[];
$table=(new \yii\db\Query())
->select(['a.id', 'a.name','a.icon','a.lat','a.lng','a.address','a.storeTel','b.name as typeName'])
->from('{{%ybwm_store}} as a')
->join('LEFT JOIN', '{{%ybwm_core_category}} as b', 'b.id = a.typeId')
->where('a.uniacid=:uniacid AND a.deleteAt=0 AND a.display=1',[':uniacid'=>$this->wqData['uniacid']])
->andWhere(['in','a.id',$collectionId]);
$table->offset(($page - 1) * $size)->limit($size);
$res=$table->all();
for($i=0;$i<count($res);$i++){
$takeOutSet=Config::getStoreSet('takeOutSet',$res[$i]['id']);
$res[$i]['timeArr']=$takeOutSet['timeArr'];
$res[$i]['timeType']=$takeOutSet['timeType']?:1;
$res[$i]['distributionSupport']=$takeOutSet['distributionSupport'];
$res[$i]['storeOpen']=Store::getStoreOutBusiness($res[$i]['id']);
}
return $this->result(1,'成功',$res);
}
// //查看弹窗优惠券ybwm_window_coupon
// public function actionWindowCoupon(){
// $now=time();
// $result=axios_request();
// $userId=$this->wqData['userId'];
// $window=(new \yii\db\Query())
// ->from('{{%ybwm_window_coupon}}')
// ->where('storeId=:storeId AND deleteAt=0 AND display=1 AND startTime<=:startTime AND endTime>:endTime AND location=:location',[':location'=>$result['location'],':startTime'=>$now,':endTime'=>$now,':storeId'=>$result['storeId']])
// ->one();
// if(!$window){
// return $this->result(1,'成功',[]);
// }
// $bill=(new \yii\db\Query())
// ->from('{{%ybwm_bill}}')
// ->where('storeId=:storeId AND userId=:userId',[':storeId'=>$result['storeId'],':userId'=>$userId])
// ->one();
// if($window['people']==1 AND $bill){
// return $this->result(1,'成功',[]);
// }
// if($window['people']==2 AND !$bill){
// return $this->result(1,'成功',[]);
// }
// $window=(new \yii\db\Query())
// ->from('{{%ybwm_window_coupon}}')
// ->where('storeId=:storeId AND deleteAt=0 AND display=1 AND startTime<=:startTime AND endTime>:endTime AND location=:location',[':location'=>$result['location'],':startTime'=>$now,':endTime'=>$now,':storeId'=>$result['storeId']])
// ->one();
// $receive=(new \yii\db\Query())
// ->from('{{%ybwm_window_coupon_receive}}')
// ->where('windowId=:windowId AND userId=:userId',[':windowId'=>$window['id'],':userId'=>$userId])
// ->one();
// if($receive){
// return $this->result(1,'成功',[]);
// }
// $list=(new \yii\db\Query())
// ->select('id,name,timeType,useEndTime,isNextDay,day,isFull,fullMoney,type,money,discount')
// ->from('{{%ybwm_coupon}}')
// ->where(['in','id',json_decode($window['coupon'],true)])
// ->all();
// for($i=0;$i<count($list);$i++){
// if($list[$i]['timeType']==1){
// //日期范围
// $list[$i]['useExplain']="有效期至".date("Y-m-d H:i",$list[$i]['useEndTime']);
// }else{
// if($list[$i]['isNextDay']==1){
// $list[$i]['useExplain']="领取后".$list[$i]['day']."天内有效(次日起)";
// }else{
// $list[$i]['useExplain']="领取后".$list[$i]['day']."天内有效";
// }
// }
// $list[$i]['explain']='';
// if($list[$i]['isFull']==1){
// $list[$i]['explain'] .='无门槛';
// }else{
// $list[$i]['explain'] .='满'.$list[$i]['fullMoney'].'可用';
// }
// }
// $window['couponList']=$list;
// return $this->result(1,'成功',$window);
// }
//领取弹窗优惠券
public function actionReceiveWindowCoupon(){
$result=axios_request();
$userId=$this->wqData['userId'];
$window=(new \yii\db\Query())
->select('coupon,id,uniacid')
->from('{{%ybwm_window_coupon}}')
->where('id=:id',[':id'=>$result['id']])
->one();
$receive=(new \yii\db\Query())
->from('{{%ybwm_window_coupon_receive}}')
->where('windowId=:windowId AND userId=:userId',[':windowId'=>$window['id'],':userId'=>$userId])
->one();
if($receive){
return $this->result(1,'成功',[]);
}
$transaction = Yii::$app->db->beginTransaction(); //开始事务
$res=Member::receiveCoupon(json_decode($window['coupon'],true),$userId,7);
if($res){
$data['windowId']=$window['id'];
$data['userId']=$userId;
$data['uniacid']=$window['uniacid'];
$data['createdAt']=time();
$receive=Yii::$app->db->createCommand()->insert('{{%ybwm_window_coupon_receive}}', $data)->execute();
if(!$receive){
$transaction->rollBack();//事务回滚
return false;
}
}
$transaction->commit();
return $this->result(1,'成功');
}
//弹窗合集
public function actionWindowCollection(){
$result=axios_request();
$userId=$this->wqData['userId'];
$data['windowCoupon']=Window::getWindowCoupon($result['location'],$result['storeId'],$userId);
$data['storeCoupon']=Window::getStoreCoupon($result['storeId'],$userId,$result['location']);
$data['rollBag']=Window::rollBag($result['storeId']);
return $this->result(1,'成功',$data);
}
//查看市区
public function actionGetCity(){
$uniacid=$this->wqData['uniacid'];
$cityArr=(new \yii\db\Query())
->select('id,name')
->from('{{%ybwm_core_district}}')
->where('id in (select cityId from {{%ybwm_store}} where uniacid='.$uniacid.' AND deleteAt=0 AND display=1)')
->all();
for($i=0;$i<count($cityArr);$i++){
$zoneArr=(new \yii\db\Query())
->select('id,name')
->from('{{%ybwm_core_district}}')
->where('upid='.$cityArr[$i]['id'].' AND id in (select zoneId from {{%ybwm_store}} where uniacid='.$uniacid.' AND deleteAt=0 AND display=1)')
->all();
$cityArr[$i]['children']=$zoneArr;
}
$data[]=[
'name'=>'中国',
'children'=>$cityArr
];
return $this->result(1,'成功',$data);
}
//坐标转地址
public function actionCoordinateToAdd() {
$result=axios_request();
$uniacid=$this->wqData['uniacid'];
$lat = $result['lat'] ?: '30.504803';
$lng = $result['lng'] ?: '114.04992';
if($result['storeId']){
$data=(new \yii\db\Query())
->select('b.name as cityName,c.name as zoneName,a.cityId,a.zoneId')
->from('{{%ybwm_store}} as a')
->join('LEFT JOIN', '{{%ybwm_core_district}} as b', 'b.id = a.cityId')
->join('LEFT JOIN', '{{%ybwm_core_district}} as c', 'c.id = a.zoneId')
->where('a.id=:id',[':id'=>$result['storeId']])->one();
return $this->result(1, '成功', $data);
}
$coordinate=$lat.",".$lng;
$set = Config::getSystemSet('system', $this->wqData['uniacid']);
$txKey=$set['txKey']?:Yii::$app->params['txKey'];
$location = "https://apis.map.qq.com/ws/geocoder/v1/?location=" . $coordinate . "&key=" . $txKey."&get_poi=0";
$coordinate = httpRequest($location);
$coordinate = json_decode($coordinate, true);
$zoneName=$coordinate['result']['address_component']['district'];
if($zoneName){
$zoneInfo=(new \yii\db\Query())
->select('id,name,upid')
->from('{{%ybwm_core_district}}')
->where('id in (select zoneId from {{%ybwm_store}} where uniacid='.$uniacid.' AND deleteAt=0 AND display=1) or id in (select cityId from {{%ybwm_store}} where uniacid='.$uniacid.' AND deleteAt=0 AND display=1)')
->andWhere(['name'=>$zoneName])
->one();
if($zoneInfo){
$cityInfo=(new \yii\db\Query())
->select('id,name')
->from('{{%ybwm_core_district}}')
->where('id=:id',[':id'=>$zoneInfo['upid']])
->one();
$data['zoneId']=$zoneInfo['id'];
$data['zoneName']=$zoneInfo['name'];
$data['cityName']=$cityInfo['name'];
$data['cityId']=$cityInfo['id'];
return $this->result(1, '成功', $data);
}
}
$table=(new \yii\db\Query())
->select('b.name as cityName,c.name as zoneName,a.cityId,a.zoneId')
->from('{{%ybwm_store}} as a')
->join('LEFT JOIN', '{{%ybwm_core_district}} as b', 'b.id = a.cityId')
->join('LEFT JOIN', '{{%ybwm_core_district}} as c', 'c.id = a.zoneId')
->where('a.uniacid=:uniacid AND a.deleteAt=0 AND a.display=1',[':uniacid'=>$uniacid]);
$data=$table->orderby(["SQRT(POWER(" . $lat . " - a.lat, 2) + POWER(" . $lng . " - a.lng,2))"=>SORT_ASC])->one();
return $this->result(1, '成功', $data);
}
}