AppuserController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\platform\modules\application\controllers;
  3. use app\platform\controllers\BaseController;
  4. use app\platform\modules\application\models\AppUser;
  5. use app\common\helpers\Cache;
  6. use app\platform\modules\application\models\UniacidApp;
  7. use app\platform\modules\user\models\AdminUser;
  8. class AppuserController extends BaseController
  9. {
  10. protected $key = 'application_user';
  11. protected $role = ['owner', 'manager', 'operator', 'founder'];
  12. public function index()
  13. {
  14. $list = AppUser::where('uniacid', request()->uniacid)->with('hasOneUser')->search(request()->search ? : '')->orderBy('id', 'desc')->paginate()->toArray();
  15. return $this->successJson('获取成功', $list);
  16. }
  17. public function add()
  18. {
  19. if (request()->input()) {
  20. $user = new AppUser();
  21. $data = $this->fillData(request()->input());
  22. if (!is_array($data)) {
  23. return $this->errorJson($data);
  24. }
  25. $user->fill($data);
  26. $validator = $user->validator();
  27. if ($validator->fails()) {
  28. return $this->error($validator->messages());
  29. } else {
  30. \Log::info('----------用户角色----------', $user);
  31. if ($user->save()) {
  32. //更新缓存
  33. // Cache::put($this->key.':'.$user->insertGetId(),$user->find($this->user->insertGetId()));
  34. // Cache::put($this->key.'_num',$user->insertGetId());
  35. return $this->successJson('添加成功');
  36. } else {
  37. return $this->errorJson('添加失败');
  38. }
  39. }
  40. }
  41. }
  42. public function delete()
  43. {
  44. $id = request()->id;
  45. $info = AppUser::find($id);
  46. if (!$id || !$info) {
  47. return $this->errorJson(0, '请选择要删除的用户');
  48. }
  49. if (\YunShop::app()->uid != 1 && $info->role == 'manager') {
  50. return $this->errorJson('您不是超级管理员,无法删除管理员!');
  51. }
  52. \Log::debug('----------删除用户(排查)----------', $id);
  53. $info->delete();
  54. // Cache::put($this->key.':'.$id, AppUser::find($id));
  55. return $this->successJson('OK');
  56. }
  57. private function fillData($data)
  58. {
  59. $checkUser = AdminUser::find($data['uid']);
  60. //用户存在且状态有效, 角色为普通用户时可以添加
  61. if (!$checkUser || $checkUser->status != 2 || $checkUser->type != 1) {
  62. return 'uid 无效';
  63. }
  64. //检测平台
  65. if (! UniacidApp::chekcApp($data['uniacid'])) {
  66. return '平台id 无效';
  67. }
  68. if (!in_array($data['role'], $this->role)) {
  69. return '权限值非法';
  70. }
  71. if(AppUser::where('uniacid', $data['uniacid'])->where('uid', $data['uid'])->first()) {
  72. return '数据重复';
  73. }
  74. if ($data['uid'] == \Auth::guard('admin')->user()->uid) {
  75. return '不能添加自己';
  76. }
  77. switch ($data['role']) {
  78. case 'manager':
  79. $name = '管理员';
  80. break;
  81. case 'clerk':
  82. $name = '店员';
  83. break;
  84. case 'operator':
  85. $name = '操作员';
  86. break;
  87. default:
  88. $name = '创始人';
  89. break;
  90. }
  91. return [
  92. 'uniacid' => $data['uniacid'],
  93. 'uid' => $data['uid'],
  94. 'role' => $data['role'] ? : 'manager',
  95. 'role_name' => $name
  96. ];
  97. }
  98. public function checkname()
  99. {
  100. $data['search']['realname'] = htmlentities(request()->name);
  101. // dd($data);
  102. if (!$data) {
  103. return $this->errorJson('请输入要查询的用户名');
  104. }
  105. // return AdminUser::searchUsers($data)->get();
  106. $user = AdminUser::where('type','!=',3)
  107. ->where(function ($query) use ($data) {
  108. $query->where('username', 'like', '%'.$data['search']['realname'].'%')
  109. ->Orwhere('uid', $data['search']['realname']);
  110. })
  111. ->get();
  112. return $this->successJson('查询成功', $user);
  113. }
  114. }