Пример #1
0
 public function ajax_profile()
 {
     if (!$this->input->is_ajax_request()) {
         return;
     }
     header('Content-Type:application/json; charset=utf-8');
     $fields = array('nickname', 'email', 'phone', 'qq', 'wechat');
     $updates = array();
     foreach ($fields as $field) {
         if (!isset($_POST[$field])) {
             echo json_encode(array('ack' => true, 'msg' => '缺少' . $field));
             return;
         } elseif ($_POST[$field] == $_SESSION[$field]) {
             // 如果提交数据无修改
             unset($_POST[$field]);
             unset($fields[$field]);
         } else {
             $updates[$field] = $_POST[$field];
         }
     }
     if (empty($updates)) {
         echo json_encode(array('ack' => false, 'msg' => '个人资料无修改'));
         return;
     }
     // 验证
     $this->load->library('form_validation');
     $this->form_validation->set_message('required', '必须填写{field}');
     $this->form_validation->set_message('min_length', '{field}至少{param}个字符');
     $this->form_validation->set_message('max_length', '{field}至多{param}个字符');
     $this->form_validation->set_message('valid_email', '{field}无效');
     $this->form_validation->set_message('is_unique', '{field}已存在');
     $this->form_validation->set_error_delimiters('', '');
     // 为了is_unique载入db
     $this->load->database();
     isset($_POST['nickname']) && $this->form_validation->set_rules('nickname', '昵称', 'trim|min_length[2]|max_length[20]|is_unique[admin_user.username]|is_unique[admin_user.nickname]');
     isset($_POST['email']) && $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
     isset($_POST['phone']) && $this->form_validation->set_rules('phone', '手机', array('required', 'is_natural', 'exact_length[11]', 'is_unique[admin_user.phone]', array('is_phone', function ($str) {
         if (!is_phone($str)) {
             $this->form_validation->set_message('is_phone', '{field}无效');
             return false;
         }
         return true;
     })));
     isset($_POST['qq']) && $this->form_validation->set_rules('qq', 'QQ', array('is_qq', function ($str) {
         if (empty($str)) {
             $updates['qq'] = 0;
         } elseif (!is_qq($str)) {
             $this->form_validation->set_message('is_qq', '{field}无效');
             return false;
         }
         return true;
     }));
     isset($_POST['wechat']) && $this->form_validation->set_rules('wechat', '微信', array('is_wechat', function ($str) {
         if (!is_wechat($str)) {
             $this->form_validation->set_message('is_wechat', '{field}无效');
             return false;
         }
         return true;
     }));
     if (!$this->form_validation->run()) {
         echo json_encode(array('ack' => false, 'msg' => $this->form_validation->error_string()));
         return;
     }
     $this->load->model('user_model');
     $this->user_model->update($_SESSION['uid'], $updates);
     // 修改了个人资料,需要更新session
     $this->user_model->update_session();
     echo json_encode(array('ack' => true, 'msg' => '修改成功'));
     return;
 }
Пример #2
0
 private function _validate_userinfo(&$data)
 {
     if (isset($data['username'])) {
         $data['username'] = strtolower(trim($data['username']));
         if (!is_username($data['username'])) {
             return '用户名不合法';
         }
     }
     if (isset($data['email'])) {
         $data['email'] = strtolower(trim($data['email']));
         if (!$data['email']) {
             return 'Email不合法';
         }
     }
     if (isset($data['phone'])) {
         $data['phone'] = (int) $data['phone'];
         if (!is_phone($data['phone'])) {
             return '手机号不合法';
         }
     }
     if (isset($data['password'])) {
         if (!is_password($data['password'])) {
             return '密码不合法';
         }
     }
     if (isset($data['qq'])) {
         $data['qq'] = (int) $data['qq'];
         !is_qq($data['qq']) && ($data['qq'] = 0);
     }
     isset($data['wechat']) && !is_wechat($data['wechat']) && ($data['wechat'] = '');
     return true;
 }