Files
xk-api-yii/member/modules/v1/controllers/StoreController.php
2026-05-08 15:37:52 +08:00

192 lines
6.3 KiB
PHP
Raw 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 member\modules\v1\controllers;
use common\core\BaseAppController;
use common\models\oldDb\Store;
use common\models\oldDb\StoreUser;
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([
// 'user_id'=>\Yii::$app->user->identity->getId(),
'store_id'=>\Yii::$app->store,
'is_delete'=>0
])->with(['store' => function ($q) {
$q->select('id,name,position,type,start_time,end_time,mobile');
}])->asArray()->one();
if (!$StoreUser) throw new Exception('门店不存在');
return $StoreUser;
}
//门店详情
public function actionDetail(){
$store = Store::find()->select('id,name,position,type,start_time,end_time,mobile')->where(['id' => \Yii::$app->store])->with(['nav' => function ($q){
$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 {
$userId = \Yii::$app->user->identity->getId();
$store = Store::findOne($post['store_id']);
if (!$store) {
throw new Exception('门店不存在');
}
// 先将该用户所有在线门店置为离线
StoreUser::updateAll(['is_online' => 0], [
'user_id' => $userId,
'is_delete' => 0,
'is_online' => 1
]);
// 先查找未删除的绑定记录
$StoreUser = StoreUser::find()->where([
'user_id' => $userId,
'store_id' => $post['store_id'],
'is_delete' => 0,
])->one();
if (!$StoreUser) {
// 未找到有效记录,尝试恢复已删除的同门店记录
$deletedStoreUser = StoreUser::find()->where([
'user_id' => $userId,
'store_id' => $post['store_id'],
'is_delete' => 1,
])->one();
if ($deletedStoreUser) {
// 恢复旧记录,避免唯一键冲突
$StoreUser = $deletedStoreUser;
$StoreUser->is_delete = 0;
} else {
// 完全新建绑定
$StoreUser = new StoreUser();
$StoreUser->store_id = $post['store_id'];
$StoreUser->user_id = $userId;
// 清除之前的默认门店(仅针对全新绑定)
StoreUser::updateAll(['is_delete' => 1], [
'user_id' => $userId,
'is_delete' => 0,
'type' => 1
]);
}
}
// 更新登录信息并保存
$StoreUser->last_login_time = time();
$StoreUser->type = 2;
$StoreUser->is_online = 1;
$StoreUser->saveOrFail();
// 更新用户当前门店
$user = User::find()->where(['id' => $userId])->one();
if (!$user) {
throw new Exception('用户不存在');
}
$user->current_store_id = $post['store_id'];
$user->saveOrFail();
$t->commit();
return ['success'];
} catch (\Exception $exception) {
$t->rollBack();
ds([1], $exception->getMessage());
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;
}
}