Files
spacestation/admin/controllers/app/App.php
T
2024-05-26 16:00:30 +08:00

156 lines
5.0 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 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, '操作成功');
}
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, '保存成功');
}
}