Index.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. public function factory_category(){
  54. $page = $this->request->param("page", 1);
  55. $limit = $this->request->param("limit", 10);
  56. $list = FactoryCategoryModel::where("status", 1)->order("weigh","desc")->paginate(['page'=>$page,'list_rows'=>$limit]);
  57. $this->success("ok", $list);
  58. }
  59. public function factory(){
  60. $category_id = $this->request->param("category_id", 0);
  61. $page = $this->request->param("page", 1);
  62. $limit = $this->request->param("limit", 10);
  63. $list = [];
  64. if($category_id > 0){
  65. $list = FactoryModel::where("category_id", $category_id)
  66. ->where("status", 1)
  67. ->order("weigh","desc")
  68. ->paginate(['page'=>$page,'list_rows'=>$limit]);
  69. }
  70. $host = $_SERVER['HTTP_HOST'];
  71. foreach ($list as $k=>$v){
  72. $list[$k]['image'] = 'http://'.$host.$v['image'];
  73. }
  74. $this->success("ok", $list);
  75. }
  76. /**
  77. * 商品列表
  78. */
  79. public function get_goods_list(){
  80. $param = $this->request->param();
  81. $this->goods_model = new \app\admin\model\Goods;
  82. $this->category_model = new \app\admin\model\GoodsCategory();
  83. $goods = collection($this->goods_model->field('id,category_id,category_name,title,little_image')
  84. ->where(['deletetime'=>null])->select())->toArray();
  85. // 拼接图片路径
  86. foreach ($goods as $k=>$v){
  87. $host = $_SERVER['HTTP_HOST'];
  88. $goods[$k]['little_image'] = 'http://'.$host.$goods[$k]['little_image'];
  89. }
  90. // 获取分类
  91. $cate = collection($this->category_model->field('id,title,pid')->where(['status_switch'=>1])->select())->toArray();
  92. $cate = array_map(function($item){
  93. $item['goodsz_list'] = []; // 在结果数组中追加新字段和值
  94. return $item;
  95. }, $cate);
  96. // 组
  97. foreach ($cate as$k=>$v){
  98. foreach ($goods as $key=>$value){
  99. if ($v['id'] ==$value['category_id']){
  100. array_push($cate[$k]['goodsz_list'],$value);
  101. }
  102. }
  103. }
  104. $cate = $this->buildTree($cate);
  105. $this->success("ok", $cate);
  106. }
  107. /**
  108. * 商品详情
  109. */
  110. public function get_goods_info(){
  111. $id = $this->request->param('id');
  112. $this->goods_model = new \app\admin\model\Goods;
  113. $this->dynamic_model = new \app\admin\model\GoodsDynamic();
  114. $host = $_SERVER['HTTP_HOST'];
  115. $fields = 'id,category_id,category_name,title,price,little_image,images,describe,dynamic,content';
  116. $goods = $this->goods_model->field($fields)->where(['id'=>$id])->find()->toArray();
  117. $goods['dynamic_list'] = [];
  118. $goods['little_image'] = 'http://'.$host.$goods['little_image'];
  119. $temp = collection($this->dynamic_model->field('title,type_price')->where(['goods_id'=>$goods['id']])->select())->toArray();
  120. $goods['dynamic_list'] = $temp;
  121. $goods['images'] = !empty($goods['images'])?explode(',',$goods['images']):[];
  122. foreach ($goods['images'] as $k=>$v){
  123. $goods['images'][$k]= 'http://'.$host.$v;
  124. }
  125. $this->success("ok", $goods);
  126. }
  127. // 二维数组组装成树形数组
  128. public function buildTree(array $array, $parentId = 0)
  129. {
  130. $tree = array();
  131. foreach ($array as $item) {
  132. if ($item['pid'] == $parentId) {
  133. $children = $this->buildTree($array, $item['id']);
  134. if ($children) {
  135. $item['children'] = $children;
  136. }else{
  137. $item['children'] = [];
  138. }
  139. $tree[] = $item;
  140. }
  141. }
  142. return $tree;
  143. }
  144. // 产品下单
  145. public function get_goods_order_add(){
  146. $param = $this->request->param();
  147. $this->goods_model = new \app\admin\model\Goods;
  148. $this->goods_order_model = new \app\admin\model\GoodsOrder();
  149. $fields = 'id as goods_id,category_id,category_name,title,little_image,images,describe,dynamic';
  150. $info = $this->goods_model->field($fields)->where(['id'=>$param['goods_id']])->find()->toArray();
  151. $info['price'] = !empty($param['price'])?$param['price']:$info['price'];
  152. $info['num'] = $param['num'];
  153. $info['total_price'] = $info['price']*$param['num'];
  154. $info['dynamic_neme'] = $param['type'];
  155. $info['contact_name'] = $param['name'];
  156. $info['contact_phone'] = $param['phone'];
  157. $info['createtime'] = time();
  158. $info['updatetime'] = time();
  159. $res = $this->goods_order_model->insert($info);
  160. if ($res){
  161. $this->success("ok");
  162. }else{
  163. $this->error("error");
  164. }
  165. }
  166. // 产品下单
  167. public function set_msg_add(){
  168. $param = $this->request->param();
  169. $this->msg_model = new \app\admin\model\ContactMsg();
  170. $temp['name'] = $param['name'];
  171. $temp['phone'] = $param['phone'];
  172. $temp['email'] = $param['email'];
  173. $temp['createtime'] = time();
  174. $temp['updatetime'] = time();
  175. $res = $this->msg_model->insert($temp);
  176. if ($res){
  177. $this->success("ok");
  178. }else{
  179. $this->error("error");
  180. }
  181. }
  182. }