Files
xk-api-yii/admin/controllers/system/DeliveryController.php

132 lines
4.4 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: chatfeed
* Date: 2022/5/21
* Time: 11:50 AM
*/
namespace admin\controllers\system;
use admin\components\BaseAdminController;
use admin\components\UI\Table;
use admin\exceptions\ApiException;
use admin\models\forms\PostageRulesEditForm;
use admin\models\search\CommonSearch;
use app\forms\common\CommonPostageRules;
use common\models\oldDb\PostageRules;
use yii\db\ActiveQuery;
class DeliveryController extends BaseAdminController
{
public function actionPostageRule(){
$table = new Table(new PostageRules());
//$table->model();
$table->title('角色管理');
$table->key('id');
$table->eventName(md5(get_called_class()));//设置事件名,后面更改数据对应
$table->url(\Yii::$app->urlManager->createUrl("system/delivery/postage-rule-list"));
$table->filter('配置名', 'name',false)->text('请输入配置名')->quick();
$table->action()->button('添加', $this->createUrl('system/delivery/postage-edit'));
$table->column('规则名','name');
$table->column('是否默认','status')->status(['否','是'],['gray','red']);
$column = $table->column('操作')->width(200);
$column->link('设为默认',$this->createUrl('system/delivery/postage-status'), ['id' => 'id'])->type('ajax', ['method' => 'post']);
$column->link('编辑', $this->createUrl('system/delivery/postage-edit'), ['id' => 'id']);
$column->link('删除', $this->createUrl('system/delivery/postage-del'), ['id' => 'id'])->type('ajax', ['method' => 'post']);
return $table->renderArray();
}
public function actionPostageStatus(){
$params = $this->requestValidate($this->get(),[
['id','required'],
['id','integer']
]);
$this->callbackEvent([],'id','refresh');
return PostageRules::setStatus($params->id);
}
public function actionPostageRuleList()
{
$commonSearch = new CommonSearch(['name']);
$commonSearch->setQuery(PostageRules::find()
->select(['id', 'name', 'status'])
->where(['mall_id'=>\Yii::$app->mallId])->andWhere(['is_delete'=>0]));
$commonSearch->additionRules = [
['name', 'string']
];
$commonSearch->bindFilter(function(/** @var ActiveQuery $q */ $q) {
$q->andFilterWhere(['like','name',$this->name]);
});
return $commonSearch->search($this->get());
}
public function actionPostageInfo(){
$id = $this->get('id');
$model = PostageRules::findOne([
'is_delete' => 0,
'mall_id' => \Yii::$app->mallId,
'id' => $id
]);
if (!$model) {
$model = new PostageRules();
$model->mall_id = \Yii::$app->mallId;
} else {
$model->detail = $model->decodeDetail();
}
return [
'model' => $model,
];
}
public function actionPostageEditSave(){
$id = $this->get('id');
$model = PostageRules::findOne([
'is_delete' => 0,
'mall_id' => \Yii::$app->mallId,
'id' => $id
]);
if (!$model) {
$model = new PostageRules();
$model->mall_id = \Yii::$app->mallId;
} else {
$model->detail = $model->decodeDetail();
}
$form = new PostageRulesEditForm();
$form->attributes = \Yii::$app->request->post('form');
$form->model = $model;
return $form->save();
}
public function actionPostageEdit(){
return $this->render('postage-edit');
}
public function actionAllList(){
$allList = PostageRules::find()->where([
'is_delete' => 0,
'mall_id' => \Yii::$app->mallId,
])->select('id as value,name as label')->asArray()->all();
foreach ($allList as &$v){
$v['value'] = (int)$v['value'];
}
unset($v);
array_unshift($allList, [
'value' => 0,
'label' => '默认运费'
]);
return [
'list' => $allList
];
}
public function actionPostageDel(){
$id = $this->get('id');
if(PostageRules::updateAll(['is_delete'=>1],['id'=>$id,'mall_id'=>\Yii::$app->mallId])){
$this->callbackEvent([],'id','refresh');
return [];
}else{
throw new ApiException("删除失败");
}
}
}