Ajax.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use fast\Http;
  5. use fast\Tree;
  6. use think\Db;
  7. use think\Lang;
  8. /**
  9. * Ajax异步请求接口
  10. * @internal
  11. */
  12. class Ajax extends Backend
  13. {
  14. protected $noNeedLogin = ['dailybg', 'lang'];
  15. protected $noNeedRight = ['*'];
  16. protected $layout = '';
  17. /**
  18. * 自动完成
  19. */
  20. public function typeahead()
  21. {
  22. $search = $this->_request->getRequest("search");
  23. $field = $this->_request->getRequest("field");
  24. $field = str_replace(['row[', ']'], '', $field);
  25. if (substr($field, -3) !== '_id' && substr($field, -4) !== '_ids')
  26. {
  27. $this->code = -1;
  28. return;
  29. }
  30. $searchfield = 'name';
  31. $field = substr($field, 0, -3);
  32. switch ($field)
  33. {
  34. case 'category':
  35. $field = 'category';
  36. $searchfield = 'name';
  37. break;
  38. case 'user':
  39. $searchfield = 'nickname';
  40. break;
  41. }
  42. $searchlist = Db::table($field)
  43. ->orWhere($searchfield, 'like', "%{$search}%")
  44. ->orWhere('id', 'like', "%{$search}%")
  45. ->limit(10)
  46. ->select("id,{$searchfield} AS name");
  47. foreach ($searchlist as $k => &$v)
  48. {
  49. $v['name'] = $v['name'] . "[id:{$v['id']}]";
  50. }
  51. unset($v);
  52. $this->code = 1;
  53. $this->data = ['searchlist' => $searchlist];
  54. }
  55. /**
  56. * 加载语言包
  57. */
  58. public function lang()
  59. {
  60. header('Content-Type: application/javascript');
  61. $modulename = $this->request->module();
  62. $callback = $this->request->get('callback');
  63. $controllername = input("controllername");
  64. Lang::load(APP_PATH . $modulename . '/lang/' . Lang::detect() . '/' . str_replace('.', '/', $controllername) . '.php');
  65. //强制输出JSON Object
  66. // $result = 'define(' . json_encode(Lang::get(), JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE) . ');';
  67. return jsonp(Lang::get());
  68. }
  69. /**
  70. * 每日一图
  71. */
  72. public function dailybg()
  73. {
  74. //采用Infinty的图片
  75. $this->code = 1;
  76. $this->data = [
  77. 'url' => 'http://img.infinitynewtab.com/wallpaper/' . (date("Ymd") % 4000) . '.jpg'
  78. ];
  79. return;
  80. //采用Bing每日一图
  81. $ret = Http::sendRequest("http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1", [], 'GET');
  82. if ($ret['ret'])
  83. {
  84. $json = json_decode($ret['msg'], TRUE);
  85. if ($json && isset($json['images'][0]))
  86. {
  87. $url = $json['images'][0]['url'];
  88. $startdate = $json['images'][0]['startdate'];
  89. $enddate = $json['images'][0]['enddate'];
  90. $copyright = $json['images'][0]['copyright'];
  91. $url = substr($url, 0, 4) != 'http' ? 'http://cn.bing.com' . $url : $url;
  92. $title = '';
  93. $intro = '';
  94. $ret = Http::sendRequest("http://cn.bing.com/cnhp/coverstory/", [], 'GET');
  95. if ($ret['ret'])
  96. {
  97. $info = json_decode($ret['msg'], TRUE);
  98. if (isset($info['title']))
  99. {
  100. $title = $info['title'];
  101. $intro = $info['para1'];
  102. }
  103. }
  104. $this->code = 1;
  105. $this->data = [
  106. 'title' => $title,
  107. 'intro' => $intro,
  108. 'url' => $url,
  109. 'startdate' => $startdate,
  110. 'enddate' => $enddate,
  111. 'copyright' => $copyright,
  112. ];
  113. }
  114. }
  115. }
  116. /**
  117. * 读取角色权限树
  118. */
  119. public function roletree()
  120. {
  121. $model = model('AuthGroup');
  122. $id = $this->request->post("id");
  123. $pid = $this->request->post("pid");
  124. $parentgroupmodel = $model->get($pid);
  125. $currentgroupmodel = NULL;
  126. if ($id)
  127. {
  128. $currentgroupmodel = $model->get($id);
  129. }
  130. if (($pid || $parentgroupmodel) && (!$id || $currentgroupmodel))
  131. {
  132. $id = $id ? $id : NULL;
  133. //读取父类角色所有节点列表
  134. $parentrulelist = model('AuthRule')->all(in_array('*', explode(',', $parentgroupmodel->rules)) ? NULL : $parentgroupmodel->rules);
  135. //读取当前角色下规则ID集合
  136. $admin_rule_ids = $this->auth->getRuleIds();
  137. $superadmin = $this->auth->isSuperAdmin();
  138. $current_rule_ids = $id ? explode(',', $currentgroupmodel->rules) : [];
  139. if (!$id || !array_key_exists($pid, Tree::instance()->init($model->all(['status' => 'normal']))->getChildrenIds($id, TRUE)))
  140. {
  141. //构造jstree所需的数据
  142. $nodelist = [];
  143. foreach ($parentrulelist as $k => $v)
  144. {
  145. if (!$superadmin && !in_array($v['id'], $admin_rule_ids))
  146. continue;
  147. $state = array('selected' => !$v['ismenu'] && in_array($v['id'], $current_rule_ids));
  148. $nodelist[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => $v['title'], 'type' => 'menu', 'state' => $state);
  149. }
  150. $this->code = 1;
  151. $this->data = $nodelist;
  152. }
  153. else
  154. {
  155. $this->code = -1;
  156. $this->data = __('Can not change the parent to child');
  157. }
  158. }
  159. else
  160. {
  161. $this->code = -1;
  162. $this->data = __('Group not found');
  163. }
  164. }
  165. /**
  166. * 上传文件
  167. */
  168. public function upload()
  169. {
  170. $this->code = -1;
  171. $this->data = NULL;
  172. $file = $this->request->file('file');
  173. dump($file);
  174. $uploaddir = '/public/uploads/';
  175. $info = $file->move(ROOT_PATH . $uploaddir);
  176. if ($info)
  177. {
  178. $this->code = 1;
  179. $this->data = $uploaddir . $info->getSaveName();
  180. }
  181. else
  182. {
  183. // 上传失败获取错误信息
  184. $this->data = $file->getError();
  185. }
  186. }
  187. /**
  188. * 通用排序
  189. */
  190. public function weigh()
  191. {
  192. //排序的数组
  193. $ids = $this->request->post("ids");
  194. //拖动的记录ID
  195. $changeid = $this->request->post("changeid");
  196. //操作字段
  197. $field = $this->request->post("field");
  198. //操作的数据表
  199. $table = $this->request->post("table");
  200. //排序的方式
  201. $orderway = $this->request->post("orderway", 'strtolower');
  202. $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
  203. $sour = $weighdata = [];
  204. $ids = explode(',', $ids);
  205. $prikey = 'id';
  206. $pid = $this->request->post("pid");
  207. // 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
  208. if ($pid !== '')
  209. {
  210. $hasids = [];
  211. $list = Db::name($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field('id,pid')->select();
  212. foreach ($list as $k => $v)
  213. {
  214. $hasids[] = $v['id'];
  215. }
  216. $ids = array_values(array_intersect($ids, $hasids));
  217. }
  218. //直接修复排序
  219. $one = Db::name($table)->field("{$field},COUNT(*) AS nums")->group($field)->having('nums > 1')->find();
  220. if ($one)
  221. {
  222. $list = Db::name($table)->field("$prikey,$field")->order($field, $orderway)->select();
  223. foreach ($list as $k => $v)
  224. {
  225. Db::name($table)->where($prikey, $v[$prikey])->update([$field => $k + 1]);
  226. }
  227. $this->code = 1;
  228. }
  229. else
  230. {
  231. $list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
  232. foreach ($list as $k => $v)
  233. {
  234. $sour[] = $v[$prikey];
  235. $weighdata[$v[$prikey]] = $v[$field];
  236. }
  237. $position = array_search($changeid, $ids);
  238. $desc_id = $sour[$position]; //移动到目标的ID值,取出所处改变前位置的值
  239. $sour_id = $changeid;
  240. $desc_value = $weighdata[$desc_id];
  241. $sour_value = $weighdata[$sour_id];
  242. //echo "移动的ID:{$sour_id}\n";
  243. //echo "替换的ID:{$desc_id}\n";
  244. $weighids = array();
  245. $temp = array_values(array_diff_assoc($ids, $sour));
  246. foreach ($temp as $m => $n)
  247. {
  248. if ($n == $sour_id)
  249. {
  250. $offset = $desc_id;
  251. }
  252. else
  253. {
  254. if ($sour_id == $temp[0])
  255. {
  256. $offset = isset($temp[$m + 1]) ? $temp[$m + 1] : $sour_id;
  257. }
  258. else
  259. {
  260. $offset = isset($temp[$m - 1]) ? $temp[$m - 1] : $sour_id;
  261. }
  262. }
  263. $weighids[$n] = $weighdata[$offset];
  264. Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
  265. }
  266. $this->code = 1;
  267. }
  268. }
  269. }