链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB

  1. <?php
  2. namespace App\Admin\Common;
  3. use App\Models\AdminRoles;
  4. use App\Models\AdminRoleUsers;
  5. use App\Models\AdminUsers;
  6. use Dcat\Admin\Controllers\AdminController;
  7. use Illuminate\Support\Facades\Hash;
  8. class Auth extends AdminController
  9. {
  10. /**
  11. * 添加管理员帐号
  12. * @param $form //表单模型
  13. * @param $model
  14. * @param $newId //添加信息成功后返回的ID
  15. * @param $roles
  16. * @return mixed
  17. */
  18. public static function addAdminUser($form,$model,$cid,$roles)
  19. {
  20. $adu = new AdminUsers();
  21. if (!$cid) {
  22. return '-2';
  23. }
  24. $row = $model::find($cid);
  25. //>>1.添加市场经理前,去查询是否已存在相同的帐号
  26. $count = $adu->where(['username' => $form->phone])->count();
  27. if ($count) {
  28. $row->delete();
  29. return '-1';
  30. }
  31. //>>2.添加市场经理登陆帐号
  32. $adu->username = $form->phone;
  33. $adu->password = Hash::make(substr($form->phone, -5));
  34. $adu->name = $form->name;
  35. $adu->status = $form->status;
  36. $res = $adu->save();
  37. if (!$res) {
  38. //删除刚添加数据
  39. $row->delete();
  40. return '-2';
  41. }
  42. //>>3.将帐号id关联
  43. $row->admin_user_id = $adu->id;
  44. $row->save();
  45. //添加到admin roles中
  46. $lanzu_role = AdminRoles::where('slug', $roles)->first();
  47. $aru = new AdminRoleUsers();
  48. $aru->role_id = $lanzu_role->id;
  49. $aru->user_id = $adu->id;
  50. $aru->save();
  51. }
  52. }