UserController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. *
  4. * @author Mike <m@9026.com>
  5. * @version 1.0
  6. * @date 2015年10月12日
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Base;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\AdminUserModel;
  12. use App\Models\AlbumManufacturerModel;
  13. use App\Models\AlbumUserModel;
  14. use App\Services\Admin\Role;
  15. use App\Services\Admin\AdminUser;
  16. use Request;
  17. class UserController extends Controller
  18. {
  19. private $_service;
  20. private $_role_service;
  21. /**
  22. * 初始化Service
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. if(!$this->_service) $this->_service = new AdminUser();
  28. if(!$this->_role_service) $this->_role_service = new Role();
  29. }
  30. /**
  31. * 列表
  32. */
  33. function index()
  34. {
  35. $request = Request::all();
  36. $search['keyword'] = Request::input('keyword');
  37. $orderby = array();
  38. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  39. $orderby[$request['sort_field']] = $request['sort_field_by'];
  40. }
  41. if ($this->_user->is_root == 1) {
  42. $store_id = 0;
  43. } else {
  44. $store_id = $this->getStoreId();
  45. }
  46. $list = $this->_service->search($search, $orderby, $store_id);
  47. //dd($list);
  48. foreach ($list as $item) {
  49. $item->store = '暂无';
  50. $store = AlbumManufacturerModel::where('store_id', $item->store_id)->first();
  51. if ($store) {
  52. $item->store = $store->name;
  53. }
  54. }
  55. $roles = pairList($this->_getRoles(), 'id', 'name');
  56. return view('admin.base.user.index', compact('list', 'roles'));
  57. }
  58. /**
  59. * 列表
  60. */
  61. function resetPwd()
  62. {
  63. // $pwd = '$2y$10$jRQGg4qdfDhdt.4TZpDaL.2pbgBJZqvdR.AMrE5rA2D3dgMyit8vS';
  64. // var_dump(crypt('abcded', $pwd));exit;
  65. $request = Request::all();
  66. $search['keyword'] = Request::input('keyword');
  67. $search['resetPwd'] =true;
  68. $orderby = array();
  69. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  70. $orderby[$request['sort_field']] = $request['sort_field_by'];
  71. }
  72. $list = $this->_service->search($search, $orderby);
  73. return view('admin.base.user.resetPwd', compact('list'));
  74. }
  75. function resetPwdPass()
  76. {
  77. $ok = $this->_service->resetPwdPass(Request::get('id'));
  78. if($ok) {
  79. $this->showMessage('操作成功');
  80. }else{
  81. $this->showWarning('操作失败');
  82. }
  83. }
  84. function resetPwdReject()
  85. {
  86. $ok = $this->_service->resetPwdReject(Request::get('id'));
  87. if($ok) {
  88. $this->showMessage('操作成功');
  89. }else{
  90. $this->showWarning('操作失败');
  91. }
  92. }
  93. /**
  94. * 更新
  95. */
  96. public function create()
  97. {
  98. if(Request::method() == 'POST'){
  99. $data = Request::input('info');
  100. if ($this->_user['is_root'] != 1) {
  101. $data['store_id'] = $this->getStoreId();
  102. }
  103. if(isset($data['admin_role_id']))$data['admin_role_id'] = implode(',', $data['admin_role_id']);
  104. if($this->_service->create($data)){
  105. $this->showMessage('操作成功', urldecode(Request::input('_referer')));
  106. }else{
  107. $this->showWarning('操作失败' . $this->_service->getMsg(), urldecode(Request::input('_referer')));
  108. }
  109. }
  110. $data = $this->_service->find(Request::input('id'));
  111. $data['is_root'] = $this->_user['is_root'];
  112. if($this->_user['is_root']) {
  113. $roles = $this->_getRoles();
  114. }else{
  115. $roles = $this->_getCurrentRoles();
  116. }
  117. return view('admin.base.user.edit', compact('data', 'roles'));
  118. }
  119. /**
  120. * 更新
  121. */
  122. public function update()
  123. {
  124. if(Request::method() == 'POST')
  125. {
  126. $data = Request::input('info');
  127. if ($data['password'] == null) {
  128. unset($data['password']);
  129. }
  130. if ($this->_user['is_root'] != 1) {
  131. $data['store_id'] = $this->getStoreId();
  132. }
  133. // dd($data);
  134. if(isset($data['admin_role_id']))$data['admin_role_id'] = implode(',', $data['admin_role_id']);
  135. if($this->_service->update(Request::input('id'), $data)){
  136. $this->showMessage('操作成功', urldecode(Request::input('_referer')));
  137. }else{
  138. $this->showWarning('操作失败' . $this->_service->getMsg(), urldecode(Request::input('_referer')));
  139. }
  140. }
  141. $data = $this->_service->find(Request::input('id'));
  142. $data['is_root'] = $this->_user['is_root'];
  143. if($this->_user['is_root']){
  144. $roles = $this->_getRoles();
  145. }else{
  146. $roles = $this->_getCurrentRoles();
  147. }
  148. $store = AlbumManufacturerModel::where('store_id', $data['store_id'])->first(['store_id', 'name', 'phone'])->toArray();
  149. return view('admin.base.user.edit', compact('data', 'roles', 'store'));
  150. }
  151. public function auth() {
  152. if(Request::method() == 'POST'){
  153. $info = Request::input('info');
  154. if(!empty($info['admin_role_id'])){
  155. $info['admin_role_id'] = implode(',', $info['admin_role_id']);
  156. }
  157. if(!$info['id']) {
  158. $this->showWarning('数据不全', urldecode(Request::input('_referer')));
  159. }
  160. if($this->_service->auth($info)){
  161. $this->showMessage('操作成功', urldecode(Request::input('_referer')));
  162. }else{
  163. $this->showWarning('操作失败'. $this->_service->getMsg(), urldecode(Request::input('_referer')));
  164. }
  165. }
  166. if($this->_user['is_root']){
  167. $roles = $this->_getRoles();
  168. }else{
  169. $roles = $this->_getCurrentRoles();
  170. }
  171. return view('admin.base.user.auth', compact( 'roles'));
  172. }
  173. public function status() {
  174. $ok = $this->_service->updateStatus(Request::get('id'),Request::get('status'));
  175. if($ok) {
  176. $this->showMessage('操作成功');
  177. }else{
  178. $this->showWarning('操作失败' . $this->_service->getMsg());
  179. }
  180. }
  181. /**
  182. * 得到当前角色所拥有的角色
  183. */
  184. private function _getCurrentRoles()
  185. {
  186. $_node = $this->_getRoleNode();
  187. return $this->_role_service->getChildByLevel($_node['level'])->toArray();
  188. }
  189. /**
  190. * 获取角色权限节点(level越小权限越大)
  191. */
  192. private function _getRoleNode()
  193. {
  194. return $this->_role_service->getLevelNode($this->_user['admin_role_id'])->toArray();
  195. }
  196. public function searchStore()
  197. {
  198. $keyword = Request::post('keywords');
  199. $query = AlbumManufacturerModel::where('id','>',0);
  200. if(isset($keyword) && $keyword) {
  201. $query = $query->where('id','like','%'.$keyword.'%')
  202. ->orWhere('phone','like','%'.$keyword.'%')
  203. ->orWhere('name','like','%'.$keyword.'%');
  204. }
  205. $list = $query->get(['name','id','phone'])->toArray();
  206. if(empty($list)){
  207. $list[0]=[
  208. 'id'=>0,
  209. 'name'=>'暂无'
  210. ];
  211. }
  212. return response()->json(['code' => 0, 'message' => '', 'data' => $list]);
  213. }
  214. /**
  215. * 得到所有角色
  216. */
  217. private function _getRoles()
  218. {
  219. return $this->_role_service->get()->toArray();
  220. }
  221. /**
  222. * 删除
  223. */
  224. public function destroy()
  225. {
  226. $user = AdminUserModel::find(Request::get('id'));
  227. $ok = $user->delete();
  228. if ($ok) {
  229. return $this->showMessage('操作成功');
  230. } else {
  231. return $this->showWarning("操作失败");
  232. }
  233. }
  234. }