Files
23cm/controllers/SiteController.php
2026-01-25 18:18:09 +08:00

317 lines
8.1 KiB
PHP

<?php
namespace app\controllers;
use app\models\Clue;
use app\models\Oauth;
use app\models\OauthAccount;
use app\models\OauthAccountLocal;
use app\models\User;
use app\models\UserAdvertiser;
use http\Client;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\web\UrlManager;
class SiteController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::class,
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'logout' => ['get'],
],
],
];
}
/**
* {@inheritdoc}
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function beforeAction($action)
{
parent::beforeAction($action);
if ($action->id == 'login') {
return true;
}
if (Yii::$app->user->isGuest) {
return $this->redirect('/login');
}
return true;
}
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex()
{
$this->layout = 'main_index';
if (Yii::$app->user->isGuest) {
return $this->redirect(['site/login']);
}
return $this->render('index');
}
/**
* Login action.
*
* @return Response|string
*/
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
$model->password = '';
return $this->render('login', [
'model' => $model,
]);
}
/**
* Logout action.
*
* @return Response
*/
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
/**
* Displays contact page.
*
* @return Response|string
*/
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
/**
* Displays about page.
*
* @return string
*/
public function actionAbout()
{
return $this->render('about');
}
public function actionConsole()
{
$this->layout = 'main_index';
return $this->render('console');
}
public function actionXiansuo()
{
$this->layout = 'main_index';
return $this->render('xiansuo');
}
public function actionUsers()
{
$this->layout = 'main_index';
return $this->render('users');
}
public function actionUsercreate()
{
$this->layout = 'main_index';
return $this->render('user/create');
}
public function actionUseredit()
{
$id = Yii::$app->request->get('id');
$this->layout = 'main_index';
$advertiser = UserAdvertiser::find()->where([
'user_id' => $id,
'is_delete' => 0
])->indexBy('advertiser_id')->asArray()->all() ?: [];
$allLocalAccount = OauthAccountLocal::find()->asArray()->all();
$advertiserList = [];
foreach ($allLocalAccount as $item) {
$advertiserList[] = [
'name' => $item['advertiser_name'],
'value' => $item['id'],
'selected' => isset($advertiser[$item['advertiser_id']])
];
}
return $this->render('user/edit', [
'advertiser' => $advertiserList,
'user_id' => $id
]);
}
public function actionUsereditpasswprd()
{
$id = Yii::$app->request->get('id');
$query = User::find()->where(['id' => $id])->asArray()->one();
$this->layout = 'main_index';
return $this->render('user/edit-password', [
'query' => $query
]);
}
public function actionOauth()
{
// http://j56ff926.natappfree.cc/?app_id=1852484891502756&auth_code=e7eb2c40cc7ebe38e359b283701e9406c3f1a382&material_auth_status=1&scope=%5B10000000%2C200000032%2C2%2C3%2C4%2C5%2C300000006%2C300000040%2C300000041%2C130%2C14%2C112%2C300000052%2C110%2C120%2C122%2C123%2C124%2C300000029%2C300000000%2C100000005%5D&state=your_custom_params&uid=4121395460312552
$request = Yii::$app->request->get();
$appId = $request['app_id'];
$authCode = $request['auth_code'];
$materialAuthStatus = $request['material_auth_status'];
$scope = $request['scope'];
$uid = $request['uid'];
$oauth = Oauth::find()->where(['uid' => $uid])->one();
if (!$oauth) {
$oauth = new Oauth();
}
$oauth->app_id = $appId;
$oauth->auth_code = $authCode;
$oauth->material_auth_status = $materialAuthStatus;
$oauth->scope = $scope;
$oauth->uid = $uid;
$oauth->save();
$curl = new \app\common\CurlApp();
$curl->setMethod();
$curl->setUrl('https://ad.oceanengine.com/open_api/oauth2/access_token/');
$curl->setPostData([
"app_id" => Yii::$app->params['app_id'],
"secret" => Yii::$app->params['secret'],
"auth_code" => $authCode
]);
$res = json_decode($curl->exec(), true);
if ($res['code'] != '0') {
throw new \Exception($res['message']);
}
$oauth->advertiser_ids = json_encode($res['data']['advertiser_ids']);
$oauth->access_token = $res['data']['access_token'];
$oauth->refresh_token = $res['data']['refresh_token'];
$oauth->save();
echo '授权成功, 请关闭此页面';die;
}
/**
*
*/
public function actionOauthmanage()
{
// TODO: 管理员验证
$this->layout = 'main_index';
return $this->render('oauth-manage');
}
/**
*
* @url oauthmanageconfig
* @return string
*/
public function actionOauthmanageconfig()
{
// TODO: 管理员验证
$this->layout = 'main_index';
$adminUid = Yii::$app->request->get('uid');
$arr = [];
$accounts = OauthAccount::find()->where(['admin_uid' => $adminUid, 'is_delete' => 0])->all();
foreach ($accounts as $account) {
$arr[$account['account_name']] = [
'id' => $account['account_id'],
'items' => OauthAccountLocal::find()->where(['account_id' => $account['account_id'], 'is_delete' => 0])->asArray()->all()
];
}
return $this->render('oauth-manage-config', [
'arr' => $arr
]);
}
public function actionGenjin()
{
$clueId = Yii::$app->request->get('clue_id');
$note = Clue::find()->where(['clue_id' => $clueId])->one();
$this->layout = 'main_index';
return $this->render('genjin', [
'clueId' => $clueId,
'note' => $note->note,
'name' => $note->name,
'covert_status' => $note->convert_status
]);
}
public function actionPrivate()
{
$this->layout = 'main_index';
return $this->render('xiansuo_private');
}
}