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'] ]); // 获取当前用户 $userId = \Yii::$app->user->identity->getId(); $user = User::findOne($userId); if (!$user) { throw new Exception('用户不存在'); } // 如果当前门店就是要切换的门店,直接返回 if ($user->current_store_id == $post['store_id']) { return ['success1']; } $t = \Yii::$app->db->beginTransaction(); try { $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) { // 绑定门店 $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->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; } }