You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.1 KiB

<?php
namespace app\base\controller\freight\admin;
use app\base\logic\District;
use think\App;
use think\facade\View;
use think\Validate;
class Freight extends Base
{
/**
* 运费设置
* @return array|mixed|string
*/
public function freight()
{
$where = [
'uid' => UID,
];
$freight_model = new \app\base\model\freight\Freight();
$dataList = $freight_model->getAllData($where);
View::assign('dataList', $dataList);
return View::fetch();
}
/**
* 规则添加
*/
public function freightmode()
{
$district_logic = new District();
//获取省市数据
$mode = input('param.mode', 1);
$list = $district_logic->listDistrictTree();
View::assign('list', $list);
View::assign('mode', $mode);
return View::fetch('freightmode');
}
/**
* 运费添加
* @return array|mixed|string
*/
public function freightAdd()
{
$freight_model = new \app\base\model\freight\Freight();
if (request()->isPost()) { //新增和更新
$data = input('post.');
$data['uid'] = UID;
if ($data['type'] == 1) {
$rule = [
'name' => 'require|max:30',
'type' => 'require',
'first_number' => 'require',
'first_number_money' => 'require',
'second_number' => 'require',
'second_number_money' => 'require',
'free_money' => 'require',
];
$msg = [
'name.require' => '规则名称必须',
'name.max' => '规则名称最多不能超过30个字符',
'type.require' => '运费方式必须',
'first_number.require' => '首件必填',
'first_number_money.require' => '首费必填',
'second_number.require' => '续件必填',
'second_number_money.require' => '续费必填',
'free_money.require' => '免邮金额必填',
];
} else {
$rule = [
'name' => 'require|max:30',
'type' => 'require',
'first_weight' => 'require',
'first_weight_money' => 'require',
'second_weight' => 'require',
'second_weight_money' => 'require',
'free_money' => 'require',
];
$msg = [
'name.require' => '规则名称必须',
'name.max' => '规则名称最多不能超过30个字符',
'type.require' => '运费方式必须',
'first_weight.require' => '首重必填',
'first_weight_money.require' => '首费必填',
'second_weight.require' => '续重必填',
'second_weight_money.require' => '续费必填',
'free_money.require' => '免邮金额必填',
];
}
$validate = new Validate($rule, $msg);
if (!$validate->check($data)) {
return sendErrorMessage(1,$validate->getError());
}
//检测数据的合理性
if (!empty($data['id'])) {
$where['id'] = ['neq', $data['id']];
}
$data['fee_conf'] = array_key_exists('fee_conf',$data)?$data['fee_conf']:null;
$res = $freight_model->dataUpdate($data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else { //编辑,添加
$id = input('param.id', 0);
$data = $freight_model->getOneData(['id' => $id]);
//运费
$index = 0;
if (!empty($data)) {
if (!empty($data['fee_conf'])) {
if(!empty($data['fee_conf'])){
$index = max(array_keys($data['fee_conf']));
}
}
}
View::assign('data', $data);
View::assign('index', $index); //搜索最大的key值
View::assign('mode', isset($data['type']) ? $data['type'] : 1);
return View::fetch('freightadd');
}
}
/**
* 运费修改
* @return array|mixed|string
*/
public function freightUpdate()
{
return $this->freightAdd();
}
/**
* 更新运费设置的默认项
* @return array|string|void
*/
public function updateDefault()
{
$data = input('post.');
$mall_freight_model = new \app\base\model\freight\Freight();
if ($data['afterchange'] == 1) {
$mall_freight_model->where('id !=' . $data['id'])->update(['is_default' => 0]);
}
$res = $mall_freight_model->dataUpdate(['is_default' => $data['afterchange'], 'id' => $data['id']]);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
}
}