Sale.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * 小程序公共API服务
  7. */
  8. namespace app\fastshop\controller\api\v3;
  9. use app\fastshop\controller\api\Base;
  10. use app\fastshop\model\Sale as AppSale;
  11. use app\fastshop\model\Item;
  12. use think\facade\Request;
  13. class Sale extends Base{
  14. /**
  15. * 获得首页
  16. */
  17. public function index(){
  18. $param['signkey'] = Request::param('signkey');
  19. $param['sign'] = Request::param('sign');
  20. $rel = $this->apiSign($param);
  21. if($rel['code'] != 200){
  22. return enjson(204,'签名失败');
  23. }
  24. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  25. $condition[] = ['types','=',1];
  26. $condition[] = ['sale_nums','>=',1];
  27. $info = AppSale::with(['Item'=> function($query) {
  28. $query->field('id,name,sell_price,img');
  29. }])
  30. ->where($condition)->field('id,types,is_vip,end_time,gift,img,item_id,market_price,member_miniapp_id,sale_nums,sale_price,start_time,title')->order('id desc')->limit(5)->select();
  31. if($info->isEmpty()){
  32. return enjson(204,'空内容');
  33. }
  34. $data = [];
  35. foreach ($info as $key => $value) {
  36. $data[$key] = $value;
  37. $data[$key]['sale_price'] = money($value->sale_price/100);
  38. $data[$key]['user'] = empty($value->user) ? [] : $value->user;
  39. $data[$key]['item'] = $value->item;
  40. $item_ids = array_column(json_decode($value->gift),'item_id');
  41. $gift = [];
  42. foreach ($item_ids as $i => $id) {
  43. $item = Item::where(['id' => $id])->field('id,name,sell_price,img')->find()->toArray();
  44. $gift[$i] = $item;
  45. $gift[$i]['sell_price'] = money($item['sell_price']);
  46. }
  47. $data[$key]['gift'] = $gift;
  48. }
  49. return enjson(200,'成功',$data);
  50. }
  51. /**
  52. * 获得首页
  53. */
  54. public function lists(){
  55. $param['time_id'] = Request::param('time_id/d',0);
  56. $param['page'] = Request::param('page/d');
  57. $param['sign'] = Request::param('sign');
  58. $rel = $this->apiSign($param);
  59. if($rel['code'] != 200){
  60. return enjson(204,'签名失败');
  61. }
  62. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  63. $condition[] = ['types','=',1];
  64. $condition[] = ['sale_nums','>=',1];
  65. if($param['time_id']){
  66. $condition[] = ['category_id','=',$param['time_id']];
  67. }
  68. $info = AppSale::with(['Item'=> function($query) {
  69. $query->field('id,name,sell_price,img');
  70. }])
  71. ->where($condition)->field('id,types,is_vip,is_newuser,end_time,gift,item_id,market_price,member_miniapp_id,sale_nums,sale_price,start_time,end_time,title')->order('sort desc,id desc')->paginate(10);
  72. if($info->isEmpty()){
  73. return enjson(204,'空内容');
  74. }
  75. $h = time();
  76. $data = [];
  77. foreach ($info as $key => $value) {
  78. $data[$key] = $value;
  79. $data[$key]['sale_price'] = money($value->sale_price/100);
  80. $data[$key]['market_price'] = money($value->market_price/100);
  81. $data[$key]['user'] = empty($value->user) ? []: $value->user;
  82. $data[$key]['item'] = $value->item;
  83. if($h <= $value->end_time){
  84. if($h >= $value->start_time && $h <= $value->end_time){
  85. $data[$key]['types'] = 1;
  86. }else if($h <= $value->end_time){
  87. $data[$key]['types'] = 2;
  88. }
  89. }else{
  90. $data[$key]['types'] = 1;
  91. }
  92. if(time() > $value->end_time){
  93. $data[$key]['state'] = 0;
  94. }else{
  95. $data[$key]['state'] = $value->sale_nums > 0 ? 2 : 0;
  96. }
  97. $data[$key]['start_time'] = '开始 '.date('d日H:i',$value->start_time);
  98. $data[$key]['end_time'] = '结束 '.date('d日H:i',$value->end_time);
  99. $data[$key]['progress'] = $value['sale_nums'] < 100 ? 100-$value['sale_nums'] : 45;
  100. $data[$key]['sale_nums'] = $value['sale_nums'] <= 0 ? 0 : $value['sale_nums'] * 10;
  101. $item_ids = array_column(json_decode($value->gift),'item_id');
  102. $gift = [];
  103. foreach ($item_ids as $i => $id) {
  104. $item = Item::where(['id' => $id])->field('id,name,sell_price,img')->find()->toArray();
  105. $gift[$i] = $item;
  106. $gift[$i]['sell_price'] = money($item['sell_price']);
  107. }
  108. $data[$key]['gift'] = $gift;
  109. }
  110. return enjson(200,'成功',$data);
  111. }
  112. /**
  113. * 获取某个产品
  114. */
  115. public function item(){
  116. $param['id'] = Request::param('id/d',1);
  117. $param['sign'] = Request::param('sign');
  118. $rel = $this->apiSign($param);
  119. if($rel['code'] != 200){
  120. return enjson(204,'签名失败');
  121. }
  122. $condition[] = ['types','=',1];
  123. $condition[] = ['id','=',$param['id']];
  124. $info = AppSale::with(['Item'=> function($query) {
  125. $query->field('id,name,sell_price,img,imgs,content');
  126. }])->where($condition)->field('id,title,item_id,sale_price,market_price,sale_nums,gift,update_time,start_time,end_time')->order('id desc')->find();
  127. if(empty($info)){
  128. return enjson(204,'空内容');
  129. }
  130. $data = $info->toArray();
  131. $data['item'] = $info->item;
  132. $data['market_price'] = money($info->market_price/100);
  133. $data['sale_price'] = money($info->sale_price/100);
  134. $data['state'] = $info->sale_nums <= 0 ? 0 : 1;
  135. $h = time();
  136. if($h <= $info->end_time && $info->sale_nums > 0){
  137. if($h >= $info->start_time && $h <= $info->end_time){
  138. $data['types'] = 1;
  139. $state_text = '立即购买';
  140. }else if($h <= $info->end_time){
  141. $data['types'] = 0;
  142. $state_text = '活动未开始';
  143. }
  144. }else{
  145. $data['types'] = 0;
  146. $state_text = '活动结束';
  147. }
  148. $data['start_time'] = date('m-d H:i',$info->start_time);
  149. $data['end_time'] = date('m-d H:i',$info->end_time);
  150. $data['state_text'] = $state_text;
  151. $data['item']['imgs'] = json_decode($info->item->imgs,true);
  152. $item_ids = array_column(json_decode($info->gift),'item_id');
  153. $gift = [];
  154. foreach ($item_ids as $i => $id) {
  155. $gift[$i] = Item::where(['id' => $id])->field('id,name,sell_price,img,imgs,content')->find()->toArray();
  156. }
  157. array_unshift($gift,$info->item);
  158. $data['gift'] = $gift;
  159. return enjson(200,'成功',$data);
  160. }
  161. }