Commit fadf8d25 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/optimize' into 'master'

sft banner See merge request !96
parents ab5d40db 812e5343
<?php
namespace api\controllers;
use Yii;
use api\base\BaseController;
use common\models\psources\CoinPlatformBanner;
class CoinPlatformBannerController extends BaseController
{
public function actionIndex()
{
$code = 0;
$msg = 'success';
$platform_id = Yii::$app->request->get('platform_id', 13);
$data = CoinPlatformBanner::find()->where(['platform_id' => $platform_id])->asArray()->all();
return ['code' => $code, 'data' => $data, 'msg' => $msg];
}
}
\ No newline at end of file
<?php
namespace backend\controllers;
use Yii;
use common\models\psources\CoinPlatformBanner;
class CoinPlatformBannerController extends BaseController
{
public function actionAdd()
{
$user_platform_id = Yii::$app->user->identity->platform_id;
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
$request = Yii::$app->request;
$image_url = $request->post('image_url', '');
if($image_url){
$banner_item = new CoinPlatformBanner();
$banner_item->image_url = $image_url;
$banner_item->platform_id = $user_platform_id;
$banner_item->save();
return ['code' => 0,'msg' => 'banner添加成功'];
}else{
return ['code' => 1,'msg' => 'banner添加加失败'];
}
}
}
/**
* @return array
* banner删除
*/
public function actionDelete()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
$request = Yii::$app->request;
$id = $request->get('id', '');
if($id){
$banner = CoinPlatformBanner::getBanner($id);
if(!$banner){
return ['code' => 1,'msg' =>'banner不存在,不能删除'];
}
$banner->delete();
return ['code' => 0,'msg' => 'banner删除成功'];
}else{
return ['code' => 1 ,'msg' => 'banner删除失败'];
}
}
}
/**
* banner列表
*/
public function actionIndex()
{
$user_platform_id = Yii::$app->user->identity->platform_id;
if(Yii::$app->request->isAjax){
Yii::$app->response->format = 'json';
$condition = [];
if(1 !== $user_platform_id){
$condition = ['platform_id' => $user_platform_id];
}
$data = CoinPlatformBanner::getItems($condition);
foreach ($data as $key => &$val){
$val['coin_name'] = isset($val['platform']['name']) ? $val['platform']['name'] : '';
}
return [ 'data' =>$data, 'code' => 0];
}
return $this->render('index');
}
}
\ No newline at end of file
<style>
.layui-form-label {
width: 100px;
}
</style>
<h4>banner管理列表</h4>
<div class="layui-row">
<div class="layui-col-md1">
<button class="layui-btn layui-btn-default" id="add">添加banner图</button>
</div>
</div>
<div class="layui-col-md9">
<table class="layui-table" id="table1" lay-filter="table1"></table>
</div>
<div class="layui-row add" style="display: none;padding: 5px;" id="_form">
<div class="layui-col-xs6 layui-col-sm6 layui-col-md11">
<form class="layui-form" action="javascript:void(0)" id="form1" method="post" lay-filter="form1">
<input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">图片</label>
<div class="layui-input-block">
<img src="" style="margin-top: 11px; max-width: 32px; max-height: 32px"
id="icon1">
</div>
<input type="hidden" name="image_url" value="">
</div>
<div class="layui-inline" style="margin-left: 50px;">
<button type="button" class="layui-btn" id="upload1" style="">
<i class="layui-icon">&#xe67c;</i>上传新图片
</button>
</div>
</div>
</form>
</div>
</div>
<script type="text/html" id="operationTpl">
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/javascript">
var table = layui.table;
table.render({
elem: '#table1',
url: '/admin/coin-platform-banner/index',
cols: [[
{
field: 'id',
title: '编号'
},
{
field: 'coin_name',
title: '钱包',
},
{
field: 'image_url',
title: '图片跳转地址',
width: 600
},
{
title: '操作',
templet: '#operationTpl',
width: 200
}
]],
});
var form = layui.form;
form.render();
table.on('tool(table1)', function(obj) {
var event = obj.event;
var data = obj.data;
if (event === 'del') {
var index = layer.confirm("确认删除?", {icon: 3, title: '删除'}, function () {
$.get('/admin/coin-platform-banner/delete', {id: data.id}, function (rev) {
layer.msg(rev.msg);
if (0==rev.code) {
table.reload('table1');
}
});
});
}
});
$('#add').click(function () {
var index = layer.open({
title: '添加banner',
area: '800px',
type: 1,
content: $("#_form"),
btn: ['保存', '取消'],
success: function() {
clearForm();
},
btn1: function() {
$.post('/admin/coin-platform-banner/add', $("#form1").serialize(), function(rev) {
layer.msg(rev.msg);
if (0 == rev.code) {
layer.close(index);
$("#_form").css('display', 'none');
clearForm();
table.reload('table1');
}
});
},
btn2: function() {
layer.close(index);
$("#_form").css('display', 'none');
},
cancel: function() {
layer.close(index);
$("#_form").css('display', 'none');
}
});
})
function clearForm(){
form.val("form1", {
image_url: '',
banner_url: ''
});
$("#icon1").attr('src', '');
}
//图片上传
var uploader = layui.upload;
$_csrf = $("input[name='_csrf']").val();
uploader.render({
elem: "#upload1",
url: '/admin/image/upload',
data: {_csrf: $_csrf,image_type:2},
done: function (res) {
if(res.code == 0){
$("input[name='image_url']").val(res.data.image_src);
$("#icon1").attr('src', res.data.image_src);
}
},
error: function (res) {
}
});
</script>
\ No newline at end of file
<?php
namespace common\models\psources;
use Yii;
use common\core\BaseActiveRecord;
class CoinPlatformBanner extends BaseActiveRecord
{
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{coin_platform_banner}}';
}
public static function getBanner($id)
{
return self::find()->where(['id' => $id])->one();
}
public static function getItems($condition = [])
{
return self::find()->joinWith('platform')->where($condition)->asArray()->all();
}
public function getPlatform()
{
return $this->hasOne(CoinPlatform::className(), ['id'=>'platform_id']);
}
}
\ 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