作者 何书鹏

1

<?php
namespace app\api\controller;
use app\common\controller\Api;
use fast\Tree;
class Category extends Api
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
$this->model = model('app\common\model\Category');
}
/**
* 分类-首页
* @ApiMethod (GET)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function index()
{
$list = $this->model->where('type','goods')->order('weigh desc,id desc')->select();
foreach($list as $v){
$v->visible(['id','pid','name','image']);
}
$tree = Tree::instance();
$tree->init(collection($list)->toArray(), 'pid');
$list = $tree->getTreeArray(0);
$this->success(__('成功'),compact('list'));
}
/**
* 分类-商品
* @ApiMethod (GET)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", description="扩展数据返回")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function goods()
{
$category_id = $this->request->request('category_id');
$list = \app\common\Goods::where("find_in_set({$category_id},category_ids)")->order('weigh desc,id desc')->select();
foreach($list as $v){
$v->visible(['id','pid','name','image']);
}
$tree = Tree::instance();
$tree->init(collection($list)->toArray(), 'pid');
$list = $tree->getTreeArray(0);
$this->success(__('成功'),compact('list'));
}
}
... ...
... ... @@ -22,20 +22,14 @@ class Index extends Api
{
$user = $this->auth->getUser();
// 轮播图
$banner_list = Banner::field("
CONCAT('http://www.baidu.com',banner_image) banner_image,
banner_url
")->order('weight desc')
->select();
$banner_list = Banner::banner_list();
$coupon_list = Coupon::couponList($user);
// 私人定制(一级分类)
$category_list = Category::where('pid',0)
->where('type','goods')
->field("
id,
name,
CONCAT('http://www.baidu.com',image) image
")->select();
$category_list = Category::where(['type'=>'goods','pid'=>0])->select();
foreach($category_list as $v){
$v->visible(['id','name','image']);
}
$category_list = collection($category_list)->toArray();
// 会员专区
$member_goods_list = Goods::getList(['ismember'=>'1'],1,2);
// 热卖爆品
... ... @@ -55,11 +49,10 @@ class Index extends Api
*/
public function searchGoodsList()
{
// 页码
$page = $this->request->request('page',1);
$rows = $this->request->request('rows',config('paginate.list_rows'));// 每页条数
$page = $this->request->request('page',1);// 页码
$keywords = $this->request->request('keywords','');
$list = Goods::getList(['keywords'=>['like','%'.$keywords.'%']],$page);
// 返回结果
$list = Goods::getList(['keywords'=>['like','%'.$keywords.'%']],$page,$rows);
$this->success(__('成功'),compact('list'));
}
... ... @@ -69,14 +62,13 @@ class Index extends Api
public function categoryList()
{
// 分类id
$id = $this->request->request('id');
$list = Category::where('pid',$id)
$category_id = $this->request->request('category_id');
$list = Category::where('pid',$category_id)
->where('type','goods')
->field("
id,
name
")->select();
// 返回结果
$this->success(__('成功'),compact('list'));
}
... ... @@ -85,12 +77,10 @@ class Index extends Api
*/
public function goodsList()
{
// 页码
$page = $this->request->request('page',1);
// 分类id
$id = $this->request->request('id');
$list = Goods::getList(['category_id'=>$id],$page);
// 返回结果
$rows = $this->request->request('rows',config('paginate.list_rows'));// 每页条数
$page = $this->request->request('page',1);// 页码
$category_id = $this->request->request('category_id');// 分类id
$list = Goods::getList("FIND_IN_SET('{$category_id}', 'category_ids')",$page,$rows);
$this->success(__('成功'),compact('list'));
}
... ... @@ -99,9 +89,9 @@ class Index extends Api
*/
public function memberGoodsList()
{
// 页码
$page = $this->request->request('page',1);
$list = Goods::getList(['ismember'=>'1'],$page);
$rows = $this->request->request('rows',config('paginate.list_rows'));// 每页条数
$page = $this->request->request('page',1);// 页码
$list = Goods::getList(['ismember'=>'1'],$page,$rows);
// 返回结果
$this->success(__('成功'),compact('list'));
}
... ... @@ -112,8 +102,8 @@ class Index extends Api
public function goodsInfo()
{
// 商品id
$id = $this->request->request('id');
$goods = Goods::get($id);
$goods_id = $this->request->request('goods_id');
$goods = Goods::get($goods_id);
empty($goods) && $this->error(__('商品不存在'));
$goods['issale'] == '0' && $this->error(__('商品已下架'));
// 返回结果
... ... @@ -126,10 +116,10 @@ class Index extends Api
public function appraiseList()
{
// 商品id
$list_rows = $this->request->request('list_rows',config('paginate.list_rows'));
$rows = $this->request->request('rows',config('paginate.list_rows'));
$page = $this->request->request('page',1);
$id = $this->request->request('id');
$list = Appraise::getList(['goods_id'=>$id],$page,$list_rows);
$goods_id = $this->request->request('goods_id');
$list = Appraise::getList(['goods_id'=>$goods_id],$page,$rows);
// 返回结果
$this->success(__('成功'),$list);
}
... ...
... ... @@ -6,5 +6,20 @@ use think\Model;
class Banner extends Model
{
public function getBannerImageAttr($value, $data)
{
return !empty($value) ? cdnurl($value,true) : '';
}
/**
* 轮播列表
*/
public function banner_list()
{
$list = Banner::order('weight desc')->select();
foreach($list as $v){
$v->visible(['banner_image','banner_url']);
}
return collection($list)->toArray();
}
}
... ...
... ... @@ -84,4 +84,9 @@ class Category extends Model
})->order('weigh', 'desc')->select())->toArray();
return $list;
}
public function getImageAttr($value, $data)
{
return !empty($value) ? cdnurl($value,true) : '';
}
}
... ...
... ... @@ -15,6 +15,18 @@ class Goods extends Model
'updatetime'
];
protected $append = [
'goods_image'
];
/**
* 商品单图
*/
public function getGoodsImageAttr($value, $data)
{
return !empty($value) ? cdnurl(explode(',',$value)[0],true) : '';
}
/**
* 商品图片
*/
... ... @@ -22,7 +34,7 @@ class Goods extends Model
{
$list = !empty($value) ? explode(',',$value) : [];
foreach($list as &$v){
$v = 'http://www.baidu.com'.$v;
$v = cdnurl($v,true);
}
return $list;
}
... ... @@ -34,7 +46,7 @@ class Goods extends Model
{
$list = !empty($value) ? explode(',',$value) : [];
foreach($list as &$v){
$v = 'http://www.baidu.com'.$v;
$v = cdnurl($v,true);
}
return $list;
}
... ... @@ -44,17 +56,14 @@ class Goods extends Model
*/
public static function getList($where,$page,$rows=15)
{
return Goods::where(array_merge([
'issale' => '1'
],$where))
->field("
id,
CONCAT('http://www.baidu.com',substring_index(goods_images,',',1)) goods_image,
goods_name,
goods_price,
ismake
")->page($page,$rows)
$list = self::where('issale','1')
->where($where)
->page($page,$rows)
->order(['updatetime'=>'desc'])
->select();
foreach($list as $v){
$v->visible(['id','goods_image','goods_name','goods_price','is_make']);
}
return collection($list)->toArray();
}
}
... ...
... ... @@ -16,7 +16,6 @@ return array (
'categorytype' =>
array (
'goods' => '商品分类',
'goods_material' => '商品材质',
),
'configgroup' =>
array (
... ...
此 diff 太大无法显示。
... ... @@ -79,9 +79,9 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
},
api: {
bindevent: function () {
$(document).on("change", "#c-spec_template_id", function () {
var goods_spec = Config.goods_spec;
var goods_spec = Config.goods_spec;
goods_spec = goods_spec == undefined ? [] : goods_spec.goods_spec;
$(document).on("change", "#c-spec_template_id", function () {
Fast.api.ajax({
url:'goods/specList',
data:{
... ... @@ -95,81 +95,30 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
$.each(list,function(index,spec){
html += '<div class="form-group">'+
'<label class="control-label col-xs-12 col-sm-2">'+spec.spec_name+':</label>'+
'<div class="col-xs-12 col-sm-8">';
// 一级规格下规格值
if(spec.options.length > 0){
html += '<div class="radio">';
$.each(spec.options,function(index,spec_v){
var img = spec_v.spec_value_image == '' ? '/assets/img/qrcode.png' : spec_v.spec_value_image;
html += '<label for="'+spec_v.id+'" style="border:1px solid #f1f4f6">'+
'<input id="'+spec_v.id+'" name="row[goods_spec]['+spec_v.spec_id+']" type="radio" value="'+spec_v.id+'">'+
'<img width="100px" src="'+img+'">'+
'<p style="text-align:center">'+spec_v.spec_value+'</p>'+
'</label>';
})
html += '</div>';
}
'<div class="col-xs-12 col-sm-8">'+
commonhHtml(index,spec);
// 一级规格下二级规格
if(spec.two_spec_list.length > 0){
$.each(spec.two_spec_list,function(index,spec_s){
html += '<div class="form-group">'+
'<label class="control-label col-xs-12 col-sm-2">'+spec_s.spec_name+':</label>'+
'<div class="col-xs-12 col-sm-8">';
// 二级规格下规格值
if(spec_s.options.length > 0){
html += '<div class="radio">';
$.each(spec_s.options,function(index,spec_s_v){
var img = spec_s_v.spec_value_image == '' ? '/assets/img/qrcode.png' : spec_s_v.spec_value_image;
html += '<label for="'+spec_s_v.id+'" style="border:1px solid #f1f4f6">'+
'<input id="'+spec_s_v.id+'" name="row[goods_spec]['+spec_s_v.spec_id+']" type="radio" value="'+spec_s_v.id+'">'+
'<img width="100px" src="'+img+'">'+
'<p style="text-align:center">'+spec_s_v.spec_value+'</p>'+
'</label>';
})
html += '</div>';
}
// 二级规格下文本框
if(spec_s.spec_type == '2'){
html += '<input id="c-'+spec_s.id+'" data-rule="required" class="form-control" name="row[goods_spec]['+spec_s.id+']" type="text" value="">';
}
// 二级规格下上传图片
if(spec.spec_type == '3'){
html += '<div class="input-group">'+
'<input id="c-'+spec_s.id+'" data-rule="" class="form-control" size="50" name="row[goods_spec]['+spec_s.id+']" type="text" value="">'+
'<div class="input-group-addon no-border no-padding">'+
'<span><button type="button" id="plupload-'+spec_s.id+'" class="btn btn-danger plupload" data-input-id="c-'+spec_s.id+'" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-'+spec_s.id+'"><i class="fa fa-upload"></i> 上传</button></span>'+
'<span><button type="button" id="fachoose-'+spec_s.id+'" class="btn btn-primary fachoose" data-input-id="c-'+spec_s.id+'" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> 选择</button></span>'+
'</div>'+
'<span class="msg-box n-right" for="c-'+spec_s.id+'"></span>'+
'</div>'+
'<ul class="row list-inline plupload-preview" id="p-'+spec_s.id+'"></ul>';
}
html += '</div>'+
'<div class="col-xs-12 col-sm-8">'+
commonhHtml(index,spec_s)+
'</div>'+
'</div>';
})
}
// 一级规格下文本框
if(spec.spec_type == '2'){
html += '<input id="c-'+spec.id+'" data-rule="required" class="form-control" name="row[goods_spec]['+spec.id+']" type="text" value="">';
}
// 一级规格下上传图片
if(spec.spec_type == '3'){
html += '<div class="input-group">'+
'<input id="c-'+spec.id+'" data-rule="" class="form-control" size="50" name="row[goods_spec]['+spec.id+']" type="text" value="">'+
'<div class="input-group-addon no-border no-padding">'+
'<span><button type="button" id="plupload-'+spec.id+'" class="btn btn-danger plupload" data-input-id="c-'+spec.id+'" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-'+spec.id+'"><i class="fa fa-upload"></i> 上传</button></span>'+
'<span><button type="button" id="fachoose-'+spec.id+'" class="btn btn-primary fachoose" data-input-id="c-'+spec.id+'" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> 选择</button></span>'+
'</div>'+
'<span class="msg-box n-right" for="c-'+spec.id+'"></span>'+
'</div>'+
'<ul class="row list-inline plupload-preview" id="p-'+spec.id+'"></ul>';
}
html += '</div>'+
'</div>';
})
html += '<div>';
$('.display_mode_1').remove();
$('.layer-footer').before(html);
// 绑定上传事件
Form.events.plupload("#spec-form");
Form.events.faselect("#spec-form");
// 显示隐藏规格
$("input:checked[type=radio]").trigger("change");
}
return false;
}, function(data, ret){
... ... @@ -181,11 +130,15 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
// 规格值下规格组
$(document).on("change", "input[type=radio]", function () {
var _this = $(this);
showSpec($(this));
});
// 显示隐藏规格
function showSpec(obj){
Fast.api.ajax({
url:'goods/valueSpecList',
data:{
spec_value_id:$(this).val(),
spec_value_id:obj.val(),
}
}, function(data, ret){
if(ret.code == 1){
... ... @@ -194,43 +147,18 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
// 一级规格
$.each(list,function(index,spec){
html += '<div class="form-group">'+
'<label class="control-label col-xs-12 col-sm-2">'+spec.spec_name+':</label>'+
'<div class="col-xs-12 col-sm-8">';
// 一级规格下规格值
if(spec.options.length > 0){
html += '<div class="radio">';
$.each(spec.options,function(index,spec_v){
var img = spec_v.spec_value_image == '' ? '/assets/img/qrcode.png' : spec_v.spec_value_image;
html += '<label for="'+spec_v.id+'" style="border:1px solid #f1f4f6">'+
'<input id="'+spec_v.id+'" name="row[goods_spec]['+spec_v.spec_id+']" type="radio" value="'+spec_v.id+'">'+
'<img width="100px" src="'+img+'">'+
'<p style="text-align:center">'+spec_v.spec_value+'</p>'+
'</label>';
})
html += '</div>';
}
// 一级规格下文本框
if(spec.spec_type == '2'){
html += '<input id="c-'+spec.id+'" data-rule="required" class="form-control" name="row[goods_spec]['+spec.id+']" type="text" value="">';
}
// 一级规格下上传图片
if(spec.spec_type == '3'){
html += '<div class="input-group">'+
'<input id="c-'+spec.id+'" data-rule="" class="form-control" size="50" name="row[goods_spec]['+spec.id+']" type="text" value="">'+
'<div class="input-group-addon no-border no-padding">'+
'<span><button type="button" id="plupload-'+spec.id+'" class="btn btn-danger plupload" data-input-id="c-'+spec.id+'" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-'+spec.id+'"><i class="fa fa-upload"></i> 上传</button></span>'+
'<span><button type="button" id="fachoose-'+spec.id+'" class="btn btn-primary fachoose" data-input-id="c-'+spec.id+'" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> 选择</button></span>'+
'</div>'+
'<span class="msg-box n-right" for="c-'+spec.id+'"></span>'+
'</div>'+
'<ul class="row list-inline plupload-preview" id="p-'+spec.id+'"></ul>';
}
html += '</div>'+
'<label class="control-label col-xs-12 col-sm-2" for="c-'+spec.id+'">'+spec.spec_name+':</label>'+
'<div class="col-xs-12 col-sm-8">'+
commonhHtml(index,spec)+
'</div>'+
'</div>';
})
html += '<div>';
_this.parent().parent().parent().parent('.form-group').next('.display_mode_2').remove();
_this.parent().parent().parent().parent('.form-group').after(html);
obj.parent().parent().parent().parent('.form-group').next('.display_mode_2').remove();
obj.parent().parent().parent().parent('.form-group').after(html);
// 绑定上传事件
Form.events.plupload();
Form.events.faselect();
}
return false;
}, function(data, ret){
... ... @@ -238,7 +166,46 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
alert(ret.msg);
return false;
});
});
}
// 共用html部分
function commonhHtml(index,spec){
var html = '';
// 规格下规格值
if(spec.options.length > 0){
html += '<div class="radio">';
$.each(spec.options,function(index,spec_v){
var img = spec_v.spec_value_image == '' ? '/assets/img/qrcode.png' : spec_v.spec_value_image;
var checked = goods_spec[spec_v.spec_id] == spec_v.id ? 'checked' : index == 0 ? 'checked' : '';
html += '<label for="'+spec_v.id+'" style="border:1px solid #f1f4f6">'+
'<input id="'+spec_v.id+'" '+checked+' name="row[goods_spec]['+spec_v.spec_id+']" type="radio" value="'+spec_v.id+'">'+
'<img width="100px" src="'+img+'">'+
'<p style="text-align:center">'+spec_v.spec_value+'</p>'+
'</label>';
})
html += '</div>';
}
// 规格下文本框
if(spec.spec_type == '2'){
var val = goods_spec[spec.id] == undefined ? '' : goods_spec[spec.id];
html += '<input id="c-'+spec.id+'" data-rule="required" class="form-control" name="row[goods_spec]['+spec.id+']" type="text" value="'+val+'">';
}
// 规格下上传图片
if(spec.spec_type == '3'){
var val = goods_spec[spec.id] == undefined ? '' : goods_spec[spec.id];
html += '<div class="input-group">'+
'<input id="c-'+spec.id+'" data-rule="required" class="form-control" size="50" name="row[goods_spec]['+spec.id+']" type="text" value="'+val+'">'+
'<div class="input-group-addon no-border no-padding">'+
'<span><button type="button" id="plupload-'+spec.id+'" class="btn btn-danger plupload" data-input-id="c-'+spec.id+'" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-'+spec.id+'"><i class="fa fa-upload"></i> 上传</button></span>'+
'<span><button type="button" id="fachoose-'+spec.id+'" class="btn btn-primary fachoose" data-input-id="c-'+spec.id+'" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> 选择</button></span>'+
'</div>'+
'<span class="msg-box n-right" for="c-'+spec.id+'"></span>'+
'</div>'+
'<ul class="row list-inline plupload-preview" id="p-'+spec.id+'"></ul>';
}
return html;
}
Form.api.bindevent($("form[role=form]"));
}
}
... ...