Profile.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\admin\controller\general;
  3. use app\admin\model\Admin;
  4. use app\common\controller\Backend;
  5. use fast\Random;
  6. use think\Session;
  7. use think\Validate;
  8. /**
  9. * 个人配置
  10. *
  11. * @icon fa fa-user
  12. */
  13. class Profile extends Backend
  14. {
  15. protected $searchFields = 'id,title';
  16. /**
  17. * 查看
  18. */
  19. public function index()
  20. {
  21. //设置过滤方法
  22. $this->request->filter(['strip_tags', 'trim']);
  23. if ($this->request->isAjax()) {
  24. $this->model = model('AdminLog');
  25. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  26. $list = $this->model
  27. ->where($where)
  28. ->where('admin_id', $this->auth->id)
  29. ->order($sort, $order)
  30. ->paginate($limit);
  31. $result = array("total" => $list->total(), "rows" => $list->items());
  32. return json($result);
  33. }
  34. return $this->view->fetch();
  35. }
  36. /**
  37. * 更新个人信息
  38. */
  39. public function update()
  40. {
  41. if ($this->request->isPost()) {
  42. $this->token();
  43. $params = $this->request->post("row/a");
  44. $params = array_filter(array_intersect_key(
  45. $params,
  46. array_flip(array('email', 'nickname', 'password', 'avatar'))
  47. ));
  48. unset($v);
  49. if (!Validate::is($params['email'], "email")) {
  50. $this->error(__("Please input correct email"));
  51. }
  52. if (isset($params['password'])) {
  53. if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  54. $this->error(__("Please input correct password"));
  55. }
  56. $params['salt'] = Random::alnum();
  57. $params['password'] = md5(md5($params['password']) . $params['salt']);
  58. }
  59. $exist = Admin::where('email', $params['email'])->where('id', '<>', $this->auth->id)->find();
  60. if ($exist) {
  61. $this->error(__("Email already exists"));
  62. }
  63. if ($params) {
  64. $admin = Admin::get($this->auth->id);
  65. $admin->save($params);
  66. //因为个人资料面板读取的Session显示,修改自己资料后同时更新Session
  67. Session::set("admin", $admin->toArray());
  68. Session::set("admin.safecode", $this->auth->getEncryptSafecode($admin));
  69. $this->success();
  70. }
  71. $this->error();
  72. }
  73. return;
  74. }
  75. }