Index.php 8.7 KB

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