Commit 4180deb9 authored by zhuwei's avatar zhuwei

c端商铺图库相关-模型-控制器-service 搬迁

parent 8792b76b
No preview for this file type
No preview for this file type
<?php
namespace app\index\controller;
use app\index\extend\Basic;
use app\index\service\ImageDepotService;
use app\model\GImageDepot;
/**
* zhuwei
* Class ImageDepot
* @package app\api\controller
* 2018-08-31 10:35:07
*/
class ImageDepot extends Basic
{
protected $imageDepot;
public function __construct($request = null)
{
parent::__construct($request);
$this->imageDepot = new GImageDepot();
}
/**
* 获取图片类型与标签
* @return \think\Response
*/
public function getImageTypeLabel()
{
header('Access-Control-Allow-Origin:*');
//图片类型:0室外图 ,1室内图 ,2人群图
$res['img_type'] = [0 =>'室外图',1 =>'室内图',2 =>'人群图'];
//0 商场美食、1 沿街餐饮、2 百货零售、3 服饰鞋包、4 休闲娱乐、5 亲子教育、6 百货超市、7 办公
$res['label_type'] = [0 =>'商场美食',1 =>'沿街餐饮',2 =>'百货零售',3 =>'服饰鞋包',4 =>'休闲娱乐',5 =>'亲子教育',6 =>'百货超市',7 =>'办公'];
if ($res) {
return $this->response("200", "成功",$res);
} else {
return $this->response("101", "失败");
}
}
/**
* 上传保存图片
* @return \think\Response
*/
public function addImageDepot()
{
header('Access-Control-Allow-Origin:*');
$params = $this->params;
/*$params = array(
"img_type" => 2,
"label_type" => 3,
"img_name" => 'cehsi'
);*/
$checkResult = $this->validate($params, "ImageDepot.addImageDepot");
if (true !== $checkResult) {
return $this->response("101", $checkResult);
}
$params_["img_type"] = $params['img_type'];
$params_["label"] = $params['label'];
$params_["img_name"] = $params['img_name'];
$res = $this->imageDepot->saveData($params_);//int(1)
if ($res) {
return $this->response("200", "成功");
} else {
return $this->response("101", "失败");
}
}
/**
* 获取图片列表
* @return \think\Response
*/
public function getImageDepotList()
{
$params = $this->params;
if (!empty($params['label'])) {
$params_['label'] = [ 'LIKE', '%' . $params['label'] . '%' ];
}
if ($this->params['img_type'] != '') {
$params_['img_type'] = $params['img_type'];
}
/*$params = array(
"agents_id" => 5740
);*/
/**
`id`
`img_type` '图片类型:0室外图 ,1室内图 ,2人群图',
`label_type` '业态标签:0商场美食、1沿街餐饮、2百货零售、3服饰鞋包、4休闲娱乐、5亲子教育、6百货超市、7办公',
`img_name` '图片名称',
`img_status` '删除状态 0正常 1删除',
`update_time` '更新时间',
`create_time` '创建时间',
*/
$pageNo = empty($params['pageNo']) ? 1 : $params['pageNo'];
$pageSize = empty($params['pageSize']) ? 15 : $params['pageSize'];
$field = 'id,img_type,label,img_name,create_time';
$params_['img_status'] = 0;
$res = $this->imageDepot->getImageDepotList($field,$params_,$pageNo,$pageSize);
$res_total = $this->imageDepot->getImageDepotListTotal($params_);
foreach($res as $k=>$v)
{
$res[$k]['img_file_path'] = CURRENT_URL . 'static/shop_image_depot/' . $v['img_name'];;
}
$data['total'] =$res_total;
$data['list'] =$res;
return $this->response("200", "成功",$data);
}
/**
* 根据ID获取记录
* @return \think\Response
*/
public function getImageDepotById()
{
$params = $this->params;
$checkResult = $this->validate($params, "ImageDepot.getImageDepotById");
if (true !== $checkResult) {
return $this->response("101", $checkResult);
}
$params_['id'] = $params['id'];
$params_['img_status'] = 0;
$field = 'id,img_type,label,img_name,create_time';
$res = $this->imageDepot->getImageDepotById($field,$params_);
if(!$res){
return $this->response("101", "查无数据");
}
$res[0]['img_file_path'] = CURRENT_URL . 'static/shop_image_depot/' . $res['img_name'];
$data['list'] =$res[0];
return $this->response("200", "成功",$data);
}
/**
* 删除
* @return \think\Response
*/
public function delImageDepot(){
$params = $this->params;
$checkResult = $this->validate($params, "ImageDepot.getImageDepotById");
if (true !== $checkResult) {
return $this->response("101", $checkResult);
}
$params_['img_status'] = 1;
$res = $this->imageDepot->updateImageDepot($params['id'],$params_);//int(1)
if($res == 1){
return $this->response("200", "成功",['data'=>$res]);
}else{
return $this->response("300", "失败");
}
}
public function ceshi(){
$ImageDepotService = new ImageDepotService();
$res = $ImageDepotService->getImage('商场美食1');
dump($res);
}
}
<?php
/**
* Created by PhpStorm.
* User: zhuwe
* Date: 2018-09-04
* Time: 10:30:34
*/
namespace app\index\service;
use app\model\GImageDepot;
class ImageDepotService
{
protected $imageDepot;
public function __construct()
{
$this->imageDepot = new GImageDepot();
}
/**
* 获取随机图片
* @param int $img_type 图片类型:0室外图 ,1室内图 ,2人群图
* @param string $label 标签
* @return array|false|\PDOStatement|string|\think\Collection
*/
public function getImageWidthRand($img_type=0,$label='商场美食',$not_in_id)
{
$params_['img_status'] = 0;
//$map['id']=array('not in','1,2,3');
if (!empty($not_in_id)) {
$params_['id'] = ['not in',$not_in_id];
}
$params_['img_type'] = $img_type;
if($img_type == 0 or $img_type == 1)
{
//室外图、室内图有标签 ,人群图无标签
if (!empty($label)) {
$params_['label'] = [ 'LIKE', '%' . $label . '%' ];
}
}
$result = $this->imageDepot->getImageDepotIDList($params_);
if(!$result){
//查询不到相关数据返回''
return '';
}
$random_keys=array_rand($result,1);
//$img_file_path= CURRENT_URL . 'static/shop_image_depot/' . $result[$random_keys]['id'];
return $result[$random_keys]['id'];
}
/**
* 获取三张图片
* @param string $label 标签
* @return string
*/
public function getImage($label='商场美食')
{
//图片类型:0室外图 ,1室内图 ,2人群图
$indoor_img_id_ = $this->getImageWidthRand(0, $label);//图片类型:0室外图
if ($indoor_img_id_) {
$return_arr[] = $indoor_img_id_;
}
$indoor_img_id = $indoor_img_id_ ? $indoor_img_id_ : 1;
$outdoor_img_id_ = $this->getImageWidthRand(1, $label, $indoor_img_id);//图片类型:1室内图
if ($outdoor_img_id_) {
$return_arr[] = $outdoor_img_id_;
}
$outdoor_img_id = $outdoor_img_id_ ? $outdoor_img_id_ : 2;
$not_in_id = $indoor_img_id . ',' . $outdoor_img_id;
$people_img_id_ = $this->getImageWidthRand(2, $label, $not_in_id);//图片类型:2人群图
if ($people_img_id_) {
$return_arr[] = $people_img_id_;
}
return implode(',',$return_arr);
}
}
\ No newline at end of file
<?php
namespace app\model;
use think\Db;
use think\Model;
class GImageDepot extends Model
{
// 设置当前模型对应的完整数据表名称
protected $table = 'g_image_depot';
/**
`id`
`img_type` '图片类型:0室外图 ,1室内图 ,2人群图',
`label_type` '业态标签:0商场美食、1沿街餐饮、2百货零售、3服饰鞋包、4休闲娱乐、5亲子教育、6百货超市、7办公',
`img_name` '图片名称',
`img_status` '删除状态 0正常 1删除',
`update_time` '更新时间',
`create_time` '创建时间',
*/
/**
* 添加图片 上传保存图片
* @param $data
* @return int|string
*/
public function saveData($data) {
$time = date("Y-m-d h:i:sa", time());
$data['create_time'] = $time;
$data['update_time'] = $time;
return $this->insert($data);
}
/**
* 查询
*/
public function getImageDepotList($field,$params,$pageNo,$pageSize)
{
$order = "create_time desc";
$result = Db::table($this->table)
->field($field)
->where($params)
->limit($pageSize)
->page($pageNo)
->order($order)
->select();
//dump($this->getLastSql());
return $result;
}
/**
* 查询id集合
*/
public function getImageDepotIDList($params)
{
$field = "id,img_name";
$result = Db::table($this->table)
->field($field)
->where($params)
->select();
if(!$result){
return $result;
}
foreach ($result as $k => $v) {
$result_['id'] = $v['id'];
$result_['img_name'] = $v['img_name'];
$result__[]=$result_;
}
//dump($this->getLastSql());
return $result__;
}
/**
* 查询统计数据
*/
public function getImageDepotListTotal($params)
{
$result = Db::table($this->table)
->where($params)
->count();
//dump($this->getLastSql());
return $result;
}
/**
* 根据ID获取记录
*/
public function getImageDepotById($field,$params,$limit = "10")
{
$result = Db::table($this->table)
->field($field)
->where($params)
->limit($limit)
->select();
//dump($this->getLastSql());
return $result;
}
/**
* 更新数据
*/
public function updateImageDepot($id,$params)
{
$result = Db::table($this->table)
->where('id', $id)
->update($params);
//dump($this->getLastSql());
return $result;
}
}
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