门店绑定优化
This commit is contained in:
@@ -97,53 +97,66 @@ class StoreController extends BaseAppController
|
||||
*/
|
||||
public function actionStoreChange()
|
||||
{
|
||||
$post=\Yii::$app->request->post();
|
||||
$post = \Yii::$app->request->post();
|
||||
|
||||
$this->requestValidate($post,[
|
||||
['store_id','required']
|
||||
$this->requestValidate($post, [
|
||||
['store_id', 'required']
|
||||
]);
|
||||
$t=\Yii::$app->db->beginTransaction();
|
||||
|
||||
// 获取当前用户
|
||||
$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('门店不存在');
|
||||
if (!$store) {
|
||||
throw new Exception('门店不存在');
|
||||
}
|
||||
|
||||
|
||||
StoreUser::updateAll(['is_online' => 0],[
|
||||
'user_id' => \Yii::$app->user->identity->getId(),
|
||||
StoreUser::updateAll(['is_online' => 0], [
|
||||
'user_id' => $userId,
|
||||
'is_delete' => 0,
|
||||
'is_online' => 1
|
||||
]);
|
||||
|
||||
$StoreUser=StoreUser::find()->where([
|
||||
'user_id' => \Yii::$app->user->identity->getId(),
|
||||
'store_id' => $post['store_id'],
|
||||
$StoreUser = StoreUser::find()->where([
|
||||
'user_id' => $userId,
|
||||
'store_id' => $post['store_id'],
|
||||
'is_delete' => 0,
|
||||
])->one();
|
||||
if (!$StoreUser){//绑定门店
|
||||
|
||||
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(),
|
||||
$StoreUser->user_id = $userId;
|
||||
// 如果是搜索小程序进入后扫码其他门店,删除默认门店数据
|
||||
StoreUser::updateAll(['is_delete' => 1], [
|
||||
'user_id' => $userId,
|
||||
'is_delete' => 0,
|
||||
'type' => 1
|
||||
'type' => 1
|
||||
]);
|
||||
}
|
||||
|
||||
$StoreUser->last_login_time = time();
|
||||
$StoreUser->type=2;
|
||||
$StoreUser->is_online=1;
|
||||
$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->current_store_id = $post['store_id'];
|
||||
$user->saveOrFail();
|
||||
|
||||
$t->commit();
|
||||
return ['success'];
|
||||
}catch (\Exception $exception){
|
||||
} catch (\Exception $exception) {
|
||||
$t->rollBack();
|
||||
throw new Exception($exception->getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user