| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace app\admin\controller\general;
- use app\admin\model\Admin;
- use app\common\controller\Backend;
- use fast\Random;
- use think\Session;
- use think\Validate;
- /**
- * 个人配置
- *
- * @icon fa fa-user
- */
- class Profile extends Backend
- {
- protected $searchFields = 'id,title';
- /**
- * 查看
- */
- public function index()
- {
- return $this->view->fetch();
- }
- /**
- * 更新个人信息
- */
- public function update()
- {
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->request->post("row/a");
- $params = array_filter(array_intersect_key(
- $params,
- array_flip(array('email', 'nickname', 'password', 'avatar', 'mobile'))
- ), function ($v, $k) {
- return $k === 'mobile' || $v !== '' && $v !== null;
- }, ARRAY_FILTER_USE_BOTH);
- if (isset($params['mobile'])) {
- $params['mobile'] = preg_replace('/\s+/', '', trim((string)$params['mobile']));
- if ($params['mobile'] !== '' && !preg_match('/^1[3-9]\d{9}$/', $params['mobile'])) {
- $this->error('请输入正确的11位手机号');
- }
- if ($params['mobile'] !== '') {
- $mobileExist = Admin::where('mobile', $params['mobile'])->where('id', '<>', $this->auth->id)->find();
- if ($mobileExist) {
- $this->error('该手机号已被其他账号使用');
- }
- }
- }
- unset($v);
- if (!Validate::is($params['email'], "email")) {
- $this->error(__("Please input correct email"));
- }
- if (isset($params['password'])) {
- if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
- $this->error(__("Please input correct password"));
- }
- $params['salt'] = Random::alnum();
- $params['password'] = md5(md5($params['password']) . $params['salt']);
- }
- $exist = Admin::where('email', $params['email'])->where('id', '<>', $this->auth->id)->find();
- if ($exist) {
- $this->error(__("Email already exists"));
- }
- if ($params) {
- $admin = Admin::get($this->auth->id);
- $admin->save($params);
- $admin = Admin::get($this->auth->id);
- if (!$admin) {
- $this->error(__('No Results were found'));
- }
- // 与登录一致:刷新 Session 并同步 safecode,避免修改资料后被判定未登录
- Session::set('admin', $admin->toArray());
- Session::set('admin.safecode', $this->auth->getEncryptSafecode($admin));
- $this->success();
- }
- $this->error();
- }
- return;
- }
- }
|