Ajax.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\controller;
  10. use app\common\controller\Common;
  11. use app\admin\model\Menu as MenuModel;
  12. use app\admin\model\Attachment as AttachmentModel;
  13. use think\facade\Cache;
  14. use think\Db;
  15. /**
  16. * 用于处理ajax请求的控制器
  17. * @package app\admin\controller
  18. */
  19. class Ajax extends Common
  20. {
  21. /**
  22. * 获取联动数据
  23. * @param string $token token
  24. * @param int $pid 父级ID
  25. * @param string $pidkey 父级id字段名
  26. * @author 蔡伟明 <314013107@qq.com>
  27. * @return \think\response\Json
  28. */
  29. public function getLevelData($token = '', $pid = 0, $pidkey = 'pid')
  30. {
  31. if ($token == '') {
  32. return json(['code' => 0, 'msg' => '缺少Token']);
  33. }
  34. $token_data = session($token);
  35. $table = $token_data['table'];
  36. $option = $token_data['option'];
  37. $key = $token_data['key'];
  38. $data_list = Db::name($table)->where($pidkey, $pid)->column($option, $key);
  39. if ($data_list === false) {
  40. return json(['code' => 0, 'msg' => '查询失败']);
  41. }
  42. if ($data_list) {
  43. $result = [
  44. 'code' => 1,
  45. 'msg' => '请求成功',
  46. 'list' => format_linkage($data_list)
  47. ];
  48. return json($result);
  49. } else {
  50. return json(['code' => 0, 'msg' => '查询不到数据']);
  51. }
  52. }
  53. /**
  54. * 获取筛选数据
  55. * @param string $token
  56. * @param array $map 查询条件
  57. * @param string $options 选项,用于显示转换
  58. * @param string $list 选项缓存列表名称
  59. * @author 蔡伟明 <314013107@qq.com>
  60. * @return \think\response\Json
  61. */
  62. public function getFilterList($token = '', $map = [], $options = '', $list = '')
  63. {
  64. if ($list != '') {
  65. $result = [
  66. 'code' => 1,
  67. 'msg' => '请求成功',
  68. 'list' => Cache::get($list)
  69. ];
  70. return json($result);
  71. }
  72. if ($token == '') {
  73. return json(['code' => 0, 'msg' => '缺少Token']);
  74. }
  75. $token_data = session($token);
  76. $table = $token_data['table'];
  77. $field = $token_data['field'];
  78. if ($field == '') {
  79. return json(['code' => 0, 'msg' => '缺少字段']);
  80. }
  81. if (!empty($map) && is_array($map)) {
  82. foreach ($map as &$item) {
  83. if (is_array($item)) {
  84. foreach ($item as &$value) {
  85. $value = trim($value);
  86. }
  87. } else {
  88. $item = trim($item);
  89. }
  90. }
  91. }
  92. if (strpos($table, '/')) {
  93. $data_list = model($table)->where($map)->group($field)->column($field);
  94. } else {
  95. $data_list = Db::name($table)->where($map)->group($field)->column($field);
  96. }
  97. if ($data_list === false) {
  98. return json(['code' => 0, 'msg' => '查询失败']);
  99. }
  100. if ($data_list) {
  101. if ($options != '') {
  102. // 从缓存获取选项数据
  103. $options = cache($options);
  104. if ($options) {
  105. $temp_data_list = [];
  106. foreach ($data_list as $item) {
  107. $temp_data_list[$item] = isset($options[$item]) ? $options[$item] : '';
  108. }
  109. $data_list = $temp_data_list;
  110. } else {
  111. $data_list = parse_array($data_list);
  112. }
  113. } else {
  114. $data_list = parse_array($data_list);
  115. }
  116. $result = [
  117. 'code' => 1,
  118. 'msg' => '请求成功',
  119. 'list' => $data_list
  120. ];
  121. return json($result);
  122. } else {
  123. return json(['code' => 0, 'msg' => '查询不到数据']);
  124. }
  125. }
  126. /**
  127. * 获取指定模块的菜单
  128. * @param string $module 模块名
  129. * @author 蔡伟明 <314013107@qq.com>
  130. * @return mixed
  131. */
  132. public function getModuleMenus($module = '')
  133. {
  134. if (!is_signin()) {
  135. $this->error('请先登录');
  136. }
  137. $menus = MenuModel::getMenuTree(0, '', $module);
  138. $result = [
  139. 'code' => 1,
  140. 'msg' => '请求成功',
  141. 'list' => format_linkage($menus)
  142. ];
  143. return json($result);
  144. }
  145. /**
  146. * 设置配色方案
  147. * @param string $theme 配色名称
  148. * @author 蔡伟明 <314013107@qq.com>
  149. */
  150. public function setTheme($theme = '') {
  151. if (!is_signin()) {
  152. $this->error('请先登录');
  153. }
  154. $themes = ['default', 'amethyst', 'city', 'flat', 'modern', 'smooth'];
  155. if (!in_array($theme, $themes)) {
  156. $this->error('非法操作');
  157. }
  158. $map['name'] = 'system_color';
  159. $map['group'] = 'system';
  160. if (Db::name('admin_config')->where($map)->setField('value', $theme)) {
  161. $this->success('设置成功');
  162. } else {
  163. $this->error('设置失败,请重试');
  164. }
  165. }
  166. /**
  167. * 获取侧栏菜单
  168. * @param string $module_id 模块id
  169. * @param string $module 模型名
  170. * @param string $controller 控制器名
  171. * @author 蔡伟明 <314013107@qq.com>
  172. * @return string
  173. */
  174. public function getSidebarMenu($module_id = '', $module = '', $controller = '')
  175. {
  176. if (!is_signin()) {
  177. $this->error('登录已失效,请重新登录', 'user/publics/signin');
  178. }
  179. role_auth();
  180. $menus = MenuModel::getSidebarMenu($module_id, $module, $controller);
  181. $output = '';
  182. foreach ($menus as $key => $menu) {
  183. if (!empty($menu['url_value'])) {
  184. $output = $menu['url_value'];
  185. break;
  186. }
  187. if (!empty($menu['child'])) {
  188. $output = $menu['child'][0]['url_value'];
  189. break;
  190. }
  191. }
  192. $this->success('获取成功', null, $output);
  193. }
  194. /**
  195. * 检查附件是否存在
  196. * @param string $md5 文件md5
  197. * @author 蔡伟明 <314013107@qq.com>
  198. * @return \think\response\Json
  199. */
  200. public function check($md5 = '')
  201. {
  202. $md5 == '' && $this->error('参数错误');
  203. // 判断附件是否已存在
  204. if ($file_exists = AttachmentModel::get(['md5' => $md5])) {
  205. if ($file_exists['driver'] == 'local') {
  206. $file_path = PUBLIC_PATH.$file_exists['path'];
  207. } else {
  208. $file_path = $file_exists['path'];
  209. }
  210. return json([
  211. 'code' => 1,
  212. 'info' => '上传成功',
  213. 'class' => 'success',
  214. 'id' => $file_exists['id'],
  215. 'path' => $file_path
  216. ]);
  217. } else {
  218. $this->error('文件不存在');
  219. }
  220. }
  221. /**
  222. * 获取我的角色集合
  223. * @throws \think\db\exception\DataNotFoundException
  224. * @throws \think\db\exception\ModelNotFoundException
  225. * @throws \think\exception\DbException
  226. * @author 蔡伟明 <314013107@qq.com>
  227. */
  228. public function getMyRoles()
  229. {
  230. if (!is_signin()) {
  231. $this->error('请先登录');
  232. }
  233. $user = Db::name('admin_user')->where('id', session('user_auth.uid'))->find();
  234. !$user && $this->error('获取失败');
  235. $roles = [$user['role']];
  236. if ($user['roles'] != '') {
  237. $roles = array_merge($roles, explode(',', $user['roles']));
  238. }
  239. $roles = array_unique($roles);
  240. $roles = Db::name('admin_role')->where('id', 'in', $roles)->column('id,name');
  241. $this->success('获取成功', null, [
  242. 'curr' => session('user_auth.role'),
  243. 'roles' => $roles
  244. ]);
  245. }
  246. /**
  247. * 设置我的当前角色
  248. * @param string $id
  249. * @throws \think\db\exception\DataNotFoundException
  250. * @throws \think\db\exception\ModelNotFoundException
  251. * @throws \think\exception\DbException
  252. * @author 蔡伟明 <314013107@qq.com>
  253. */
  254. public function setMyRole($id = '')
  255. {
  256. if (!is_signin()) {
  257. $this->error('请先登录');
  258. }
  259. $id == '' && $this->error('请选择要设置的角色');
  260. // 读取当前用户能设置的角色
  261. $user = Db::name('admin_user')->where('id', session('user_auth.uid'))->find();
  262. !$user && $this->error('设置失败');
  263. $roles = [$user['role']];
  264. if ($user['roles'] != '') {
  265. $roles = array_merge($roles, explode(',', $user['roles']));
  266. }
  267. $roles = array_unique($roles);
  268. if (!in_array($id, $roles)) {
  269. $this->error('无法设置当前角色');
  270. }
  271. cache('role_menu_auth_'.session('user_auth.role'), null);
  272. session('user_auth.role', $id);
  273. session('user_auth.role_name', Db::name('admin_role')->where('id', $id)->value('name'));
  274. session('user_auth_sign', data_auth_sign(session('user_auth')));
  275. $this->success('设置成功');
  276. }
  277. }