Operator.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\library\Redis;
  4. use app\common\model\AuthGroup;
  5. use app\common\controller\Backend;
  6. use app\main\service\AdminService;
  7. use fast\Random;
  8. use fast\Tree;
  9. use app\common\model\Admin;
  10. use app\common\model\VipAdminBind;
  11. /**
  12. * 管理员管理
  13. *
  14. * @icon fa fa-users
  15. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  16. */
  17. class Operator extends Backend
  18. {
  19. /**
  20. * @var Admin
  21. */
  22. protected $model = null;
  23. /**
  24. * @var VipAdminBind
  25. */
  26. protected $vipAdminBind = null;
  27. protected $dataLimit = false;
  28. protected $childrenGroupIds = [];
  29. protected $childrenAdminIds = [];
  30. protected $searchFields = 'id,username,nickname';
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = model('Admin');
  35. $this->vipAdminBind = model('VipAdminBind');
  36. // $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin() ? true : false);
  37. //
  38. // $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  39. // Tree::instance()->init($groupList);
  40. // $result = [];
  41. // if ($this->auth->isSuperAdmin())
  42. // {
  43. // $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  44. // }
  45. // else
  46. // {
  47. // $groups = $this->auth->getGroups();
  48. // foreach ($groups as $m => $n)
  49. // {
  50. // $result = array_merge($result, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['pid'])));
  51. // }
  52. // }
  53. // $groupName = [];
  54. // foreach ($result as $k => $v)
  55. // {
  56. // $groupName[$v['id']] = $v['name'];
  57. // }
  58. // $this->view->assign('groupdata', $groupName);
  59. $this->assignconfig("admin", ['id' => $this->auth->id]);
  60. }
  61. /**
  62. * 查看
  63. */
  64. public function index()
  65. {
  66. if ($this->request->isAjax())
  67. {
  68. $operatorids = $this->model->getOperatorIdsByVipId($this->auth->id);
  69. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  70. $total = $this->model
  71. ->where($where)
  72. ->where('id', 'in', $operatorids)
  73. ->order($sort, $order)
  74. ->count();
  75. $list = $this->model
  76. ->where($where)
  77. ->join("admin_extend ae","admin.id = ae.admin_id")
  78. ->where('id', 'in', $operatorids)
  79. ->field("id,username,nickname,mobile,status,ae.remark remark")
  80. ->order($sort, $order)
  81. ->limit($offset, $limit)
  82. ->select();
  83. $result = array("total" => $total, "rows" => $list);
  84. return json($result);
  85. }
  86. return $this->view->fetch();
  87. }
  88. /**
  89. * 添加
  90. */
  91. public function add()
  92. {
  93. if ($this->request->isPost())
  94. {
  95. $params = $this->request->post("row/a");
  96. if ($params)
  97. {
  98. if(! AdminService::instance()->checkPassword($params['password'])){
  99. $this->error(AdminService::instance()->getPasswordRule());
  100. }
  101. $params['salt'] = Random::alnum();
  102. $params['password'] = md5(md5($params['password']) . $params['salt']);
  103. $params['avatar'] = asset('/img/avatar.png'); //设置新管理员默认头像。
  104. $result = $this->model->validate('Admin.add')->save($params);
  105. if ($result === false)
  106. {
  107. $this->error($this->model->getError());
  108. }
  109. $dataset = ['uid' => $this->model->id, 'group_id' => 8];
  110. model('AuthGroupAccess')->save($dataset);
  111. $extends = $this->request->post("extends/a");
  112. $extends['admin_id'] = $this->model->id;
  113. $extends['create_by'] = $this->auth->id;
  114. $extends['benefit'] = 0.00;
  115. $extends['card_holder'] = '';
  116. $extends['card_num'] = '';
  117. model("AdminExtend")->save($extends);
  118. $this->success();
  119. }
  120. $this->error();
  121. }
  122. return $this->view->fetch();
  123. }
  124. /**
  125. * 编辑
  126. */
  127. public function edit($ids = NULL)
  128. {
  129. $row = $this->model->get(['id' => $ids]);
  130. if (!$row)
  131. $this->error(__('No Results were found'));
  132. if ($this->request->isPost())
  133. {
  134. $params = $this->request->post("row/a");
  135. if ($params)
  136. {
  137. if ($params['password'])
  138. {
  139. if(! AdminService::instance()->checkPassword($params['password'])){
  140. $this->error(AdminService::instance()->getPasswordRule());
  141. }
  142. $params['salt'] = Random::alnum();
  143. $params['password'] = md5(md5($params['password']) . $params['salt']);
  144. AdminService::instance()->updateAdminSessionStatus($ids);
  145. }
  146. else
  147. {
  148. unset($params['password'], $params['salt']);
  149. }
  150. //这里需要针对username和email做唯一验证
  151. $adminValidate = \think\Loader::validate('Admin');
  152. $adminValidate->rule([
  153. 'username' => 'require|max:50|unique:admin,username,' . $row->id,
  154. 'email' => 'email|unique:admin,email,' . $row->id
  155. ]);
  156. $result = $row->validate('Admin.edit')->save($params);
  157. if ($result === false)
  158. {
  159. $this->error($row->getError());
  160. }
  161. //删除当前运营与渠道商的关系
  162. if ($params['status'] == 'hidden') {
  163. $this->vipAdminBind->where('admin_id_master', $ids)->delete();
  164. }
  165. $extends = $this->request->post("extends/a");
  166. $extends['admin_id'] = $row['id'];
  167. model("AdminExtend")->update($extends);
  168. $this->success();
  169. }
  170. $this->error();
  171. }
  172. $row['remark'] = model("AdminExtend")->where("admin_id",$row['id'])->value('remark');
  173. $this->view->assign("row", $row);
  174. return $this->view->fetch();
  175. }
  176. /**
  177. * 编辑
  178. */
  179. /**
  180. * 编辑
  181. */
  182. public function editSelf()
  183. {
  184. //
  185. // $sql = '';
  186. // for($i=256;$i<=511;$i++){
  187. // $sql.="USE test_cps_user_{$i};DELETE FROM USER WHERE id>0;DELETE FROM OPENID WHERE id>0;DELETE FROM RECHARGE WHERE id>0;";
  188. // }
  189. // echo $sql;die;
  190. $ids = $this->auth->id;
  191. $group = model('AuthGroupAccess')->where('uid',$ids)->find();
  192. $this->assign('groupId',$group->group_id);
  193. $row = $this->model->get(['id' => $ids]);
  194. if (!$row)
  195. $this->error(__('No Results were found'));
  196. if ($this->request->isPost())
  197. {
  198. $params = $this->request->post("row/a");
  199. if ($params)
  200. {
  201. if ($params['password'])
  202. {
  203. $params['salt'] = Random::alnum();
  204. $params['password'] = md5(md5($params['password']) . $params['salt']);
  205. }
  206. else
  207. {
  208. unset($params['password'], $params['salt']);
  209. }
  210. //这里需要针对username和email做唯一验证
  211. $adminValidate = \think\Loader::validate('Admin');
  212. $adminValidate->rule([
  213. 'username' => 'require|max:50|unique:admin,username,' . $row->id,
  214. 'email' => 'require|email|unique:admin,email,' . $row->id
  215. ]);
  216. $extends = $this->request->post("extend/a");
  217. //验证身份证是否合法
  218. if(!empty($extends['idcard_no'])){
  219. $flag = validateIDCard($extends['idcard_no']);
  220. if(!$flag){
  221. $this->error('身份证号不合法,请重新填写');
  222. }else{
  223. model('AdminExtend')->save(['idcard_no'=>$extends['idcard_no']],['admin_id'=>$ids]);
  224. $redis = Redis::instance();
  225. $key = 'AE:'.$ids;
  226. $redis->del($key);
  227. }
  228. }
  229. $result = $row->validate('Admin.edit')->save($params);
  230. if ($result === false)
  231. {
  232. $this->error($row->getError());
  233. }
  234. $this->success();
  235. }
  236. $this->error();
  237. }
  238. $extends = model('Admin_extend')->where('admin_id',$row['id'])->find();
  239. $this->view->assign("payMethodList", model('AdminExtend')->getPayMethodList());
  240. $this->view->assign('extends',$extends);
  241. $this->view->assign("row", $row);
  242. return $this->view->fetch();
  243. }
  244. /**
  245. * 删除
  246. */
  247. public function del($ids = "")
  248. {
  249. if ($ids)
  250. {
  251. // 避免越权删除管理员
  252. $childrenGroupIds = $this->childrenGroupIds;
  253. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
  254. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  255. })->select();
  256. if ($adminList)
  257. {
  258. $deleteIds = [];
  259. foreach ($adminList as $k => $v)
  260. {
  261. $deleteIds[] = $v->id;
  262. }
  263. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  264. if ($deleteIds)
  265. {
  266. $this->model->where('id','in',$deleteIds)->update(['status'=>'hidden']);
  267. // $this->model->destroy($deleteIds);
  268. // model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  269. $this->success();
  270. }
  271. }
  272. }
  273. $this->error();
  274. }
  275. /**
  276. * 批量更新
  277. * @internal
  278. */
  279. public function multi($ids = "")
  280. {
  281. // 管理员禁止批量操作
  282. $this->error();
  283. }
  284. }