Commit adf067c6 authored by clone's avatar clone

banner 增删改查

parent 0cd944eb
<?php
namespace app\api\controller;
/**
* Created by PhpStorm.
* User : zw
......@@ -9,8 +11,65 @@ namespace app\api\controller;
*/
use app\api\extend\Basic;
use app\model\BannerModel;
use think\Request;
class Banner extends Basic
{
protected $bannerModel;
public function __construct($request = null)
{
parent::__construct($request);
$this->bannerModel = new BannerModel();
}
/**
* 查询banner列表
* @return \think\Response
*/
public function getBannerList()
{
$field = "id,title,pic_path,url,sort,is_show,create_time,update_time";
$params['is_show'] = array( "eq", 0 );
$result = $this->bannerModel
->getBannerList(1, 15, "sort desc", $field, $params);
return $this->response("200", "request success", $result);
}
/**
* 新增or修改数据
* @return \think\Response
*/
public function addOrSave()
{
$params = array(
"id" => 2,
"is_show" => 0
);
$msg = isset($params['id']) ? "修改" : "新增";
$result = $this->bannerModel->addOrUpdate($params);
if ($result['code'] == 200) {
return $this->response("200", $msg . "成功", $result["msg"]);
} else {
return $this->response("101", $msg . $result["msg"]);
}
}
public function upIsShow()
{
$params = array(
"id" => 1,
"is_show" => 1
);
$result = $this->bannerModel->upIsShow($params);
if ($result['code'] == 200) {
return $this->response("200", "update success", $result["msg"]);
} else {
return $this->response("101", "update error");
}
}
}
\ No newline at end of file
......@@ -27,6 +27,15 @@ class Basic extends Controller
*/
public $token;
/**
* @var int userId
*/
protected $userId;
protected $access_token;
protected $phone;
/**
* 基础接口SDK
* @param Request|null $request
......
<?php
/**
* Created by PhpStorm.
* User : zw
* Date : 2017/12/7
* Time : 14:45
* Intro:
*/
define("AAA",123123);
\ No newline at end of file
......@@ -11,17 +11,19 @@ namespace app\model;
*/
use think\Db;
use think\Model;
use think\Log;
class Banner extends Model
class BannerModel extends Model
{
// 设置当前模型对应的完整数据表名称
protected $table = 'u_banner';
protected $db;
protected $logger;
function __construct()
{
$this->db = Db($this->table);
$this->logger = new Log();
}
/**
......@@ -32,14 +34,25 @@ class Banner extends Model
function addOrUpdate($param)
{
$banner = $this->db;
$arr = array(
"title" => $param['title'],
"pic_path" => $param['pic_path'],
"url" => $param['url'],
"sort" => $param['sort'],
"is_show" => $param['is_show'],
"update_time" => time()
);
$arr = array();
if (isset($param['title'])) {
$arr["title"] = $param['title'];
}
if (isset($param['pic_path'])) {
$arr["pic_path"] = $param['pic_path'];
}
if (isset($param['url'])) {
$arr["url"] = $param['url'];
}
if (isset($param['sort'])) {
$arr["sort"] = $param['sort'];
}
if (isset($param['title'])) {
$arr["is_show"] = $param['is_show'];
}
if (isset($param['id'])) {
$result = $this->db
->where("id=" . $param['id'])
......@@ -47,7 +60,7 @@ class Banner extends Model
if (count($result) > 0) {
$arr["id"] = $param["id"];
} else {
return [ "101", "要修改数据不存在" ];
return [ "code" => "101", "msg" => "数据不存在" ];
}
} else {
$arr["create_time"] = time();
......@@ -56,10 +69,10 @@ class Banner extends Model
try {
$id = $banner->save($arr);
Db::commit();
return $id;
return [ "code" => "200", "msg" => $id ];
} catch (\Exception $e) {
Db::rollback();
return [ "101", "保存失败,数据异常" ];
return [ "code" => "101", "msg" => "失败,数据异常" ];
}
}
......@@ -69,21 +82,24 @@ class Banner extends Model
* @param $param
* @return array
*/
function upIsShow($param){
function upIsShow($param)
{
$this->logger->info('日志信息');
$banner = $this->db;
$arr = array(
"id" => $param['id'],
"is_show" => $param['is_show'],
"update_time" => time()
);
var_dump($arr);exit;
Db::startTrans();
try {
$id = $banner->save($arr);
Db::commit();
return $id;
return [ "code" => "200", "msg" => $id ];
} catch (\Exception $e) {
Db::rollback();
return [ "101", "删除失败,数据异常" ];
return [ "code" => "101", "msg" => "删除失败,数据异常" ];
}
}
......@@ -96,8 +112,9 @@ class Banner extends Model
* @param $params
* @return mixed
*/
function getBannerList($pageNo = 1, $pageSize = 15, $order_ = 'id desc', $field, $params){
return $data = $this->db
function getBannerList($pageNo = 1, $pageSize = 15, $order_ = 'id desc', $field, $params)
{
return $this->db
->field($field)
->where($params)
->order($order_)
......
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