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

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

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

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

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

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

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

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

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

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

            // 站点模式
            '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
v1.2.0  
devil_gong 已提交
140
        ];
G
gongfuxiang 已提交
141

D
devil_gong 已提交
142 143 144 145 146 147 148
        // 支付宝小程序在线客服
        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);
        }

D
devil 已提交
149
        // 限时秒杀
G
gongfuxiang 已提交
150 151
        if($result['common_app_is_limitedtimediscount'] == 1)
        {
D
devil 已提交
152 153 154
            $ret = PluginsService::PluginsControlCall(
                'limitedtimediscount', 'index', 'goods', 'api', ['goods_id'=>$goods_id]);
            if($ret['code'] == 0 && isset($ret['data']['code']) && $ret['data']['code'] == 0)
G
gongfuxiang 已提交
155
            {
D
devil 已提交
156
                $result['plugins_limitedtimediscount_data'] = $ret['data']['data'];
G
gongfuxiang 已提交
157 158
            }
        }
D
devil_gong 已提交
159

D
devil 已提交
160 161 162 163
        // 优惠券
        $ret = PluginsService::PluginsControlCall(
                'coupon', 'index', 'goods', 'api', ['goods_id'=>$goods_id]);
        if($ret['code'] == 0 && isset($ret['data']['code']) && $ret['data']['code'] == 0)
D
devil_gong 已提交
164
        {
D
devil 已提交
165
            $result['plugins_coupon_data'] = $ret['data']['data'];
D
devil_gong 已提交
166
        }
D
devil_gong 已提交
167

D
devil 已提交
168
        return DataReturn('success', 0, $result);
D
devil_gong 已提交
169 170
    }

D
v1.2.0  
devil_gong 已提交
171 172 173 174 175 176 177 178 179 180 181
    /**
     * 用户商品收藏
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-07-17
     * @desc    description
     */
    public function Favor()
    {
        // 登录校验
G
gongfuxiang 已提交
182
        $this->IsLogin();
D
v1.2.0  
devil_gong 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

        // 开始操作
        $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 已提交
202 203 204 205 206 207
        $ret = GoodsService::GoodsSpecType($params);
        if($ret['code'] == 0)
        {
            $ret['data'] = $ret['data']['spec_type'];
        }
        return $ret;
D
v1.2.0  
devil_gong 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221
    }

    /**
     * 商品规格信息
     * @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 已提交
222 223 224 225 226 227
        $ret = GoodsService::GoodsSpecDetail($params);
        if($ret['code'] == 0)
        {
            $ret['data'] = $ret['data']['spec_base'];
        }
        return $ret;
D
v1.2.0  
devil_gong 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    }

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

    /**
     * 商品评分
     * @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 已提交
273
    public function Comments()
D
devil_gong 已提交
274 275 276 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 305 306 307 308 309 310
    {
        // 参数
        $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 已提交
311 312
}
?>