adminOperateLogger = new UserOperate([ // 'fetchClass' => BaReceiveAddress::class, // ]); } /** * @inheritdoc */ public function rules() { return [ [['id', 'consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address', 'isDefault'], 'trim'], [['id', 'consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address'], 'required'], [['mobile'], 'mobile'], [['province'], 'exist', 'targetClass' => Region::class, 'targetAttribute' => 'REGION_CODE'], ]; } public function attributeLabels() { return [ 'consignee' => 'Consignee', 'mobile' => 'Phone', 'province' => 'State', 'lgaName' => 'Local Government Area', 'cityName' => 'City Name', 'address' => 'Address', ]; } /** * 指定校验场景 * @return array */ public function scenarios() { $parentScenarios = parent::scenarios(); $customScenarios = [ 'userAdd' => ['consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address', 'isDefault'], 'userEdit' => ['id', 'consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address', 'isDefault'], 'userIsDefault' => ['id', 'isDefault'], ]; return array_merge($parentScenarios, $customScenarios); } /** * 校验之前 * @return bool */ 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' => $userId])->count(); if ($count > 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', 'Address does not exist'); // 地址不存在 return $parentResult; } if ($this->_model['USER_ID'] != $userId){ $this->addError('id', 'You have no right to modify this address'); // 无权修改此地址 return $parentResult; } } else { $this->_model = new BaReceiveAddress(); } } return $parentResult; } /** * 添加编辑 * @return BaReceiveAddress|null * @throws \yii\db\Exception */ public function edit(){ 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' => $userId]); } if($this->scenario == 'userAdd'){ $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; $this->_model->LGA_NAME = $this->lgaName; $this->_model->CITY_NAME = $this->cityName; $this->_model->ADDRESS = $this->address; $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0; $this->_model->CREATED_AT = Date::nowTime(); } elseif($this->scenario == 'userEdit') { $this->_model->CONSIGNEE = $this->consignee; $this->_model->MOBILE = $this->mobile; $this->_model->PROVINCE = $this->province; $this->_model->LGA_NAME = $this->lgaName; $this->_model->CITY_NAME = $this->cityName; $this->_model->ADDRESS = $this->address; $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0; $this->_model->UPDATED_AT = Date::nowTime(); } elseif($this->scenario == 'userIsDefault') { $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0; } else { throw new Exception('The scene does not exist'); // 场景不存在 } if(!$this->_model->save()){ throw new Exception(Form::formatErrorsForApi($this->_model->getErrors())); } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); $this->addError('edit', $e->getMessage()); return null; } if($this->scenario == 'adminAdd'){ $this->adminOperateLogger->afterInsert($this->_model)->clean()->save([ 'optType' => 'Add shipping address', // 添加收货地址 ]); } elseif($this->scenario == 'adminEdit') { $this->adminOperateLogger->afterUpdate($this->_model)->clean()->save([ 'optType' => 'Edit shipping address', // 编辑收货地址 ]); } return $this->_model; } /** * 删除前 * @param $selected */ public function beforeDelete($selected) { $this->adminOperateLogger->fetchClass = Ad::class; $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID'); } /** * 删除 * @param $selected * @throws Exception */ public function delete($selected) { $this->adminOperateLogger->clean()->save([ 'optType' => 'Delete shipping address', // 删除收货地址 ]); } }