Ajax.php 9.5 KB

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