chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
 
namespace app\admin\controller;
 
use think\Exception;
use think\facade\Config;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\admin\model\Language as LanguageModel;
 
class Language extends Common {
    private $tableName = 'language';
    private $viewPath = 'language';
    
    public function index() {
        ////////
        if (!Request::isPost()) {
            $other['title'] = '多语言管理';
            View::assign('other', $other);
            View::assign('viewPath', $this->viewPath);
            return View::fetch();
        } else {
            try {
                $kw = input('kw');
                if (!empty($kw)) {
                    $condition[] = array('name', 'like', "%$kw%");
                }
                $p = input('p', 1);
                $other['page_size'] = input('page_size', 20);
                $order = 'order_id asc,id asc';
                $count = LanguageModel::where($condition)->count();
                $list = LanguageModel::field("*")->where($condition)->order($order)->page($p, $other['page_size'])->select()->toArray();
                $other['count'] = $count;
                if (!$list) {
                    $list = [];
                }
                $result['other'] = $other;
                $result['list'] = $list;
                return $this->successResponse($result);
            } catch (Exception $exc) {
                return $this->errorResponse($exc->getMessage());
            }
        }
    }
    
    function updateExt() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $postField = 'id,is_show';
            $data = Request::only(explode(',', $postField), 'post');
            if (!$data['id']) {
                throw new Exception(lang('parameter_error'));
            }
            Db::name($this->tableName)->update($data);
            $result['message'] = '操作成功';
            return $this->successResponse($result);
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 保存
     */
    public function save() {
        //开始事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $data = Request::post();
            $validate=new \app\admin\validate\Language();
            $check_result = $validate->check($data);
            if (!$check_result) {
                throw new Exception($validate->getError());
            }
            $id = input('post.id');
            if (is_numeric($id)) {
                //更新数据
                $update_result = Db::name($this->tableName)->where('id', $id)->withoutField('id,delete_time')->save($data);
                if ($update_result === FALSE) {
                    throw new Exception(lang('editing_failed'));
                }
                //提交事务
                Db::commit();
                return $this->successResponse('修改成功!');
            } else {
                //添加数据
                unset($data['id']);
                $add_result = Db::name($this->tableName)->insertGetId($data);
                if (!$add_result) {
                    throw new Exception(lang('add_failed'));
                }
                Db::name($this->tableName)->where('id', $add_result)->save(['order_id' => $add_result]);
                //提交事务
                Db::commit();
                return $this->successResponse('添加成功!');
            }
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 删除
     * @return array|int[]
     */
    public function delete() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        //开始事务
        Db::startTrans();
        try {
            $id = input('id');
            if (!is_numeric($id)) {
                return $this->errorResponse(lang('parameter_error'));
            }
            $condition['id'] = $id;
            $info = Db::name($this->tableName)->field('id')->where($condition)->find();
            if (!$info) {
                return $this->errorResponse(lang('parameter_error'));
            }
            Db::name($this->tableName)->where('id', $id)->useSoftDelete('delete_time',time())->delete();
            //提交事务
            Db::commit();
            return $this->successResponse('删除成功!');
        } catch (Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    
}