Profile.php 2.6 KB

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