作者 guosheng

后台订单管理开发

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use app\index\model\OrderInfo;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
/**
* 订单子管理
*
* @icon fa fa-circle-o
*/
class OrderDetail extends Backend
{
/**
* OrderDetail模型对象
* @var \app\admin\model\OrderDetail
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\OrderDetail;
$this->view->assign("payTypeList", $this->model->getPayTypeList());
$this->view->assign("expressList", $this->model->getExpressList());
$this->view->assign("statusList", $this->model->getStatusList());
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax())
{
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField'))
{
return $this->selectpage();
}
$where_store = [
'order_detail.store_id' => session('admin.store_id')
];
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->with(['user','store'])
->where($where)
->where($where_store)
->order($sort, $order)
->count();
$list = $this->model
->with(['user','store'])
->where($where)
->where($where_store)
->order($sort, $order)
->limit($offset, $limit)
->select();
foreach ($list as $row) {
$row->getRelation('user')->visible(['nickname']);
$row->getRelation('store')->visible(['name']);
}
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list,'s'=>$this->model->getLastSql());
return json($result);
}
return $this->view->fetch();
}
/**
* 详情
*/
public function detail($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$order_info_model = new OrderInfo();
$row->goods = $order_info_model->where('order_detail_id',$row->id)->select();
$this->view->assign("row", $row);
return $this->view->fetch();
}
/**
* 发货
*/
public function send($ids = null)
{
if($ids) {
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
$params = $this->request->param();
if($row->status != 2) {
$this->error('当前状态无法执行发货操作');
}
$row->status = $params['status'];
Db::startTrans();
try {
$result = $row->save();
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (\think\Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if(!$result) {
$this->error('发货失败');
}
$this->success('发货成功');
}
}
/**
* 退款
*/
public function refund($ids = null)
{
if($ids) {
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
$params = $this->request->param();
if($row->status != 8) {
$this->error('当前状态无法执行退款操作');
}
$row->status = $params['status'];
Db::startTrans();
try {
$result = $row->save();
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (\think\Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if(!$result) {
$this->error('退款失败');
}
$this->success('退款成功');
}
}
}
... ...
<?php
return [
'User_id' => '用户id',
'Order_id' => '主订单id',
'Store_id' => '店铺id',
'Num' => '子订单号',
'Pay_type' => '支付类型',
'Pay_type 1' => '微信支付',
'Pay_type 2' => '支付宝支付',
'Pay_type 3' => '云闪付',
'Express' => '物流方式',
'Express 1' => '快递运输',
'Express 2' => '达达下单',
'Express 3' => '美团跑腿',
'Total' => '总金额',
'Goods_total' => '商品金额',
'Postage_total' => '物流费用',
'Name' => '收货人',
'Mobile' => '手机号',
'Province_name' => '省',
'City_name' => '市',
'County_name' => '区/县',
'Address' => '详细地址',
'Createtime' => '创建时间',
'Paytime' => '支付时间',
'Status' => '状态',
'Status 1' => '待支付',
'Status 2' => '待发货',
'Status 3' => '待收货',
'Status 4' => '待评价',
'Status 5' => '售后',
'Status 6' => '已完成',
'Status 7' => '已取消',
'Status 8' => '退款中',
'Status 9' => '已退款',
'Status 10' => '已删除',
'User.nickname' => '昵称',
'Store.name' => '店铺名称',
'Goodsname' => '商品名称',
'Goods_image' => '商品图片',
'Goods_price' => '商品单价',
'Number' => '商品数量',
];
... ...
<?php
namespace app\admin\model;
use think\Model;
class OrderDetail extends Model
{
// 表名
protected $name = 'order_detail';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
'pay_type_text',
'express_text',
'paytime_text',
'status_text'
];
public function getPayTypeList()
{
return ['1' => __('Pay_type 1'), '2' => __('Pay_type 2'), '3' => __('Pay_type 3')];
}
public function getExpressList()
{
return ['1' => __('Express 1'), '2' => __('Express 2'), '3' => __('Express 3')];
}
public function getStatusList()
{
return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5'), '6' => __('Status 6'), '7' => __('Status 7'), '8' => __('Status 8'), '9' => __('Status 9'), '10' => __('Status 10')];
}
public function getPayTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
$list = $this->getPayTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getExpressTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['express']) ? $data['express'] : '');
$list = $this->getExpressList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getPaytimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
protected function setPaytimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
public function user()
{
return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function store()
{
return $this->belongsTo('Store', 'store_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
}
... ...
<?php
namespace app\admin\validate;
use think\Validate;
class OrderDetail extends Validate
{
/**
* 验证规则
*/
protected $rule = [
];
/**
* 提示消息
*/
protected $message = [
];
/**
* 验证场景
*/
protected $scene = [
'add' => [],
'edit' => [],
];
}
... ...
<style>
.content_config img {
max-width: 100% !important;
}
</style>
<table class="table table-striped">
<!-- <thead>-->
<!-- <tr>-->
<!-- <th></th>-->
<!-- <th></th>-->
<!-- </tr>-->
<!-- </thead>-->
<tbody>
{volist name="row.goods" id="vo"}
<tr>
<td width="150">{:__('Goodsname')}</td>
<td>{$vo.goodsname}</td>
</tr>
<tr>
<td>{:__('Goods_image')}</td>
<td>{$vo.goods_image}</td>
</tr>
<tr>
<td>{:__('Goods_price')}</td>
<td>{$vo.goods_price}</td>
</tr>
<tr>
<td>{:__('Number')}</td>
<td>{$vo.number}</td>
</tr>
{/volist}
</tbody>
</table>
<div class="hide layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
<button type="reset" class="btn btn-primary btn-embossed btn-close" onclick="Layer.closeAll();">{:__('Close')}</button>
</div>
</div>
\ No newline at end of file
... ...
<div class="panel panel-default panel-intro">
<div class="panel-heading">
{:build_heading(null,FALSE)}
<ul class="nav nav-tabs" data-field="status">
<li class="active"><a href="#t-all" data-value="" data-toggle="tab">{:__('All')}</a></li>
{foreach name="statusList" item="vo"}
<li><a href="#t-{$key}" data-value="{$key}" data-toggle="tab">{$vo}</a></li>
{/foreach}
</ul>
</div>
<div class="panel-body">
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade active in" id="one">
<div class="widget-body no-padding">
<div id="toolbar" class="toolbar">
<a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
<!-- <a href="javascript:;" class="btn btn-success btn-add {:$auth->check('order_detail/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>-->
<!-- <a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('order_detail/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>-->
<a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('order_detail/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
</div>
<table id="table" class="table table-striped table-bordered table-hover table-nowrap"
data-operate-detail="{:$auth->check('order_detail/detail')}"
data-operate-edit=""
data-operate-del="{:$auth->check('order_detail/del')}"
width="100%">
</table>
</div>
</div>
</div>
</div>
</div>
... ...
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'order_detail/index' + location.search,
del_url: 'order_detail/del',
detail_url: 'order_detail/detail',
send_url: 'order_detail/send',
refund_url: 'order_detail/refund',
table: 'order_detail',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'user_id', title: __('User_id')},
{field: 'user.nickname', title: __('User.nickname')},
{field: 'store.name', title: __('Store.name')},
{field: 'num', title: __('Num')},
{field: 'pay_type', title: __('Pay_type'), searchList: {"1":__('Pay_type 1'),"2":__('Pay_type 2'),"3":__('Pay_type 3')}, formatter: Table.api.formatter.normal},
{field: 'express', title: __('Express'), searchList: {"1":__('Express 1'),"2":__('Express 2'),"3":__('Express 3')}, formatter: Table.api.formatter.normal},
{field: 'total', title: __('Total'), operate:'BETWEEN'},
{field: 'goods_total', title: __('Goods_total'), operate:'BETWEEN'},
{field: 'postage_total', title: __('Postage_total'), operate:'BETWEEN'},
{field: 'name', title: __('Name')},
{field: 'mobile', title: __('Mobile')},
{field: 'province_name', title: __('Province_name')},
{field: 'city_name', title: __('City_name')},
{field: 'county_name', title: __('County_name')},
{field: 'address', title: __('Address')},
{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
{field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
{field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5'),"6":__('Status 6'),"7":__('Status 7'),"8":__('Status 8'),"9":__('Status 9'),"10":__('Status 10')}, formatter: Table.api.formatter.status},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
buttons: [
{
name: 'detail',
text: __('详情'),
title: __('详情'),
classname: 'btn btn-xs btn-primary btn-dialog',
icon: '',
url: $.fn.bootstrapTable.defaults.extend.detail_url,
extend:"data-area='[\"50%\",\"80%\"]'",
},
{
name: 'send',
text: __('发货'),
title: __('发货'),
classname: 'btn btn-xs btn-info btn-ajax',
icon: '',
url: $.fn.bootstrapTable.defaults.extend.send_url + '/status/3',
confirm: '是否确认通过已发货?',
hidden:function(row){
if(row.status != 2){
return true;
}
},
success: function (data) {
table.bootstrapTable('refresh');
}
},
{
name: 'refund',
text: __('已退款'),
title: __('已退款'),
classname: 'btn btn-xs btn-success btn-ajax',
icon: '',
url: $.fn.bootstrapTable.defaults.extend.refund_url + '/status/9',
confirm: '是否确认通过已退款?',
hidden:function(row){
if(row.status != 8){
return true;
}
},
success: function (data) {
table.bootstrapTable('refresh');
}
},
{
name: 'del',
text: __(''),
title: __('删除'),
classname: 'btn btn-xs btn-danger btn-delone',
icon: 'fa fa-trash',
url: $.fn.bootstrapTable.defaults.extend.refund_url,
hidden:function(row){
if(row.status != 7 && row.status != 10){
return true;
}
},
success: function (data) {
table.bootstrapTable('refresh');
}
},
], formatter: Table.api.formatter.operate}
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
},
detail: function () {
Controller.api.bindevent();
},
api: {
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});
\ No newline at end of file
... ...