Shop.php 6.0 KB

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