Commit 33866811 authored by ZhuChunYang's avatar ZhuChunYang

update

parent a871782e
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/11/1
* Time: 17:31
*/
namespace backend\assets\applicateRank;
use yii\web\AssetBundle;
use yii\web\View;
class IndexAsset extends AssetBundle
{
public $sourcePath = '@backend/web/js/applicate-rank';
public $js = [
'index.js',
];
public $jsOptions = [
'position' => View::POS_END,
];
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/11/1
* Time: 17:14
*/
namespace backend\controllers;
use common\models\psources\CoinApplicateRank;
use common\models\psources\CoinApplicateRecommend;
use common\models\psources\CoinApplication;
use Yii;
class ApplicateRankController extends BaseController
{
public function actionAdd()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
$request = Yii::$app->request->post();
$applicate_id = $request['applicate_id'];
$type = $request['type'];
if($applicate_id && $type){
$applicate = CoinApplication::getApplicateById($applicate_id);
if(!$applicate){
return ['code' => 1, 'msg' => '应用不存在'];
}
$applicate_rank = CoinApplicateRank::getApplicate($applicate_id, $type);
if($applicate_rank){
return ['code' => 1, 'msg' => '应用已添加,不能再添加'];
}
$rank = new CoinApplicateRank();
$rank->relate_id = $applicate_id;
$rank->type = $type;
$rank->save();
return ['code' => 0, 'msg' => '添加成功'];
}else{
return ['code' => 1, 'msg' => '添加失败'];
}
}
}
public function actionDelete()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
$request = Yii::$app->request;
$id = $request->get('id', '');
$type = $request->get('type', '');
if ($id && $type) {
$rank = CoinApplicateRank::getApplicate($id, $type);
if (!$rank) {
return ['code' => 1, 'msg' => '应用不存在,不能删除'];
}
$rank->delete();
return ['code' => 0, 'msg' => '应用删除成功'];
} else {
return ['code' => 1, 'msg' => '应用删除失败'];
}
}
}
/**
* 排行榜列表
*/
public function actionIndex()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
$request = Yii::$app->request;
$type = $request->get('type', '');
$data = CoinApplicateRank::getApplicateList($type);
return ['data' => $data, 'code' => 0];
}
$items = CoinApplication::getAllApplicate();
return $this->render('index',['items' => $items]);
}
public function actionSetSort()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
$request = Yii::$app->request;
$id = $request->get('id', '');
$type = $request->get('type', '');
$sort = $request->get('sort', 1);
if ($id && $type) {
$recommend = CoinApplicateRecommend::getRecommend($id, $type);
if (!$recommend) {
return ['code' => 1, 'msg' => '首页推荐不存在,不能设置排序'];
}
$recommend->sort = $sort;
$recommend->save();
return ['code' => 0, 'msg' => '首页推荐排序设置成功'];
} else {
return ['code' => 1, 'msg' => '首页推荐排序设置失败'];
}
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/11/1
* Time: 17:26
*/
use backend\assets\applicateRank\IndexAsset;
IndexAsset::register($this);
?>
<div class="layui-row">
<div class="layui-tab layui-tab-card">
<ul class="layui-tab-title">
<li class="layui-this">热度榜</li>
<li>新品榜</li>
</ul>
<div class="layui-tab-content">
<div class="layui-tab-item layui-show">
<button class="layui-btn layui-btn-default" id="add1">添加热度应用</button>
<table class="layui-table" id="table1" lay-filter="table"></table>
</div>
<div class="layui-tab-item">
<button class="layui-btn layui-btn-default" id="add2">添加新品应用</button>
<table class="layui-table" id="table2" lay-filter="table"></table>
</div>
</div>
</div>
</div>
<!-- 添加页面 -->
<div class="layui-row add" style="display: none;padding: 5px;">
<div class="layui-col-xs6 layui-col-sm6 layui-col-md11">
<form class="layui-form form_add" action="javascript:void(0)" method="post" lay-filter="form1">
<input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>">
<input type="hidden" name="type" value="">
<div class="layui-form-item">
<label class="layui-form-label">应用</label>
<div class="layui-input-block">
<select name="applicate_id" id="applicate_id" lay-search >
<?php foreach ($items as $item): ?>
<option value="<?= $item['id'] ?>"><?= $item['name'] ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn btn_add_submit" lay-submit lay-filter="formDemo">立即提交</button>
</div>
</div>
</form>
</div>
</div>
<script type="text/html" id="operTpl">
<a class="btn_delete" lay-event="delete"><i class="layui-icon" style="color: #4b67ff">&#xe640;</i></a>
</script>
\ No newline at end of file
var table1 = layui.table;
var table2 = layui.table;
var form = layui.form;
var layer_add;
table1.render({
elem: '#table1',
where: {type: 1},
url: '/admin/applicate-rank/index',
cols: [[
{field: 'id', title: '应用id'},
{field: 'name', title: '应用名称'},
{field: 'sort', title: '排序'},
{title: '操作', templet: '#operTpl'},
]]
});
table2.render({
elem: '#table2',
where: {type: 2},
url: '/admin/applicate-rank/index',
cols: [[
{field: 'id', title: '应用id'},
{field: 'name', title: '应用名称'},
{field: 'sort', title: '排序'},
{title: '操作', templet: '#operTpl'},
]]
});
form.render();
layui.table.on('tool(table)', function (obj) {
var data = obj.data;
var event = obj.event;
if (event == 'delete') {
var ldelete = layer.confirm('确定要删除?', {
icon: 3,
title: '删除'
}, function (index) {
$.get('/admin/applicate-rank/delete', {id: data.id,type:data.type}, function (rev) {
layer.msg(rev.msg);
if (rev.code == 0) {
obj.del();
layer.close(ldelete);
}
});
});
}
});
//添加图层
$("#add1").click(function () {
layer_add = layer.open({
type: 1,
title: '版本信息',
content: $('.add'), //这里content是一个普通的String
scrollbar: false,
area: ['700px','300px'],
cancel: function (index, layero) {
after_close_add(false);
},
success: function (index, layero) {
$('input[name="type"]').val(1);
}
});
});
$("#add2").click(function () {
layer_add = layer.open({
type: 1,
title: '版本信息',
content: $('.add'), //这里content是一个普通的String
scrollbar: false,
area: ['700px','300px'],
cancel: function (index, layero) {
after_close_add(false);
},
success: function (index, layero) {
$('input[name="type"]').val(2);
}
});
});
form.on('submit(formDemo)', function (data) {
$.post('/admin/applicate-rank/add', data.field, function (rev) {
layer.msg(rev.msg);
if (rev.code == 0) {
layer.close(layer_add);
after_close_add();
}
});
return false;
});
function after_close_add(reflush = true) {
if (reflush) {
table1.reload('table1', {});
table2.reload('table2', {});
}
$('.add').attr('style', 'display:none');
$('input[name="id"]').val('');
$('select[name="applicate_id"]').val(0);
form.render();
}
<?php
/**
* Created by PhpStorm.
* User: ZCY
* Date: 2018/11/2
* Time: 11:14
*/
namespace common\models\psources;
use common\core\BaseActiveRecord;
use Yii;
class CoinApplicateRank extends BaseActiveRecord
{
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_applicate_rank}}';
}
public function getApplication()
{
return $this->hasOne(CoinApplication::class, ['id' => 'relate_id']);
}
public static function getApplicateList($type)
{
$applicate_rank_model = self::find();
$data = $applicate_rank_model->JoinWith(['application'], false)
->select('relate_id as id,sort,name,'.self::tableName().".type")
->orderBy(self::tableName().'.sort asc')->where([self::tableName().'.type' => $type])->asArray()->all();
return $data;
}
public static function getApplicate($relate_id,$type)
{
return self::find()->where(['relate_id' => $relate_id,'type' => $type])->one();
}
}
\ No newline at end of file
...@@ -144,4 +144,10 @@ class CoinApplication extends BaseActiveRecord ...@@ -144,4 +144,10 @@ class CoinApplication extends BaseActiveRecord
return self::find()->where(['id' => $id])->one(); return self::find()->where(['id' => $id])->one();
} }
public static function getAllApplicate()
{
return self::find()->select('id,name')->asArray()->all();
}
} }
\ 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