Profile.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. return $this->view->fetch();
  22. }
  23. /**
  24. * 更新个人信息
  25. */
  26. public function update()
  27. {
  28. if ($this->request->isPost()) {
  29. $this->token();
  30. $params = $this->request->post("row/a");
  31. $params = array_filter(array_intersect_key(
  32. $params,
  33. array_flip(array('email', 'nickname', 'password', 'avatar', 'mobile'))
  34. ), function ($v, $k) {
  35. return $k === 'mobile' || $v !== '' && $v !== null;
  36. }, ARRAY_FILTER_USE_BOTH);
  37. if (isset($params['mobile'])) {
  38. $params['mobile'] = preg_replace('/\s+/', '', trim((string)$params['mobile']));
  39. if ($params['mobile'] !== '' && !preg_match('/^1[3-9]\d{9}$/', $params['mobile'])) {
  40. $this->error('请输入正确的11位手机号');
  41. }
  42. if ($params['mobile'] !== '') {
  43. $mobileExist = Admin::where('mobile', $params['mobile'])->where('id', '<>', $this->auth->id)->find();
  44. if ($mobileExist) {
  45. $this->error('该手机号已被其他账号使用');
  46. }
  47. }
  48. }
  49. unset($v);
  50. if (!Validate::is($params['email'], "email")) {
  51. $this->error(__("Please input correct email"));
  52. }
  53. if (isset($params['password'])) {
  54. if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  55. $this->error(__("Please input correct password"));
  56. }
  57. $params['salt'] = Random::alnum();
  58. $params['password'] = md5(md5($params['password']) . $params['salt']);
  59. }
  60. $exist = Admin::where('email', $params['email'])->where('id', '<>', $this->auth->id)->find();
  61. if ($exist) {
  62. $this->error(__("Email already exists"));
  63. }
  64. if ($params) {
  65. $admin = Admin::get($this->auth->id);
  66. $admin->save($params);
  67. $admin = Admin::get($this->auth->id);
  68. if (!$admin) {
  69. $this->error(__('No Results were found'));
  70. }
  71. // 与登录一致:刷新 Session 并同步 safecode,避免修改资料后被判定未登录
  72. Session::set('admin', $admin->toArray());
  73. Session::set('admin.safecode', $this->auth->getEncryptSafecode($admin));
  74. $this->success();
  75. }
  76. $this->error();
  77. }
  78. return;
  79. }
  80. }