UserController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 07/03/2017
  6. * Time: 16:13
  7. */
  8. namespace app\backend\modules\user\controllers;
  9. use app\backend\modules\user\services\PermissionService;
  10. use app\common\components\BaseController;
  11. use app\common\helpers\Cache;
  12. use app\common\helpers\PaginationHelper;
  13. use app\common\helpers\Url;
  14. use app\common\models\user\User;
  15. use app\common\models\user\UserProfile;
  16. use app\common\models\user\YzRole;
  17. use app\common\services\Utils;
  18. use app\common\models\user\YzUserRole;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Hash;
  21. class UserController extends BaseController
  22. {
  23. const PageSize = 10;
  24. /*
  25. * 操作员分页列表
  26. **/
  27. public function index()
  28. {
  29. if (request()->ajax()) {
  30. $records = User::records();
  31. $search = request()->search;
  32. if ($search) {
  33. $records = $records->search($search);
  34. }
  35. if (config('app.framework') == 'platform') {
  36. $userList = $records->orderBy('created_at', 'desc')->paginate(static::PageSize);
  37. } else {
  38. $userList = $records->orderBy('starttime', 'desc')->paginate(static::PageSize);
  39. }
  40. $roleList = YzRole::getRoleListToUser();
  41. return $this->successJson('请求接口', [
  42. 'roleList' => $roleList,
  43. 'userList' => $userList,
  44. ]);
  45. }
  46. return view('user.user.user')->render();
  47. }
  48. public function switchUser()
  49. {
  50. $id = request()->uid;
  51. $user = User::where('uid', $id)->first();
  52. if ($user) {
  53. $dispatcher = User::getEventDispatcher();
  54. User::unsetEventDispatcher(); //临时禁用观察者
  55. switch ($user->status) {
  56. case User::ROLE_ENABLE:
  57. $user->status = User::ROLE_DISABLE;
  58. if ($user->save()) {
  59. User::setEventDispatcher($dispatcher);
  60. return $this->successJson('角色禁用成功');
  61. }
  62. break;
  63. case User::ROLE_DISABLE:
  64. $user->status = User::ROLE_ENABLE;
  65. if ($user->save()) {
  66. User::setEventDispatcher($dispatcher);
  67. return $this->successJson('角色启用成功');
  68. }
  69. break;
  70. }
  71. return $this->errorJson('数据出错,保存失败');
  72. } else {
  73. return $this->errorJson('找不到该操作员,请重试');
  74. }
  75. }
  76. /*
  77. * 添加操作员
  78. **/
  79. public function store()
  80. {
  81. $userModel = new User();
  82. $requestUser = request()->user;
  83. if ($requestUser) {
  84. $requestUser['username'] = trim($requestUser['username']);
  85. $userData = $this->addedUserData($requestUser);
  86. if (config('app.framework') == 'platform') {
  87. $userData['owner_uid'] = 0;
  88. }
  89. $userModel->fill($userData);
  90. $userModel->widgets = request()->widgets;
  91. $userModel->widgets['perms'] = request()->perms;
  92. $validator = $userModel->validator();
  93. if ($validator->fails()) {
  94. return $this->errorJson($validator->messages());
  95. } else {
  96. $verifyPassword = verifyPasswordStrength($userModel->password);
  97. if($verifyPassword !== true){
  98. return $this->errorJson($verifyPassword);
  99. }
  100. if (config('app.framework') == 'platform') {
  101. $userModel->password = bcrypt($userModel->password);
  102. } else {
  103. $userModel->password = $this->password($userModel->password, $userModel->salt);
  104. }
  105. if ($userModel->save()) {
  106. Cache::flush();
  107. return $this->successJson('添加操作员成功.', Url::absoluteWeb('user.user.index'));
  108. } else {
  109. return $this->errorJson('请检查手机号或电话格式');
  110. }
  111. }
  112. }
  113. $permissions = PermissionService::getPermission();
  114. $permissions = PermissionService::getApiData($permissions);
  115. $roleList = YzRole::getRoleListToUser();
  116. if (request()->ajax()) {
  117. return $this->successJson('请求接口', [
  118. 'roleList' => $roleList,
  119. 'permissions' => $permissions,
  120. ]);
  121. }
  122. return view('user.user.form', [
  123. 'roleList' => json_encode(($roleList?:[])),
  124. 'permissions' => json_encode(($permissions?:[])),
  125. ])->render();
  126. }
  127. /*
  128. * 修改操作员
  129. **/
  130. public function update()
  131. {
  132. $id = request()->id;
  133. $userModel = User::getUserByid($id);
  134. if (!$userModel) {
  135. return $this->errorJson("未找到数据或已删除!");
  136. }
  137. $permissionService = new PermissionService();
  138. $userPermissions = $permissionService->handlePermission($userModel->permissions->toArray());
  139. $permissions = PermissionService::getPermission();
  140. $roleList = YzRole::getRoleListToUser();
  141. $rolePermissions = [];
  142. if ($userModel->userRole && $userModel->userRole->role) {
  143. $rolePermissions = $permissionService->handlePermission($userModel->userRole->permissions->toArray());
  144. $userPermissions = array_merge($rolePermissions, $userPermissions);
  145. //dd($permissionService->handlePermission($userModel->userRole->permissions->toArray()));
  146. }
  147. //dd($userPermissions);
  148. //修改 start
  149. $requestUser = request()->user;
  150. if ($requestUser) {
  151. //dd(\YunShop::request());
  152. $userModel->status = $requestUser['status'];
  153. if ($requestUser['password']) {
  154. $verifyPassword = verifyPasswordStrength($requestUser['password']);
  155. if($verifyPassword !== true){
  156. return $this->errorJson($verifyPassword);
  157. }
  158. $userModel->password = user_hash($requestUser['password'], $userModel->salt);
  159. }
  160. $userModel->widgets = request()->widgets;
  161. $userModel->widgets['perms'] = request()->perms;
  162. if ($userModel->save()) {
  163. Cache::flush();
  164. // $key = 'user.permissions.'.$userModel->uid;
  165. // \Cache::forget($key);
  166. // \Cache::forget('menu_list'.$userModel->uid);
  167. return $this->successJson('修改操作员成功.', Url::absoluteWeb('user.user.update', array('id' => $userModel->uid)));
  168. }
  169. }
  170. $permissions = PermissionService::getApiData($permissions);
  171. if ($userModel->userRole->role_id == 0) {
  172. $userModel = $userModel->toArray();
  173. $userModel['user_role']['role_id'] = '';
  174. }
  175. return view('user.user.edit', [
  176. 'user' => $userModel,
  177. 'roleList' => $roleList,
  178. 'permissions' => $permissions,
  179. 'rolePermission' => $rolePermissions,
  180. 'userPermissions' => $userPermissions
  181. ])->render();
  182. }
  183. /**
  184. * 删除操作员
  185. *
  186. * @return \Illuminate\Http\JsonResponse
  187. * @throws \Exception
  188. */
  189. public function destroy()
  190. {
  191. $userModel = User::find(request()->id);
  192. $profileModel = UserProfile::where('uid',$userModel->uid)->first();
  193. if (!$userModel and !$profileModel) return $this->errorJson("记录不存在或已删除!");
  194. DB::beginTransaction();
  195. $res = $userModel->delete();
  196. $res2 = $profileModel->delete();
  197. if (!($res and $res2)){
  198. DB::rollBack();
  199. return $this->errorJson('删除失败,请重试!');
  200. }
  201. DB::commit();
  202. $this->debugLog();
  203. return $this->successJson("删除操作员成功。", Url::absoluteWeb('user.user.index'));
  204. }
  205. /**
  206. * 获取当前登录用户信息
  207. */
  208. public function getAdminUserInfo()
  209. {
  210. //获取当前登录用户的账号
  211. $array = [];
  212. $array['uid'] = \YunShop::app()->uid;
  213. $array['uniacid'] = \YunShop::app()->uniacid;
  214. $array['acid'] = \YunShop::app()->acid;
  215. $array['username'] = \YunShop::app()->username;
  216. //获取当前登录用户的手机号
  217. $array['mobile'] = DB::table('yz_users_profile')->where('uid',$array['uid'])->value('mobile');
  218. return $this->successJson("获取成功", $array);
  219. }
  220. /**
  221. * 修改用户登录密码
  222. */
  223. public function resetPassword()
  224. {
  225. $old_password = request()->old_password;
  226. $new_pass = request()->new_pass;
  227. $username = request()->username;
  228. $userModel = User::where('uid',\YunShop::app()->uid)->first();
  229. if (!$userModel)
  230. {
  231. return $this->errorJson('用户不存在');
  232. }
  233. if (!Hash::check($old_password, $userModel->password))
  234. {
  235. return $this->errorJson('原密码错误');
  236. }
  237. //平台的验证统一使用 validatePassword方法
  238. $verifyPassword = validatePassword($new_pass);
  239. if($verifyPassword !== true){
  240. return $this->errorJson($verifyPassword);
  241. }
  242. //密码加密
  243. if (config('app.framework') == 'platform')
  244. {
  245. $new_pass = bcrypt($new_pass);
  246. } else {
  247. $new_pass = $this->password($old_pass, $userModel->salt);
  248. }
  249. $data = [];
  250. $data['password'] = $new_pass;
  251. $res = User::where('uid', $userModel->uid)->update($data);
  252. return $this->successJson("修改成功");
  253. }
  254. /**
  255. * 用户被删除BUG-log
  256. */
  257. private function debugLog()
  258. {
  259. $find = base_path() . '\storage\logs\user_admin_delete_log.log';
  260. if (!file_exists($find)) {
  261. fopen($find, 'a');
  262. }
  263. $array = [];
  264. $array['deleteid'] = request()->id;
  265. $array['uid'] = \YunShop::app()->uid;
  266. $array['uniacid'] = \YunShop::app()->uniacid;
  267. $array['acid'] = \YunShop::app()->acid;
  268. $array['username'] = \YunShop::app()->username;
  269. $array['siteurl'] = \YunShop::app()->siteurl;
  270. $array['time'] = date('Y-m-d H:i:s', time());
  271. $txt = "app\backend\modules\user\controllers\UserController.php\n";
  272. $txt .= json_encode($array, true) . "\n\n";
  273. file_put_contents($find, $txt, FILE_APPEND);
  274. \Log::debug("====用户被删除BUG-log===", $array);
  275. }
  276. /**
  277. * 附加的用户数据
  278. * @param string $data 需要储存的数据
  279. * @return string
  280. */
  281. private function addedUserData(array $data = [])
  282. {
  283. if (config('app.framework') == 'platform') {
  284. $data['lastvisit'] = time();
  285. $data['lastip'] = Utils::getClientIp();
  286. $data['joinip'] = Utils::getClientIp();
  287. $data['salt'] = randNum(8);
  288. } else {
  289. $data['joindate'] = $data['lastvisit'] = $data['starttime'] = time();
  290. $data['lastip'] = CLIENT_IP;
  291. $data['joinip'] = CLIENT_IP;
  292. $data['salt'] = $this->randNum(8);
  293. }
  294. return $data;
  295. }
  296. /**
  297. * 计算用户密码
  298. * @param string $passwordinput 输入字符串
  299. * @param string $salt 附加字符串
  300. * @return string
  301. */
  302. private function password($passwordinput, $salt)
  303. {
  304. $authkey = \YunShop::app()->config['setting']['authkey'];
  305. $passwordinput = "{$passwordinput}-{$salt}-{$authkey}";
  306. return sha1($passwordinput);
  307. }
  308. /**
  309. * 获取随机字符串
  310. * @param number $length 字符串长度
  311. * @param boolean $numeric 是否为纯数字
  312. * @return string
  313. */
  314. private function randNum($length, $numeric = FALSE)
  315. {
  316. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  317. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  318. if ($numeric) {
  319. $hash = '';
  320. } else {
  321. $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
  322. $length--;
  323. }
  324. $max = strlen($seed) - 1;
  325. for ($i = 0; $i < $length; $i++) {
  326. $hash .= $seed{mt_rand(0, $max)};
  327. }
  328. return $hash;
  329. }
  330. }