AdminUser.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * 用户操作
  4. * @author Mike <m@9026.com>
  5. * @version 1.0
  6. * @date 2015年11月13日
  7. *
  8. */
  9. namespace App\Services\Admin;
  10. use App\Models\AdminUserModel;
  11. use Hash;
  12. use App\Services\Base\BaseProcess;
  13. class AdminUser extends BaseProcess
  14. {
  15. /**
  16. * 模型
  17. *
  18. * @var object
  19. *
  20. */
  21. private $objModel;
  22. /**
  23. * 初始化
  24. */
  25. public function __construct()
  26. {
  27. if (!$this->objModel) $this->objModel = new AdminUserModel();
  28. }
  29. public function model()
  30. {
  31. $this->setSucessCode();
  32. return $this->objModel;
  33. }
  34. /**
  35. * 搜索
  36. * @param $search
  37. * @param $pagesize
  38. */
  39. public function search($search, $orderby = array(), $pagesize = PAGE_NUMS)
  40. {
  41. $currentQuery = $this->objModel;
  42. if (isset($search['keyword']) && !empty($search['keyword'])) {
  43. $keywords = '%' . $search['keyword'] . '%';
  44. $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
  45. $query->where('username', 'like', $keywords)
  46. ->orwhere('email', 'like', $keywords)
  47. ->orwhere('mobile', 'like', $keywords);
  48. });
  49. }
  50. if (isset($search['resetPwd']) && $search['resetPwd']) {
  51. $currentQuery = $currentQuery->where('reset_password', '<>', '');
  52. }
  53. if ($orderby && is_array($orderby)) {
  54. foreach ($orderby AS $field => $value) {
  55. $currentQuery = $currentQuery->orderBy($field, $value);
  56. }
  57. } else {
  58. $currentQuery = $currentQuery->orderBy('id', 'DESC');
  59. }
  60. $currentQuery = $currentQuery->paginate($pagesize);
  61. return $currentQuery;
  62. }
  63. /**
  64. * 添加
  65. * @param $data
  66. */
  67. public function create($data)
  68. {
  69. // 判断是否唯一
  70. if ($this->objModel->where('username', '=', $data['username'])->count()) {
  71. $this->setMsg("已经存在【{$data['username']}】用户!");
  72. return false;
  73. }
  74. $data['username'] = isset($data['username']) ? $data['username'] : "";
  75. $data['email'] = isset($data['email']) ? $data['email'] : "";
  76. $data['mobile'] = isset($data['mobile']) ? $data['mobile'] : "";
  77. $data['real_name'] = isset($data['real_name']) ? $data['real_name'] : "";
  78. if ($data['username'] == "" && $data['email'] == "" && $data['mobile'] == "") {
  79. $this->setMsg("至少有一个登陆凭证");
  80. return false;
  81. }
  82. if (isset($data['username']) && $data['username']) {
  83. if (is_numeric($data['username'])) {
  84. $this->setMsg("用户名不能是全数字!");
  85. return false;
  86. }
  87. if (strpos($data['username'], "@")) {
  88. $this->setMsg("用户名不能有@符号!");
  89. return false;
  90. }
  91. if ($this->objModel->where('username', '=', $data['username'])->count()) {
  92. $this->setMsg("已经存在的用户名!");
  93. return false;
  94. }
  95. }
  96. if (isset($data['email']) && $data['email']) {
  97. if ($this->objModel->where('email', '=', $data['email'])->count()) {
  98. $this->setMsg("已经存在的邮箱!");
  99. return false;
  100. }
  101. }
  102. if (isset($data['mobile']) && $data['mobile']) {
  103. if ($this->objModel->where('mobile', '=', $data['mobile'])->count()) {
  104. $this->setMsg("已经存在的手机号!");
  105. return false;
  106. }
  107. }
  108. if (isset($data['password']) && $data['password']) {
  109. $data['password'] = bcrypt($data['password']);
  110. }
  111. return $this->objModel->create($data);
  112. }
  113. /**
  114. * 更新
  115. * @param $id
  116. * @param $data
  117. */
  118. public function update($id, $data)
  119. {
  120. $obj = $this->objModel->find($id);
  121. if (!$obj) {
  122. return false;
  123. }
  124. if (isset($data['real_name']) && $data['real_name']) {
  125. $data['real_name'] = $data['real_name'];
  126. }
  127. if (isset($data['password']) && $data['password']) {
  128. $data['password'] = bcrypt($data['password']);
  129. }
  130. if (isset($data['username']) && $data['username']) {
  131. if (is_numeric($data['username'])) {
  132. $this->setMsg("用户名不能是全数字!");
  133. return false;
  134. }
  135. if (strpos($data['username'], "@")) {
  136. $this->setMsg("用户名不能有@符号!");
  137. return false;
  138. }
  139. if ($this->objModel->where('username', '=', $data['username'])->where('id', "<>", $id)->count()) {
  140. $this->setMsg("已经存在的用户名!");
  141. return false;
  142. }
  143. }
  144. if (isset($data['email']) && $data['email']) {
  145. if ($this->objModel->where('email', '=', $data['email'])->where('id', "<>", $id)->count()) {
  146. $this->setMsg("已经存在的邮箱!");
  147. return false;
  148. }
  149. }
  150. if (isset($data['mobile']) && $data['mobile']) {
  151. if ($this->objModel->where('mobile', '=', $data['mobile'])->where('id', "<>", $id)->count()) {
  152. $this->setMsg("已经存在的手机号!");
  153. return false;
  154. }
  155. }
  156. $ok = $obj->update($data);
  157. return $ok;
  158. }
  159. /**
  160. * 更新状态
  161. * @param $id
  162. * @param $status
  163. */
  164. public function updateStatus($id, $status)
  165. {
  166. $data = $this->objModel->find($id);
  167. //禁用
  168. $mobile = $data->mobile;
  169. $data->status = $status;
  170. return $data->save();
  171. }
  172. /**
  173. * 删除
  174. * @param $id
  175. */
  176. public function destroy($id)
  177. {
  178. return $this->objModel->destroy($id);
  179. }
  180. /**
  181. * 获取一行数据
  182. * @param $where
  183. * @return
  184. */
  185. public function find($id)
  186. {
  187. return $this->objModel->find($id);
  188. }
  189. public function login($login, $password)
  190. {
  191. if (is_numeric($login)) {
  192. $loginField = "mobile";
  193. } elseif (strpos($login, "@")) {
  194. $loginField = "email";
  195. } else {
  196. $loginField = "username";
  197. }
  198. if (\Auth::guard('admin')->attempt(array($loginField => $login, 'password' => $password))) {
  199. $data = \Auth::guard('admin')->user();
  200. if ($data->status == 0) {
  201. $this->setMsg('账号正在努力审核中...');
  202. return false;
  203. }
  204. if (!$data->status) {
  205. $this->setMsg('账号被禁用');
  206. return false;
  207. }
  208. $role = [];
  209. if ($data->is_root) {
  210. $aclObj = new Acl();
  211. $data = $aclObj->getRootFunc();
  212. $role['role'] = $data['func'];
  213. $role['menus'] = $data['menus'];
  214. } elseif ($data->admin_role_id) {
  215. $aclObj = new Acl();
  216. $data = $aclObj->getRoleFunc($data->admin_role_id);
  217. $role['role'] = $data['func'];
  218. $role['menus'] = $data['menus'];
  219. }
  220. session()->put(LOGIN_MARK_SESSION_KEY, $role);
  221. session()->save();
  222. return true;
  223. } else {
  224. $this->setMsg('用户密码错误');
  225. return false;
  226. }
  227. }
  228. /**
  229. * 重设密码
  230. * @param $loginName
  231. * @param $data
  232. */
  233. public function resetPwd($loginName, $data)
  234. {
  235. if (is_numeric($loginName)) {
  236. $loginField = "mobile";
  237. } elseif (strpos($loginName, "@")) {
  238. $loginField = "email";
  239. } else {
  240. $loginField = "username";
  241. }
  242. $user = $this->objModel->where($loginField, $loginName)->first();
  243. if (!$user) {
  244. $this->setMsg('没有找到用户');
  245. return false;
  246. }
  247. $user->reset_password = bcrypt($data['reset_password']);
  248. $user->reset_password_img = $data['reset_password_img'];
  249. $ok = $user->save();
  250. if (!$ok) {
  251. $this->setMsg('操作失败');
  252. return false;
  253. }
  254. return true;
  255. }
  256. /**
  257. * 查询多条数据
  258. */
  259. public function get($pagesize = PAGE_NUMS)
  260. {
  261. return $this->objModel->take($pagesize)->orderBy('id', 'DESC')->get();
  262. }
  263. /**
  264. * 查询多条数据并分页
  265. */
  266. public function getPage($pagesize = PAGE_NUMS)
  267. {
  268. return $this->objModel->orderBy('id', 'DESC')->paginate($pagesize);
  269. }
  270. }