文件
mashangyoujiu/Application/SmallProgramApi/Controller/StoreController.class.php

363 行
14 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace SmallProgramApi\Controller;
use Common\Business\MerchantConfig;
use Common\Model\MerchantConfigModel;
use Common\Model\RetailStoreModel;
use Common\Model\RetailVipModel;
use Think\Controller;
//[1.8.50]
class StoreController extends BaseController
{
public function getStore()
{
$lng = I('post.lng'); //经度
$lat = I('post.lat'); //纬度
if ($lng == null || $lng == 'undefined' || $lat == null || $lat == 'undefined') {
$result['status'] = 0;
$result['msg'] = '请允许获取您的地理位置';
$this->ajaxReturn($result);
}
$StoreModel = D('RetailStore');
$map = array(
$StoreModel::STATUS => 1, //0正常 1删除
$StoreModel::STORE_STATUS => 0, //门店状态 0正常 1关闭
$StoreModel::STATE => 0, //商城状态 1开启 0关闭
$StoreModel::MERCHANT_ID => self::$merchant_id,
$StoreModel::LATITUDE => array('NEQ', ''),
$StoreModel::LONGITUDE => array('NEQ', ''),
);
$name = I('post.name');
if (!empty($name)) {
$map[$StoreModel::STORE_NAME] = array('like', '%' . $name . '%');
}
if (!empty(self::$store_id)) {
$map[$StoreModel::ID] = self::$store_id;
}
$fields = array(
$StoreModel::SERVICE_RADIUS,
$StoreModel::ID . ' as store_id',
$StoreModel::ADDRESS,
$StoreModel::BUSINESS,
$StoreModel::LATITUDE,
$StoreModel::LONGITUDE,
$StoreModel::STORE_NAME . ' as store_name',
$StoreModel::STORE_IMG_ID,
$StoreModel::PHONE
);
$list = $StoreModel->getSelect($map, $fields);
if ($list) {
//计算出店铺和用户之间的距离
$time = time(); //当前时间
$storeArr = array();
foreach ($list as &$val) {
$val['distance'] = get_distance($lng, $lat, $val[$StoreModel::LONGITUDE], $val[$StoreModel::LATITUDE]); //距离
if (!empty($val['business'])) {
$arr = explode('-', $val['business']);
$sta_time = strtotime($arr[0]);
$end_time = strtotime($arr[1]);
if ($time < $end_time && $time > $sta_time) {
$val['time_status'] = 1; //在营业时间范围
} else {
$val['time_status'] = 0; //不在营业时间范围
}
}
$val['image'] = getImage($val[$StoreModel::STORE_IMG_ID]); //门店图片
unset($val[$StoreModel::STORE_IMG_ID]);
$storeArr[] = $val;
}
//按距离给数组排序
$list = array_sequence($storeArr, 'distance');
$res['status'] = 1;
$res['store'] = $list;
} else {
$res['status'] = 0;
$res['store'] = array();
$res['msg'] = '未找到门店';
}
$this->ajaxReturn($res);
}
/**
* 小程序轮播图 2019-6-29
*/
public function getBanner()
{
$SlideShowModel = D('RetailSlideShow');
$map = array(
$SlideShowModel::MERCHANT_ID => self::$merchant_id,
$SlideShowModel::STATUS => 1
);
$slideShow = $SlideShowModel->getSelect($map, $SlideShowModel::IMAGE_ID);
if (!empty($slideShow)) {
foreach ($slideShow as $k => &$v) {
// 轮播图
$v['image'] = getImage($v[$SlideShowModel::IMAGE_ID]);
unset($v[$SlideShowModel::IMAGE_ID]);
}
}
if (!empty($slideShow)) {
$res['status'] = 1;
$res['msg'] = '查询成功';
$res['banner'] = $slideShow;
} else {
$res['status'] = 0;
$res['msg'] = '未设置轮播图';
}
$this->ajaxReturn($res);
}
/**
* 新版获取门店列表
*/
public function getStore_v1()
{
$lng = I('post.lng'); //经度
$lat = I('post.lat'); //纬度
if ($lng == null || $lng == 'undefined' || $lat == null || $lat == 'undefined') {
$result['status'] = 0;
$result['msg'] = '请允许获取您的地理位置';
$this->ajaxReturn($result);
}
$VipModel = new RetailVipModel();
$StoreModel = new RetailStoreModel();
$MerchantConfigModel = new MerchantConfigModel();
//商户设置
$MerchantConfig = $MerchantConfigModel->getFind(['merchant_id' => self::$merchant_id], 'store_access,store_switch,tourist_store_access');
$map = [
'status' => 1, //0正常 1删除
'store_status' => 0, //门店状态 0正常 1关闭
'state' => 0, //商城状态 1开启 0关闭
'merchantid' => self::$merchant_id,
'latitude' => ['NEQ', ''],
'longitude' => ['NEQ', ''],
];
$fields = [
'math',
'id as store_id',
'address',
'business',
'business_time',
'latitude',
'longitude',
'storetag as store_name',
'shopimg_id',
'phone',
'tip', //推广语
'store_type',
];
//用户是会员并且不是进入指定门店
$vid = I('post.vid');
if ((!empty(self::$user) && !empty(self::$user['vid']) || !empty($vid)) && empty(self::$store_id)) {
$vip_store_id = $VipModel->getFindField(['id' => $vid], 'storeid');
} else {
$vip_store_id = '';
}
$userVid = self::$user['vid'];
//非会员,使用非会员的进店方式。
if (empty($userVid)) {
$MerchantConfig['store_access'] = $MerchantConfig['tourist_store_access'];
}
$order = 'store_type'; //门店类型:0,主店,1分店
$list = $StoreModel->getSelect($map, $fields, $order);
$vip_store_name = ''; //会员归属门店
if ($list) {
//计算出店铺和用户之间的距离
$time = time(); //当前时间
$storeArr = [];
$GoodsModel = new \Common\Model\GoodsModel();
foreach ($list as &$val) {
$val['distance'] = get_distance($lng, $lat, $val['longitude'], $val['latitude']); //距离
if (!empty($val['business'])) {
$val['new_business'] = explode(",", $val['business']);
foreach ($val['new_business'] as &$value) {
$value = explode("-", $value);
}
$val['business'] = $val['new_business'][0][0] . '-' . $val['new_business'][0][1];
$sta_time = strtotime($val['new_business'][0][0]);
$end_time = strtotime($val['new_business'][0][1]);
$time_status = ($time < $end_time && $time > $sta_time) ? 1 : 0;
//如果存在多时间段配送
if ($val['new_business'][1]) {
$pre_s = strtotime($val['new_business'][1][0]);
$pre_e = strtotime($val['new_business'][1][1]);
$time_status = ($time < $pre_e && $time > $pre_s) ? 1 : $time_status;
}
if ($val['new_business'][2]) {
$pre_s = strtotime($val['new_business'][2][0]);
$pre_e = strtotime($val['new_business'][2][1]);
$time_status = ($time < $pre_e && $time > $pre_s) ? 1 : $time_status;
}
$val['time_status'] = $time_status; //1在营业时间范围 0不在营业时间范围
}
$val['image'] = getImage($val['shopimg_id']); //门店图片
unset($val['shopimg_id']);
if ($vip_store_id == $val['store_id']) {
$vip_store_name = $val['store_name'];
}
$val['tip'] = $val['tip'] ? $val['tip'] : '';
$goods = $GoodsModel->getSelect(['merchant_id' => self::$merchant_id, 'store_id' => $val['store_id']], 'id, goods_img_id, fictitious_num, sell_num', 'sell_num desc', 1);
if (!empty($goods)) {
foreach ($goods as &$good) {
$good['image'] = getImage($good['goods_img_id']);
$good['sales_volume'] = $good['sell_num'] + $good['fictitious_num'];
unset($good['goods_img_id'], $good['fictitious_num'], $good['sell_num']);
}
}
$val['goods'] = $goods;
$val['banners'] = M('retail_banner')->field('id, imgurl')->where(['storeid' => $val['store_id'], 'merchantid' => self::$merchant_id, 'status' => 1])->order('id desc')->select();
$val['business_hours'] = $val['business_time'];
if ($val['store_id'] == self::$store_id) {
$store = $val;
} else if ($val['store_id'] == $vip_store_id) {
$vip_store = $val;
} else if ($val['store_type'] == 0) {
//把主店,拿出来
$store_type = $val;
} else {
$storeArr[] = $val;
}
}
if (!empty($vip_store) && $vip_store['store_type'] == 0 && $MerchantConfig['store_access'] == 3) {
//所属门店不为空,并且所属门店还是主店,进入门店方式 1最近门店,2所属门店,3主店
$store_type = $vip_store;
$vip_store = '';
}
//进入最近门店(不管是否可切换),把会员所属门店插入进行排序
if ($MerchantConfig['store_access'] == 1 && !empty($vip_store)) {
array_unshift($storeArr, $vip_store);
}
if ($MerchantConfig['store_access'] == 1 && !empty($store_type)) {
//因为不等于3,把主店插入
array_unshift($storeArr, $store_type);
}
//按主店
if ($MerchantConfig['store_access'] == 3 && !empty($vip_store)) {
//不等于类型,1和2,把所属门店插入
array_unshift($storeArr, $vip_store);
}
//按距离给门店排序
if (count($storeArr) > 1) {
$storeArr = array_sequence($storeArr, 'distance');
}
//按主店
if ($MerchantConfig['store_access'] == 3 && !empty($store_type)) {
//经过距离排序,把主店插入
array_unshift($storeArr, $store_type);
}
if (!empty($store_type) && $MerchantConfig['store_access'] == 2) {
//把主店插入
array_unshift($storeArr, $store_type);
//按距离给门店排序
if (count($storeArr) > 1) {
$storeArr = array_sequence($storeArr, 'distance');
}
}
if (!empty($store)) {
//携带门店ID请求该接口,把该门店插入首位
array_unshift($storeArr, $store);
} else if (!empty($vip_store) && $MerchantConfig['store_access'] == 2) {
//进入会员所属(不管是否可切换),把会员所属门店插入首位
array_unshift($storeArr, $vip_store);
}
if ($MerchantConfig['store_switch'] == 2) {
//不可切换门店(不管设置默认进入哪个门店),只留第一个门店
$tempStoreList = $storeArr[0];
$storeArr = [$tempStoreList];
}
$res['vip_store_name'] = $vip_store_name;
$res['vip_store_id'] = $vip_store_id;
$res['status'] = 1;
$res['msg'] = '获取成功';
$res['store'] = $storeArr;
$res['store_switch'] = $MerchantConfig['store_switch']; //商户设定是否可以切换门店 1可 2否
} else {
$res['status'] = 0;
$res['store'] = [];
$res['msg'] = '未找到门店';
}
$this->ajaxReturn($res);
}
/**
* 验证当前时间是否在营业时间内
* 支持多时段和跨天营业时间
*/
public function checkBusinessTime()
{
try {
$store_id = I('post.store_id');
if (empty($store_id)) {
$store_id = self::$store_id;
}
if (empty($store_id)) {
$result['status'] = 0;
$result['msg'] = '门店ID不能为空';
$this->ajaxReturn($result);
}
$StoreModel = new RetailStoreModel();
$store = $StoreModel->getFind(['id' => $store_id], 'business_time, business_non_order');
if (empty($store)) {
$result['status'] = 0;
$result['msg'] = '门店不存在';
$this->ajaxReturn($result);
}
// 如果允许非营业时间下单,直接返回成功
if ($store['business_non_order'] == 1) {
$result['status'] = 1;
$result['msg'] = '允许非营业时间下单';
$result['in_business_time'] = true;
$this->ajaxReturn($result);
}
// 检查营业时间
$business_time = $store['business_time'];
if (empty($business_time)) {
$result['status'] = 0;
$result['msg'] = '未设置营业时间';
$this->ajaxReturn($result);
}
// 使用BaseController中的营业时间验证逻辑
$is_in_business_time = $this->isCurrentTimeInBusinessHours($business_time);
if ($is_in_business_time) {
$result['status'] = 1;
$result['msg'] = '当前时间在营业时间内';
$result['in_business_time'] = true;
} else {
$result['status'] = 0;
$result['msg'] = '当前时间不在营业时间范围内';
$result['in_business_time'] = false;
}
$result['business_time'] = $business_time;
$this->ajaxReturn($result);
} catch (\Exception $e) {
$result['status'] = 0;
$result['msg'] = $e->getMessage();
$this->ajaxReturn($result);
}
}
}