| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- namespace app\api\controller;
- use app\admin\model\BannerModel;
- use app\admin\model\CapacityLoad;
- use app\admin\model\CompanyInfo;
- use app\admin\model\FactoryCategoryModel;
- use app\admin\model\FactoryModel;
- use app\admin\model\NewsModel;
- use app\common\controller\Api;
- /**
- * 首页接口
- */
- class Index extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 首页
- *
- */
- public function index()
- {
- $this->success('请求成功');
- }
- public function banner(){
- $type = $this->request->param("type",1);
- $host = $_SERVER['HTTP_HOST'];
- $list = BannerModel::where("type", $type)->order("sort", "desc")->select();
- foreach ($list as $k=>$v){
- $list[$k]['image'] = 'http://'.$host.$v['image'];
- }
- $this->success("ok", $list);
- }
- public function news(){
- $type = $this->request->param("type", "");
- $page = $this->request->param("page", 1);
- $limit = $this->request->param("limit", 10);
- $query = NewsModel::where(function ($query) use($type){
- if(isset($type) && $type != ""){
- $query->where("type", $type);
- }
- });
- $list = $query->order("id","desc")->paginate(['page'=>$page,'list_rows'=>$limit]);
- $this->success("ok", $list);
- }
- public function news_detail(){
- $id = $this->request->param("id", 0);
- $newsModel = new NewsModel();
- $list['now'] = $newsModel->where("id", $id)->find();
- $list['prev'] = $newsModel->getPrevArticle($id);
- $list['next'] = $newsModel->getNextArticle($id);
- NewsModel::where("id", $id)->setInc("browse",1);
- $this->success("ok", $list);
- }
- /**
- * 获取指定分类最新的公司动态内容
- *
- * @param int $category_id 公司动态分类ID
- */
- public function company_dynamic_latest()
- {
- $categoryId = $this->request->param('category_id/d', 0);
- $typeList = (new NewsModel())->getTypeList();
- if (!$categoryId || !isset($typeList[(string)$categoryId])) {
- $this->error('分类ID无效');
- }
- $news = NewsModel::where('type', $categoryId)
- ->order('updatetime', 'desc')
- ->order('id', 'desc')
- ->find();
- if (!$news) {
- $this->error('该分类暂无内容');
- }
- $data = $news->toArray();
- if (!empty($data['image']) && strpos($data['image'], 'http') !== 0) {
- $data['image'] = 'http://' . $this->request->host() . $data['image'];
- }
- foreach (['content', 'intro', 'business_scope'] as $field) {
- if (isset($data[$field])) {
- $data[$field] = trim(html_entity_decode(strip_tags($data[$field]), ENT_QUOTES, 'UTF-8'));
- }
- }
- $this->success('ok', $data);
- }
- /**
- * 获取最新公司信息
- */
- public function company_info()
- {
- $companyInfo = CompanyInfo::order('updatetime', 'desc')
- ->order('id', 'desc')
- ->find();
- if (!$companyInfo) {
- $this->error('暂无公司信息');
- }
- $this->success('ok', $companyInfo->toArray());
- }
- /**
- * 获取产能负载列表
- */
- public function capacity_load()
- {
- $list = CapacityLoad::field('id,name,capacity,percentage')
- ->order('weigh', 'desc')
- ->order('id', 'desc')
- ->select();
- $this->success('ok', collection($list)->toArray());
- }
- public function factory_category(){
- $page = $this->request->param("page", 1);
- $limit = $this->request->param("limit", 10);
- $list = FactoryCategoryModel::where("status", 1)->order("weigh","desc")->paginate(['page'=>$page,'list_rows'=>$limit]);
- $this->success("ok", $list);
- }
- public function factory(){
- $category_id = $this->request->param("category_id", 0);
- $page = $this->request->param("page", 1);
- $limit = $this->request->param("limit", 10);
- $list = [];
- if($category_id > 0){
- $list = FactoryModel::where("category_id", $category_id)
- ->where("status", 1)
- ->order("weigh","desc")
- ->paginate(['page'=>$page,'list_rows'=>$limit]);
- }
- $host = $_SERVER['HTTP_HOST'];
- foreach ($list as $k=>$v){
- $list[$k]['image'] = 'http://'.$host.$v['image'];
- }
- $this->success("ok", $list);
- }
- /**
- * 商品列表
- */
- public function get_goods_list(){
- $param = $this->request->param();
- $this->goods_model = new \app\admin\model\Goods;
- $this->category_model = new \app\admin\model\GoodsCategory();
- $goods = collection($this->goods_model->field('id,category_id,category_name,title,little_image')
- ->where(['deletetime'=>null])->select())->toArray();
- // 拼接图片路径
- foreach ($goods as $k=>$v){
- $host = $_SERVER['HTTP_HOST'];
- $goods[$k]['little_image'] = 'http://'.$host.$goods[$k]['little_image'];
- }
- // 获取分类
- $cate = collection($this->category_model->field('id,title,pid')->where(['status_switch'=>1])->select())->toArray();
- $cate = array_map(function($item){
- $item['goodsz_list'] = []; // 在结果数组中追加新字段和值
- return $item;
- }, $cate);
- // 组
- foreach ($cate as$k=>$v){
- foreach ($goods as $key=>$value){
- if ($v['id'] ==$value['category_id']){
- array_push($cate[$k]['goodsz_list'],$value);
- }
- }
- }
- $cate = $this->buildTree($cate);
- $this->success("ok", $cate);
- }
- /**
- * 商品详情
- */
- public function get_goods_info(){
- $id = $this->request->param('id');
- $this->goods_model = new \app\admin\model\Goods;
- $this->dynamic_model = new \app\admin\model\GoodsDynamic();
- $host = $_SERVER['HTTP_HOST'];
- $fields = 'id,category_id,category_name,title,price,little_image,images,describe,dynamic,content';
- $goods = $this->goods_model->field($fields)->where(['id'=>$id])->find()->toArray();
- $goods['dynamic_list'] = [];
- $goods['little_image'] = 'http://'.$host.$goods['little_image'];
- $temp = collection($this->dynamic_model->field('title,type_price')->where(['goods_id'=>$goods['id']])->select())->toArray();
- $goods['dynamic_list'] = $temp;
- $goods['images'] = !empty($goods['images'])?explode(',',$goods['images']):[];
- foreach ($goods['images'] as $k=>$v){
- $goods['images'][$k]= 'http://'.$host.$v;
- }
- $this->success("ok", $goods);
- }
- // 二维数组组装成树形数组
- public function buildTree(array $array, $parentId = 0)
- {
- $tree = array();
- foreach ($array as $item) {
- if ($item['pid'] == $parentId) {
- $children = $this->buildTree($array, $item['id']);
- if ($children) {
- $item['children'] = $children;
- }else{
- $item['children'] = [];
- }
- $tree[] = $item;
- }
- }
- return $tree;
- }
- // 产品下单
- public function get_goods_order_add(){
- $param = $this->request->param();
- $this->goods_model = new \app\admin\model\Goods;
- $this->goods_order_model = new \app\admin\model\GoodsOrder();
- $fields = 'id as goods_id,category_id,category_name,title,little_image,images,describe,dynamic';
- $info = $this->goods_model->field($fields)->where(['id'=>$param['goods_id']])->find()->toArray();
- $info['price'] = !empty($param['price'])?$param['price']:$info['price'];
- $info['num'] = $param['num'];
- $info['total_price'] = $info['price']*$param['num'];
- $info['dynamic_neme'] = $param['type'];
- $info['contact_name'] = $param['name'];
- $info['contact_phone'] = $param['phone'];
- $info['createtime'] = time();
- $info['updatetime'] = time();
- $res = $this->goods_order_model->insert($info);
- if ($res){
- $this->success("ok");
- }else{
- $this->error("error");
- }
- }
- // 产品下单
- public function set_msg_add(){
- $param = $this->request->param();
- $this->msg_model = new \app\admin\model\ContactMsg();
- $temp['name'] = $param['name'];
- $temp['phone'] = $param['phone'];
- $temp['email'] = $param['email'];
- $temp['createtime'] = time();
- $temp['updatetime'] = time();
- $res = $this->msg_model->insert($temp);
- if ($res){
- $this->success("ok");
- }else{
- $this->error("error");
- }
- }
- }
|