Ajax.php 10 KB

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