Goods.php 8.2 KB
Newer Older
D
v1.2.0  
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
devil_gong 已提交
5
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
D
v1.2.0  
devil_gong 已提交
6 7 8 9 10 11 12
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\api\controller;

13
use app\service\GoodsService;
G
gongfuxiang 已提交
14
use app\service\GoodsCommentsService;
D
v1.2.0  
devil_gong 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

/**
 * 商品
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class Goods extends Common
{
    /**
     * [__construct 构造方法]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-03T12:39:08+0800
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();
    }

    /**
     * 获取商品详情
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-12
     * @desc    description
     */
    public function Detail()
    {
        // 参数
        if(empty($this->data_post['goods_id']))
        {
            return DataReturn('参数有误', -1);
        }

G
gongfuxiang 已提交
54 55 56
        // 商品详情方式
        $is_use_mobile_detail = intval(MyC('common_app_is_use_mobile_detail'));

D
v1.2.0  
devil_gong 已提交
57 58 59 60 61 62 63 64 65
        // 获取商品
        $goods_id = intval($this->data_post['goods_id']);
        $params = [
            'where' => [
                'id' => $goods_id,
                'is_delete_time' => 0,
            ],
            'is_photo' => true,
            'is_spec' => true,
G
gongfuxiang 已提交
66
            'is_content_app' => ($is_use_mobile_detail == 1),
D
v1.2.0  
devil_gong 已提交
67
        ];
68 69
        $ret = GoodsService::GoodsList($params);
        if(empty($ret['data'][0]) || $ret['data'][0]['is_delete_time'] != 0)
D
v1.2.0  
devil_gong 已提交
70 71 72
        {
            return DataReturn('商品不存在或已删除', -1);
        }
G
gongfuxiang 已提交
73 74 75 76 77 78 79 80 81 82

        // 商品详情处理
        if($is_use_mobile_detail == 1)
        {
            unset($ret['data'][0]['content_web']);
        } else {
            // 标签处理,兼容小程序rich-text
            $search = [
                '<img ',
                '<section',
D
1.6  
devil_gong 已提交
83 84 85
                '/section>',
                '<p>',
                '<div>',
D
devil_gong 已提交
86 87
                '<table',
                '<tr',
G
gongfuxiang 已提交
88 89
            ];
            $replace = [
D
devil_gong 已提交
90
                '<img style="max-width:100%;margin:0;padding:0;display:block;" ',
G
gongfuxiang 已提交
91 92
                '<div',
                '/div>',
D
1.6  
devil_gong 已提交
93 94
                '<p style="margin:0;">',
                '<div style="margin:0;">',
D
devil_gong 已提交
95 96
                '<table style="width:100%;margin:0px;border-collapse:collapse;border-color:#ddd;border-style:solid;border-width:0 1px 1px 1px;"',
                '<tr style="margin:0;padding:5px;border-top:1px solid #ddd;"',
G
gongfuxiang 已提交
97 98 99
            ];
            $ret['data'][0]['content_web'] = str_replace($search, $replace, $ret['data'][0]['content_web']);
        }
D
v1.2.0  
devil_gong 已提交
100 101 102

        // 当前登录用户是否已收藏
        $ret_favor = GoodsService::IsUserGoodsFavor(['goods_id'=>$goods_id, 'user'=>$this->user]);
103
        $ret['data'][0]['is_favor'] = ($ret_favor['code'] == 0) ? $ret_favor['data'] : 0;
D
v1.2.0  
devil_gong 已提交
104

G
gongfuxiang 已提交
105 106 107
        // 商品评价总数
        $ret['data'][0]['comments_count'] = GoodsCommentsService::GoodsCommentsTotal(['goods_id'=>$goods_id, 'is_show'=>1]);

D
v1.2.0  
devil_gong 已提交
108 109 110 111 112 113
        // 商品访问统计
        GoodsService::GoodsAccessCountInc(['goods_id'=>$goods_id]);

        // 用户商品浏览
        GoodsService::GoodsBrowseSave(['goods_id'=>$goods_id, 'user'=>$this->user]);

D
devil_gong 已提交
114 115 116 117
        // 商品所属分类名称
        $category = GoodsService::GoodsCategoryNames($goods_id);
        $ret['data'][0]['category_names'] = $category['data'];

D
v1.2.0  
devil_gong 已提交
118 119
        // 数据返回
        $result = [
G
gongfuxiang 已提交
120
            'goods'                             => $ret['data'][0],
D
devil_gong 已提交
121
            'common_order_is_booking'           => (int) MyC('common_order_is_booking'),
G
gongfuxiang 已提交
122
            'common_app_is_use_mobile_detail'   => $is_use_mobile_detail,
D
devil_gong 已提交
123
            'common_app_is_online_service'      => (int) MyC('common_app_is_online_service'),
G
gongfuxiang 已提交
124
            'common_app_is_limitedtimediscount' => (int) MyC('common_app_is_limitedtimediscount'),
D
devil_gong 已提交
125
            'common_app_is_good_thing'          => (int) MyC('common_app_is_good_thing'),
D
v1.2.0  
devil_gong 已提交
126
        ];
G
gongfuxiang 已提交
127 128 129 130

        // 秒杀
        if($result['common_app_is_limitedtimediscount'] == 1)
        {
G
gongfuxiang 已提交
131
            $ret = CallPluginsServiceMethod('limitedtimediscount', 'Service', 'GoodsDetailCountdown', $goods_id);
G
gongfuxiang 已提交
132 133 134 135 136
            if($ret['code'] == 0)
            {
                $result['plugins_limitedtimediscount_data'] = $ret['data'];
            }
        }
D
v1.2.0  
devil_gong 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150
        return DataReturn('success', 0, $result);
    }

    /**
     * 用户商品收藏
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-17
     * @desc    description
     */
    public function Favor()
    {
        // 登录校验
G
gongfuxiang 已提交
151
        $this->IsLogin();
D
v1.2.0  
devil_gong 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

        // 开始操作
        $params = $this->data_post;
        $params['user'] = $this->user;
        return GoodsService::GoodsFavor($params);
    }

    /**
     * 商品规格类型
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-14
     * @desc    description
     */
    public function SpecType()
    {
        // 开始处理
        $params = $this->data_post;
D
devil_gong 已提交
171 172 173 174 175 176
        $ret = GoodsService::GoodsSpecType($params);
        if($ret['code'] == 0)
        {
            $ret['data'] = $ret['data']['spec_type'];
        }
        return $ret;
D
v1.2.0  
devil_gong 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190
    }

    /**
     * 商品规格信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-14
     * @desc    description
     */
    public function SpecDetail()
    {
        // 开始处理
        $params = $this->data_post;
D
devil_gong 已提交
191 192 193 194 195 196
        $ret = GoodsService::GoodsSpecDetail($params);
        if($ret['code'] == 0)
        {
            $ret['data'] = $ret['data']['spec_base'];
        }
        return $ret;
D
v1.2.0  
devil_gong 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    }

    /**
     * 商品分类
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-14
     * @desc    description
     */
    public function Category()
    {
        // 开始处理
        $params = $this->data_post;
        $data = GoodsService::GoodsCategory($params);
        return DataReturn('success', 0, $data);
    }
D
devil_gong 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

    /**
     * 商品评分
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-07-11
     * @desc    description
     * @return  [type]          [description]
     */
    public function GoodsScore()
    {
        if(empty($this->data_post['goods_id']))
        {
            return DataReturn('参数有误', -1);
        }

        // 获取商品评分
        return GoodsCommentsService::GoodsCommentsScore($this->data_post['goods_id']);
    }

    /**
     * 商品评论
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2019-05-13T21:47:41+0800
     */
    public function Comment()
    {
        // 参数
        $params = $this->data_post;

        // 分页
        $number = 10;
        $page = max(1, isset($params['page']) ? intval($params['page']) : 1);

        // 条件
        $where = [
            'goods_id'      => $params['goods_id'],
            'is_show'       => 1,
        ];

        // 获取总数
        $total = GoodsCommentsService::GoodsCommentsTotal($where);
        $page_total = ceil($total/$number);
        $start = intval(($page-1)*$number);

        // 获取列表
        $data_params = array(
            'm'         => $start,
            'n'         => $number,
            'where'     => $where,
            'is_public' => 1,
        );
        $data = GoodsCommentsService::GoodsCommentsList($data_params);
        
        // 返回数据
        $result = [
            'number'            => $number,
            'total'             => $total,
            'page_total'        => $page_total,
            'data'              => $data['data'],
        ];
        return DataReturn('success', 0, $result);
    }
D
v1.2.0  
devil_gong 已提交
280 281
}
?>