Shop.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\ais\controller\api\v1;
  9. use app\ais\controller\api\Base;
  10. use app\ais\model\AisCity;
  11. use app\ais\model\AisShop;
  12. use app\ais\model\AisShopCate;
  13. use app\ais\model\AisShopOrder;
  14. use app\ais\model\AisStore;
  15. use app\common\model\SystemUser;
  16. use util\Util;
  17. class Shop extends Base{
  18. protected $store_ids;
  19. /**
  20. * 初始化API并读取城市或你的经纬度
  21. * @return void
  22. */
  23. public function initialize() {
  24. parent::initialize();
  25. $citycode = $this->request->param('citycode/d',0);
  26. if($citycode){
  27. $this->store_ids = AisStore::whereIn('citycode', AisCity::whereIn('parent_id', AisCity::where(['code' => $citycode])->column('parent_id'))->column('code'))->column('id');
  28. }
  29. }
  30. /**
  31. * 读取单个商品信息
  32. * @param integer $id 商品ID
  33. * @return void
  34. */
  35. public function item(){
  36. $param['id'] = $this->request->param('id/d',0);
  37. $this->apiSign($param);
  38. $item = AisShop::with(['store' => function($query) {
  39. $query->field('id,img,create_time,tags,name,address,longitude,latitude');
  40. },'coupon' => function($query) {
  41. $query->field('id,types,discount,price,amount');
  42. }])->where(['is_sale'=>1,'id' => $param['id'],'member_miniapp_id' => $this->miniapp_id])
  43. ->field('id,store_id,category_id,warehouse_num,warehouse_sellnum,share_price,share_vip_price,vip_price,like,group_title,group_img,group_note,coupon_id,name,title,img,imgs,points_price,sell_price,market_price,end_time,content,notice,keyword')
  44. ->find();
  45. if(empty($item)){
  46. return enjson(204);
  47. }
  48. $item->like += 1;
  49. $item->save();
  50. //产品信息
  51. $item->content = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($item->content));
  52. $item->notice = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($item->notice));
  53. $item->market_price = money($item->market_price);
  54. $item->sell_price = money($item->sell_price);
  55. $item->coupon_price = AisShop::couponPrice($item->sell_price,$item->coupon);
  56. $item->points_price = money($item->points_price);
  57. $item->share_price = ($item->sell_price-$item->vip_price-$item->points_price-$item->coupon_price)*$item->share_price/100;
  58. $item->share_vip_price = ($item->sell_price-$item->vip_price-$item->points_price-$item->coupon_price)*$item->share_vip_price/100;
  59. $item->warehouse_sellnum = $item->warehouse_sellnum + AisShopOrder::where(['shop_id' => $item->id,'paid_at'=>1])->count();
  60. $item->end_time = ($item->warehouse_num ? (($item->end_time + 60 * 10) - time()) * 1000 : 0) ?: 0;
  61. //店铺信息
  62. $store = [];
  63. if($item->store_id > 0){
  64. $store['img'] = $item->store->img;
  65. $store['name'] = $item->store->name;
  66. $store['address'] = $item->store->address;
  67. }
  68. $item['store'] = $store;
  69. return enjson(200,$item);
  70. }
  71. /**
  72. * 首页显示
  73. */
  74. public function index(){
  75. $this->apiSign(['citycode' => $this->request->param('citycode')]);
  76. $where['member_miniapp_id'] = $this->miniapp_id;
  77. $where['types'] = 1;
  78. $where['is_sale'] = 1;
  79. $where['is_del'] = 0;
  80. if ($this->store_ids) {
  81. $where['store_id'] = $this->store_ids;
  82. }
  83. $rel = AisShop::where($where)->field('id,category_id,name,title,img,share_price,points_price,warehouse_num,sell_price,market_price,vip_price,coupon_id,end_time,update_time')->limit(8)->select();
  84. $data = [];
  85. foreach ($rel as $key => $value) {
  86. $data[$key] = $value;
  87. $data[$key]['coupon_price'] = AisShop::couponPrice($value->sell_price,$value->coupon);
  88. $data[$key]['img'] = $value->img;
  89. $data[$key]['tags'] = time()-$value->update_time <= 86400 ? '上新':'';
  90. $end_time = $value['warehouse_num'] ? (($value->end_time + 60 * 10) - time()) * 1000 : 0;
  91. $data[$key]['end_time'] = $end_time ?: 0;
  92. }
  93. return enjson(200,$data);
  94. }
  95. /**
  96. * 首页推荐专题
  97. */
  98. public function special(){
  99. $this->apiSign(['citycode' => $this->request->param('citycode')]);
  100. $cate = AisShopCate::where(['member_miniapp_id' => $this->miniapp_id,'types' => 1])->field('id,title,show_type,content,picture')->select();
  101. if(empty($cate)){
  102. return enjson(204);
  103. }
  104. $where['member_miniapp_id'] = $this->miniapp_id;
  105. $where['types'] = 2;
  106. $where['is_sale'] = 1;
  107. $where['is_del'] = 0;
  108. $where['category_id'] = array_column($cate->toArray(),'id');;
  109. if ($this->store_ids){
  110. $where['store_id'] = $this->store_ids;
  111. }
  112. $rel = AisShop::where($where)->field('id,category_id,name,title,img,share_price,points_price,warehouse_num,sell_price,market_price,vip_price,coupon_id,end_time,update_time')->limit(6)->select();
  113. $shop = [];
  114. foreach ($rel as $key => $value) {
  115. $shop[$key] = $value;
  116. $shop[$key]['coupon_price'] = AisShop::couponPrice($value->sell_price,$value->coupon);
  117. $shop[$key]['img'] = $value->img;
  118. $shop[$key]['tags'] = time()-$value->update_time <= 86400 ? '上新':'';
  119. $end_time = $value['warehouse_num'] ? (($value->end_time + 60 * 10) - time()) * 1000 : 0;
  120. $shop[$key]['end_time'] = $end_time ?: 0;
  121. }
  122. $goods = dataGroup($shop,'category_id');
  123. $data = [];
  124. foreach ($cate as $key => $value) {
  125. if(!empty($goods[$value->id])){
  126. $data[$key] = $value;
  127. $data[$key]['goods'] = $goods[$value->id];
  128. }
  129. }
  130. return enjson(200,$data);
  131. }
  132. /**
  133. * 专栏产品展示
  134. */
  135. public function lists(){
  136. $param['id'] = $this->request->param('id/d');
  137. $param['page'] = $this->request->param('page/d',0);
  138. $param['citycode'] = $this->request->param('citycode/d',0);
  139. $this->apiSign($param);
  140. if ($this->store_ids){
  141. $where['store_id'] = $this->store_ids;
  142. }
  143. $cate = [];
  144. if($param['page'] == 1){
  145. $cate = AisShopCate::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->field('id,title,content,picture')->find();
  146. if(empty($cate)){
  147. return enjson(403);
  148. }
  149. }
  150. $rel = AisShop::with(['store' => function($query) {
  151. $query->field('name,id,address,longitude,latitude');
  152. }])->where(['is_sale' => 1,'is_del' => 0,'category_id' => $param['id']])->field('id,share_price,store_id,category_id,name,title,img,warehouse_num,points_price,sell_price,market_price,vip_price,coupon_id,end_time,update_time')->page($param['page'],10)->select();
  153. if($rel->isEmpty()){
  154. return enjson(204);
  155. }
  156. $shop = [];
  157. foreach ($rel as $key => $value) {
  158. $shop[$key] = $value;
  159. $shop[$key]['coupon_price'] = AisShop::couponPrice($value->sell_price,$value->coupon);
  160. $shop[$key]['img'] = $value->img;
  161. $shop[$key]['tags'] = time()-$value->update_time <= 86400 ? '上新':'';
  162. $end_time = $value['warehouse_num'] ? (($value->end_time + 60 * 10) - time()) * 1000 : 0;
  163. $shop[$key]['end_time'] = $end_time ?: 0;
  164. $shop[$key]['store'] = $value->store;
  165. }
  166. return enjson(200,'成功',['shop' => $shop,'special' => $cate]);
  167. }
  168. /**
  169. * 查询指定商家的商品
  170. */
  171. public function store(){
  172. $param['page'] = $this->request->param('page/d',1);
  173. $param['store_id']= $this->request->param('store_id/d',0);
  174. $param['types'] = $this->request->param('types/d');
  175. $param['keyword'] = $this->request->param('keyword');
  176. $this->apiSign($param);
  177. $where = [];
  178. $where['is_del'] = 0;
  179. $where['is_sale'] = 1;
  180. $where['member_miniapp_id'] = $this->miniapp_id;
  181. if($param['types']){
  182. $where['types'] = $param['types'];
  183. }
  184. if($param['store_id']){
  185. $where['store_id'] = $param['store_id'];
  186. }
  187. $rel = AisShop::where($where)->withSearch(['name'],['name' => $param['keyword']])->field('id,share_price,store_id,category_id,name,title,img,warehouse_num,points_price,sell_price,market_price,vip_price,coupon_id,end_time,update_time')->page($param['page'],10)->select();
  188. if($rel->isEmpty()){
  189. return enjson(204);
  190. }
  191. $data = [];
  192. foreach ($rel as $key => $value) {
  193. $data[$key] = $value;
  194. $data[$key]['coupon_price'] = AisShop::couponPrice($value->sell_price,$value->coupon);
  195. $data[$key]['img'] = $value->img;
  196. $end_time = $value['warehouse_num'] ? (($value->end_time + 60 * 10) - time()) * 1000 : 0;
  197. $data[$key]['end_time'] = $end_time ?: 0;
  198. }
  199. return enjson(200,$data);
  200. }
  201. /**
  202. * 单个商品下单用户列表
  203. */
  204. public function orderUser(){
  205. $param['id'] = $this->request->param('id/d',0);
  206. $this->apiSign($param);
  207. $order = AisShopOrder::where(['shop_id' => $param['id']])->limit(6)->select();
  208. $data = [];
  209. if($order->isEmpty()){
  210. $user = SystemUser::where(['member_miniapp_id' => $this->miniapp_id])->field('face')->limit(6)->select()->toArray();
  211. $data = array_column($user,'face');
  212. }else{
  213. foreach ($order as $key => $value) {
  214. $data[$key] = $value->user->face;
  215. }
  216. }
  217. $new_order['id'] = 0;
  218. $new_order['create_time'] = 0;
  219. if($this->user){
  220. $info = AisShopOrder::where(['uid'=> $this->user->id,'shop_id' =>$param['id'],'paid_at' => 0,'is_del' => 0])->field('id,create_time')->find();
  221. if($info){
  222. $create_time = (($info->create_time + 60 * 10) - time()) * 1000;
  223. if($create_time < 0){
  224. $info->is_del = 1;
  225. $info->save();
  226. }
  227. $new_order['id'] = $info->id;
  228. $new_order['create_time'] = $create_time;
  229. }
  230. }
  231. return enjson(200,['share' => $data,'order' => $new_order]);
  232. }
  233. }