Index.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\BannerModel;
  4. use app\admin\model\FactoryCategoryModel;
  5. use app\admin\model\FactoryModel;
  6. use app\admin\model\NewsModel;
  7. use app\common\controller\Api;
  8. /**
  9. * 首页接口
  10. */
  11. class Index extends Api
  12. {
  13. protected $noNeedLogin = ['*'];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 首页
  17. *
  18. */
  19. public function index()
  20. {
  21. $this->success('请求成功');
  22. }
  23. public function banner(){
  24. $type = $this->request->param("type",1);
  25. $host = $_SERVER['HTTP_HOST'];
  26. $list = BannerModel::where("type", $type)->order("sort", "desc")->select();
  27. foreach ($list as $k=>$v){
  28. $list[$k]['image'] = 'http://'.$host.$v['image'];
  29. }
  30. $this->success("ok", $list);
  31. }
  32. public function news(){
  33. $type = $this->request->param("type", "");
  34. $page = $this->request->param("page", 1);
  35. $limit = $this->request->param("limit", 10);
  36. $query = NewsModel::where(function ($query) use($type){
  37. if(isset($type) && $type != ""){
  38. $query->where("type", $type);
  39. }
  40. });
  41. $list = $query->order("id","desc")->paginate(['page'=>$page,'list_rows'=>$limit]);
  42. $this->success("ok", $list);
  43. }
  44. public function news_detail(){
  45. $id = $this->request->param("id", 0);
  46. $newsModel = new NewsModel();
  47. $list['now'] = $newsModel->where("id", $id)->find();
  48. $list['prev'] = $newsModel->getPrevArticle($id);
  49. $list['next'] = $newsModel->getNextArticle($id);
  50. NewsModel::where("id", $id)->setInc("browse",1);
  51. $this->success("ok", $list);
  52. }
  53. /**
  54. * 获取指定分类最新的公司动态内容
  55. *
  56. * @param int $category_id 公司动态分类ID
  57. */
  58. public function company_dynamic_latest()
  59. {
  60. $categoryId = $this->request->param('category_id/d', 0);
  61. $typeList = (new NewsModel())->getTypeList();
  62. if (!$categoryId || !isset($typeList[(string)$categoryId])) {
  63. $this->error('分类ID无效');
  64. }
  65. $news = NewsModel::where('type', $categoryId)
  66. ->order('updatetime', 'desc')
  67. ->order('id', 'desc')
  68. ->find();
  69. if (!$news) {
  70. $this->error('该分类暂无内容');
  71. }
  72. $data = $news->toArray();
  73. if (!empty($data['image']) && strpos($data['image'], 'http') !== 0) {
  74. $data['image'] = 'http://' . $this->request->host() . $data['image'];
  75. }
  76. foreach (['content', 'intro', 'business_scope'] as $field) {
  77. if (isset($data[$field])) {
  78. $data[$field] = trim(html_entity_decode(strip_tags($data[$field]), ENT_QUOTES, 'UTF-8'));
  79. }
  80. }
  81. $this->success('ok', $data);
  82. }
  83. public function factory_category(){
  84. $page = $this->request->param("page", 1);
  85. $limit = $this->request->param("limit", 10);
  86. $list = FactoryCategoryModel::where("status", 1)->order("weigh","desc")->paginate(['page'=>$page,'list_rows'=>$limit]);
  87. $this->success("ok", $list);
  88. }
  89. public function factory(){
  90. $category_id = $this->request->param("category_id", 0);
  91. $page = $this->request->param("page", 1);
  92. $limit = $this->request->param("limit", 10);
  93. $list = [];
  94. if($category_id > 0){
  95. $list = FactoryModel::where("category_id", $category_id)
  96. ->where("status", 1)
  97. ->order("weigh","desc")
  98. ->paginate(['page'=>$page,'list_rows'=>$limit]);
  99. }
  100. $host = $_SERVER['HTTP_HOST'];
  101. foreach ($list as $k=>$v){
  102. $list[$k]['image'] = 'http://'.$host.$v['image'];
  103. }
  104. $this->success("ok", $list);
  105. }
  106. /**
  107. * 商品列表
  108. */
  109. public function get_goods_list(){
  110. $param = $this->request->param();
  111. $this->goods_model = new \app\admin\model\Goods;
  112. $this->category_model = new \app\admin\model\GoodsCategory();
  113. $goods = collection($this->goods_model->field('id,category_id,category_name,title,little_image')
  114. ->where(['deletetime'=>null])->select())->toArray();
  115. // 拼接图片路径
  116. foreach ($goods as $k=>$v){
  117. $host = $_SERVER['HTTP_HOST'];
  118. $goods[$k]['little_image'] = 'http://'.$host.$goods[$k]['little_image'];
  119. }
  120. // 获取分类
  121. $cate = collection($this->category_model->field('id,title,pid')->where(['status_switch'=>1])->select())->toArray();
  122. $cate = array_map(function($item){
  123. $item['goodsz_list'] = []; // 在结果数组中追加新字段和值
  124. return $item;
  125. }, $cate);
  126. // 组
  127. foreach ($cate as$k=>$v){
  128. foreach ($goods as $key=>$value){
  129. if ($v['id'] ==$value['category_id']){
  130. array_push($cate[$k]['goodsz_list'],$value);
  131. }
  132. }
  133. }
  134. $cate = $this->buildTree($cate);
  135. $this->success("ok", $cate);
  136. }
  137. /**
  138. * 商品详情
  139. */
  140. public function get_goods_info(){
  141. $id = $this->request->param('id');
  142. $this->goods_model = new \app\admin\model\Goods;
  143. $this->dynamic_model = new \app\admin\model\GoodsDynamic();
  144. $host = $_SERVER['HTTP_HOST'];
  145. $fields = 'id,category_id,category_name,title,price,little_image,images,describe,dynamic,content';
  146. $goods = $this->goods_model->field($fields)->where(['id'=>$id])->find()->toArray();
  147. $goods['dynamic_list'] = [];
  148. $goods['little_image'] = 'http://'.$host.$goods['little_image'];
  149. $temp = collection($this->dynamic_model->field('title,type_price')->where(['goods_id'=>$goods['id']])->select())->toArray();
  150. $goods['dynamic_list'] = $temp;
  151. $goods['images'] = !empty($goods['images'])?explode(',',$goods['images']):[];
  152. foreach ($goods['images'] as $k=>$v){
  153. $goods['images'][$k]= 'http://'.$host.$v;
  154. }
  155. $this->success("ok", $goods);
  156. }
  157. // 二维数组组装成树形数组
  158. public function buildTree(array $array, $parentId = 0)
  159. {
  160. $tree = array();
  161. foreach ($array as $item) {
  162. if ($item['pid'] == $parentId) {
  163. $children = $this->buildTree($array, $item['id']);
  164. if ($children) {
  165. $item['children'] = $children;
  166. }else{
  167. $item['children'] = [];
  168. }
  169. $tree[] = $item;
  170. }
  171. }
  172. return $tree;
  173. }
  174. // 产品下单
  175. public function get_goods_order_add(){
  176. $param = $this->request->param();
  177. $this->goods_model = new \app\admin\model\Goods;
  178. $this->goods_order_model = new \app\admin\model\GoodsOrder();
  179. $fields = 'id as goods_id,category_id,category_name,title,little_image,images,describe,dynamic';
  180. $info = $this->goods_model->field($fields)->where(['id'=>$param['goods_id']])->find()->toArray();
  181. $info['price'] = !empty($param['price'])?$param['price']:$info['price'];
  182. $info['num'] = $param['num'];
  183. $info['total_price'] = $info['price']*$param['num'];
  184. $info['dynamic_neme'] = $param['type'];
  185. $info['contact_name'] = $param['name'];
  186. $info['contact_phone'] = $param['phone'];
  187. $info['createtime'] = time();
  188. $info['updatetime'] = time();
  189. $res = $this->goods_order_model->insert($info);
  190. if ($res){
  191. $this->success("ok");
  192. }else{
  193. $this->error("error");
  194. }
  195. }
  196. // 产品下单
  197. public function set_msg_add(){
  198. $param = $this->request->param();
  199. $this->msg_model = new \app\admin\model\ContactMsg();
  200. $temp['name'] = $param['name'];
  201. $temp['phone'] = $param['phone'];
  202. $temp['email'] = $param['email'];
  203. $temp['createtime'] = time();
  204. $temp['updatetime'] = time();
  205. $res = $this->msg_model->insert($temp);
  206. if ($res){
  207. $this->success("ok");
  208. }else{
  209. $this->error("error");
  210. }
  211. }
  212. }