Profile.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 个人资料
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller\general;
  15. use app\admin\model\Adminlog as AdminlogModel;
  16. use app\admin\model\AdminUser;
  17. use app\common\controller\Backend;
  18. use think\exception\ValidateException;
  19. use think\facade\Session;
  20. use think\facade\Validate;
  21. class Profile extends Backend
  22. {
  23. public function index()
  24. {
  25. if ($this->request->isAjax()) {
  26. $this->modelClass = new AdminlogModel;
  27. [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
  28. $count = $this->modelClass
  29. ->where($where)
  30. ->where('admin_id', (int) $this->auth->id)
  31. ->order($sort, $order)
  32. ->count();
  33. $list = $this->modelClass
  34. ->where($where)
  35. ->where('admin_id', (int) $this->auth->id)
  36. ->order($sort, $order)
  37. ->page($page, $limit)
  38. ->select();
  39. $result = ["code" => 0, 'count' => $count, 'data' => $list];
  40. return json($result);
  41. }
  42. return $this->fetch();
  43. }
  44. //更新个人信息
  45. public function update()
  46. {
  47. if ($this->request->isPost()) {
  48. $this->token();
  49. $params = $this->request->param("row/a");
  50. $params['id'] = $this->auth->id;
  51. $rule = [
  52. 'email|邮箱' => 'require|email|unique:admin',
  53. 'mobile|手机' => 'mobile|unique:admin',
  54. ];
  55. try {
  56. $this->validate($params, $rule);
  57. } catch (ValidateException $e) {
  58. $this->error($e->getMessage());
  59. }
  60. if (isset($params['password']) && $params['password']) {
  61. if (!Validate::regex($params['password'], "/^[\S]{6,16}$/")) {
  62. $this->error('密码长度必须在6-16位之间,不能包含空格');
  63. }
  64. $data = encrypt_password($params['password']);
  65. $params['encrypt'] = $data['encrypt'];
  66. $params['password'] = $data['password'];
  67. } else {
  68. unset($params['password'], $params['encrypt']);
  69. }
  70. if ($params) {
  71. $admin = AdminUser::find($this->auth->id);
  72. $admin->save($params);
  73. //因为个人资料面板读取的Session显示,修改自己资料后同时更新Session
  74. Session::set("admin", $admin->toArray());
  75. Session::set("admin.safecode", $this->auth->getEncryptSafecode($admin));
  76. $this->success('修改成功!');
  77. }
  78. $this->error();
  79. }
  80. return;
  81. }
  82. }