Ajax.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use fast\Random;
  5. use think\Cache;
  6. use think\Config;
  7. use think\Db;
  8. use think\Lang;
  9. /**
  10. * Ajax异步请求接口
  11. * @internal
  12. */
  13. class Ajax extends Backend
  14. {
  15. protected $noNeedLogin = ['lang'];
  16. protected $noNeedRight = ['*'];
  17. protected $layout = '';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. //设置过滤方法
  22. $this->request->filter(['strip_tags', 'htmlspecialchars']);
  23. }
  24. /**
  25. * 加载语言包
  26. */
  27. public function lang()
  28. {
  29. header('Content-Type: application/javascript');
  30. $controllername = input("controllername");
  31. //默认只加载了控制器对应的语言名,你还根据控制器名来加载额外的语言包
  32. $this->loadlang($controllername);
  33. return jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
  34. }
  35. /**
  36. * 上传文件
  37. */
  38. public function upload()
  39. {
  40. Config::set('default_return_type', 'json');
  41. $file = $this->request->file('file');
  42. if (empty($file))
  43. {
  44. $this->error(__('No file upload or server upload limit exceeded'));
  45. }
  46. //判断是否已经存在附件
  47. $sha1 = $file->hash();
  48. $upload = Config::get('upload');
  49. preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
  50. $type = strtolower($matches[2]);
  51. $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
  52. $size = (int) $upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
  53. $fileInfo = $file->getInfo();
  54. $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
  55. $suffix = $suffix ? $suffix : 'file';
  56. $mimetypeArr = explode(',', $upload['mimetype']);
  57. $typeArr = explode('/', $fileInfo['type']);
  58. //验证文件后缀
  59. if ($upload['mimetype'] !== '*' && !in_array($suffix, $mimetypeArr) && !in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr))
  60. {
  61. $this->error(__('Uploaded file format is limited'));
  62. }
  63. $replaceArr = [
  64. '{year}' => date("Y"),
  65. '{mon}' => date("m"),
  66. '{day}' => date("d"),
  67. '{hour}' => date("H"),
  68. '{min}' => date("i"),
  69. '{sec}' => date("s"),
  70. '{random}' => Random::alnum(16),
  71. '{random32}' => Random::alnum(32),
  72. '{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'],
  73. '{suffix}' => $suffix,
  74. '{.suffix}' => $suffix ? '.' . $suffix : '',
  75. '{filemd5}' => md5_file($fileInfo['tmp_name']),
  76. ];
  77. $savekey = $upload['savekey'];
  78. $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);
  79. $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
  80. $fileName = substr($savekey, strripos($savekey, '/') + 1);
  81. //
  82. $splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
  83. if ($splInfo)
  84. {
  85. $imagewidth = $imageheight = 0;
  86. if (in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']))
  87. {
  88. $imgInfo = getimagesize($splInfo->getPathname());
  89. $imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
  90. $imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
  91. }
  92. $params = array(
  93. 'filesize' => $fileInfo['size'],
  94. 'imagewidth' => $imagewidth,
  95. 'imageheight' => $imageheight,
  96. 'imagetype' => $suffix,
  97. 'imageframes' => 0,
  98. 'mimetype' => $fileInfo['type'],
  99. 'url' => $uploadDir . $splInfo->getSaveName(),
  100. 'uploadtime' => time(),
  101. 'storage' => 'local',
  102. 'sha1' => $sha1,
  103. );
  104. $attachment = model("attachment");
  105. $attachment->data(array_filter($params));
  106. $attachment->save();
  107. \think\Hook::listen("upload_after", $attachment);
  108. $this->success(__('Upload successful'), null, [
  109. 'url' => $uploadDir . $splInfo->getSaveName()
  110. ]);
  111. }
  112. else
  113. {
  114. // 上传失败获取错误信息
  115. $this->error($file->getError());
  116. }
  117. }
  118. /**
  119. * 通用排序
  120. */
  121. public function weigh()
  122. {
  123. //排序的数组
  124. $ids = $this->request->post("ids");
  125. //拖动的记录ID
  126. $changeid = $this->request->post("changeid");
  127. //操作字段
  128. $field = $this->request->post("field");
  129. //操作的数据表
  130. $table = $this->request->post("table");
  131. //排序的方式
  132. $orderway = $this->request->post("orderway", 'strtolower');
  133. $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
  134. $sour = $weighdata = [];
  135. $ids = explode(',', $ids);
  136. $prikey = 'id';
  137. $pid = $this->request->post("pid");
  138. //限制更新的字段
  139. $field = in_array($field, ['weigh']) ? $field : 'weigh';
  140. // 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
  141. if ($pid !== '')
  142. {
  143. $hasids = [];
  144. $list = Db::name($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field('id,pid')->select();
  145. foreach ($list as $k => $v)
  146. {
  147. $hasids[] = $v['id'];
  148. }
  149. $ids = array_values(array_intersect($ids, $hasids));
  150. }
  151. //直接修复排序
  152. $one = Db::name($table)->field("{$field},COUNT(*) AS nums")->group($field)->having('nums > 1')->find();
  153. if ($one)
  154. {
  155. $list = Db::name($table)->field("$prikey,$field")->order($field, $orderway)->select();
  156. foreach ($list as $k => $v)
  157. {
  158. Db::name($table)->where($prikey, $v[$prikey])->update([$field => $k + 1]);
  159. }
  160. $this->success();
  161. }
  162. else
  163. {
  164. $list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
  165. foreach ($list as $k => $v)
  166. {
  167. $sour[] = $v[$prikey];
  168. $weighdata[$v[$prikey]] = $v[$field];
  169. }
  170. $position = array_search($changeid, $ids);
  171. $desc_id = $sour[$position]; //移动到目标的ID值,取出所处改变前位置的值
  172. $sour_id = $changeid;
  173. $desc_value = $weighdata[$desc_id];
  174. $sour_value = $weighdata[$sour_id];
  175. //echo "移动的ID:{$sour_id}\n";
  176. //echo "替换的ID:{$desc_id}\n";
  177. $weighids = array();
  178. $temp = array_values(array_diff_assoc($ids, $sour));
  179. foreach ($temp as $m => $n)
  180. {
  181. if ($n == $sour_id)
  182. {
  183. $offset = $desc_id;
  184. }
  185. else
  186. {
  187. if ($sour_id == $temp[0])
  188. {
  189. $offset = isset($temp[$m + 1]) ? $temp[$m + 1] : $sour_id;
  190. }
  191. else
  192. {
  193. $offset = isset($temp[$m - 1]) ? $temp[$m - 1] : $sour_id;
  194. }
  195. }
  196. $weighids[$n] = $weighdata[$offset];
  197. Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
  198. }
  199. $this->success();
  200. }
  201. }
  202. /**
  203. * 清空系统缓存
  204. */
  205. public function wipecache()
  206. {
  207. $wipe_cache_type = ['TEMP_PATH', 'LOG_PATH', 'CACHE_PATH'];
  208. foreach ($wipe_cache_type as $item)
  209. {
  210. $dir = constant($item);
  211. if (!is_dir($dir))
  212. continue;
  213. rmdirs($dir);
  214. }
  215. Cache::clear();
  216. \think\Hook::listen("wipecache_after");
  217. $this->success();
  218. }
  219. /**
  220. * 读取分类数据,联动列表
  221. */
  222. public function category()
  223. {
  224. $type = $this->request->get('type');
  225. $pid = $this->request->get('pid');
  226. $where = ['status' => 'normal'];
  227. $categorylist = null;
  228. if ($pid !== '')
  229. {
  230. if ($type)
  231. {
  232. $where['type'] = $type;
  233. }
  234. if ($pid)
  235. {
  236. $where['pid'] = $pid;
  237. }
  238. $categorylist = Db::name('category')->where($where)->field('id as value,name')->order('weigh desc,id desc')->select();
  239. }
  240. $this->success('', null, $categorylist);
  241. }
  242. /**
  243. * 读取省市区数据,联动列表
  244. */
  245. public function area()
  246. {
  247. $province = $this->request->get('province');
  248. $city = $this->request->get('city');
  249. $where = ['pid' => 0, 'level' => 1];
  250. $provincelist = null;
  251. if ($province !== '')
  252. {
  253. if ($province)
  254. {
  255. $where['pid'] = $province;
  256. $where['level'] = 2;
  257. }
  258. if ($city !== '')
  259. {
  260. if ($city)
  261. {
  262. $where['pid'] = $city;
  263. $where['level'] = 3;
  264. }
  265. $provincelist = Db::name('area')->where($where)->field('id as value,name')->select();
  266. }
  267. }
  268. $this->success('', null, $provincelist);
  269. }
  270. }