180 行
6.6 KiB
PHP
180 行
6.6 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\service;
|
|
|
|
use app\common\controller\Backend;
|
|
use think\Db;
|
|
use Exception;
|
|
|
|
/**
|
|
* 系统图库管理
|
|
*
|
|
* @icon fa fa-picture-o
|
|
*/
|
|
class Gallery extends Backend
|
|
{
|
|
/**
|
|
* Gallery模型对象
|
|
* @var \app\admin\model\service\Gallery
|
|
*/
|
|
protected $model = null;
|
|
protected $relationSearch = true;
|
|
protected $searchFields = 'id,title,service.name,cat.name';
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->model = new \app\admin\model\service\Gallery;
|
|
$this->view->assign('statusList', $this->model->getStatusList());
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
if (false === $this->request->isAjax()) {
|
|
$cats = Db::name('service_cat')
|
|
->where('status', 'normal')
|
|
->where('id', '<>', 15)
|
|
->order('weigh desc, id asc')
|
|
->select();
|
|
$initServiceId = (int)$this->request->get('service_id', 0);
|
|
$initServiceName = $initServiceId > 0 ? (string)Db::name('service')->where('id', $initServiceId)->value('name') : '';
|
|
$this->view->assign([
|
|
'catList' => $cats,
|
|
'initServiceId' => $initServiceId,
|
|
'initServiceName' => $initServiceName,
|
|
]);
|
|
return $this->view->fetch();
|
|
}
|
|
if ($this->request->request('keyField')) {
|
|
return $this->selectpage();
|
|
}
|
|
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
|
|
$list = $this->model
|
|
->with(['cat', 'service'])
|
|
->where($where)
|
|
->order($sort, $order)
|
|
->paginate($limit);
|
|
|
|
return json(['total' => $list->total(), 'rows' => $list->items()]);
|
|
}
|
|
|
|
public function add()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$params = $this->request->post('row/a');
|
|
if (!$params) {
|
|
$this->error(__('Parameter %s can not be empty', ''));
|
|
}
|
|
$params = $this->preExcludeFields($params);
|
|
$serviceId = isset($params['service_id']) ? (int)$params['service_id'] : 0;
|
|
if ($serviceId <= 0) {
|
|
$this->error('请选择所属商品');
|
|
}
|
|
$svc = Db::name('service')->where('id', $serviceId)->find();
|
|
if (!$svc) {
|
|
$this->error('所选商品不存在');
|
|
}
|
|
$catId = (int)$svc['cat_id'];
|
|
$rawImages = isset($params['image']) ? trim((string)$params['image']) : '';
|
|
$imgArr = array_values(array_filter(array_map('trim', explode(',', $rawImages))));
|
|
if (empty($imgArr)) {
|
|
$this->error('请至少上传一张系统参考图');
|
|
}
|
|
|
|
$baseTitle = isset($params['title']) ? trim((string)$params['title']) : '';
|
|
$weigh = isset($params['weigh']) ? (int)$params['weigh'] : 100;
|
|
$status = (!empty($params['status']) && in_array($params['status'], ['normal', 'hidden'], true)) ? $params['status'] : 'normal';
|
|
$now = time();
|
|
|
|
Db::startTrans();
|
|
try {
|
|
foreach ($imgArr as $idx => $imgUrl) {
|
|
$itemTitle = $baseTitle !== '' ? $baseTitle : (string)$svc['name'];
|
|
if (count($imgArr) > 1 && $baseTitle !== '') {
|
|
$itemTitle = $baseTitle . ' ' . ($idx + 1);
|
|
}
|
|
Db::name('system_gallery')->insert([
|
|
'cat_id' => $catId,
|
|
'service_id' => $serviceId,
|
|
'spec_id' => 0,
|
|
'title' => mb_substr($itemTitle, 0, 80),
|
|
'image' => $imgUrl,
|
|
'weigh' => max(0, $weigh - $idx),
|
|
'status' => $status,
|
|
'createtime' => $now,
|
|
'updatetime' => $now,
|
|
]);
|
|
}
|
|
Db::commit();
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
$this->success();
|
|
}
|
|
|
|
$initServiceId = (int)$this->request->get('service_id', 0);
|
|
$initCatId = (int)$this->request->get('cat_id', 0);
|
|
if ($initServiceId > 0 && $initCatId <= 0) {
|
|
$initCatId = (int)Db::name('service')->where('id', $initServiceId)->value('cat_id');
|
|
}
|
|
$this->view->assign([
|
|
'initCatId' => $initCatId ?: '',
|
|
'initServiceId' => $initServiceId ?: '',
|
|
]);
|
|
return $this->view->fetch();
|
|
}
|
|
|
|
public function edit($ids = null)
|
|
{
|
|
$row = $this->model->get($ids);
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
if ($this->request->isPost()) {
|
|
$params = $this->request->post('row/a');
|
|
if (!$params) {
|
|
$this->error(__('Parameter %s can not be empty', ''));
|
|
}
|
|
$params = $this->preExcludeFields($params);
|
|
$serviceId = isset($params['service_id']) ? (int)$params['service_id'] : (int)$row['service_id'];
|
|
if ($serviceId <= 0) {
|
|
$this->error('请选择所属商品');
|
|
}
|
|
$svc = Db::name('service')->where('id', $serviceId)->find();
|
|
if (!$svc) {
|
|
$this->error('所选商品不存在');
|
|
}
|
|
$params['cat_id'] = (int)$svc['cat_id'];
|
|
if (empty($params['title']) || trim((string)$params['title']) === '') {
|
|
$params['title'] = (string)$svc['name'];
|
|
}
|
|
if (empty($params['image']) || trim((string)$params['image']) === '') {
|
|
$this->error('系统参考图不能为空');
|
|
}
|
|
|
|
Db::startTrans();
|
|
try {
|
|
$row->allowField(true)->save($params);
|
|
if ((int)$row['spec_id'] > 0) {
|
|
Db::name('service_spec')
|
|
->where('id', (int)$row['spec_id'])
|
|
->where('service_id', $serviceId)
|
|
->update([
|
|
'image' => trim((string)$params['image']),
|
|
'updatetime' => time(),
|
|
]);
|
|
}
|
|
Db::commit();
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
$this->success();
|
|
}
|
|
$this->view->assign('row', $row);
|
|
return $this->view->fetch();
|
|
}
|
|
}
|