Преглед изворни кода

BA会员收货地址修改

kevin_zhangl пре 3 година
родитељ
комит
4551ca5697

+ 19 - 15
common/models/forms/BaReceiveAddressForm.php

@@ -35,9 +35,9 @@ class BaReceiveAddressForm extends Model
 
     public function init() {
         parent::init();
-        $this->adminOperateLogger = new UserOperate([
-            'fetchClass' => BaReceiveAddress::class,
-        ]);
+//        $this->adminOperateLogger = new UserOperate([
+//            'fetchClass' => BaReceiveAddress::class,
+//        ]);
     }
 
     /**
@@ -86,23 +86,25 @@ class BaReceiveAddressForm extends Model
      */
     public function beforeValidate()
     {
+        $userId = \Yii::$app->getUser()->getId();
         $parentResult = parent::beforeValidate();
         if ($this->scenario == 'userAdd' || $this->scenario == 'userEdit' || $this->scenario == 'userIsDefault') {
             if ($this->scenario == 'userAdd') {
-                $count = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id])->count();
+
+                $count = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->count();
                 if ($count > 10) {
-                    $this->addError('id', '最多只能添加10个收货地址');
+                    $this->addError('id', 'A maximum of 10 shipping addresses can be added'); // 最多只能添加10个收货地址
                     return $parentResult;
                 }
             }
             if ($this->id) {
                 $this->_model = BaReceiveAddress::findOne(["ID" => $this->id]);
                 if (!$this->_model){
-                    $this->addError('id', '地址不存在');
+                    $this->addError('id', 'Address does not exist'); // 地址不存在
                     return $parentResult;
                 }
-                if ($this->_model['USER_ID'] != \Yii::$app->user->id){
-                    $this->addError('id', '无权修改此地址');
+                if ($this->_model['USER_ID'] != $userId){
+                    $this->addError('id', 'You have no right to modify this address'); // 无权修改此地址
                     return $parentResult;
                 }
             } else {
@@ -122,17 +124,19 @@ class BaReceiveAddressForm extends Model
         if(!$this->validate()){
             return null;
         }
+
+        $userId = \Yii::$app->getUser()->getId();
         $db = \Yii::$app->db;
         $transaction = $db->beginTransaction();
         try {
             // 如果设置了此项为默认则清空所有默认
             if ($this->isDefault) {
-                BaReceiveAddress::updateAll(['IS_DEFAULT' => 0], 'USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id]);
+                BaReceiveAddress::updateAll(['IS_DEFAULT' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
             }
 
             if($this->scenario == 'userAdd'){
-                $this->_model->USER_ID = \Yii::$app->user->id;
-                $this->_model->USER_NAME = Info::getUserNameByUserId(\Yii::$app->user->id); // TODO:
+                $this->_model->USER_ID = $userId;
+                $this->_model->USER_NAME = Info::getBaUserNameByUserId($userId); // TODO:
                 $this->_model->CONSIGNEE = $this->consignee;
                 $this->_model->MOBILE = $this->mobile;
                 $this->_model->PROVINCE = $this->province;
@@ -153,7 +157,7 @@ class BaReceiveAddressForm extends Model
             } elseif($this->scenario == 'userIsDefault') {
                 $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
             } else {
-                throw new Exception('提交场景不存在');
+                throw new Exception('The scene does not exist'); // 场景不存在
             }
             if(!$this->_model->save()){
                 throw new Exception(Form::formatErrorsForApi($this->_model->getErrors()));
@@ -166,11 +170,11 @@ class BaReceiveAddressForm extends Model
         }
         if($this->scenario == 'adminAdd'){
             $this->adminOperateLogger->afterInsert($this->_model)->clean()->save([
-                'optType' => '添加收货地址',
+                'optType' => 'Add shipping address', // 添加收货地址
             ]);
         } elseif($this->scenario == 'adminEdit') {
             $this->adminOperateLogger->afterUpdate($this->_model)->clean()->save([
-                'optType' => '编辑收货地址',
+                'optType' => 'Edit shipping address', // 编辑收货地址
             ]);
         }
         return $this->_model;
@@ -191,7 +195,7 @@ class BaReceiveAddressForm extends Model
      */
     public function delete($selected) {
         $this->adminOperateLogger->clean()->save([
-            'optType' => '删除收货地址',
+            'optType' => 'Delete shipping address', // 删除收货地址
         ]);
     }
 }

+ 7 - 7
common/models/forms/ReceiveAddressForm.php

@@ -95,18 +95,18 @@ class ReceiveAddressForm extends Model
             if ($this->scenario == 'userAdd') {
                 $count = ReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->count();
                 if ($count > 10) {
-                    $this->addError('id', '最多只能添加10个收货地址');
+                    $this->addError('id', 'A maximum of 10 shipping addresses can be added'); // 最多只能添加10个收货地址
                     return $parentResult;
                 }
             }
             if ($this->id) {
                 $this->_model = ReceiveAddress::findOne(["ID"=>$this->id]);
                 if (!$this->_model){
-                    $this->addError('id', '地址不存在');
+                    $this->addError('id', 'Address does not exist'); // 地址不存在
                     return $parentResult;
                 }
                 if ($this->_model['USER_ID'] != \Yii::$app->user->id){
-                    $this->addError('id', '无权修改此地址');
+                    $this->addError('id', 'You have no right to modify this address'); // 无权修改此地址
                     return $parentResult;
                 }
             } else {
@@ -156,7 +156,7 @@ class ReceiveAddressForm extends Model
             } elseif($this->scenario == 'userIsDefault') {
                 $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
             } else {
-                throw new Exception('提交场景不存在');
+                throw new Exception('The scene does not exist'); // 场景不存在
             }
             if(!$this->_model->save()){
                 throw new Exception(Form::formatErrorsForApi($this->_model->getErrors()));
@@ -169,11 +169,11 @@ class ReceiveAddressForm extends Model
         }
         if($this->scenario == 'adminAdd'){
             $this->adminOperateLogger->afterInsert($this->_model)->clean()->save([
-                'optType' => '添加收货地址',
+                'optType' => 'Add shipping address', // 添加收货地址
             ]);
         } elseif($this->scenario == 'adminEdit') {
             $this->adminOperateLogger->afterUpdate($this->_model)->clean()->save([
-                'optType' => '编辑收货地址',
+                'optType' => 'Edit shipping address', // 编辑收货地址
             ]);
         }
         return $this->_model;
@@ -194,7 +194,7 @@ class ReceiveAddressForm extends Model
      */
     public function delete($selected) {
         $this->adminOperateLogger->clean()->save([
-            'optType' => '删除收货地址',
+            'optType' => 'Delete shipping address', // 删除收货地址
         ]);
     }
 }

+ 58 - 19
frontendApi/modules/v1/controllers/BaseController.php

@@ -11,6 +11,8 @@ namespace frontendApi\modules\v1\controllers;
 use common\components\ActiveRecord;
 use common\helpers\Date;
 use common\helpers\Form;
+use common\helpers\LoggerTool;
+use \frontendApi\modules\v1\models\brand\User AS Brand;
 use frontendApi\modules\v1\models\User;
 use Yii;
 use yii\db\Exception;
@@ -38,6 +40,17 @@ class BaseController extends \yii\rest\ActiveController {
         }
     }
 
+    /**
+     * @throws ForbiddenHttpException
+     */
+    protected function forbiddenQuicklyBaUser() {
+        $isQuickly = Brand::isQuicklyLogin();
+        $requestMethod = Yii::$app->request->getMethod();
+        if ($isQuickly == 1 && strtoupper($requestMethod) != 'GET') {
+            throw new ForbiddenHttpException('快速登录的会员无法进行任何操作!');
+        }
+    }
+
     /**
      * @param $action
      * @return bool
@@ -45,27 +58,53 @@ class BaseController extends \yii\rest\ActiveController {
      * @throws \yii\web\BadRequestHttpException
      */
     public function beforeAction($action) {
-        $this->forbiddenQuicklyUser();
+        if (!\Yii::$app->getUser()->isGuest) {
+            $this->forbiddenQuicklyUser();
 
-        // 增加的判断用户登录后未操作后的超时 和 快速登录的逻辑
-        $isQuickly = User::isQuicklyLogin();
-        if ($isQuickly != 1 && Yii::$app->getUser()->getUserInfo()){
-            $userId = Yii::$app->getUser()->getUserInfo()['id'];
-            $redisKey = 'user:timeOut';
+            // 增加的判断用户登录后未操作后的超时 和 快速登录的逻辑
+            $isQuickly = User::isQuicklyLogin();
+            if ($isQuickly != 1 && Yii::$app->getUser()->getUserInfo()) {
+                $userId = Yii::$app->getUser()->getUserInfo()['id'];
+                $redisKey = 'user:timeOut';
 
-            $lastTime = '';
-            if (!Yii::$app->tokenRedis->hget($redisKey, $userId)) {
-                $lastTime = time();
-            }else{
-                $lastTime = Yii::$app->tokenRedis->hget($redisKey, $userId);
+                $lastTime = '';
+                if (!Yii::$app->tokenRedis->hget($redisKey, $userId)) {
+                    $lastTime = time();
+                } else {
+                    $lastTime = Yii::$app->tokenRedis->hget($redisKey, $userId);
+                }
+
+                $currentTime = time();
+                $timeOut = Yii::$app->params['operationTimeOut'];
+                if ($currentTime - $lastTime > $timeOut) {
+                    return self::notice('Connection not operated for too long', 402);
+                } else {
+                    Yii::$app->tokenRedis->hset($redisKey, $userId, time());
+                }
             }
+        } else {
+            $this->forbiddenQuicklyBaUser();
 
-            $currentTime = time();
-            $timeOut = Yii::$app->params['operationTimeOut'];
-            if ($currentTime - $lastTime > $timeOut) {
-                return self::notice('Connection not operated for too long', 402);
-            } else {
-                Yii::$app->tokenRedis->hset($redisKey, $userId, time());
+            // 增加的判断用户登录后未操作后的超时 和 快速登录的逻辑
+            $isQuickly = Brand::isQuicklyLogin();
+            if ($isQuickly != 1 && \Yii::$app->getUser()->getId()) {
+                $userId = Yii::$app->getUser()->getId();
+                $redisKey = 'user:timeOut';
+
+                $lastTime = '';
+                if (!Yii::$app->tokenRedis->hget($redisKey, $userId)) {
+                    $lastTime = time();
+                } else {
+                    $lastTime = Yii::$app->tokenRedis->hget($redisKey, $userId);
+                }
+
+                $currentTime = time();
+                $timeOut = Yii::$app->params['operationTimeOut'];
+                if ($currentTime - $lastTime > $timeOut) {
+                    return self::notice('Connection not operated for too long', 402);
+                } else {
+                    Yii::$app->tokenRedis->hset($redisKey, $userId, time());
+                }
             }
         }
 
@@ -184,7 +223,7 @@ class BaseController extends \yii\rest\ActiveController {
             if ($isDelData) {
                 // 真实删除数据
                 if (!$modelClass::deleteAll($condition, $params)) {
-                    throw new Exception('删除失败');
+                    throw new Exception('failed to delete');//删除失败
                 }
             } else {
                 // 设置IS_DEL字段为1
@@ -192,7 +231,7 @@ class BaseController extends \yii\rest\ActiveController {
             }
             if ($afterFun) $afterFun($selected);
             $transaction->commit();
-            return self::notice('删除成功');
+            return self::notice('delete successfully');//删除成功
         } catch (Exception $e) {
             $transaction->rollBack();
             return self::notice($e->getMessage(), 500);

+ 5 - 5
frontendApi/modules/v1/controllers/ConfigController.php

@@ -180,7 +180,7 @@ class ConfigController extends BaseController
      */
     public function actionBaReceiveAddressList() {
         $condition = ' AND USER_ID=:USER_ID';
-        $params[':USER_ID'] = \Yii::$app->user->id;
+        $params[':USER_ID'] = \Yii::$app->getUser()->getId();
         $data = BaReceiveAddress::lists($condition, $params, [
             'SELECT' => 'ID,CONSIGNEE,MOBILE,PROVINCE,LGA_NAME,CITY_NAME,ADDRESS,IS_DEFAULT',
             'orderBy' => 'IS_DEFAULT DESC,CREATED_AT DESC',
@@ -202,7 +202,7 @@ class ConfigController extends BaseController
     public function actionBaReceiveAddressOne() {
         $data = BaReceiveAddress::findOneAsArray(
             'USER_ID=:USER_ID AND ID=:ID',
-            [':USER_ID'=>\Yii::$app->user->id, ':ID' => \Yii::$app->request->get('id')]
+            [':USER_ID'=>\Yii::$app->getUser()->getId(), ':ID' => \Yii::$app->request->get('id')]
         );
         return static::notice($data);
     }
@@ -254,10 +254,10 @@ class ConfigController extends BaseController
         if(\Yii::$app->request->isPost) {
             return parent::delete(BaReceiveAddress::class, null, function() {
                 // 如果没有默认地址的话,就在设置一个默认地址
-                if (!BaReceiveAddress::find()->where('USER_ID=:USER_ID AND IS_DEFAULT=1', [':USER_ID' => \Yii::$app->user->id])->exists()) {
-                    $addressNumber = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id])->count();
+                if (!BaReceiveAddress::find()->where('USER_ID=:USER_ID AND IS_DEFAULT=1', [':USER_ID' => \Yii::$app->getUser()->getId()])->exists()) {
+                    $addressNumber = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->getUser()->getId()])->count();
                     if ($addressNumber >= 1) {
-                        $model = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id])->one();
+                        $model = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->getUser()->getId()])->one();
                         $model->IS_DEFAULT = 1;
                         if (!$model->save()) {
                             throw new Exception('更新默认地址失败');