Profile.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. $this->success();
  69. }
  70. $this->error();
  71. }
  72. return;
  73. }
  74. }