<?php<hyzwd@outlook.com>namespace app\index\controller;
use app\index\model\Logistics AS LogisticsModel;
use think\Db;
use think\Url;
use think\Request;
use think\Session;
class Logistics extends Common_base
{
public function _initialize()
{
IS_ROOT([1,2]) ? true : $this->error('没有权限');
return parent::_initialize(); }
public function index() {
$Request = Request::instance();
$query = $Request->param(); $q = $Request->param('q');
$model = new LogisticsModel();
if ($q !== null) {
$data = $model->where('log_name', 'like', '%'.$q.'%')->where('status', 1)->order('log_id', 'desc')->paginate('', false, ['query' => $query ]);
} else {
$data = $model->where('status', 1)->order('log_id', 'desc')->paginate(20);
}
$page = $data->render();
$assign = [
'title' => '物流信息',
'data' => $data,
'page' => $page,
'empty' => '<tr><td colspan="7" align="center">当前条件没有查到数据</td></tr>',
];
$this->assign($assign);
return $this->fetch();
}
public function add() {
return $this->fetch();
}
public function add_do() {
$Request = Request::instance();
if ($Request->isPost()) {
$name = $Request->param('log_name');
if (LogisticsModel::getByLogName($name)) {
$this->error('物流名称重复,请更换!');
}
$model = new LogisticsModel($_POST);
$result = $model->allowField(true)->save();
if ($result) {
$this->success('新增物流信息成功');
} else {
$this->error('数据操作错误,请联系管理员!');
}
}
}
public function edit() {
$Request = Request::instance();
$id = $Request->param('id');
if (empty($id)) {
$this->error('参数错误!');
}
if (!LogisticsModel::get($id)) {
$this->error('没有数据!');
}
$model = new LogisticsModel();
$data = $model->where('log_id', $id)->find();
$this->assign('title', '修改物流信息');
$this->assign('data', $data);
return $this->fetch();
}
public function edit_do() {
$Request = Request::instance();
if ($Request->isPost()) {
$id = $Request->param('log_id');
if (empty($id)) {
$this->error('参数错误!');
}
if (!LogisticsModel::get($id)) {
$this->error('没有数据!');
}
Db::name('logistics')->where('log_id', $id)->update([
'log_phone' => $Request->param('log_phone'),
'log_fax' => $Request->param('log_fax'),
'log_address' => $Request->param('log_address'),
]);
$this->success('修改成功', Url::build('logistics/index'));
}
}
public function check_name() {
$Request = Request::instance();
if ($Request->isPost()) {
$name = $Request->param('name');
if (empty($name)) {
$this->error('参数错误!');
}
$ByName = Db::name('logistics')->where('log_name', $name)->find();
if ($ByName) {
return false;
} else {
return true;
}
}
}
public function select() {
$Request = Request::instance();
$query = $Request->param(); $q = $Request->param('q');
$model = new LogisticsModel();
if ($q === null) {
$data = $model->where('status', 1)->order('log_id', 'desc')->paginate(10);
} else {
$data = $model->where('log_name', 'like', '%'.$q.'%')->where('status', 1)->order('log_id', 'desc')->paginate('10', false, ['query' => $query ]);
}
$page = $data->render();
$assign = [
'data' => $data,
'page' => $page,
'empty' => '<tr><td colspan="5" align="center">当前条件没有查到数据</td></tr>',
];
$this->assign($assign);
return $this->fetch();
}
public function delete() {
\think\Config::set("default_return_type","json");
$Request = Request::instance();
if ($Request->isPost()) {
$uid = $Request->param("uid");
$name = $Request->param("name");
if (empty($uid)) {
$this->error('传入参数错误');
}
if ($name == 'delone') {
Db::name('logistics')->where('log_id', $uid)->update(['status'=>'-1']);
$this->success('删除成功', Url::build('logistics/index'));
} elseif ($name == 'delallattr') {
$arrUid = explode(",",$uid);
if (!empty($arrUid)) {
$i=0;
foreach ($arrUid as $key=>$val) {
Db::name('logistics')->where('log_id', $val)->update(['status'=>'-1']);
$i++;
}
$this->success($i.' 条记录删除成功', Url::build('logistics/index'));
}
} else {
$this->error('传入参数错误');
}
}
}
}