Goods.php 8.8 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',
D
devil_gong 已提交
88
                '<td',
G
gongfuxiang 已提交
89 90
            ];
            $replace = [
D
devil_gong 已提交
91
                '<img style="max-width:100%;margin:0;padding:0;display:block;" ',
G
gongfuxiang 已提交
92 93
                '<div',
                '/div>',
D
1.6  
devil_gong 已提交
94 95
                '<p style="margin:0;">',
                '<div style="margin:0;">',
D
devil_gong 已提交
96 97 98
                '<table style="width:100%;margin:0px;border-collapse:collapse;border-color:#ddd;border-style:solid;border-width:0 1px 1px 0;"',
                '<tr style="border-top:1px solid #ddd;"',
                '<td style="margin:0;padding:5px;border-left:1px solid #ddd;"',
G
gongfuxiang 已提交
99 100 101
            ];
            $ret['data'][0]['content_web'] = str_replace($search, $replace, $ret['data'][0]['content_web']);
        }
D
v1.2.0  
devil_gong 已提交
102 103 104

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

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

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

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

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

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

        // 秒杀
        if($result['common_app_is_limitedtimediscount'] == 1)
        {
G
gongfuxiang 已提交
134
            $ret = CallPluginsServiceMethod('limitedtimediscount', 'Service', 'GoodsDetailCountdown', $goods_id);
G
gongfuxiang 已提交
135 136 137 138 139
            if($ret['code'] == 0)
            {
                $result['plugins_limitedtimediscount_data'] = $ret['data'];
            }
        }
D
v1.2.0  
devil_gong 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153
        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 已提交
154
        $this->IsLogin();
D
v1.2.0  
devil_gong 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

        // 开始操作
        $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 已提交
174 175 176 177 178 179
        $ret = GoodsService::GoodsSpecType($params);
        if($ret['code'] == 0)
        {
            $ret['data'] = $ret['data']['spec_type'];
        }
        return $ret;
D
v1.2.0  
devil_gong 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193
    }

    /**
     * 商品规格信息
     * @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 已提交
194 195 196 197 198 199
        $ret = GoodsService::GoodsSpecDetail($params);
        if($ret['code'] == 0)
        {
            $ret['data'] = $ret['data']['spec_base'];
        }
        return $ret;
D
v1.2.0  
devil_gong 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    }

    /**
     * 商品分类
     * @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 已提交
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 280 281 282

    /**
     * 商品评分
     * @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);
    }
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

    /**
     * 商品海报
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2019-08-17T21:10:41+0800
     */
    public function Poster()
    {
        // 是否开启海报功能
        if(MyC('common_app_is_poster_share') == 1)
        {
            return CallPluginsServiceMethod('distribution', 'PosterGoodsService', 'GoodsCreateMiniWechat', $this->data_post);
        }
        return DataReturn('海报功能未启用', -100);
    }
D
v1.2.0  
devil_gong 已提交
300 301
}
?>