Index.php 8.4 KB

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