Files
liche/admin/controllers/app/material/Template.php
T
2021-10-14 17:52:12 +08:00

288 lines
11 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Notes:素材模板
* Created on: 2021/9/30 12:45
* Created by: dengbw
*/
class Template extends HD_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('app/material/Material_template_model', 'mdTemplate');
$this->load->model('auto/auto_brand_model', 'mdAutoBrand');
}
//首页信息
public function index()
{
$this->lists();
}
//数据列表
public function lists()
{
$params = $this->input->get();
$params['page'] = $params['page'] ? intval($params['page']) : 1;
$params['size'] = $params['size'] ? intval($params['size']) : 20;
$lists = $users = array();
$where = array('app_id' => $params['app_id']);
if ($params['type']) {
$where['type'] = $params['type'];
}
if (strlen($params['status'])) {
$where['status'] = $params['status'];
}
$count = $this->mdTemplate->count($where);
if ($count) {
$fileds = "id,title,type,status,brand_ids,c_time";
$res = $this->mdTemplate->select($where, "id desc", $params['page'], $params['size'], $fileds);
foreach ($res as $key => $value) {
$setValue = array();
$setValue['id'] = $value['id'];
$setValue['title'] = $value['title'];
$setValue['type_name'] = $this->mdTemplate->typeAry()[$value['type']];
$setValue['status_name'] = $this->mdTemplate->statusAry()[$value['status']];
$brands = '';
if ($value['brand_ids']) {
$res_b = $this->mdAutoBrand->select(array("id in ({$value['brand_ids']})" => null, 'status' => 1), "id desc", 0, 0, 'name');
if ($res_b) {
$brands = array_column($res_b, 'name');
$brands = implode(',', $brands);
}
}
$setValue['brands'] = $brands;
$setValue['c_time'] = date('Y-m-d H:i:s', $value["c_time"]);
$lists[] = $setValue;
}
}
$this->data['lists'] = $lists;
$this->data['params'] = $params;
$this->data['typeAry'] = $this->mdTemplate->typeAry();
$this->data['statusAry'] = $this->mdTemplate->statusAry();
$this->data['_title'] = '素材模板列表';
$this->data['pager'] = array('count' => ceil($count / $params['size']), 'curr' => $params['page'], 'totle' => $count);
return $this->show_view('/app/material/template/lists', true);
}
//展示单条数据
public function get()
{
$id = $this->input->get('id');
$info['app_id'] = intval($this->input->get('app_id'));
$info['status'] = 1;
$info['type'] = 1;
$info['cover'] = array('value' => '', 'src' => '');
$brands = $tgsp_list = $lbhb_list = $pyqwa_list = array();
if ($id) {
$re = $this->mdTemplate->get(array('id' => $id));
if (!$re || empty($re)) {
return $this->show_json(SYS_CODE_FAIL, '模板不存在!');
}
if ($re['json_data']) {
$json_data = json_decode($re['json_data'], true);
$lists = array();
foreach ($json_data['lists'] as $key => $value) {
if ($re['type'] == 1) {
if ($value['video_type'] == 1) {
$value['video_file_url'] = $value['video'];
$value['video_url'] = build_qiniu_image_url($value['video'], 0, 0, 'video');
$value['video_cover'] = $value['video_url'] . '?vframe/jpg/offset/1';
$value['img'] = $value['img_url'] = '';
} else {
$value['img_url'] = $value['img'] ? build_qiniu_image_url($value['img']) : '';
$value['video_file_url'] = $value['video_url'] = $value['video_cover'] = '';
}
} else if ($re['type'] == 2) {
$value['img_url'] = $value['img'] ? build_qiniu_image_url($value['img']) : '';
} else if ($re['type'] == 3) {
if ($value['brands']) {
$value['brands'] = $this->mdAutoBrand->select(array("id in ({$value['brands']})" => null, 'status' => 1), "id desc", 0, 0, 'id,name');
} else {
$value['brands'] = array();
}
}
$lists[] = $value;
}
if ($re['type'] == 1) {
$tgsp_list = $lists;
} else if ($re['type'] == 2) {
$lbhb_list = $lists;
} else if ($re['type'] == 3) {
$pyqwa_list = $lists;
}
unset($re['json_data']);
}
$info = $re;
$info['cover'] = $info['cover'] ? array('value' => $info['cover'], 'src' => build_qiniu_image_url($info['cover']))
: array('value' => '', 'src' => '');
if ($info['brand_ids']) {
$brand_ids = str_replace(",0", "", $info['brand_ids']);
$brands = $this->mdAutoBrand->select(array("id in ({$brand_ids})" => null, 'status' => 1), "id desc", 0, 0, 'id,name');
}
$_title = '编辑素材模板';
$edit_url = '/app/material/template/edit';
} else {
$_title = '新增素材模板';
$edit_url = '/app/material/template/add';
}
$showInfo['statusAry'] = $this->mdTemplate->statusAry();
$showInfo['typeAry'] = $this->mdTemplate->typeAry();
$showInfo['brands'] = $brands;
$showInfo['tgsp_list'] = $tgsp_list;
$showInfo['lbhb_list'] = $lbhb_list;
$showInfo['pyqwa_list'] = $pyqwa_list;
$this->data['info'] = $info;
$this->data['showInfo'] = $showInfo;
$this->data['edit_url'] = $edit_url;
$this->data['_title'] = $_title;
return $this->show_view('/app/material/template/edit', true);
}
private function checkInfo($info)
{
$msg = '';
$code = SYS_CODE_FAIL;
if (!$info) {
$msg = '非法参数';
} else if (!$info['title']) {
$msg = '请输入模版名称';
} else if (!$info['cover']['value'] && $info['type'] != 3) {
$msg = '请上传封面图';
} else if (!$info['brand_ids'] && $info['type'] != 3) {
$msg = '请选择适用品牌';
} else {
$code = SYS_CODE_SUCCESS;
}
return array('code' => $code, 'msg' => $msg);
}
//添加单条数据
public function add()
{
$info = $this->input->post('info');
$checkInfo = $this->checkInfo($info);
if (!$checkInfo['code']) {
return $this->show_json($checkInfo['code'], $checkInfo['msg']);
}
$brand_ids = '';
if ($info['brand_ids']) {
$brand_ids = implode(',', $info['brand_ids']);
if (!strstr($brand_ids, ',0')) {
$brand_ids .= ',0';
}
}
if ($info['lists']) {
$json_data = $info['json_data'];
$lists = array();
foreach ($info['lists'] as $key => $value) {
if ($info['type'] == 1) {
if ($value['video_type'] == 1) {
$value['video'] = $value['video_file_url'];
unset($value['img']);
}
unset($value['video_file_url']);
unset($value['video_url']);
unset($value['video_cover']);
unset($value['img_url']);
} else if ($info['type'] == 2) {
unset($value['img_url']);
} else if ($info['type'] == 3) {
$brands = '';
if ($value['brands']) {
$brands = array_column($value['brands'], 'id');
$brands = implode(',', $brands);
}
$value['brands'] = $brands;
}
$lists[] = $value;
}
$json_data['lists'] = $lists;
$editData['json_data'] = json_encode($json_data, JSON_UNESCAPED_UNICODE);
}
$editData['app_id'] = $info['app_id'];
$editData['title'] = $info['title'];
$editData['cover'] = $info['cover']['value'];
$editData['type'] = $info['type'];
$editData['brand_ids'] = $info['type'] == 3 ? '' : $brand_ids;
$editData['status'] = $info['status'];
$editData['c_time'] = time();
$id = $this->mdTemplate->add($editData);
if (!$id) {
return $this->show_json(SYS_CODE_FAIL, '新增失败!');
}
return $this->show_json(SYS_CODE_SUCCESS, '新增成功!', '/app/material/template/index?app_id=' . $info['app_id']);
}
//编辑单条数据
public function edit()
{
$info = $this->input->post('info');
$checkInfo = $this->checkInfo($info);
if (!$checkInfo['code']) {
return $this->show_json($checkInfo['code'], $checkInfo['msg']);
}
$brand_ids = '';
if ($info['brand_ids']) {
$brand_ids = implode(',', $info['brand_ids']);
if (!strstr($brand_ids, ',0')) {
$brand_ids .= ',0';
}
}
if ($info['lists']) {
$json_data = $info['json_data'];
$lists = array();
foreach ($info['lists'] as $key => $value) {
if ($info['type'] == 1) {
$value['video'] = $value['video_file_url'];
unset($value['video_file_url']);
unset($value['video_url']);
unset($value['video_cover']);
} else if ($info['type'] == 2) {
unset($value['img_url']);
} else if ($info['type'] == 3) {
$brands = '';
if ($value['brands']) {
$brands = array_column($value['brands'], 'id');
$brands = implode(',', $brands);
}
$value['brands'] = $brands;
}
$lists[] = $value;
}
$json_data['lists'] = $lists;
$editData['json_data'] = json_encode($json_data, JSON_UNESCAPED_UNICODE);
}
$editData['title'] = $info['title'];
$editData['cover'] = $info['cover']['value'];
$editData['type'] = $info['type'];
$editData['brand_ids'] = $info['type'] == 3 ? '' : $brand_ids;
$editData['status'] = $info['status'];
$ret = $this->mdTemplate->update($editData, array('id' => $info['id']));
if (!$ret) {
return $this->show_json(SYS_CODE_FAIL, '修改失败!');
}
return $this->show_json(SYS_CODE_SUCCESS, '修改成功!');
}
//删除单条数据
public function del()
{
}
//批量操作(默认修改状态)
public function batch()
{
}
//导出数据列表
public function export()
{
}
}