Profile.php 2.5 KB

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