审查视图

application/admin/controller/Ajax.php 8.9 KB
Karson authored
1 2 3 4 5
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
Karson authored
6 7
use app\common\exception\UploadException;
use app\common\library\Upload;
Karson authored
8
use fast\Random;
9
use think\addons\Service;
Karson authored
10 11 12 13
use think\Cache;
use think\Config;
use think\Db;
use think\Lang;
Karson authored
14
use think\Validate;
Karson authored
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

/**
 * Ajax异步请求接口
 * @internal
 */
class Ajax extends Backend
{

    protected $noNeedLogin = ['lang'];
    protected $noNeedRight = ['*'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();

        //设置过滤方法
        $this->request->filter(['strip_tags', 'htmlspecialchars']);
    }

    /**
     * 加载语言包
     */
    public function lang()
    {
        header('Content-Type: application/javascript');
41 42 43 44 45 46
        header("Cache-Control: public");
        header("Pragma: cache");

        $offset = 30 * 60 * 60 * 24; // 缓存一个月
        header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
Karson authored
47 48 49 50 51 52 53 54 55 56 57 58
        $controllername = input("controllername");
        //默认只加载了控制器对应的语言名,你还根据控制器名来加载额外的语言包
        $this->loadlang($controllername);
        return jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
    }

    /**
     * 上传文件
     */
    public function upload()
    {
        Config::set('default_return_type', 'json');
Karson authored
59 60
        //必须设定cdnurl为空,否则cdnurl函数计算错误
        Config::set('upload.cdnurl', '');
Karson authored
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        $chunkid = $this->request->post("chunkid");
        if ($chunkid) {
            if (!Config::get('upload.chunking')) {
                $this->error(__('Chunk file disabled'));
            }
            $action = $this->request->post("action");
            $chunkindex = $this->request->post("chunkindex/d");
            $chunkcount = $this->request->post("chunkcount/d");
            $filename = $this->request->post("filename");
            $method = $this->request->method(true);
            if ($action == 'merge') {
                $attachment = null;
                //合并分片文件
                try {
                    $upload = new Upload();
                    $attachment = $upload->merge($chunkid, $chunkcount, $filename);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
Karson authored
80
                $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
Karson authored
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
            } elseif ($method == 'clean') {
                //删除冗余的分片文件
                try {
                    $upload = new Upload();
                    $upload->clean($chunkid);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            } else {
                //上传分片文件
                //默认普通上传文件
                $file = $this->request->file('file');
                try {
                    $upload = new Upload($file);
                    $upload->chunk($chunkid, $chunkindex, $chunkcount);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
Karson authored
101
            }
102
        } else {
Karson authored
103 104 105 106 107 108 109 110 111 112
            $attachment = null;
            //默认普通上传文件
            $file = $this->request->file('file');
            try {
                $upload = new Upload($file);
                $attachment = $upload->upload();
            } catch (UploadException $e) {
                $this->error($e->getMessage());
            }
Karson authored
113
            $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
Karson authored
114
        }
Karson authored
115
Karson authored
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }

    /**
     * 通用排序
     */
    public function weigh()
    {
        //排序的数组
        $ids = $this->request->post("ids");
        //拖动的记录ID
        $changeid = $this->request->post("changeid");
        //操作字段
        $field = $this->request->post("field");
        //操作的数据表
        $table = $this->request->post("table");
Karson authored
131 132 133
        if (!Validate::is($table, "alphaDash")) {
            $this->error();
        }
134 135
        //主键
        $pk = $this->request->post("pk");
Karson authored
136
        //排序的方式
137
        $orderway = strtolower($this->request->post("orderway", ""));
LessCodeToDoMore authored
138
        $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
Karson authored
139 140
        $sour = $weighdata = [];
        $ids = explode(',', $ids);
141
        $prikey = $pk ? $pk : (Db::name($table)->getPk() ?: 'id');
Karson authored
142 143 144 145 146
        $pid = $this->request->post("pid");
        //限制更新的字段
        $field = in_array($field, ['weigh']) ? $field : 'weigh';

        // 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
147
        if ($pid !== '') {
Karson authored
148
            $hasids = [];
149
            $list = Db::name($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field("{$prikey},pid")->select();
150
            foreach ($list as $k => $v) {
151
                $hasids[] = $v[$prikey];
Karson authored
152 153 154 155
            }
            $ids = array_values(array_intersect($ids, $hasids));
        }
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        $list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
        foreach ($list as $k => $v) {
            $sour[] = $v[$prikey];
            $weighdata[$v[$prikey]] = $v[$field];
        }
        $position = array_search($changeid, $ids);
        $desc_id = $sour[$position];    //移动到目标的ID值,取出所处改变前位置的值
        $sour_id = $changeid;
        $weighids = array();
        $temp = array_values(array_diff_assoc($ids, $sour));
        foreach ($temp as $m => $n) {
            if ($n == $sour_id) {
                $offset = $desc_id;
            } else {
                if ($sour_id == $temp[0]) {
                    $offset = isset($temp[$m + 1]) ? $temp[$m + 1] : $sour_id;
172
                } else {
173
                    $offset = isset($temp[$m - 1]) ? $temp[$m - 1] : $sour_id;
Karson authored
174 175
                }
            }
176 177 178
            if (!isset($weighdata[$offset])) {
                continue;
            }
179 180
            $weighids[$n] = $weighdata[$offset];
            Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
Karson authored
181
        }
182
        $this->success();
Karson authored
183 184 185 186 187 188 189
    }

    /**
     * 清空系统缓存
     */
    public function wipecache()
    {
190 191
        $type = $this->request->request("type");
        switch ($type) {
192 193
            case 'all':
            case 'content':
194 195
                rmdirs(CACHE_PATH, false);
                Cache::clear();
Karson authored
196
                if ($type == 'content') {
197
                    break;
Karson authored
198
                }
199
            case 'template':
200
                rmdirs(TEMP_PATH, false);
Karson authored
201
                if ($type == 'template') {
202
                    break;
Karson authored
203
                }
204
            case 'addons':
205
                Service::refresh();
Karson authored
206
                if ($type == 'addons') {
207
                    break;
Karson authored
208
                }
Karson authored
209
        }
210
Karson authored
211 212 213 214 215 216 217 218 219 220 221 222
        \think\Hook::listen("wipecache_after");
        $this->success();
    }

    /**
     * 读取分类数据,联动列表
     */
    public function category()
    {
        $type = $this->request->get('type');
        $pid = $this->request->get('pid');
        $where = ['status' => 'normal'];
223 224 225 226 227 228
        $categorylist = null;        
        if ($pid || $pid === '0') {
            $where['pid'] = $pid;
        }
        if ($type) {
            $where['type'] = $type;
Karson authored
229
        }
230 231 232
        
        $categorylist = Db::name('category')->where($where)->field('id as value,name')->order('weigh desc,id desc')->select();
Karson authored
233 234 235 236 237 238 239 240
        $this->success('', null, $categorylist);
    }

    /**
     * 读取省市区数据,联动列表
     */
    public function area()
    {
241 242 243 244 245 246 247 248
        $params = $this->request->get("row/a");
        if (!empty($params)) {
            $province = isset($params['province']) ? $params['province'] : '';
            $city = isset($params['city']) ? $params['city'] : null;
        } else {
            $province = $this->request->get('province');
            $city = $this->request->get('city');
        }
Karson authored
249 250
        $where = ['pid' => 0, 'level' => 1];
        $provincelist = null;
251 252
        if ($province !== '') {
            if ($province) {
Karson authored
253 254 255
                $where['pid'] = $province;
                $where['level'] = 2;
            }
256 257
            if ($city !== '') {
                if ($city) {
Karson authored
258 259 260 261 262 263 264 265 266
                    $where['pid'] = $city;
                    $where['level'] = 3;
                }
                $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
            }
        }
        $this->success('', null, $provincelist);
    }
267 268 269 270 271 272 273 274 275 276 277 278
    /**
     * 生成后缀图标
     */
    public function icon()
    {
        $suffix = $this->request->request("suffix");
        header('Content-type: image/svg+xml');
        $suffix = $suffix ? $suffix : "FILE";
        echo build_suffix_image($suffix);
        exit;
    }
Karson authored
279
}