Files
liche/admin/controllers/app/App.php
T
2021-07-27 18:56:21 +08:00

326 lines
12 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Created by PhpStorm.
* User: xuxb
* Date: 2019/11/20
* Time: 14:45
*/
class App extends HD_Controller
{
protected $log_file = 'app.log';
function __construct()
{
parent::__construct();
$this->load->model('app/app_model');
}
public function index()
{
$this->lists();
}
public function lists()
{
$keyword = $this->data['keyword'] = $this->input->get('keyword');
$page = $this->input->get('page');
$size = $this->input->get('size');
!$page && $page = 1;
!$size && $size = 20;
$where = array();
$keyword && $where['name like'] = "%{$keyword}%";
$total = $this->app_model->count($where);
$lists = array();
if($total){
$rows = $this->app_model->select($where, 'id DESC', $page, $size, 'id,name,jsondata,c_time');
foreach($rows as $item){
$jsondata = json_decode($item['jsondata'],true);
$lists[] = array(
'id' => $item['id'],
'name' => $item['name'],
'publish' => $jsondata['publish'] ? 1 : 0,
'c_time' => date('Y-m-d H:i', $item['c_time']),
);
}
}
$this->data['lists'] = $lists;
$this->data['pager'] = array('count'=>ceil($total/$size),'curr'=>$page,'totle'=>$total);
$this->data['_title'] = '小程序管理';
return $this->show_view('app/appusual/lists',true);
}
public function get()
{
$id = $this->input->get('id');
if($id){
$row = $this->app_model->get(array('id' => $id));
if(!$row){
return $this->show_json(SYS_CODE_FAIL, '不存在');
}
$item = array(
'id' => $row['id'],
'name' => $row['name'],
'logo' => $row['logo'],
'logo_src' => $row['logo'] ? build_qiniu_image_url($row['logo']) : '',
);
$this->data['url'] = 'edit';
} else {
$this->data['url'] = 'add';
$item = array();
}
$this->data['item'] = $item;
return $this->show_view('app/appusual/get');
}
/**
* 客服设置
*/
public function get_servicer(){
$app_id = $this->input->get('app_id');
$target_id = intval($this->input->get('id'));
$type = $this->input->get('type');
$this->load->model('app/app_servicer_model', 'servicer_model');
$where = array('app_id' => $app_id, 'type' => $type, 'target_id' => $target_id);
$total = $this->servicer_model->count($where);
$info = array();
$lists = array();
if($total){
$select = 'id,type,show_type,msg_type,title,contact,jsondata';
$rows = $this->servicer_model->select($where, 'id ASC', 0, 0, $select);
foreach($rows as $row){
$info = array(
'app_id' => $app_id,
'type' => $type,
'show_type' => $row['show_type'],
'contact' => $row['contact'],
'target_id' => $target_id,
);
if(1 == $info['show_type']){//自定义才显示列表
$arr = array(
'id' => $row['id'],
'msg_type' => $row['msg_type'],
'keyword' => $row['title'],
);
if($row['jsondata']){
$json = json_decode($row['jsondata'], true);
$arr = array_merge($arr, $json);
$arr['msg']['img'] && $arr['msg']['img_url'] = build_qiniu_image_url($arr['msg']['img']);
}
$lists[] = $arr;
}
}
}
!$info && $info = array(
'app_id' => $app_id,
'type' => $type,
'show_type' => 0,
'target_id' => $target_id,
);
$this->data['msg_types'] = array('图文', '文章链接');
$this->data['info'] = $info;
$this->data['lists'] = $lists;
$this->data['max_num'] = 2;//最多两个
if ($app_id == 13){ // 20210428莫莫:要支持不限制数量
$this->data['max_num'] = 200;
}
$this->data['_title'] = '编辑客服';
$this->show_view('app/appusual/get_servicer');
}
public function add()
{
$name = $this->input->post('name');
$logo = $this->input->post('logo');
if(!$name){
return $this->show_json(SYS_CODE_FAIL, '名称必填');
}
$add = array(
'name' => $name,
'logo' => $logo,
'jsondata' => '',
'c_time' => time(),
);
$ret = $this->app_model->add($add);
if(!$ret){
debug_log("[error]" . __FUNCTION__ . ":" . $this->app_model->db->last_query(), $this->log_file);
return $this->show_json(SYS_CODE_FAIL, '操作失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
public function edit()
{
if(!$this->if_ajax){
return $this->show_json(SYS_CODE_FAIL,'提交出错!');
}
$id = $this->input->post('id');
$name = $this->input->post('name');
$logo = $this->input->post('logo');
if(!$id){
return $this->show_json(SYS_CODE_FAIL, '选择ID');
}
if(!$name){
return $this->show_json(SYS_CODE_FAIL, '名称必填');
}
$upd = array(
'name' => $name,
'logo' => $logo,
);
$ret = $this->app_model->update($upd, array('id' => $id));
if(!$ret){
debug_log("[error]" . __FUNCTION__ . ":" . $this->app_model->db->last_query(), $this->log_file);
return $this->show_json(SYS_CODE_FAIL, '操作失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
/**
* 编辑客服信息
* @return bool
*/
public function edit_servicer(){
$info = $this->input->post('info');
$lists = $this->input->post('lists');
$this->load->model('app/app_servicer_model', 'servicer_model');
$app_id = $info['app_id'];
$type = $info['type'];
$where = array('app_id' => $app_id, 'type' => $info['type'], 'target_id' => $info['target_id']);
$select = 'id, show_type, msg_type, title, contact, jsondata';
$olds = $this->servicer_model->select($where, 'id ASC', 0, 0, $select);
$where_del = array();
$upds = array();
$adds = array();
switch($info['show_type']){
case 0://默认
$where_del = $where;
break;
case 1://自定义
$where_del = $where;
//id作数组key
$olds_key = array();
foreach($olds as $row){
$olds_key[$row['id']] = $row;
}
$ids = array();//被编辑的ID
if($lists){
foreach($lists as $row){
//口令是否被设置
$keyword = $row['keyword'];
//判断口令是否被占用
$where = array('app_id' => $app_id, 'title' => $keyword);
$rows = $this->servicer_model->select($where, 'id DESC', 0, 0, 'type, target_id');
foreach($rows as $arr){
if($type != $arr['type'] || $info['target_id'] != $arr['target_id']){
return $this->show_json(SYS_CODE_FAIL, "口令“{$keyword}”已经被设置");
}
}
if($row['id']){//编辑
$row_old = $olds_key[$row['id']];
$jsondata = $row_old['jsondata'] ? json_decode($row_old['jsondata'], true) : array();
$jsondata['msg'] = $row['msg'];
$upds[] = array(
array(
'msg_type' => $row['msg_type'],
'title' => $keyword,
'contact' => $info['contact'],
'show_type' => $info['show_type'],
'jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE),
),
array('id' => $row['id']),
);
$ids[] = $row['id'];
} else{//新增
$jsondata['msg'] = $row['msg'];
$adds[] = array(
'app_id' => $app_id,
'type' => $type,
'target_id' => $info['target_id'],
'show_type' => $info['show_type'],
'contact' => $info['contact'],
'msg_type' => $row['msg_type'],
'title' => $keyword,
'jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE),
'c_time' => time(),
);
}
}
if($ids){
$where_del["id not in (" . implode(',', $ids) . ")"] = null;
}
break;
}
break;
case -1://隐藏
if(!$olds){
$adds[] = array(
'app_id' => $app_id,
'type' => $type,
'target_id' => $info['target_id'],
'show_type' => $info['show_type'],
'c_time' => time(),
);
}elseif($olds && -1 != $olds[0]['show_type']){
$where_del = $where;
$adds[] = array(
'app_id' => $app_id,
'type' => $type,
'target_id' => $info['target_id'],
'show_type' => $info['show_type'],
'c_time' => time(),
);
}
break;
}
$where_del && $this->servicer_model->delete($where_del);
$adds && $this->servicer_model->add_batch($adds);
foreach($upds as $row){
$this->servicer_model->update($row[0], $row[1]);
}
return $this->show_json(SYS_CODE_SUCCESS, '保存成功!');
}
public function del()
{
$id = $this->input->post('id');
if(!$id) {
return $this->show_json(SYS_CODE_FAIL, '提交错误');
}
$this->app_model->delete(array('id' => $id));
return $this->show_json(SYS_CODE_SUCCESS, '删除成功');
}
public function batch()
{
// TODO: Implement batch() method.
}
public function export()
{
// TODO: Implement export() method.
}
public function edit_publish(){
$id = $this->input->post('id');
$value = $this->input->post('value');
$row = $this->app_model->get(['id'=>$id]);
if(!$row){
return $this->show_json(SYS_CODE_FAIL, '参数错误');
}
$jsondata = json_decode($row['jsondata'],true);
$jsondata['publish'] = $value ? 1:0;
$jsondata = json_encode($jsondata,JSON_UNESCAPED_UNICODE);
$this->app_model->update(['jsondata'=>$jsondata],['id'=>$id]);
return $this->show_json(SYS_CODE_SUCCESS, '保存成功');
}
}