User.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. namespace app\admin\controller\seller;
  3. use app\admin\model\Admin;
  4. use app\admin\model\AdminMoneyLog;
  5. use app\admin\model\AuthGroup;
  6. use app\admin\model\AuthGroupAccess;
  7. use app\common\controller\Backend;
  8. use fast\Random;
  9. use fast\Tree;
  10. use think\Db;
  11. use think\Model;
  12. use think\Validate;
  13. /**
  14. * 会员组管理
  15. *
  16. * @icon fa fa-users
  17. */
  18. class User extends Backend
  19. {
  20. /**
  21. * @var \app\admin\model\UserGroup
  22. */
  23. protected $model = null;
  24. protected $childrenGroupIds = [];
  25. protected $childrenAdminIds = [];
  26. protected $searchFields='username,mobile,nickname';
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model=new Admin;
  31. $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());
  32. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());
  33. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  34. Tree::instance()->init($groupList);
  35. $groupdata = [];
  36. if ($this->auth->isSuperAdmin()) {
  37. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  38. foreach ($result as $k => $v) {
  39. $groupdata[$v['id']] = $v['name'];
  40. }
  41. } else {
  42. $result = [];
  43. $groups = $this->auth->getGroups();
  44. foreach ($groups as $m => $n) {
  45. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  46. $temp = [];
  47. foreach ($childlist as $k => $v) {
  48. $temp[$v['id']] = $v['name'];
  49. }
  50. $result[__($n['name'])] = $temp;
  51. }
  52. $groupdata = $result;
  53. }
  54. $this->view->assign('groupdata', $groupdata);
  55. $this->assignconfig("admin", ['id' => $this->auth->id]);
  56. }
  57. public function share(){
  58. $this->assign('admin',$this->admin());
  59. return $this->fetch();
  60. }
  61. public function changepwd(){
  62. if($this->request->isGet()){
  63. return $this->fetch();
  64. }else{
  65. $data=input('row/a');
  66. $this->validate($data,[
  67. 'old_pwd'=>['require',],
  68. 'new_pwd|新密码'=>['require','confirm:new_pwd_confirm'],
  69. 'new_pwd_confirm|确认新密码'=>['require',],
  70. 'code|验证码'=>['require','captcha'],
  71. ]);
  72. if($this->admin('password')!=md5(md5($data['old_pwd']).$this->admin('salt'))){
  73. $this->error('原密码错误');
  74. }
  75. $admin=$this->admin();
  76. $admin['password']=md5(md5($data['new_pwd']) . $this->admin('salt'));
  77. $admin->save();
  78. $this->success();
  79. }
  80. }
  81. /**
  82. * 查看
  83. */
  84. public function index()
  85. {
  86. //设置过滤方法
  87. $this->request->filter(['strip_tags', 'trim']);
  88. if ($this->request->isAjax()) {
  89. //如果发送的来源是Selectpage,则转发到Selectpage
  90. if ($this->request->request('keyField')) {
  91. return $this->selectpage();
  92. }
  93. $childrenGroupIds = $this->childrenGroupIds;
  94. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  95. ->column('id,name');
  96. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  97. ->field('uid,group_id')
  98. ->select();
  99. $adminGroupName = [];
  100. foreach ($authGroupList as $k => $v) {
  101. if (isset($groupName[$v['group_id']])) {
  102. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  103. }
  104. }
  105. $groups = $this->auth->getGroups();
  106. foreach ($groups as $m => $n) {
  107. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  108. }
  109. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  110. $list = $this->model
  111. ->where($where)
  112. ->where('id', 'in', $this->childrenAdminIds)
  113. ->where('user_type',Admin::UT_SELLER)
  114. ->field(['password', 'salt', 'token'], true)
  115. ->order($sort, $order)
  116. ->paginate($limit);
  117. foreach ($list as $k => &$v) {
  118. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  119. $v['groups'] = implode(',', array_keys($groups));
  120. $v['groups_text'] = implode(',', array_values($groups));
  121. }
  122. unset($v);
  123. $result = array("total" => $list->total(), "rows" => $list->items());
  124. return json($result);
  125. }
  126. return $this->view->fetch();
  127. }
  128. /**
  129. * 添加
  130. */
  131. public function add()
  132. {
  133. if ($this->request->isPost()) {
  134. $this->token();
  135. $params = $this->request->post("row/a");
  136. if ($params) {
  137. Db::startTrans();
  138. try {
  139. if (!Validate::is($params['password'], '\S{6,16}')) {
  140. exception(__("Please input correct password"));
  141. }
  142. $params['salt'] = Random::alnum();
  143. $params['password'] = md5(md5($params['password']) . $params['salt']);
  144. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  145. $params['user_type'] = Admin::UT_SELLER;
  146. $result = $this->model->validate('Admin.add')->save($params);
  147. if ($result === false) {
  148. exception($this->model->getError());
  149. }
  150. $group = $this->request->post("group/a");
  151. //过滤不允许的组别,避免越权
  152. $group = array_intersect($this->childrenGroupIds, $group);
  153. if (!$group) {
  154. exception(__('The parent group exceeds permission limit'));
  155. }
  156. $dataset = [];
  157. foreach ($group as $value) {
  158. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  159. }
  160. model('AuthGroupAccess')->saveAll($dataset);
  161. Db::commit();
  162. } catch (\Exception $e) {
  163. Db::rollback();
  164. $this->error($e->getMessage());
  165. }
  166. $this->success();
  167. }
  168. $this->error(__('Parameter %s can not be empty', ''));
  169. }
  170. return $this->view->fetch();
  171. }
  172. /**
  173. * 编辑
  174. */
  175. public function edit($ids = null)
  176. {
  177. $row = $this->model->get(['id' => $ids]);
  178. if (!$row) {
  179. $this->error(__('No Results were found'));
  180. }
  181. if (!in_array($row->id, $this->childrenAdminIds)) {
  182. $this->error(__('You have no permission'));
  183. }
  184. if ($this->request->isPost()) {
  185. $this->token();
  186. $params = $this->request->post("row/a");
  187. if ($params) {
  188. Db::startTrans();
  189. try {
  190. if ($params['password']) {
  191. if (!Validate::is($params['password'], '\S{6,16}')) {
  192. exception(__("Please input correct password"));
  193. }
  194. $params['salt'] = Random::alnum();
  195. $params['password'] = md5(md5($params['password']) . $params['salt']);
  196. } else {
  197. unset($params['password'], $params['salt']);
  198. }
  199. //这里需要针对username和email做唯一验证
  200. $adminValidate = \think\Loader::validate('Admin');
  201. $adminValidate->rule([
  202. 'username' => 'require|regex:\w{3,12}|unique:admin,username,' . $row->id,
  203. 'email' => 'require|email|unique:admin,email,' . $row->id,
  204. 'password' => 'regex:\S{32}',
  205. ]);
  206. dd($params);
  207. $result = $row->validate('Admin.edit')->save($params);
  208. if ($result === false) {
  209. exception($row->getError());
  210. }
  211. // 先移除所有权限
  212. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  213. $group = $this->request->post("group/a");
  214. // 过滤不允许的组别,避免越权
  215. $group = array_intersect($this->childrenGroupIds, $group);
  216. if (!$group) {
  217. exception(__('The parent group exceeds permission limit'));
  218. }
  219. $dataset = [];
  220. foreach ($group as $value) {
  221. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  222. }
  223. model('AuthGroupAccess')->saveAll($dataset);
  224. Db::commit();
  225. } catch (\Exception $e) {
  226. Db::rollback();
  227. $this->error($e->getMessage());
  228. }
  229. $this->success();
  230. }
  231. $this->error(__('Parameter %s can not be empty', ''));
  232. }
  233. $grouplist = $this->auth->getGroups($row['id']);
  234. $groupids = [];
  235. foreach ($grouplist as $k => $v) {
  236. $groupids[] = $v['id'];
  237. }
  238. $this->view->assign("row", $row);
  239. $this->view->assign("groupids", $groupids);
  240. return $this->view->fetch();
  241. }
  242. /**
  243. * 删除
  244. */
  245. public function del($ids = "")
  246. {
  247. if (!$this->request->isPost()) {
  248. $this->error(__("Invalid parameters"));
  249. }
  250. $ids = $ids ? $ids : $this->request->post("ids");
  251. if ($ids) {
  252. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  253. // 避免越权删除管理员
  254. $childrenGroupIds = $this->childrenGroupIds;
  255. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  256. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  257. })->select();
  258. if ($adminList) {
  259. $deleteIds = [];
  260. foreach ($adminList as $k => $v) {
  261. $deleteIds[] = $v->id;
  262. }
  263. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  264. if ($deleteIds) {
  265. Db::startTrans();
  266. try {
  267. $this->model->destroy($deleteIds);
  268. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  269. Db::commit();
  270. } catch (\Exception $e) {
  271. Db::rollback();
  272. $this->error($e->getMessage());
  273. }
  274. $this->success();
  275. }
  276. $this->error(__('No rows were deleted'));
  277. }
  278. }
  279. $this->error(__('You have no permission'));
  280. }
  281. public function detail($ids){
  282. $admin=$this->model->findOrFail($ids);
  283. $this->assign('user',$admin);
  284. if($this->request->isAjax()){
  285. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  286. $list = \app\admin\model\User::where($where)
  287. ->where('admin_id',$admin['id'])
  288. ->order($sort, $order)
  289. ->paginate($limit);
  290. $result = array("total" => $list->total(), "rows" => $list->items());
  291. return json($result);
  292. }
  293. return $this->fetch();
  294. }
  295. /**
  296. * 批量更新
  297. * @internal
  298. */
  299. public function multi($ids = "")
  300. {
  301. // 管理员禁止批量操作
  302. $this->error('禁止批量操作');
  303. }
  304. protected function selectpage()
  305. {
  306. //设置过滤方法
  307. $this->request->filter(['trim', 'strip_tags', 'htmlspecialchars']);
  308. //搜索关键词,客户端输入以空格分开,这里接收为数组
  309. $word = (array)$this->request->request("q_word/a");
  310. //当前页
  311. $page = $this->request->request("pageNumber");
  312. //分页大小
  313. $pagesize = $this->request->request("pageSize");
  314. //搜索条件
  315. $andor = $this->request->request("andOr", "and", "strtoupper");
  316. //排序方式
  317. $orderby = (array)$this->request->request("orderBy/a");
  318. //显示的字段
  319. $field = $this->request->request("showField");
  320. //主键
  321. $primarykey = $this->request->request("keyField");
  322. //主键值
  323. $primaryvalue = $this->request->request("keyValue");
  324. //搜索字段
  325. $searchfield = (array)$this->request->request("searchField/a");
  326. //自定义搜索条件
  327. $custom = (array)$this->request->request("custom/a");
  328. //是否返回树形结构
  329. $istree = $this->request->request("isTree", 0);
  330. $ishtml = $this->request->request("isHtml", 0);
  331. if ($istree) {
  332. $word = [];
  333. $pagesize = 999999;
  334. }
  335. $order = [];
  336. foreach ($orderby as $k => $v) {
  337. $order[$v[0]] = $v[1];
  338. }
  339. $field = $field ? $field : 'name';
  340. //如果有primaryvalue,说明当前是初始化传值
  341. if ($primaryvalue !== null) {
  342. $where = [$primarykey => ['in', $primaryvalue]];
  343. $pagesize = 999999;
  344. } else {
  345. $where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
  346. $logic = $andor == 'AND' ? '&' : '|';
  347. $searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
  348. $searchfield = str_replace(',', $logic, $searchfield);
  349. $word = array_filter(array_unique($word));
  350. if (count($word) == 1) {
  351. $query->where($searchfield, "like", "%" . reset($word) . "%");
  352. } else {
  353. $query->where(function ($query) use ($word, $searchfield) {
  354. foreach ($word as $index => $item) {
  355. $query->whereOr(function ($query) use ($item, $searchfield) {
  356. $query->where($searchfield, "like", "%{$item}%");
  357. });
  358. }
  359. });
  360. }
  361. if ($custom && is_array($custom)) {
  362. foreach ($custom as $k => $v) {
  363. if (is_array($v) && 2 == count($v)) {
  364. $query->where($k, trim($v[0]), $v[1]);
  365. } else {
  366. $query->where($k, '=', $v);
  367. }
  368. }
  369. }
  370. };
  371. }
  372. $adminIds = $this->getDataLimitAdminIds();
  373. if (is_array($adminIds)) {
  374. $this->model->where($this->dataLimitField, 'in', $adminIds);
  375. }
  376. $list = [];
  377. $total = $this->model->where($where)->count();
  378. if ($total > 0) {
  379. if (is_array($adminIds)) {
  380. $this->model->where($this->dataLimitField, 'in', $adminIds);
  381. }
  382. $fields = is_array($this->selectpageFields) ? $this->selectpageFields : ($this->selectpageFields && $this->selectpageFields != '*' ? explode(',', $this->selectpageFields) : []);
  383. //如果有primaryvalue,说明当前是初始化传值,按照选择顺序排序
  384. if ($primaryvalue !== null && preg_match("/^[a-z0-9_\-]+$/i", $primarykey)) {
  385. $primaryvalue = array_unique(is_array($primaryvalue) ? $primaryvalue : explode(',', $primaryvalue));
  386. //修复自定义data-primary-key为字符串内容时,给排序字段添加上引号
  387. $primaryvalue = array_map(function ($value) {
  388. return '\'' . $value . '\'';
  389. }, $primaryvalue);
  390. $primaryvalue = implode(',', $primaryvalue);
  391. $this->model->orderRaw("FIELD(`{$primarykey}`, {$primaryvalue})");
  392. } else {
  393. $this->model->order($order);
  394. }
  395. $datalist = $this->model->where($where)
  396. ->where('user_type',Admin::UT_SELLER)
  397. ->page($page, $pagesize)
  398. ->select();
  399. foreach ($datalist as $index => $item) {
  400. unset($item['password'], $item['salt']);
  401. if ($this->selectpageFields == '*') {
  402. $result = [
  403. $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
  404. $field => isset($item[$field]) ? $item[$field] : '',
  405. ];
  406. } else {
  407. $result = array_intersect_key(($item instanceof Model ? $item->toArray() : (array)$item), array_flip($fields));
  408. }
  409. $result['pid'] = isset($item['pid']) ? $item['pid'] : (isset($item['parent_id']) ? $item['parent_id'] : 0);
  410. $list[] = $result;
  411. }
  412. if ($istree && !$primaryvalue) {
  413. $tree = Tree::instance();
  414. $tree->init(collection($list)->toArray(), 'pid');
  415. $list = $tree->getTreeList($tree->getTreeArray(0), $field);
  416. if (!$ishtml) {
  417. foreach ($list as &$item) {
  418. $item = str_replace('&nbsp;', ' ', $item);
  419. }
  420. unset($item);
  421. }
  422. }
  423. }
  424. //这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
  425. return json(['list' => $list, 'total' => $total]);
  426. }
  427. public function show(){
  428. if($this->request->isGet()){
  429. return $this->fetch();
  430. }
  431. }
  432. #佣金明细
  433. public function my_wallet(){
  434. //设置过滤方法
  435. $this->request->filter(['strip_tags', 'trim']);
  436. if ($this->request->isAjax()) {
  437. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  438. $list=AdminMoneyLog::where('admin_id',$this->admin('id'))
  439. ->with(['admin'])
  440. ->order($sort, $order)
  441. ->where($where)
  442. ->paginate($limit);
  443. $result = array("total" => $list->total(), "rows" => $list->items());
  444. return json($result);
  445. }
  446. $this->assign('types',AdminMoneyLog::getMoneyTypes());
  447. return $this->view->fetch();
  448. }
  449. }