Ajax.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. $file = $this->request->file('file');
  172. $uploaddir = '/public/uploads/';
  173. $info = $file->move(ROOT_PATH . $uploaddir);
  174. if ($info)
  175. {
  176. $this->code = 200;
  177. $this->data = $uploaddir . $info->getSaveName();
  178. }
  179. else
  180. {
  181. // 上传失败获取错误信息
  182. $this->data = $file->getError();
  183. }
  184. }
  185. /**
  186. * 通用排序
  187. */
  188. public function weigh()
  189. {
  190. //排序的数组
  191. $ids = $this->request->post("ids");
  192. //拖动的记录ID
  193. $changeid = $this->request->post("changeid");
  194. //操作字段
  195. $field = $this->request->post("field");
  196. //操作的数据表
  197. $table = $this->request->post("table");
  198. //排序的方式
  199. $orderway = $this->request->post("orderway", 'strtolower');
  200. $orderway = $orderway == 'asc' ? 'ASC' : 'DESC';
  201. $sour = $weighdata = [];
  202. $ids = explode(',', $ids);
  203. $prikey = 'id';
  204. $pid = $this->request->post("pid");
  205. // 如果设定了pid的值,此时只匹配满足条件的ID,其它忽略
  206. if ($pid !== '')
  207. {
  208. $hasids = [];
  209. $list = Db::name($table)->where($prikey, 'in', $ids)->where('pid', 'in', $pid)->field('id,pid')->select();
  210. foreach ($list as $k => $v)
  211. {
  212. $hasids[] = $v['id'];
  213. }
  214. $ids = array_values(array_intersect($ids, $hasids));
  215. }
  216. //直接修复排序
  217. $one = Db::name($table)->field("{$field},COUNT(*) AS nums")->group($field)->having('nums > 1')->find();
  218. if ($one)
  219. {
  220. $list = Db::name($table)->field("$prikey,$field")->order($field, $orderway)->select();
  221. foreach ($list as $k => $v)
  222. {
  223. Db::name($table)->where($prikey, $v[$prikey])->update([$field => $k + 1]);
  224. }
  225. $this->code = 1;
  226. }
  227. else
  228. {
  229. $list = Db::name($table)->field("$prikey,$field")->where($prikey, 'in', $ids)->order($field, $orderway)->select();
  230. foreach ($list as $k => $v)
  231. {
  232. $sour[] = $v[$prikey];
  233. $weighdata[$v[$prikey]] = $v[$field];
  234. }
  235. $position = array_search($changeid, $ids);
  236. $desc_id = $sour[$position]; //移动到目标的ID值,取出所处改变前位置的值
  237. $sour_id = $changeid;
  238. $desc_value = $weighdata[$desc_id];
  239. $sour_value = $weighdata[$sour_id];
  240. //echo "移动的ID:{$sour_id}\n";
  241. //echo "替换的ID:{$desc_id}\n";
  242. $weighids = array();
  243. $temp = array_values(array_diff_assoc($ids, $sour));
  244. foreach ($temp as $m => $n)
  245. {
  246. if ($n == $sour_id)
  247. {
  248. $offset = $desc_id;
  249. }
  250. else
  251. {
  252. if ($sour_id == $temp[0])
  253. {
  254. $offset = isset($temp[$m + 1]) ? $temp[$m + 1] : $sour_id;
  255. }
  256. else
  257. {
  258. $offset = isset($temp[$m - 1]) ? $temp[$m - 1] : $sour_id;
  259. }
  260. }
  261. $weighids[$n] = $weighdata[$offset];
  262. Db::name($table)->where($prikey, $n)->update([$field => $weighdata[$offset]]);
  263. }
  264. $this->code = 1;
  265. }
  266. }
  267. }