Profile.php 2.4 KB

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