/** * 获取个应用系统登录接口地址 * * @param bool $is_register true 表示注册 * @return array|null * @throws \yii\base\Exception */ public function getCookieUrl($is_register = false) { $cookieUrl = null; $appId = null; $info = null; $userModel = new UserBaseInfo(); $userInfo = $userModel->getUserInfoCache(); $sso = SsoSettingInfo::findAll(); if ($sso) { foreach ($sso as $k => $val) { if (isset($val['log_out']['sign_in']) && $val['log_out']['sign_in']) { $cookieUrl[] = $val['log_out']['sign_in']; $appId[] = $val['app_id']; } } } if (isset($userInfo['id']) && $userInfo['id'] && $cookieUrl) { $user = $userModel->getUserById($userInfo['id']); $info['openid'] = $user['open_id']; $info['mobile'] = $user['mobile']; $info['email'] = $user['email']; if ($is_register) { $info['is_register'] = $is_register; $info['salt'] = $user['password_salt']; $info['password'] = $user['password']; } $info = json_encode($info); foreach ($cookieUrl as $key => &$url) { $appDir = $appId[$key]; $encrypt = AsymmetryCrypt::opensslPublicEncrypt($info, dirname(Yii::$app->getBasePath()) . '/common/rsa_key/' . $appDir . '/rsa_public_key.pem'); // TODO:: 为解决 +/ 丢失 / bug,临时解决方案 $encrypt = str_replace('+/', '+_', $encrypt); $encrypt = rawurlencode($encrypt); $bool = filter_var($url, FILTER_VALIDATE_URL); if ($bool === false) { unset($cookieUrl[$key]); continue; } $path = parse_url($url); if (!isset($path['query'])) { $url .= '?c=' . $encrypt; continue; } $query = queryToArray($path['query']); $query['c'] = $encrypt; $query_params = arrayToQuery($query); $url = $path['scheme'] . '://' . $path['host'] . '?' . $query_params; } shuffle($cookieUrl); return $cookieUrl; } else { return null; } }
/** * 获取创业家下所有应用系统设置COOKIE接口地址 */ public function actionAppCookie() { // 获取JS回调函数 $callback = Yii::$app->getRequest()->get('callback'); $is_register = Yii::$app->getRequest()->get('is_register') ?: 0; try { $info = null; $userModel = new UserBaseInfo(); $userInfo = $userModel->getUserInfoCache(); if (isset($userInfo['id']) && $userInfo['id']) { $user = $userModel->getUserById($userInfo['id']); $info['openid'] = $user['open_id']; $info['mobile'] = $user['mobile']; $info['email'] = $user['email']; $info['password_salt'] = $user['password_salt']; $info['password'] = $user['password']; $info['username'] = $user['username']; if ($is_register) { $info['is_register'] = $is_register; $info['salt'] = $user['password_salt']; $info['password'] = $user['password']; } $json = json_encode($info); // 记录日志 $logger = FileLogger::getInstance('sso_' . date('Ymd') . '.log'); $encrypt = AsymmetryCrypt::opensslPublicEncrypt($json, dirname(Yii::$app->getBasePath()) . '/common/rsa_public_key.pem'); // $logger->writeOne($json . '====' . $encrypt, Logger::LEVEL_INFO,'SSO-IHEIMA'); // TODO:: +/ 替换成 +_ $encrypt = str_replace('+/', '+_', $encrypt); $encrypt = rawurlencode($encrypt); $logger->writeOne($json . '====' . $encrypt, Logger::LEVEL_INFO, 'SSO-IHEIMA'); $thinksns = AsymmetryCrypt::opensslPublicEncrypt($json, dirname(Yii::$app->getBasePath()) . '/common/rsa_key/100003/rsa_public_key.pem'); $thinksns = rawurlencode($thinksns); // TODO:: 获取应用系统COOKIE接口地址 $appCookie = ['code' => 0, 'sso' => [Url::toRoute(['sso/index'], true), 'http://app.iheima.net/?app=ihminterface&controller=index&action=ihmlogin&c=' . $encrypt, 'http://dev-group.chuangyejia.com/index.php?app=public&mod=Passport&act=grouplogin&c=' . $thinksns, 'http://test-group.chuangyejia.com/index.php?app=public&mod=Passport&act=grouplogin&c=' . $thinksns, 'http://group.chuangyejia.com/index.php?app=public&mod=Passport&act=grouplogin&c=' . $thinksns, 'http://cyj.zhiyicx.com/index.php?app=public&mod=Passport&act=grouplogin&c=' . $thinksns, 'http://group-t.chuangyejia.com/index.php?app=public&mod=Passport&act=grouplogin&c=' . $thinksns], 'msg' => []]; echo $callback . '(' . json_encode($appCookie) . ')'; } else { echo $callback . '(' . json_encode(['code' => 1, 'msg' => '用户未登录']) . ')'; } } catch (Exception $ex) { // TODO:: 异常处理 echo $callback . '(' . json_encode(['code' => 2, 'msg' => '系统繁忙,请稍后重试']) . ')'; } }
/** * 修改个人信息 */ public function actionModify() { $this->layout = false; if (!$this->isLogin) { $this->redirect(Url::toRoute('user/login')); return false; } $mUser = new UserBaseInfo(); //获取登录用户信息 //$user = $mUser::getLoginUser(); $params = Yii::$app->request->post(); $params = array_map('trim', $params); $mAvatar = new Avatar(); $ocutpos = json_decode($params['ocutpos'], true); $options = ['pointer' => [$ocutpos['x'], $ocutpos['y']], 'size' => $ocutpos['w']]; try { $avatar = $mAvatar->upload('avatarfile', $options); if (!empty($avatar)) { //存储图片 $mPicture = new UserImage(); $datas = ['uid' => $params['id'], 'url' => $avatar, 'type' => UserImage::USER_AVATAR, 'status' => UserImage::STATUS_USABLE]; $picture = $mPicture->store($datas); } else { //将失败的信息记录到日志 FileLogger::getInstance(date('Ymd') . '_uc_profile.log')->writeOne($params['oavatar'], Logger::LEVEL_ERROR, 'image'); } } catch (\Exception $e) { // throw new \Exception('上传图片失败,请重试!!!'); } $session = Yii::$app->session; $cookies = Yii::$app->getResponse()->cookies; //session更新 $tmp = $mUser->getUserInfoCache(); if ($avatar) { $data['avatar'] = $avatar; if ($tmp) { $tmp['avatar'] = $avatar; } $cookie = new Cookie(['name' => 'avatar', 'value' => $avatar]); $cookies->add($cookie); } $tmp['name'] = $params['username']; $session[UserBaseInfo::SESSION_KEY_USER] = $tmp; //cookie更新 $cookie = new Cookie(['name' => 'avatar', 'value' => $avatar]); $cookie = new Cookie(['name' => 'username', 'value' => $params['username']]); $cookies->add($cookie); //4-20个字符,可全部由字母组成,或数字、字母、“_”、“-”任意两种以上组合 $preg = '/^((?!^\\d+$)(?!^\\-+$)(?!^\\_+$)[\\x{4e00}-\\x{9fa5}a-zA-Z0-9\\_\\-]{2,20})$/u'; $res = preg_match($preg, $params['username']); if (!$res) { echo "<script>alert('4-20个字符,可全部由字母组成,或数字、字母、“_”、“-”任意两种以上组合');history.go(-1);</script>"; return false; } $data['id'] = $params['id']; if (!empty($params['username'])) { $data['username'] = strip_tags($params['username']); } else { echo "<script>alert('昵称不能为空');history.go(-1);</script>"; return false; } $relname = preg_match($preg, $params['realname']); if (!$relname) { echo "<script>alert('4-20个字符,可全部由字母组成,或数字、字母、“_”、“-”任意两种以上组合');history.go(-1);</script>"; return false; } if (!empty($params['realname'])) { $data['realname'] = strip_tags($params['realname']); } else { echo "<script>alert('昵称不能为空');history.go(-1);</script>"; return false; } $data['gender'] = intval($params['gender']); $genderArr = [UserBaseInfo::GENDER_UNKNOWN, UserBaseInfo::GENDER_MALE, UserBaseInfo::GENDER_FEMALE]; if (!in_array($data['gender'], $genderArr)) { echo "<script>alert('性别非法修改');history.go(-1);</script>"; return false; } if (!is_numeric($params['year'])) { echo "<script>alert('年份非法修改');history.go(-1);</script>"; return false; } $month = str_pad(intval($params['month']), 2, "0", STR_PAD_LEFT); $day = str_pad(intval($params['day']), 2, "0", STR_PAD_LEFT); if (checkdate($month, $day, $params['year'])) { if ($params['year'] <= date('Y', time()) && $params['year'] >= 1950) { $data['birthday'] = $params['year'] . '-' . $month . '-' . $day; } else { echo "<script>alert('日期不在规定时间之内');history.go(-1);</script>"; return false; } } else { echo "<script>alert('非法日期');history.go(-1);</script>"; return false; } $params['province'] = intval($params['province']); $params['city'] = intval($params['city']); if (!$params['province']) { echo "<script>alert('请选择省');history.go(-1);</script>"; return false; } if (!$params['city']) { echo "<script>alert('请选择市');history.go(-1);</script>"; return false; } $params['province'] = intval($params['province']); $params['city'] = intval($params['city']); $region = District::getById($params['city'], true); if (!$region) { echo "<script>alert('城市信息不存在');history.go(-1);</script>"; return false; } $data['city'] = $params['city']; //用户行业配置 $industry = Yii::$app->util->loadConfig('user-industry', '/platform_i/config/'); $data['industry'] = $params['industry']; if (!isset($industry[$data['industry']])) { echo "<script>alert('行业信息不存在');history.go(-1);</script>"; return false; } $comStrLength = mb_strlen($params['company'], 'utf8'); $posStrLength = mb_strlen($params['position'], 'utf8'); if ($comStrLength < 0 || $comStrLength > 50) { echo "<script>alert('公司名称填写仅限于50个字以内');history.go(-1);</script>"; return false; } if ($posStrLength < 0 || $posStrLength > 30) { echo "<script>alert('职位填写仅限于30个字以内');history.go(-1);</script>"; return false; } $data['company'] = strip_tags($params['company']); $data['position'] = strip_tags($params['position']); $data['gender'] = $params['gender']; $this->syncProfile($data); //根据id获取用户修改前的信息 $oriUser = $mUser->getUserById($tmp['id'], UserBaseInfo::USER_NORMAL_STATUS, true); array_pop($oriUser); if ($mUser->updateUserById($data)) { //将成功信息记录到日志 $this->profileLog($oriUser, $data); echo "<script type='text/javascript'>window.location.href='" . Url::toRoute('user/profile') . "'</script>"; } else { //将失败的信息记录到日志 FileLogger::getInstance(date('Ymd') . '_uc_profile.log')->writeOne($params, Logger::LEVEL_ERROR, 'profile'); echo "<script type='text/javascript'>alert('修改失败');history.go(-1);</script>"; return false; } }