where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 1])->orderBy('sort desc')->select('id, name as title, desc as subtitle, pic_url as icon')->asArray()->all(); if (empty($cats)) { return $this->asJson([ 'code' => 1, 'msg' => '分类未找到' ]); } $store = Store::findOne(get_store_id()); $name = $store->name; $store_lat = ''; $store_long = ''; $coordinate = explode(',', $store->coordinate); if ($coordinate) { $store_lat = $coordinate[0]; $store_long = $coordinate[1]; } $distance = Tools::getDistance($lat, $lng, $store_lat, $store_long); foreach ($cats as $key => &$cat) { $query = Goods::find()->where(['store_id' => $store_id, 'cat_id' => $cat['id'], 'is_delete' => 0, 'status' => 1])->andWhere(['!=', 'md_food_id', 0]); if ($type) { $query->andWhere(['like', 'delivery_type', $type]); } $goods_list = $query->select('id, name, cover_pic as goods_pic, key_word as desc, price, original_price, attr, use_attr, virtual_sales as sales')->orderBy('sort desc')->asArray()->all(); if (empty($goods_list)) { // 如果当前分类下无商品,不作展示 unset($cats[$key]); continue; } foreach ($goods_list as $k => $goods) { $goods_list[$k]['attr'] = Json::decode($goods['attr']); $goods_info = Goods::findOne($goods['id']); $goods_list[$k]['attr_group_list'] = $goods_info->getAttrGroupList(); $goods_list[$k]['num'] = $goods_info->getNum(); } $cat['list'] = $goods_list; } sort($cats); // 轮播图 $banner_arr = []; $banners = Banner::find()->where(['store_id' => $store_id, 'type' => Banner::TYPE_FOOD, 'is_delete' => 0 ])->orderBy('sort desc')->asArray()->all(); if (!empty($banners)) { foreach ($banners as $value) { $banner_arr[] = [ 'image' => $value['pic_url'], 'link' => $value['page_url'], 'title' => $value['title'] ]; } } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'goods_list' => $cats, 'md_name' => $name, 'distance' => $distance, 'banner' => $banner_arr ] ]); } /** * 店铺信息 */ public function actionMdInfo() { $store = Store::findOne(get_store_id()); $md_info['name'] = $store->name; $md_info['mobile'] = $store->contact_tel; $md_info['address'] = $store->address; $md_info['province_name'] = isset(District::findOne($store->province_id)->name)? District::findOne($store->province_id)->name:''; $md_info['city_name'] = isset(District::findOne($store->city_id)->name)?District::findOne($store->city_id)->name:''; $md_info['district_name'] = isset(District::findOne($store->district_id)->name)?District::findOne($store->district_id)->name:''; $md_info['cover_url'] = $store->logo; $md_info['open_status'] = $store->open_status; $md_info['shop_time_type'] = $store->shop_time_type; $md_info['latitude'] = ''; $md_info['longitude'] = ''; $coordinate = explode(',', $store->coordinate); if ($coordinate) { $md_info['latitude'] = $coordinate[0]; $md_info['longitude'] = $coordinate[1]; } $md_info['start_time'] = $store->s_time; $md_info['end_time'] = $store->e_time; return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $md_info ]); } public function actionGoodsDetail() { $goods = Goods::findOne(['store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1, 'id' => get_params('id')]); if (!$goods) { return $this->asJson([ 'code' => 1, 'msg' => '商品不存在' ]); } $goods_info = [ 'goods_pic' => array_column(GoodsPic::find()->where(['goods_id' => $goods->id, 'is_delete' => 0])->select('pic_url')->asArray()->all(), 'pic_url'), 'detail' => $goods->detail, 'name' => $goods->name, 'desc' => $goods->key_word, 'price' => $goods->price, 'original_price' => $goods->original_price, 'id' => $goods->id, 'sales' => $goods->virtual_sales, 'attr_group_list' => $goods->getAttrGroupList(), 'attr' => $goods->attr, 'use_attr' => $goods->use_attr, 'num' => $goods->getNum(), ]; return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => $goods_info ]); } /** * 添加购物车 */ public function actionAddCart() { $post = post_params(); $md_id = 0; $form = new FoodAddCartForm(); $form->store_id = get_store_id(); $form->user_id = 0; $form->saas_id = get_saas_user_id(); $form->md_id = 0; if (isset($post['flag_id']) && !empty($post['flag_id'])) { $form->flag_id = $post['flag_id']; } else if (isset($post['table_num']) && !empty($post['table_num'])) { $food_flag = FoodFlag::findOne(['store_id' => get_store_id(), 'md_id' => $md_id, 'table_num' => $post['table_num'], 'status' => 0, 'type' => 1]); if ($food_flag) { $form->flag_id = $food_flag->id; } else { $food_flag = new FoodFlag(); $food_flag->md_id = $md_id; $food_flag->store_id = get_store_id(); $food_flag->table_num = $post['table_num']; $food_flag->type = 1; $food_flag->user_id = 0; $food_flag->saas_id = get_saas_user_id(); $food_flag->save(); $form->flag_id = $food_flag->id; } } else { $food_flag = new FoodFlag(); $food_flag->md_id = $md_id; $food_flag->store_id = get_store_id(); $food_flag->table_num = 0; $food_flag->type = 0; $food_flag->user_id = 0; $food_flag->saas_id = get_saas_user_id(); $food_flag->save(); $form->flag_id = $food_flag->id; } if (isset($post['attr_list']) && !empty($post['attr_list'])) { $attr_list = Json::decode($post['attr_list']); $form->goods_id = $post['goods_id']; $error = 0; foreach ($attr_list as $value){ $form->attr = Json::encode($value['attr_list']); $form->num = $value['number']; $status = $form->save(); if($status['code'] != 0){ $error++; } } if ($error > 0) { return $this->asJson([ 'code' => 0, 'data' => [ 'flag_id' => $form->flag_id ], 'msg' => '添加成功,' . $error . "个失败", ]); } else { return $this->asJson([ 'code' => 0, 'data' => [ 'flag_id' => $form->flag_id ], 'msg' => '添加成功' ]); } } else { $form->attributes = post_params(); return $this->asJson($form->save()); } } public function actionQueryGoods() { $flag_id = get_params('flag_id'); $form = new FoodCartListForm(); $form->store_id = get_store_id(); $form->table_num = get_params('table_num', 0); $form->flag_id = $flag_id; return $this->asJson($form->search()); } /** * 购物车详情 */ public function actionUpdateNum() { $cart = FoodCart::findOne(['id' => post_params('cart_id', 0), 'is_delete' => 0]); if (empty($cart)) { return $this->asJson([ 'code' => 1, 'msg' => '参数错误' ]); } $cart->num = (int)post_params('num'); if ($cart->save()) { if ($cart->num == 0) { $cart->is_delete = 1; $cart->save(); } return $this->asJson([ 'code' => 0, 'msg' => '保存成功' ]); } else { return $this->asJson([ 'code' => 1, 'msg' => '保存失败' ]); } } public function actionClearCart() { $flag_id = post_params('flag_id'); $table_num = get_params('table_num'); if (empty($flag_id)) { $food_flag = FoodFlag::findOne(['store_id' => get_store_id(), 'table_num' => $table_num, 'md_id' => 0, 'status' => 0]); $flag_id = $food_flag->id; } else { $food_flag = FoodFlag::findOne(['store_id' => get_store_id(), 'id' => $flag_id, 'md_id' => 0, 'status' => 0]); } if (empty($flag_id)) { return $this->asJson([ 'code' => 1, 'msg' => '参数异常' ]); } if (!$food_flag) { return $this->asJson([ 'code' => 1, 'msg' => '数据异常' ]); } $user_id = post_params('user_id', 0); $params = [ 'flag_id' => $flag_id, 'is_delete' => 0 ]; if (!empty($user_id)) { $params['saas_id'] = $user_id; } // $saas_id = get_saas_user_id(); // $params['saas_id'] = $saas_id; FoodCart::updateAll(['is_delete' => 1], $params); return $this->asJson([ 'code' => 0, 'msg' => '清空成功' ]); } public function actionClose() { $flag_id = post_params('flag_id'); $user_id = 0; $saas_id = get_saas_user_id(); $food_flag = FoodFlag::findOne(['id' => $flag_id, 'status' => 0, 'md_id' => 0, 'store_id' => get_store_id()]); if ($food_flag->saas_id != $saas_id) { return $this->asJson([ 'code' => 1, 'msg' => '只有发起人才能清空购物车' ]); } if (empty($flag_id)) { return $this->asJson([ 'code' => 1, 'msg' => '参数异常' ]); } FoodCart::updateAll(['is_delete' => 1], ['flag_id' => $flag_id]); FoodFlag::updateAll(['status' => 1], ['id' => $flag_id]); return $this->asJson([ 'code' => 0, 'msg' => '关单成功' ]); } public function actionUserComplete() { $flag_id = post_params('flag_id'); $user_id = get_user_id(); $food_flag = FoodFlag::findOne(['id' => $flag_id, 'status' => 0, 'md_id' => 0, 'store_id' => get_store_id()]); if (!$food_flag) { return $this->asJson([ 'code' => 1, 'msg' => '数据异常' ]); } if (empty($flag_id)) { return $this->asJson([ 'code' => 1, 'msg' => '参数异常' ]); } FoodCart::updateAll(['status' => 1], ['flag_id' => $flag_id, 'user_id' => 0,'saas_id'=>get_saas_user_id(), 'is_delete' => 0]); return $this->asJson([ 'code' => 0, 'msg' => '成功' ]); } public function actionCompleteStatus() { $flag_id = get_params('flag_id'); $user_id = get_user_id(); $food_cart = FoodCart::findOne(['flag_id' => $flag_id, 'user_id' =>0,'saas_id'=>get_saas_user_id(), 'is_delete' => 0]); return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'status' => $food_cart->status ] ]); } public function actionFlag() { $food_flag = new FoodFlag(); $food_flag->md_id = 0; $food_flag->store_id = get_store_id(); $food_flag->table_num = 0; $food_flag->type = 0; $food_flag->status = 0; $food_flag->user_id = 0; $food_flag->saas_id = get_saas_user_id(); $food_flag->save(); return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'flag_id' => $food_flag->id ] ]); } public function actionStatus() { $flag_id = get_params('flag_id'); $store_id = get_store_id(); $md_id = 0; $food_flag = FoodFlag::findOne(['id' => $flag_id, 'store_id' => $store_id, 'md_id' => 0]); if (!$food_flag) { return $this->asJson([ 'code' => 1, 'msg' => '未找到数据' ]); } return $this->asJson([ 'code' => 0, 'msg' => 'success', 'data' => [ 'table_num' => $food_flag->table_num, 'type' => $food_flag->type, 'order_id' => $food_flag->order_id, 'user_id' => $food_flag->saas_id, 'saas_id' => $food_flag->saas_id, 'status' => $food_flag->status ] ]); } }