10], [['keyword'], 'trim'], [['keyword'], 'string'], [['longitude', 'latitude'], 'double'] ]; } /** * 搜索页接口 */ public function search() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } // 搜索店铺 if ($this->cat->key == self::SEARCH_CAT_KEY_SHOP) { return $this->searchShop(); } $select = ['g.id', 'g.name', 'g.sort', 'g.created_at', 'g.price', 'g.cover_pic pic_url', 'g.store_id', 'g.status', 'g.is_delete']; switch ($this->cat->key) { case self::SEARCH_CAT_KEY_GOODS : $query = (new Query())->from(['g' => Goods::tableName()])->select($select) ->leftJoin(['m' => Mch::tableName()], 'm.id = g.mch_id')->andWhere([ 'or', ['g.mch_id' => 0], ['m.is_delete' => 0] ]); break; default: $query = (new Query())->from(['g' => Goods::tableName()])->select($select); break; } $query->andWhere(['g.status' => 1, 'g.is_delete' => 0]); if ($this->store_id) { $query->andWhere(['g.store_id' => $this->store_id]); } if ($this->keyword) { $query->andWhere(['LIKE', 'g.name', $this->keyword])->orWhere(['LIKE', 'g.key_word', $this->keyword]); } if ($this->mch_id) { $query->andWhere(['LIKE', 'g.mch_id', $this->mch_id]); } $count = $query->count(1); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); // 综合,自定义排序+时间最新 $query->orderBy('g.sort DESC, g.created_at DESC'); $list = $query->limit($pagination->limit)->offset($pagination->offset)->all(); foreach ($list as $k => $item) { $list[$k]['url'] = $this->cat->url . $item['id']; } $data = [ 'row_count' => $count, 'page_count' => $pagination->pageCount, 'list' => $list, ]; return [ 'code' => 0, 'msg' => 'success', 'data' => $data ]; } /** * 搜索店铺 * @return array * @throws \yii\db\Exception */ public function searchShop() { $count_sql = 'SELECT count(1) AS total FROM ' . Mch::tableName() . ' as m LEFT JOIN ' . Goods::tableName() . ' as g ON g.mch_id = m.id'; $count_sql .= ' WHERE m.store_id = :store_id AND g.store_id = :store_id AND '; $count_sql .= ' ((g.name LIKE "%' . $this->keyword . '%" AND g.is_delete = 0 AND g.status = 1) OR (m.name LIKE "%' . $this->keyword . '%" AND m.is_delete = 0 AND m.is_lock = 0))'; $count_sql .= ' GROUP BY m.id'; $count_sql = \Yii::$app->db->quoteSql($count_sql); $count = \Yii::$app->db->createCommand($count_sql, [ ':store_id' => get_store_id(), ])->queryScalar(); $juli_select = 'ROUND(6370.996*2*ASIN(SQRT(POW(SIN((:latitude*PI()/180-m.latitude*PI()/180)/2),2)+COS(:latitude*PI()/180)*COS(m.latitude*PI()/180)*POW(SIN((:longitude*PI()/180-m.longitude*PI()/180)/2),2))), 2) AS juli'; $sql = 'SELECT m.*, ' . $juli_select . ' FROM '.Mch::tableName() . ' as m LEFT JOIN ' . Goods::tableName() . ' as g ON g.mch_id = m.id'; $sql .= ' WHERE m.store_id = :store_id AND g.store_id = :store_id AND'; $sql .= ' ((g.name LIKE "%' . $this->keyword.'%" AND g.is_delete = 0 AND g.status = 1) OR (m.name LIKE "%' . $this->keyword . '%" AND m.is_delete = 0 AND m.is_lock = 0))'; $sql .= ' GROUP BY m.id ORDER BY m.longitude DESC, m.latitude DESC, juli ASC'; $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); $sql .= ' LIMIT ' . $pagination->offset . ',' . $pagination->limit; $sql = \Yii::$app->db->quoteSql($sql); $list = \Yii::$app->db->createCommand($sql, [ ':latitude' => $this->latitude, ':longitude' => $this->longitude, ':store_id' => get_store_id(), ])->queryAll(); foreach ($list as $key => $value) { $goods_list = Goods::find()->where([ 'is_delete' => 0, 'store_id' => get_store_id(), 'mch_id' => $value['id'], 'status' => 1, ])->select('id, cover_pic, name, price')->orderBy('id desc')->limit(6)->all(); $list[$key]['goods_list'] = $goods_list; } $data = [ 'row_count' => $count ? $count : 0, 'page_count' => $pagination->pageCount, 'list' => $list, ]; return [ 'code' => 0, 'msg' => 'success', 'data' => $data ]; } public function cats() { $list = [ [ 'id' => 0, 'name' => '商品', 'key' => 'goods', 'url' => '/goods/goods/goods?id=' ], [ 'id' => 1, 'name' => '店铺', 'key' => 'shop', 'url' => '/mch/shop/shop?mch_id=' ] ]; $cats = [ 'list' => $list, 'default_cat' => $list[0], ]; // TODO key值应该与插件名统一,这样循环就不用这么复杂 // 搜索分类根据插件权限显示 foreach ($cats['list'] as $k => $v) { } return [ 'code' => 0, 'msg' => 'success', 'data' => $cats ]; } }