Goods.php 11.1 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;
D
Devil 已提交
14
use app\service\BuyService;
G
gongfuxiang 已提交
15
use app\service\GoodsCommentsService;
D
v1.2.0  
devil_gong 已提交
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 54

/**
 * 商品
 * @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 已提交
55 56 57
        // 商品详情方式
        $is_use_mobile_detail = intval(MyC('common_app_is_use_mobile_detail'));

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

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

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

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

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

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

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

D
v1.2.0  
devil_gong 已提交
123 124
        // 数据返回
        $result = [
G
gongfuxiang 已提交
125
            'goods'                             => $ret['data'][0],
D
devil_gong 已提交
126
            'common_order_is_booking'           => (int) MyC('common_order_is_booking'),
G
gongfuxiang 已提交
127
            'common_app_is_use_mobile_detail'   => $is_use_mobile_detail,
D
devil_gong 已提交
128
            'common_app_is_online_service'      => (int) MyC('common_app_is_online_service'),
G
gongfuxiang 已提交
129
            'common_app_is_limitedtimediscount' => (int) MyC('common_app_is_limitedtimediscount'),
D
devil_gong 已提交
130
            'common_app_is_good_thing'          => (int) MyC('common_app_is_good_thing'),
131
            'common_app_is_poster_share'        => (int) MyC('common_app_is_poster_share'),
D
Devil 已提交
132
            'common_cart_total'                 => BuyService::UserCartTotal(['user'=>$this->user]),
D
debug  
devil_gong 已提交
133
            'customer_service_tel'              => MyC('common_app_customer_service_tel', null, true),
D
devil_gong 已提交
134
            'common_is_goods_detail_show_photo' => MyC('common_is_goods_detail_show_photo', 0, true),
D
devil_gong 已提交
135 136 137 138

            // 站点模式
            'common_site_type'                  => (int) MyC('common_site_type', 0, true),
            'common_is_exhibition_mode_btn_text'=> MyC('common_is_exhibition_mode_btn_text', '立即咨询', true),
D
devil_gong 已提交
139 140

            // 优惠劵
D
devil_gong 已提交
141
            'plugins_coupon_data'               => $this->PluginsCouponGoods($goods_id),
D
v1.2.0  
devil_gong 已提交
142
        ];
G
gongfuxiang 已提交
143

D
devil_gong 已提交
144 145 146 147 148 149 150
        // 支付宝小程序在线客服
        if(APPLICATION_CLIENT_TYPE == 'alipay')
        {
            $result['common_app_mini_alipay_tnt_inst_id'] = MyC('common_app_mini_alipay_tnt_inst_id', null, true);
            $result['common_app_mini_alipay_scene'] = MyC('common_app_mini_alipay_scene', null, true);
        }

G
gongfuxiang 已提交
151 152 153
        // 秒杀
        if($result['common_app_is_limitedtimediscount'] == 1)
        {
G
gongfuxiang 已提交
154
            $ret = CallPluginsServiceMethod('limitedtimediscount', 'Service', 'GoodsDetailCountdown', $goods_id);
G
gongfuxiang 已提交
155 156 157 158 159
            if($ret['code'] == 0)
            {
                $result['plugins_limitedtimediscount_data'] = $ret['data'];
            }
        }
D
v1.2.0  
devil_gong 已提交
160 161 162
        return DataReturn('success', 0, $result);
    }

D
devil_gong 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 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    2019-10-17
     * @desc    description
     * @param   [int]          $goods_id [商品id]
     */
    private function PluginsCouponGoods($goods_id)
    {
        // 获取基础配置信息
        $base = CallPluginsData('coupon');

        // 优惠劵列表
        $coupon_params = [
            'where'             => [
                'is_enable'         => 1,
                'is_user_receive'   => 1,
            ],
            'm'                 => 0,
            'n'                 => 0,
            'is_sure_receive'   => 1,
            'user'              => $this->user,
        ];
        $ret = CallPluginsServiceMethod('coupon', 'CouponService', 'CouponList', $coupon_params);

        // 排除商品不支持的活动
D
devil_gong 已提交
191 192 193 194
        if(!empty($ret['data']))
        {
            $ret['data'] = CallPluginsServiceMethod('coupon', 'BaseService', 'CouponListGoodsExclude', ['data'=>$ret['data'], 'goods_id'=>$goods_id]);
        }
D
devil_gong 已提交
195 196 197 198 199 200 201 202

        // 返回数据
        return [
            'base'  => $base['data'],
            'data'  => $ret['data'],
        ];
    }

D
v1.2.0  
devil_gong 已提交
203 204 205 206 207 208 209 210 211 212 213
    /**
     * 用户商品收藏
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-17
     * @desc    description
     */
    public function Favor()
    {
        // 登录校验
G
gongfuxiang 已提交
214
        $this->IsLogin();
D
v1.2.0  
devil_gong 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

        // 开始操作
        $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 已提交
234 235 236 237 238 239
        $ret = GoodsService::GoodsSpecType($params);
        if($ret['code'] == 0)
        {
            $ret['data'] = $ret['data']['spec_type'];
        }
        return $ret;
D
v1.2.0  
devil_gong 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253
    }

    /**
     * 商品规格信息
     * @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 已提交
254 255 256 257 258 259
        $ret = GoodsService::GoodsSpecDetail($params);
        if($ret['code'] == 0)
        {
            $ret['data'] = $ret['data']['spec_base'];
        }
        return $ret;
D
v1.2.0  
devil_gong 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    }

    /**
     * 商品分类
     * @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 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

    /**
     * 商品评分
     * @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
     */
D
devil_gong 已提交
305
    public function Comments()
D
devil_gong 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    {
        // 参数
        $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);
    }
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

    /**
     * 商品海报
     * @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 已提交
360 361
}
?>