'ID', 'store_id' => 'Store ID', 'brand_name' => '品牌名称', ]; } public function getBrandList() { $brand_name = $this->brand_name; $query = GoodsBrand::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'is_show' => 1]) ->select('id, brand_name, brand_logo, brand_desc'); if (!empty(trim($this->brand_name))) { $query->andWhere(['LIKE', 'brand_name', $brand_name]); } $goods_brand = $query->orderBy('sort DESC')->asArray()->all(); $new_arr = []; foreach ($goods_brand as &$item) { $f_char = $this->getFirstCharter($item['brand_name']); $new_arr[$f_char][] = $item; } ksort($new_arr); if (key($new_arr) === '#') { $first_item = $new_arr['#']; array_splice($new_arr, 0, 1); $new_arr = array_merge($new_arr, ['#' => $first_item]); } return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'list' => $new_arr ] ]; } public function getList() { $brand_name = $this->brand_name; $brand_cat_id = $this->brand_cat_id; $user_id = $this->user_id; $store_id = $this->store_id; $query = GoodsBrand::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 1]) ->select('id, brand_name, brand_logo, brand_desc'); if (!empty(trim($this->brand_name))) { $query->andWhere(['LIKE', 'brand_name', $brand_name]); } if ($brand_cat_id > 0) { $query->andWhere(['brand_cat_id' => $brand_cat_id]); } elseif ($brand_cat_id < 0) { $brand_id = GoodsBrandSubscribeLog::getUserSubscribeLog($user_id); $query->andWhere(['id' => $brand_id]); } $query->orderBy('sort DESC'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $item['goods_num'] = count(Goods::getBrandGoodsList($item['id'], $store_id)); $item['brand_desc'] = $item['brand_desc'] ?: ''; } return [ 'code' => 0, 'msg' => '获取成功', 'data' => $list ]; } public function getCatList() { try { $store_id = $this->store_id; $user_id = $this->user_id; $goodsBrandCatList = GoodsBrandCat::getList($store_id); array_unshift($goodsBrandCatList, ['id' => 0, 'brand_cat_name' => '全部'], ['id' => -1, 'brand_cat_name' => '我的订阅']); foreach ($goodsBrandCatList as &$item) { if ($item['id'] < 0) { $item['brand_count'] = count(GoodsBrandSubscribeLog::getUserSubscribeLog($user_id)); } else { $item['brand_count'] = count(GoodsBrand::getList($store_id, $item['id'])); } } return [ 'code' => 0, 'msg' => '', 'data' => [ 'list' => $goodsBrandCatList ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function userBrandSubscribe() { try { $user_id = $this->user_id; $id = $this->id; $store_id = $this->store_id; $goodsBrand = GoodsBrand::getBrand($id, $store_id); if (!$goodsBrand) { throw new \Exception('查询品牌失败'); } //查询是否关注当前品牌 $userSubscribeLog = GoodsBrandSubscribeLog::getUserSubscribeLog($user_id); if (in_array($id, $userSubscribeLog)) { //已经关注就取消关注 GoodsBrandSubscribeLog::deleteAll(['user_id' => $user_id, 'goods_brand_id' => $id]); return [ 'code' => 0, 'msg' => '取消订阅成功' ]; } $model = new GoodsBrandSubscribeLog(); $model->user_id = $user_id; $model->goods_brand_id = $id; if (!$model->save()) { throw new \Exception('操作失败'); } return [ 'code' => 0, 'msg' => '订阅成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取品牌详情 public function brandInfo() { try { $id = $this->id; $store_id = $this->store_id; $user_id = $this->user_id; $md_id = $this->md_id; $goods_list = Goods::getBrandGoodsList($id, $store_id); $data = []; $goodsBrand = GoodsBrand::getBrand($id, $store_id); $data['brand'] = [ 'brand_name' => $goodsBrand->brand_name, 'brand_logo' => $goodsBrand->brand_logo, 'brand_desc' => $goodsBrand->brand_desc ?: '', 'brand_bg' => $goodsBrand->brand_bg ?: '', 'goods_num' => count($goods_list), 'subscribe_num' => count(GoodsBrandSubscribeLog::getBrandList($id)), 'is_subscribe' => GoodsBrandSubscribeLog::isSubscribe($user_id, $id) ? 1 : 0 ]; $goods_id = array_column($goods_list, 'id'); $goods_cat = GoodsCat::find()->where(['goods_id' => $goods_id])->select('cat_id')->column(); $goods_cat = array_unique($goods_cat); $goods_cat_id = []; foreach ($goods_cat as $goods_cat_item) { $first_cat = Cat::find()->where(['id' => $goods_cat_item, 'is_delete' => 0])->select('parent_id') ->asArray()->one(); if ($first_cat) { if (!intval($first_cat['parent_id'])) { $goods_cat_id[] = $goods_cat_item; } else { $second_cat = Cat::find()->where(['id' => $first_cat['parent_id'], 'is_delete' => 0])->select('parent_id') ->asArray()->one(); if ($second_cat) { if (!intval($second_cat['parent_id'])) { $goods_cat_id[] = $first_cat['parent_id']; } else { $third_cat = Cat::find()->where(['id' => $second_cat['parent_id'], 'is_delete' => 0])->select('parent_id') ->asArray()->one(); if ($third_cat) { $goods_cat_id[] = $second_cat['parent_id']; } } } } } } $data['cat_list'] = Cat::find()->where(['id' => $goods_cat_id])->select('id, name')->asArray()->all(); $cartQuery = Cart::find()->alias('c')->leftJoin(['g' => Goods::tableName()], 'c.goods_id = g.id') ->where(['c.store_id' => $store_id, 'c.user_id' => $user_id, 'c.is_delete' => 0, 'g.status' => 1, 'g.is_delete' => 0]); if ($md_id < 0) { $md_id = [-1, 0]; } $data['cart_count'] = $cartQuery->andWhere(['c.md_id' => $md_id])->select('c.goods_id, c.num') ->sum('c.num') ?: 0; return [ 'code' => 0, 'msg' => '获取成功', 'data' => $data ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function getFirstCharter($str) { if (empty($str)) { return ''; } $f_char = ord($str{0}); if ($f_char >= ord('A') && $f_char <= ord('z')) return strtoupper($str{0}); $s1 = iconv('UTF-8', 'gb2312', $str); $s2 = iconv('gb2312', 'UTF-8', $s1); $s = $s2 == $str ? $s1 : $str; $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; if ($asc >= -20319 && $asc <= -20284) return 'A'; if ($asc >= -20283 && $asc <= -19776) return 'B'; if ($asc >= -19775 && $asc <= -19219) return 'C'; if ($asc >= -19218 && $asc <= -18711) return 'D'; if ($asc >= -18710 && $asc <= -18527) return 'E'; if ($asc >= -18526 && $asc <= -18240) return 'F'; if ($asc >= -18239 && $asc <= -17923) return 'G'; if ($asc >= -17922 && $asc <= -17418) return 'H'; if ($asc >= -17417 && $asc <= -16475) return 'J'; if ($asc >= -16474 && $asc <= -16213) return 'K'; if ($asc >= -16212 && $asc <= -15641) return 'L'; if ($asc >= -15640 && $asc <= -15166) return 'M'; if ($asc >= -15165 && $asc <= -14923) return 'N'; if ($asc >= -14922 && $asc <= -14915) return 'O'; if ($asc >= -14914 && $asc <= -14631) return 'P'; if ($asc >= -14630 && $asc <= -14150) return 'Q'; if ($asc >= -14149 && $asc <= -14091) return 'R'; if ($asc >= -14090 && $asc <= -13319) return 'S'; if ($asc >= -13318 && $asc <= -12839) return 'T'; if ($asc >= -12838 && $asc <= -12557) return 'W'; if ($asc >= -12556 && $asc <= -11848) return 'X'; if ($asc >= -11847 && $asc <= -11056) return 'Y'; if ($asc >= -11055 && $asc <= -10247) return 'Z'; return '#'; } }