Shop.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 商城管理
  7. */
  8. namespace app\popupshop\controller\api\v1;
  9. use app\popupshop\controller\api\Base;
  10. use app\popupshop\model\Item;
  11. use app\popupshop\model\Category;
  12. use think\Db;
  13. use think\facade\Request;
  14. class Shop extends Base{
  15. /**
  16. * 全部分类布局
  17. * @param integer $cate_id 读取ID
  18. * @return json
  19. */
  20. public function cate(){
  21. $root_cate = Category::where(['member_miniapp_id' => $this->miniapp_id,'parent_id' => 0])->field('id,parent_id,title,name,picture')->order('sort desc,id desc')->select()->toArray();
  22. if (empty($root_cate)) {
  23. return enjson(403,'读取目录失败');
  24. }else{
  25. $data['root_data'] = $root_cate;
  26. $ids = array_column($root_cate,'id');
  27. $data['subs_data'] = Category::whereIn('parent_id',$ids)->order('sort desc,id desc')->select();
  28. return json(['code'=>200,'msg'=>'成功','data' => $data]);
  29. }
  30. }
  31. /**
  32. * 读取首页分类
  33. * @return json
  34. */
  35. public function cateTop(){
  36. $data['types'] = Request::param('types',0);
  37. $data['cate_id'] = Request::param('cate_id',0);
  38. $data['sign'] = Request::param('sign');
  39. $rel = $this->apiSign($data);
  40. if($rel['code'] == 200){
  41. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  42. if($data['types']){
  43. $condition[] = ['types','=',1];
  44. }else{
  45. $condition[] = ['parent_id','=',$data['cate_id']];
  46. }
  47. $data = Category::where($condition)->field("id,parent_id,title,name,picture")->order('sort desc,id desc')->select()->toArray();
  48. if (empty($data)) {
  49. return enjson(204,'分类不存在');
  50. }
  51. return enjson(200,'成功',$data);
  52. }
  53. return enjson($rel['code'],'签名验证失败');
  54. }
  55. /**
  56. * 读取某个分类下的商品列表
  57. * @param integer api 读取ID
  58. * @return json
  59. */
  60. public function cateItem(){
  61. $data['cate_id'] = Request::param('cate_id',0);
  62. $data['page'] = Request::param('page',0);
  63. $data['sign'] = Request::param('sign');
  64. $data['types'] = Request::param('types',0);
  65. $rel = $this->apiSign($data);
  66. if($rel['code'] == 200){
  67. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  68. $condition[] = ['is_sale','=',2];
  69. if($data['cate_id']){
  70. $condition[] = ['','exp',Db::raw("FIND_IN_SET({$data['cate_id']},category_path_id)")];
  71. }else{
  72. $condition[] = ['types','=',$data['types']];
  73. }
  74. $result = Item::where($condition)->field('id,category_id,name,sell_price,market_price,note,img')->order('sort desc,id desc')->paginate(20,true)->toArray();
  75. if(empty($result['data'])){
  76. return enjson(204,'没有内容了');
  77. }
  78. return enjson(200,'成功',$result['data']);
  79. }
  80. return enjson($rel['code'],'签名验证失败');
  81. }
  82. /**
  83. * 搜索商品ID
  84. * @param integer api 读取ID
  85. * @return json
  86. */
  87. public function search(){
  88. $data['keyword'] = Request::param('keyword','');
  89. $data['page'] = Request::param('page',0);
  90. $data['sign'] = Request::param('sign');
  91. $rel = $this->apiSign($data);
  92. if($rel['code'] == 200){
  93. $keyword = Request::param('keyword');
  94. if(empty($keyword)){
  95. return enjson(204,'未输入关键字');
  96. }
  97. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  98. $condition[] = ['is_sale','=',2];
  99. $result = Item::where($condition)->withSearch(['name'],['name' => $keyword])->field('id,category_id,name,sell_price,market_price,img')->order('sort desc,id desc')->paginate(10,true);
  100. $data = [];
  101. foreach ($result as $key => $value) {
  102. $data[$key] = $value;
  103. $data[$key]['img'] = $value['img'].'?x-oss-process = style/300';
  104. }
  105. return enjson(200,'成功',$data);
  106. }
  107. return enjson(204,'未输入关键字');
  108. }
  109. /**
  110. * 读取单个商品信息
  111. * @param integer $id 商品ID
  112. * @return void
  113. */
  114. public function item(int $id){
  115. $param['id'] = Request::param('id',0);
  116. $param['sign'] = Request::param('sign');
  117. $rel = $this->apiSign($param);
  118. if($rel['code'] == 200){
  119. //查找商品SPU
  120. $item = Item::where(['is_sale'=>2,'id' => $id,'member_miniapp_id' => $this->miniapp_id])
  121. ->field('id,category_id,name,note,sell_price,market_price,points,repoints,weight,img,imgs,content')
  122. ->find();
  123. if(empty($item)){
  124. return json(['code'=>403,'msg'=>'没有内容']);
  125. }
  126. $data = [];
  127. $data['content'] = $item->content;
  128. $data['name'] = $item->name;
  129. $data['note'] = $item->note;
  130. $data['market_price'] = $item->market_price;
  131. $data['sell_price'] = $item->sell_price;
  132. $data['points'] = $item->points;
  133. $data['repoints'] = $item->repoints;
  134. $data['img'] = $item->img.'?x-oss-process = style/500';
  135. $data['imgs'] = json_decode($item->imgs,true);
  136. return enjson(200,'成功',$data);
  137. }
  138. return enjson(204,'签名错误');
  139. }
  140. }