cmsContentTemplateSet = $cmsContentTemplateSet; $this->cmsContentTemplate = $cmsContentTemplate; $this->cmsSubjectProduct = $cmsSubjectProduct; $this->cmsSubject = $cmsSubject; } /** * 内容预览 */ public function preview($request) { $where = [ 'city_id' => $request['city_id'], 'apply_type' => $request['type'], 'is_open' => 1, 'status'=>1, 'deleted_at'=>null ]; $temalates = $this->cmsContentTemplate->select('title', 'id')->where($where)->orderBy('id','desc')->first(); if (!$temalates) { throw new HttpException(500, '没有找到对应模板'); } $group_array = []; $group_key = env('REDIS_HOME_GROUP_KEY'); $market_key = env('REDIS_HOME_MARKET_KEY'); //团购首页 if ($request['type'] == 0) { if (Cache::has($group_key)) { return Cache::store('redis')->get($group_key); } $group_array['apply_type'] = "group"; $group_array['title'] = $temalates->title; $group_array['content'] = []; }else {//菜市场首页 if (Cache::has($market_key)) { return Cache::store('redis')->get($market_key); } $group_array['apply_type'] = "market"; $group_array['title'] = $temalates->title; $group_array['content'] = []; } $group_array['content'][0]['area_type'] = "banner"; $banner_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 0,'status'=>1])->get(); $new_rule = []; foreach ($banner_rule->toArray() as $k => $v) { $decode_banner = \GuzzleHttp\json_decode($v['rule'], true); if (count($decode_banner)>0) { foreach ($decode_banner as $key=>$value){ $new_rule[$key]['id'] = $key; $new_rule[$key]['rule'] = $value; $new_rule[$key]['rule']['b_id'] = $v['id']; } } } $group_array['content'][0]['rule'] = $new_rule; $subject_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 1,'status'=>1])->get(); $new_rule1 = []; foreach ($subject_rule->toArray() as $k => $v) { $new_rule1[$k]['id'] = $v['id']; $new_rule1[$k]['rule'] = \GuzzleHttp\json_decode($v['rule'], true); } $floor_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 2,'status'=>1])->get(); $new_rule2 = []; foreach ($floor_rule->toArray() as $k => $v) { $new_rule2[$k]['id'] = $v['id']; $new_rule2[$k]['rule'] = \GuzzleHttp\json_decode($v['rule'], true); } foreach ($new_rule2 as $k => $v) { if ($v) { $rules = $v['rule']; $show_num = intval($rules['show_num']); $show_type = $this->cmsSubject->select('show_type')->where('id', $rules['link_url'])->first(); $product = $this->cmsSubjectProduct->where('subject_id', $rules['link_url'])->orderBy('sort', 'asc')->limit($show_num)->get(); $pro_array = $product->toArray(); $res_id = implode(",", array_column($pro_array, 'product_id')); $new_rule2[$k]['product_id'] = $res_id; $new_rule2[$k]['subject_id'] = $rules['link_url']; $new_rule2[$k]['show_type'] = $show_type->show_type ?? ''; unset($v['url']); unset($v['show_num']); unset($rules['link_url']); unset($v['link_type']); } } if ($request['type'] == 1) { $group_array['content'][1]['area_type'] = "category"; $category_rule = $this->cmsContentTemplateSet->select('id', 'rule')->where(['tpl_id' => $temalates->id, 'area_type' => 3,'status'=>1])->get(); $new_rule3 = []; foreach ($category_rule->toArray() as $k => $v) { $new_rule3[$k]['id'] = $v['id']; $new_rule3[$k]['rule'] = \GuzzleHttp\json_decode($v['rule'], true); } $group_array['content'][1]['rule'] = $new_rule3; $group_array['content'][2]['area_type'] = "special"; $group_array['content'][2]['rule'] = $new_rule1; $group_array['content'][3]['area_type'] = "floor"; $group_array['content'][3]['rule'] = $new_rule2; if (!Cache::has($market_key)) { Cache::store('redis')->put($market_key, $group_array, 600);//10分钟过期 } } else { $group_array['content'][1]['area_type'] = "special"; $group_array['content'][1]['rule'] = $new_rule1; $group_array['content'][2]['area_type'] = "floor"; $group_array['content'][2]['rule'] = $new_rule2; if (!Cache::has($group_key)) { Cache::store('redis')->put($group_key, $group_array, 600); } } return $group_array; } public function getTemplate($cityId) { return $this->cmsContentTemplate->where(['city_id' => $cityId, 'is_open' => 1,'status'=>1])->where('deleted_at', null)->select('id','title', 'apply_type')->orderBy('apply_type', 'asc')->get(); } public function productList($request) { $area_type = ''; if ($request['area_type'] == 'floor') { $area_type = 2; } $template_set = $this->cmsContentTemplateSet->select('rule')->where(['area_type' => $area_type, 'id' => $request['id']])->first(); if (!$template_set) { throw new HttpException(500, '没有找到对应模板内容'); } $template_array = json_decode($template_set->rule, true); $product = $this->cmsSubjectProduct->where('subject_id', $template_array['link_url'])->orderBy('sort', 'asc')->get(); $pro_array = $product->toArray(); $res_id = implode(",", array_column($pro_array, 'product_id')); try { $sign = generateSign(['ids' => $res_id], config('customer.app_secret')); $url = config("customer.app_service_url") . '/product/homeProduct'; $array = [ 'json' => ['sign' => $sign, 'ids' => $res_id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()] ]; return http($url, $array, 'get'); } catch (\Exception $e) { return []; } } }