Commit adf067c6 authored by clone's avatar clone

banner 增删改查

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