cxfoot/modules/api/controllers/StoreController.php
2023-10-27 14:25:12 +08:00

238 lines
6.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\modules\api\controllers;
use app\models\DeviceUniqueConfig;
use app\models\DeviceUniqueData;
use function AlibabaCloud\Client\value;
use app\models\Banner;
use app\models\Store;
use app\modules\api\models\StoreForm;
use app\modules\api\models\StoreCityForm;
use app\modules\api\models\UserBasicInfoForm;
use Yii;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\modules\api\behaviors\LoginBehavior;
/**
*m门店
*/
class StoreController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return array_merge(parent::behaviors(), [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
'login' => [
'class' => LoginBehavior::className(),
'ignore' => [
'api/store/index',
'api/store/store-one',
'api/store/store-home',
'api/store/coach',
'api/store/city',
'api/store/test-avg',
'api/store/config',
'api/store/store-service',
]
]
]);
}
/**
* showdoc
* @catalog 门店
* @title 门店列表
* @description 门店列表
* @method get
* @url /api/store/index
* @param page 非必选 int 页码不传默认为1
* @param limit 非必选 int 每页显示条数不传默认10条
* @param city_id 非必选 string 城市id
* @param name 非必选 string 搜索内容
* @param lat 非必选 string 纬度
* @param lng 非必选 string 经度
* @return {"code":0,"msg":"ok","data":{}}
* @remark
*/
public function actionIndex()
{
$request = Yii::$app->request;
$get = $request->get();
$storeForm = new StoreForm();
$storeForm->page = $get['page'];
$storeForm->limit = $get['limit'];
$cityId = $get['city_id'];
$name = $get['name'];
$lat = $get['lat'];
$lng = $get['lng'];
$data = $storeForm->search($cityId, $name, $lat, $lng);
return $this->responseHandler($data);
}
//门店信息
public function actionStoreOne()
{
$request = Yii::$app->request;
$get = $request->get();
$storeForm = new StoreForm();
$storeForm->store_id = $get['id'];
$data = $storeForm->searchOne();
return $this->responseHandler($data);
}
/**
* 教练信息
*/
public function actionCoach()
{
$request = Yii::$app->request;
$get = $request->get();
$storeForm = new StoreForm();
$storeForm->coach_id = $get['coach_id'];
$data = $storeForm->coachInfo();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 门店
* @title 切换门店
* @description 切换门店
* @method post
* @url /api/store/store-binding
* @param store_id 必选 int 页码不传默认为1
* @return {"code":0,"msg":"ok","data":{}}
* @remark
*/
public function actionStoreBinding()
{
$form = new StoreForm();
$form->scenario = 'city';
$post = \Yii::$app->request->post('store_id');
$form->cx_mch_id = $this->cx_mch_id;
$form->user_id = \Yii::$app->user->identity->id;
$form->store_id = $post;
$data = $form->storeBinding();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 门店
* @title 首页附近门店
* @description 首页附近门店
* @method get
* @url /api/store/store-home
* @param lat 必选 int 纬度
* @param lng 必选 int 经度
* @return {"code":0,"msg":"ok","data":{}}
* @remark
*/
public function actionStoreHome()
{
$request = Yii::$app->request;
$get = $request->get();
$storeForm = new StoreForm();
$storeForm->cx_mch_id = $this->cx_mch_id;
$storeForm->user_id = \Yii::$app->user->identity->id;
$lat = $get['lat'];
$lng = $get['lng'];
$data = $storeForm->storeHome($lat, $lng);
return $this->responseHandler($data);
}
public function avg($data)
{
$res = [];
foreach ($data as $key => $val) {
foreach ($val as $k => $v) {
if (empty($k)) {
$res[$k] = [];
}
$res[$k][] = floatval($v);
}
}
$count = count($data);
foreach ($res as $key => $val) {
$res[$key] = strval(round(array_sum($val) / $count, 2));
}
return $res;
}
// 测试结果
public function actionTestAvg()
{
$select = DeviceUniqueData::find()->limit(100)->select('*')->asArray()->all();
$arr = [];
foreach ($select as $key => $val) {
$arr[] = json_decode($val['data'], true);
}
$start_time = microtime(1);
$res = $this->avg($arr);
$end_time = microtime(1);
$console = [
'执行时长' => $end_time - $start_time,
'数量' => count($arr),
'结果' => $res,
];
var_dump($console);
exit();
}
public function actionConfigTest()
{
$select = DeviceUniqueData::find()->where(['id' => 1])->select('data')->asArray()->one();
$data = json_decode($select['data'], true);
foreach ($data as $key => $value) {
$arr[$key] = 1;
}
$model = new DeviceUniqueConfig();
$model->user_id = 0;
$model->config = json_encode($arr);
$model->created_at = time();
$model->save();
}
/**
* showdoc
* @catalog 门店
* @title 城市索引列表
* @description 本接口提供门店城市索引列表
* @method get
* @url /api/store/city
* @return {"code":0,"msg":"ok","data":{"X":[{"id":"146","parent_id":"13","name":"厦门市","level":"city"}]}}
* @remark
*/
public function actionCity()
{
$form = new StoreCityForm();
$data = $form->search();
return $this->responseHandler($data);
}
/**
* 门店服务
*/
public function actionStoreService()
{
$form = new StoreForm();
$data = $form->storeGoodsList();
return $this->responseHandler($data);
}
}