Files
xk-api-yii/member/modules/v1/controllers/StoreController.php

166 lines
5.5 KiB
PHP
Raw Normal View History

2024-09-19 11:32:39 +08:00
<?php
namespace member\modules\v1\controllers;
use common\core\BaseAppController;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\Store;
use common\models\oldDb\StoreUser;
2024-09-19 11:32:39 +08:00
use member\models\User;
use yii\db\Exception;
/**
* @doc-module 门店
*/
class StoreController extends BaseAppController
{
public $optional = ['issue', 'detail'];
/**
* @doc-name 门店信息
* @doc-return mixed @Data{id,store_id-int-门店id,@Store{*}} 信息
*/
public function actionInfo()
{
$StoreUser= StoreUser::find()->where([
2026-01-14 18:27:05 +08:00
// 'user_id'=>\Yii::$app->user->identity->getId(),
2024-09-19 11:32:39 +08:00
'store_id'=>\Yii::$app->store,
'is_delete'=>0
])->with(['store' => function ($q) {
2025-10-22 16:04:31 +08:00
$q->select('id,name,position,type,start_time,end_time,mobile');
2024-09-19 11:32:39 +08:00
}])->asArray()->one();
if (!$StoreUser) throw new Exception('门店不存在');
return $StoreUser;
}
//门店详情
public function actionDetail(){
2025-10-22 16:04:31 +08:00
$store = Store::find()->select('id,name,position,type,start_time,end_time,mobile')->where(['id' => \Yii::$app->store])->with(['nav' => function ($q){
2024-09-19 11:32:39 +08:00
$q->andWhere(['type' => 1]);//首页轮播图
$q->select(['id','store_id','pic','link_type','external_link']);
}])->asArray()->one();
if(!$store){
throw new Exception('门店不存在');
}
return $store;
}
/**
* @doc-name 用户所有门店
* @doc-return mixed @Data{id,store_id-int-门店id,is_online-int-是否在该门店0否1是,@Store{*}} 门店信息
*/
public function actionList()
{
#为了微信评审能顺利通过,做个开关
$open_issue = env('OPEN_ISSUE');
if($open_issue == 'true')
{
#此时开关已打开就做一些假数据demo
$list = [
[
'image' => 'https://p3.maiyaole.com/img/971/971752/org_org.jpg?v=1',
'name' => '【日康】对乙酰氨基酚片0.3g*12片*2板/盒',
'desc' => '清热解毒,咽喉肿痛,牙痛',
'price' => '¥34.00',
'num' => 1
],
[
'image' => 'https://p2.maiyaole.com/img/item/202210/24/202210241042513.jpg',
'name' => '同仁堂 六味地黄丸(浓缩丸) 200丸/瓶',
'desc' => '滋阴补肾。用于阴肾亏损,头晕耳鸣,腰膝酸软,骨蒸潮热,盗汗遗精。',
'price' => '¥22.00',
'num' => 1
]
];
}else{
#首页门店列表
$list = StoreUser::find()->where([
'user_id'=>\Yii::$app->user->identity->getId(),
'is_delete'=>0
])->with(['store' => function ($q){
$q->select('id,drugstore_id,name,position,contact,start_time,end_time');
}])->asArray()->all();
if (!$list) throw new Exception('暂时没有任何门店');
}
$data['open_issue'] = $open_issue;
$data['list'] = $list;
return $data;
}
/**
* @doc-name 门店切换
* @doc-param int store_id 要切换的门店id
*/
public function actionStoreChange()
{
$post=\Yii::$app->request->post();
$this->requestValidate($post,[
['store_id','required']
]);
$t=\Yii::$app->db->beginTransaction();
try {
$store = Store::findOne($post['store_id']);
if(!$store)throw new Exception('门店不存在');
StoreUser::updateAll(['is_online' => 0],[
'user_id' => \Yii::$app->user->identity->getId(),
'is_delete' => 0,
'is_online' => 1
]);
$StoreUser=StoreUser::find()->where([
'user_id' => \Yii::$app->user->identity->getId(),
'store_id' => $post['store_id'],
'is_delete' => 0,
])->one();
if (!$StoreUser){//绑定门店
$StoreUser = new StoreUser();
$StoreUser->store_id = $post['store_id'];
$StoreUser->user_id = \Yii::$app->user->identity->getId();
//如果是搜索小程序进入后扫码其他门店,删除默认门店数据
StoreUser::updateAll(['is_delete' => 1],[
'user_id' => \Yii::$app->user->identity->getId(),
'is_delete' => 0,
'type' => 1
]);
}
$StoreUser->last_login_time = time();
$StoreUser->type=2;
$StoreUser->is_online=1;
$StoreUser->saveOrFail();
$user=User::find()->where(['id'=>\Yii::$app->user->identity->getId()])->one();
if (!$user) throw new Exception('用户不存在');
$user->current_store_id=$post['store_id'];
$user->saveOrFail();
$t->commit();
return ['success'];
}catch (\Exception $exception){
$t->rollBack();
throw new Exception($exception->getMessage());
}
}
/**
* @doc-name 用户端首页开关
* @doc-return mixed @Data{id,store_id-int-门店id,is_online-int-是否在该门店0否1是,@Store{*}} 门店信息
*/
public function actionIssue()
{
#为了微信评审能顺利通过,做个开关
$open_issue = env('OPEN_ISSUE');
$data['open_issue'] = $open_issue;
return $data;
}
}