Commit 74f824c2 authored by shajiaiming's avatar shajiaiming

探索后端接口

parent f229d7fd
...@@ -12,6 +12,10 @@ use Yii; ...@@ -12,6 +12,10 @@ use Yii;
class CoinBannerItem extends BaseActiveRecord class CoinBannerItem extends BaseActiveRecord
{ {
//定义场景
const SCENARIOS_CREATE = 'create';
const SCENARIOS_UPDATE = 'update';
public static function getDb() public static function getDb()
{ {
return Yii::$app->get('p_sources'); return Yii::$app->get('p_sources');
...@@ -22,6 +26,25 @@ class CoinBannerItem extends BaseActiveRecord ...@@ -22,6 +26,25 @@ class CoinBannerItem extends BaseActiveRecord
return '{{coin_banner_item}}'; return '{{coin_banner_item}}';
} }
public function rules()
{
return [
[['image_url', 'platform_id'], 'required'],
[['banner_url', 'title'], 'safe'],
[['banner_url', 'image_url'], 'url']
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['image_url', 'banner_url', 'title', 'platform_id'],
self:: SCENARIOS_UPDATE => ['id', 'image_url', 'banner_url', 'title', 'platform_id'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
public static function getItems($condition = []) public static function getItems($condition = [])
{ {
return self::find()->joinWith('platform')->where($condition)->asArray()->all(); return self::find()->joinWith('platform')->where($condition)->asArray()->all();
......
...@@ -31,6 +31,8 @@ class ExploreApp extends BaseActiveRecord ...@@ -31,6 +31,8 @@ class ExploreApp extends BaseActiveRecord
{ {
return [ return [
[['name', 'icon', 'app_url', 'slogan', 'type', 'sort', 'status', 'platform_id', 'app_category_id'], 'required'], [['name', 'icon', 'app_url', 'slogan', 'type', 'sort', 'status', 'platform_id', 'app_category_id'], 'required'],
[['type', 'sort', 'status', 'platform_id', 'app_category_id'], 'integer'],
[['icon'], 'url']
]; ];
} }
...@@ -38,6 +40,7 @@ class ExploreApp extends BaseActiveRecord ...@@ -38,6 +40,7 @@ class ExploreApp extends BaseActiveRecord
{ {
$scenarios = [ $scenarios = [
self:: SCENARIOS_CREATE => ['name', 'icon', 'app_url', 'slogan', 'type', 'sort', 'status', 'platform_id', 'app_category_id'], self:: SCENARIOS_CREATE => ['name', 'icon', 'app_url', 'slogan', 'type', 'sort', 'status', 'platform_id', 'app_category_id'],
self:: SCENARIOS_UPDATE => ['id', 'name', 'icon', 'app_url', 'slogan', 'type', 'sort', 'status', 'platform_id', 'app_category_id'],
]; ];
return array_merge(parent:: scenarios(), $scenarios); return array_merge(parent:: scenarios(), $scenarios);
} }
......
...@@ -79,13 +79,20 @@ class ExploreAppController extends BaseController ...@@ -79,13 +79,20 @@ class ExploreAppController extends BaseController
if (Yii::$app->request->isGet) { if (Yii::$app->request->isGet) {
$id = Yii::$app->request->get('id'); $id = Yii::$app->request->get('id');
$data = ExploreAppCategory::find()->select('id, name, sort, limit, style')->where(['platform_id' => $platform_id, 'id' => $id])->asArray()->one(); $data = ExploreAppCategory::find()->select('id, name, sort, limit, style')->where(['platform_id' => $platform_id, 'id' => $id])->asArray()->one();
goto doEnd;
} }
if (Yii::$app->request->isPut) { if (Yii::$app->request->isPut) {
$id = Yii::$app->request->get('id'); $params = Yii::$app->request->post();
$id = isset($params['id']) ? $params['id'] : null;
if (false == $id) {
$msg = '参数错误';
$code = -1;
goto doEnd;
}
$model = ExploreAppCategory::findOne($id); $model = ExploreAppCategory::findOne($id);
$model->setScenario(ExploreAppCategory::SCENARIOS_UPDATE); $model->setScenario(ExploreAppCategory::SCENARIOS_UPDATE);
$params = Yii::$app->request->post(); unset($params['id']);
$params['platform_id'] = $platform_id; $params['platform_id'] = $platform_id;
$lang = [ $lang = [
'zh-CN', 'zh-CN',
...@@ -116,16 +123,21 @@ class ExploreAppController extends BaseController ...@@ -116,16 +123,21 @@ class ExploreAppController extends BaseController
$code = 0; $code = 0;
$data = null; $data = null;
$platform_id = Yii::$app->request->getPlatformId(); $platform_id = Yii::$app->request->getPlatformId();
$id = Yii::$app->request->get('id');
if (false == $id) {
$msg = '参数错误';
$code = -1;
goto doEnd;
}
if (Yii::$app->request->isDelete) { if (Yii::$app->request->isDelete) {
$id = Yii::$app->request->get('id');
$model_app = ExploreApp::find()->where(['app_category_id' => (int)$id])->asArray()->all(); $model_app = ExploreApp::find()->where(['app_category_id' => (int)$id])->asArray()->all();
if (false != $model_app){ if (false != $model_app) {
$msg = '请先删除该类别下的应用'; $msg = '请先删除该类别下的应用';
$code = -1; $code = -1;
goto doEnd; goto doEnd;
} }
$model = ExploreAppCategory::find()->where(['id' => $id, 'platform_id' => $platform_id])->one(); $model = ExploreAppCategory::find()->where(['id' => $id, 'platform_id' => $platform_id])->one();
if (!$model->delete()){ if (!$model->delete()) {
$msg = '删除失败'; $msg = '删除失败';
$code = -1; $code = -1;
goto doEnd; goto doEnd;
...@@ -174,7 +186,7 @@ class ExploreAppController extends BaseController ...@@ -174,7 +186,7 @@ class ExploreAppController extends BaseController
goto doEnd; goto doEnd;
} }
if (Yii::$app->request->isPost){ if (Yii::$app->request->isPost) {
$model = new ExploreApp(); $model = new ExploreApp();
$model->setScenario(ExploreApp::SCENARIOS_CREATE); $model->setScenario(ExploreApp::SCENARIOS_CREATE);
$params = Yii::$app->request->post(); $params = Yii::$app->request->post();
...@@ -203,4 +215,77 @@ class ExploreAppController extends BaseController ...@@ -203,4 +215,77 @@ class ExploreAppController extends BaseController
doEnd : doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data]; return ['code' => $code, 'msg' => $msg, 'data' => $data];
} }
public function actionAppUpdate()
{
$msg = 'ok';
$code = 0;
$data = null;
$platform_id = Yii::$app->request->getPlatformId();
if (Yii::$app->request->isGet) {
$id = Yii::$app->request->get('id');
$data = ExploreApp::find()->select('id, name, icon, app_url, slogan, type, sort, status')->where(['platform_id' => $platform_id, 'id' => $id])->asArray()->one();
goto doEnd;
}
if (Yii::$app->request->isPut) {
$params = Yii::$app->request->post();
$id = isset($params['id']) ? $params['id'] : null;
if (false == $id) {
$msg = '参数错误';
$code = -1;
goto doEnd;
}
$model = ExploreApp::findOne($id);
$model->setScenario(ExploreApp::SCENARIOS_UPDATE);
unset($params['id']);
$params['platform_id'] = $platform_id;
$lang = [
'zh-CN',
'en-US',
'ja'
];
$name_arr = $params['name'];
$name = [];
foreach ($name_arr as $key => $val) {
$name[$lang[$key]] = $val;
}
unset($params['name']);
$params['name'] = $name;
if ($model->load($params, '') && $model->update()) {
goto doEnd;
}
$msg = $model->errors;
$code = -1;
goto doEnd;
}
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public function actionAppRemove()
{
$msg = 'ok';
$code = 0;
$data = null;
$platform_id = Yii::$app->request->getPlatformId();
$id = Yii::$app->request->get('id');
if (false == $id) {
$msg = '参数错误';
$code = -1;
goto doEnd;
}
if (Yii::$app->request->isDelete) {
$model = ExploreApp::find()->where(['id' => $id, 'platform_id' => $platform_id])->one();
if (false == $model || !$model->delete()) {
$msg = '删除失败';
$code = -1;
goto doEnd;
}
}
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
} }
\ No newline at end of file
...@@ -13,30 +13,100 @@ class ExploreBannerController extends BaseController ...@@ -13,30 +13,100 @@ class ExploreBannerController extends BaseController
{ {
$msg = 'ok'; $msg = 'ok';
$code = 0; $code = 0;
$data = null;
$platform_id = Yii::$app->request->getPlatformId(); $platform_id = Yii::$app->request->getPlatformId();
$page = Yii::$app->request->get('page', 1); $page = Yii::$app->request->get('page', 1);
if (false == $platform_id) { if (Yii::$app->request->isGet) {
$query = CoinBannerItem::find()
->select('id, banner_url, image_url, title')
->where(['platform_id' => $platform_id])
->asArray();
$banner_model = $query->offset(($page - 1) * 10)->limit(10)->asArray()->all();
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '10']);
$data = [
'list' => $banner_model,
'page' => [
'pageCount' => $pages->pageCount,
'pageSize' => 10,
'currentPage' => $page,
]
];
goto doEnd;
}
if (Yii::$app->request->isPost) {
$model = new CoinBannerItem();
$model->setScenario(CoinBannerItem::SCENARIOS_CREATE);
$params = Yii::$app->request->post();
$params['platform_id'] = $platform_id;
if ($model->load($params, '') && $model->save()) {
goto doEnd;
}
$msg = $model->errors;
$code = -1;
goto doEnd;
}
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public function actionUpdate()
{
$msg = 'ok';
$code = 0;
$data = null;
$platform_id = Yii::$app->request->getPlatformId();
if (Yii::$app->request->isGet) {
$id = Yii::$app->request->get('id');
$data = CoinBannerItem::find()->select('id, banner_url, image_url, title')->where(['platform_id' => $platform_id, 'id' => $id])->asArray()->one();
goto doEnd;
}
if (Yii::$app->request->isPut) {
$params = Yii::$app->request->post();
$id = isset($params['id']) ? $params['id'] : null;
if (false == $id) {
$msg = '参数错误';
$code = -1;
goto doEnd;
}
$model = CoinBannerItem::findOne($id);
$model->setScenario(CoinBannerItem::SCENARIOS_UPDATE);
unset($params['id']);
if ($model->load($params, '') && $model->save()) {
goto doEnd;
}
$msg = $model->errors;
$code = -1;
goto doEnd;
}
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public function actionRemove()
{
$msg = 'ok';
$code = 0;
$data = null;
$platform_id = Yii::$app->request->getPlatformId();
$id = Yii::$app->request->get('id');
if (false == $id) {
$msg = '参数错误'; $msg = '参数错误';
$code = -1; $code = -1;
$data = null;
goto doEnd; goto doEnd;
} }
if (Yii::$app->request->isDelete) {
$model = CoinBannerItem::find()->where(['id' => $id, 'platform_id' => $platform_id])->one();
if (false == $model || !$model->delete()) {
$msg = '删除失败';
$code = -1;
goto doEnd;
}
}
$query = CoinBannerItem::find()
->select('id, banner_url, image_url, title')
->where(['platform_id' => $platform_id])
->asArray();
$banner_model = $query->offset(($page - 1) * 10)->limit(10)->asArray()->all();
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '10']);
$data = [
'list' => $banner_model,
'page' => [
'pageCount' => $pages->pageCount,
'pageSize' => 10,
'currentPage' => $page,
]
];
doEnd : doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data]; return ['code' => $code, 'msg' => $msg, 'data' => $data];
} }
......
<?php
namespace wallet\controllers;
use Yii;
use OSS\OssClient;
use yii\web\UploadedFile;
use wallet\base\BaseController;
class OssController extends BaseController
{
protected $accessKeyId = "LTAI4FxZ787zpBmjLmr6yMwA";
protected $accessKeySecret = "5OMu030RFIE2KP3fNJrhVRTlVqBLaE";
protected $endpoint = "http://oss-cn-shanghai.aliyuncs.com";
protected $bucket = "bqbwallet";
public function actionUpload()
{
$data = [];
$type = Yii::$app->request->post('type', '');
$uploaded_file = UploadedFile::getInstanceByName('file');
if (false == $uploaded_file || false == $type) {
$msg = '参数错误';
$code = -1;
goto doEnd;
}
$object = md5(date('Y-m-d H:i:s') . $uploaded_file->tempName) . '.' . $uploaded_file->extension;
$filePath = $uploaded_file->tempName;
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
$result = $ossClient->uploadFile($this->bucket, 'upload/' . (empty($type) ? '' : $type . '/') . $object, $filePath);
if (!isset($result["info"]['url'])) {
$msg = '上传失败';
$code = -1;
goto doEnd;
}
$code = 0;
$msg = 'ok';
$data = $result["info"]['url'];
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment