&$image) { $image && $image = cdnurl($image, true); } return array_filter($images); } public function setImagesAttr($value, $data) { return is_array($value) ? implode(',', $value) : $value; } public function getContentAttr($value, $data) { //组装卡片信息 return Service::formatSourceTpl($value); } public static function getIndexGoodsList() { return self::where('status', 'normal') ->where("FIND_IN_SET('recommend',`flag`)") ->order('weigh desc') ->limit(12) ->cache(false) ->select(); } /** * 获取SQL查询结果 */ public static function getQueryList($params) { $sql = $params['sql'] ?? ''; $bind = isset($params['bind']) ? explode(',', $params['bind']) : []; list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('sql', $params); $list = Cache::tag('shop')->get($cacheKey); if (!$list) { $list = Db::query($sql, $bind); Cache::tag('shop')->set($cacheKey, $list, $cacheExpire); } return $list; } public static function refreshStar($goods_id) { $goods = self::get($goods_id); if ($goods) { $one = Comment::where('goods_id', $goods_id)->field("COUNT(*) as nums,SUM(star) as amount")->find(); if ($one) { $goods->star = floor($one['amount'] / $one['nums']); $goods->save(); } } } /** * 获取商品列表 * @param $params * @return array|false|\PDOStatement|string|\think\Collection */ public static function getGoodsList($params) { $type = empty($params['type']) ? '' : $params['type']; $category = !isset($params['category']) ? '' : $params['category']; $condition = empty($params['condition']) ? '' : $params['condition']; $field = empty($params['field']) ? '*' : $params['field']; $flag = empty($params['flag']) ? '' : $params['flag']; $row = empty($params['row']) ? 10 : (int)$params['row']; $orderby = empty($params['orderby']) ? 'createtime' : $params['orderby']; $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']); $limit = empty($params['limit']) ? $row : $params['limit']; $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc'; $paginate = !isset($params['paginate']) ? false : $params['paginate']; $page = !isset($params['page']) ? 1 : (int)$params['page']; $with = !isset($params['with']) ? 'category' : $params['with']; list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('goodslist', $params); $where = ['status' => 'normal']; $where['deletetime'] = ['exp', Db::raw('IS NULL')]; self::$tagCount++; $goodsModel = self::with($with)->alias('a'); if ($category !== '') { $categoryIds = []; if ($type === 'son') { $subQuery = Category::where('parent_id', 'in', $category)->field('id')->buildSql(); //子级 $categoryIds = Db::query($subQuery); $categoryIds = array_column($categoryIds, 'id'); } elseif ($type === 'sons') { //所有子级 $categoryIds = Category::getCategoryChildrenIds($category); } else { $categoryIds = is_array($category) ? $category : explode(',', $category); } if (!empty($categoryIds)) { $condition .= ($condition ? ' AND ' : '') . '('; $conditionParts = []; foreach ($categoryIds as $categoryId) { $conditionParts[] = "FIND_IN_SET('{$categoryId}', category_ids)"; } $condition .= implode(' OR ', $conditionParts) . ')'; } } //如果有设置标志,则拆分标志信息并构造condition条件 if ($flag !== '') { if (stripos($flag, '&') !== false) { $arr = []; foreach (explode('&', $flag) as $k => $v) { $arr[] = "FIND_IN_SET('{$v}', flag)"; } if ($arr) { $condition .= "(" . implode(' AND ', $arr) . ")"; } } else { $condition .= ($condition ? ' AND ' : ''); $arr = []; foreach (explode(',', str_replace('|', ',', $flag)) as $k => $v) { $arr[] = "FIND_IN_SET('{$v}', flag)"; } if ($arr) { $condition .= "(" . implode(' OR ', $arr) . ")"; } } } $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}"); $order = $orderby == 'weigh' ? $order . ',id DESC' : $order; $modelInfo = null; $prefix = config('database.prefix'); $goodsModel ->where($where) ->where($condition) ->field($field, false, $prefix . "shop_goods", "a") ->orderRaw($order); if ($paginate) { $paginateArr = explode(',', $paginate); $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row); $config = []; $config['var_page'] = $paginateArr[2] ?? (isset($params['page']) ? 'page' : 'apage' . self::$tagCount); $config['path'] = $paginateArr[3] ?? ''; $config['fragment'] = $paginateArr[4] ?? ''; $config['query'] = request()->get(); $config['page'] = $page; $list = $goodsModel->paginate($listRows, ($paginateArr[1] ?? false), $config); } else { $list = $goodsModel->limit($limit)->cache($cacheKey, $cacheExpire, 'shop')->select(); } return $list; } // public function Category() // { // // 由于现在使用category_ids字段,这里返回主分类(第一个分类) // return $this->belongsTo('Category', 'category_id', 'id'); // } public function Categories() { // 新增:获取商品的所有分类 $categoryIds = explode(',', $this->category_ids ?: ''); return Category::where('id', 'in', array_filter($categoryIds))->select(); } public function Sku() { return $this->hasMany('Sku', 'goods_id', 'id'); } public function Comment() { return $this->hasMany('Comment', 'goods_id', 'id'); } public function Brand() { return $this->belongsTo('Brand', 'brand_id', 'id', [], 'LEFT'); } }