Files
liche/admin/controllers/auto/Introduce.php
T
2022-03-22 17:50:49 +08:00

307 lines
12 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Notes:车型介绍
* Created on: 2022/3/18 10:31
* Created by: dengbw
*/
class Introduce extends HD_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('auto/auto_introduce_model', 'mdAutoIntroduce');
$this->load->model('auto/auto_brand_model', 'mdAutoBrand');
$this->load->model('auto/auto_series_model', 'mdAutoSeries');
}
//首页信息
public function index()
{
return $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;
$statusAry = $this->mdAutoIntroduce->statusAry();
$lists = array();
$where = ["status<>-1" => null];
if (strlen($params['status'])) {
$where['status'] = $params['status'];
}
$count = $this->mdAutoIntroduce->count($where);
if ($count) {
$res = $this->mdAutoIntroduce->select($where, "id desc", $params['page'], $params['size']);
$map_brand = $map_series = [];
$brand_ids = array_unique(array_column($res, 'brand_id'));
if ($brand_ids) {
$str_ids = implode(',', $brand_ids);
$map_brand = $this->mdAutoBrand->map('id', 'name', ["id in ({$str_ids})" => null]);
}
$s_ids = array_unique(array_column($res, 's_id'));
if ($s_ids) {
$str_ids = implode(',', $s_ids);
$map_series = $this->mdAutoSeries->map('id', 'name', ["id in ({$str_ids})" => null]);
}
foreach ($res as $key => $value) {
$brand_name = $map_brand[$value['brand_id']];
$s_name = $map_series[$value['s_id']];
$lists[] = [
'id' => $value['id'],
'title' => "{$brand_name}-{$s_name}",
'status' => $value['status'],
'status_name' => $statusAry[$value['status']],
'c_time' => date('Y-m-d', $value['c_time']),
];
}
}
$brandList = $this->mdAutoBrand->select(["status<>" => -1], 'id desc', 0, 0, 'id, name');
$this->data['lists'] = $lists;
$this->data['params'] = $params;
$this->data['showInfo'] = ['statusAry' => $statusAry, 'brandList' => $brandList];
$this->data['_title'] = '车型介绍列表';
$this->data['pager'] = array('count' => ceil($count / $params['size']), 'curr' => $params['page'], 'totle' => $count);
return $this->show_view('/auto/introduce/lists', true);
}
//物料列表
public function lists_material()
{
$params = $this->input->post();
if (!$params['biz_id'] || !$params['introduces']) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
$list = [];
$introduces_ids = array_unique(array_column($params['introduces'], 'id'));
$str_ids = implode(',', $introduces_ids);
$res = $this->mdAutoIntroduce->select(["id in({$str_ids})" => null], "id desc", 0, 0);
$map_brand = $map_series = [];
$brand_ids = array_unique(array_column($res, 'brand_id'));
if ($brand_ids) {
$str_ids = implode(',', $brand_ids);
$map_brand = $this->mdAutoBrand->map('id', 'name', ["id in ({$str_ids})" => null]);
}
$s_ids = array_unique(array_column($res, 's_id'));
if ($s_ids) {
$str_ids = implode(',', $s_ids);
$map_series = $this->mdAutoSeries->map('id', 'name', ["id in ({$str_ids})" => null]);
}
foreach ($res as $key => $value) {
$brand_name = $map_brand[$value['brand_id']];
$s_name = $map_series[$value['s_id']];
$title = "{$brand_name}-{$s_name}";
$scene = "{$value['id']}_{$params['biz_id']}";
$page = 'pages/modelShow/index';
$name = $params['biz_name'] . '_' . $title;
//$name && $name = preg_replace('# #', '', $name);//正则去除所有空格
$qr_code = $this->qrcode($scene, $page, '1280px', $name);
$list[] = [
'title' => $title,
'url' => "{$page}?id={$scene}",
'qr_code' => $qr_code['url'] ? $qr_code['url'] : '',
];
}
$this->data['list'] = $list;
return $this->show_json(SYS_CODE_SUCCESS, '物料列表!');
}
//展示单条数据
public function get()
{
$id = intval($this->input->get('id'));
if ($id) {
$re = $this->mdAutoIntroduce->get(['id' => $id]);
if (!$re) {
return $this->show_json(SYS_CODE_FAIL, '车型不存在!');
}
$video = $re['video'] ? build_qiniu_image_url($re['video'], 0, 0, 'video') : '';
$info = array(
'id' => $re['id'],
'brand_id' => $re['brand_id'],
's_id' => $re['s_id'],
'video_file_url' => $re['video'],
'video' => $video,
'video_cover' => $video . '?vframe/jpg/offset/1',
'content' => $re['content'],
);
$title = '请选择车型';
if ($re['s_id']) {
$re_b = $this->mdAutoBrand->get(['id' => $re['brand_id']]);
$re_b && $title = $re_b['name'];
$re_s = $this->mdAutoSeries->get(['id' => $re['s_id']]);
$re_s && $title = $title ? $title . '-' . $re_s['name'] : $title;
}
$_title = '编辑车型介绍';
$showInfo = ['title' => $title, 'url' => '/auto/introduce/edit'];
} else {
$_title = '新增车型介绍';
$info = array(
'brand_id' => 0,
's_id' => 0,
'video_file_url' => '',
'video' => '',
'video_cover' => '',
'content' => '',
);
$showInfo = ['title' => '请选择车型', 'url' => '/auto/introduce/add'];
}
$showInfo['brandList'] = $this->mdAutoBrand->select(["status<>-1" => null], 'id desc', 0, 0, 'id, name');
$this->data['_title'] = $_title;
$this->data['info'] = $info;
$this->data['showInfo'] = $showInfo;
return $this->show_view('/auto/introduce/edit', true);
}
//添加单条数据
public function add()
{
$params = $this->input->post();
$info = $params['info'];
if (!$info) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
if (!$info['brand_id'] || !$info['s_id']) {
return $this->show_json(SYS_CODE_FAIL, '请选择车型!');
}
if (!$info['video_file_url'] && !$info['video']) {
return $this->show_json(SYS_CODE_FAIL, '请上传视频!');
}
$re = $this->mdAutoIntroduce->get(array('brand_id' => $info['brand_id'], 's_id' => $info['s_id']));
if ($re) {
return $this->show_json(SYS_CODE_FAIL, '车型已存在了!');
}
$video = $info['video_file_url'] ? $info['video_file_url'] : $info['video'];
$this->mdAutoIntroduce->add(['brand_id' => $info['brand_id'], 's_id' => $info['s_id'], 'video' => $video
, 'content' => $info['content'], 'c_time' => time()]);
return $this->show_json(SYS_CODE_SUCCESS, '新增成功', '/auto/introduce');
}
//编辑单条数据
public function edit()
{
$params = $this->input->post();
$info = $params['info'];
if (!$info) {
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
if (!$info['brand_id'] || !$info['s_id']) {
return $this->show_json(SYS_CODE_FAIL, '请选择车型!');
}
if (!$info['video_file_url']) {
return $this->show_json(SYS_CODE_FAIL, '请上传视频!');
}
$re = $this->mdAutoIntroduce->get(array('brand_id' => $info['brand_id'], 's_id' => $info['s_id']));
if ($re && $re['id'] != $info['id']) {
return $this->show_json(SYS_CODE_FAIL, '车型已存在了!');
}
$this->mdAutoIntroduce->update(['brand_id' => $info['brand_id'], 's_id' => $info['s_id'], 'content' => $info['content']
, 'video' => $info['video_file_url']], ['id' => $info['id']]);
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
function edit_status()
{
$id = $this->input->post('id');
$stauts = intval($this->input->post('status'));
if (!$id) {
$this->show_json(SYS_CODE_FAIL, '参数错误');
}
$this->mdAutoIntroduce->update(['status' => $stauts], ['id' => $id]);
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
//删除单条数据
public function del()
{
$id = $this->input->post('id');
if (!$id) {
$this->show_json(SYS_CODE_FAIL, '参数错误');
}
$this->mdAutoIntroduce->update(['status' => '-1'], ['id' => $id]);
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
//批量操作(默认修改状态)
public function batch()
{
}
//导出数据列表
public function export()
{
}
private function qrcode($scene, $page = '', $width = 0, $name = '')
{
$path = "{$page}?{$scene}";
$width && $path .= "{$width}";
$this->load->model('app/app_model', 'mdApp');
$wxconfig = $this->mdApp->appConfig()[1]['wx'];
//$filename = "{$this->mdApp->appConfig()[1]['app_key']}/" . substr(md5($path), 8, 16);
$filename = "{$this->mdApp->appConfig()[1]['app_key']}/{$name}";
$this->load->library('hdwechat', $wxconfig);
$result = $this->hdwechat->qrcode($filename, $scene, $page, $width);
if (!$result) {
return ['url' => ''];
}
$data = array('url' => http_host_com() . '/' . $result['url']);
return $data;
}
/**
* Notes:
* Created on: 2022/3/21 16:16
* Created by: dengbw
* @return bool
*/
function json_lists()
{
$page = $this->input->post('page');
$size = $this->input->post('size');
$status = $this->input->post('status');
$brand_id = intval($this->input->post('brand_id'));
$where = array();
$brand_id && $where["brand_id"] = $brand_id;
if (strlen($status) > 0) {
$where['status'] = $status;
} else {
$where['status > -1'] = null;
}
$total = $this->mdAutoIntroduce->count($where);
$lists = array();
if ($total) {
$orderby = 'id desc';
$select = 'id,brand_id, s_id';
$res = $this->mdAutoIntroduce->select($where, $orderby, $page, $size, $select);
$map_brand = $map_series = [];
$brand_ids = array_unique(array_column($res, 'brand_id'));
if ($brand_ids) {
$str_ids = implode(',', $brand_ids);
$map_brand = $this->mdAutoBrand->map('id', 'name', ["id in ({$str_ids})" => null]);
}
$s_ids = array_unique(array_column($res, 's_id'));
if ($s_ids) {
$str_ids = implode(',', $s_ids);
$map_series = $this->mdAutoSeries->map('id', 'name', ["id in ({$str_ids})" => null]);
}
foreach ($res as $v) {
$brand_name = $map_brand[$v['brand_id']];
$s_name = $map_series[$v['s_id']];
$lists[] = array(
'id' => $v['id'],
'name' => "{$brand_name}-{$s_name}",
);
}
}
$this->data = array('total' => $total, 'list' => $lists);
return $this->show_json(SYS_CODE_SUCCESS);
}
}