Shop.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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\allwin\controller\api\v4;
  9. use app\allwin\controller\api\Base;
  10. use app\allwin\model\AllwinShop;
  11. use app\allwin\model\AllwinShopCate;
  12. use app\allwin\model\AllwinShopOrder;
  13. use app\common\model\SystemUser;
  14. use util\Util;
  15. class Shop extends Base{
  16. /**
  17. * 首页显示
  18. */
  19. public function index(){
  20. $param['signkey'] = $this->request->param('signkey');
  21. $param['sign'] = $this->request->param('sign');
  22. $rel = $this->apiSign($param);
  23. if($rel['code'] != 200){
  24. return enjson($rel['code'],'签名验证失败');
  25. }
  26. $rel = AllwinShop::where(['member_miniapp_id' => $this->miniapp_id,'types' => 1,'is_sale' => 1,'is_del' => 0])->field('id,category_id,name,title,img,sell_price,end_time')->limit(8)->select();
  27. $data = [];
  28. foreach ($rel as $key => $value) {
  29. $data[$key] = $value;
  30. $data[$key]['coupon_price'] = money(empty($value->coupon) ? 0 : AllwinShop::calculatePrice($value->sell_price,$value->coupon));
  31. $data[$key]['img'] = $value->img.'?x-oss-process=style/640';
  32. $data[$key]['tags'] = time()-$value->update_time <= 86400 ? '上新':'';
  33. }
  34. return enjson(200,'成功',$data);
  35. }
  36. /**
  37. * 首页推荐专题
  38. */
  39. public function indexSpecial(){
  40. $param['signkey'] = $this->request->param('signkey');
  41. $param['sign'] = $this->request->param('sign');
  42. $rel = $this->apiSign($param);
  43. if($rel['code'] != 200){
  44. return enjson($rel['code'],'签名验证失败');
  45. }
  46. $cate = AllwinShopCate::where(['member_miniapp_id' => $this->miniapp_id,'types' => 1])->field('id,title,content,picture')->select();
  47. if(empty($cate)){
  48. return enjson(200);
  49. }
  50. $cate_id = array_column($cate->toArray(),'id');
  51. $rel = AllwinShop::whereIN('category_id',$cate_id)->where(['types' => 2,'is_sale' => 1,'is_del' => 0])->field('id,category_id,name,title,img,points,warehouse_num,sell_price,market_price,vip_price,coupon_id,end_time,update_time')->limit(8)->select();
  52. $shop = [];
  53. foreach ($rel as $key => $value) {
  54. $shop[$key] = $value;
  55. $shop[$key]['coupon_price'] = money(empty($value->coupon) ? 0 : AllwinShop::calculatePrice($value->sell_price,$value->coupon));
  56. $shop[$key]['img'] = $value->img.'?x-oss-process=style/640';
  57. $shop[$key]['tags'] = time()-$value->update_time <= 86400 ? '上新':'';
  58. $end_time = $value['warehouse_num'] ? (($value->end_time + 60 * 10) - time()) * 1000 : 0;
  59. $shop[$key]['end_time'] = $end_time ?: 0;
  60. }
  61. $goods = dataGroup($shop,'category_id');
  62. $data = [];
  63. foreach ($cate as $key => $value) {
  64. $data[$key] = $value;
  65. $data[$key]['goods'] = $goods[$value['id']] ?? [];
  66. }
  67. return enjson(200,'成功',$data);
  68. }
  69. /**
  70. * 专栏专题页面
  71. */
  72. public function special(){
  73. $param['id'] = $this->request->param('id/d');
  74. $param['page'] = $this->request->param('page/d',0);
  75. $param['sign'] = $this->request->param('sign');
  76. $rel = $this->apiSign($param);
  77. if($rel['code'] != 200){
  78. return enjson($rel['code'],'签名验证失败');
  79. }
  80. $cate = [];
  81. if($param['page'] == 1){
  82. $cate = AllwinShopCate::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->field('id,title,content,picture')->find();
  83. if(empty($cate)){
  84. return enjson(403);
  85. }
  86. }
  87. $rel = AllwinShop::where(['is_sale' => 1,'is_del' => 0,'category_id' => $param['id']])->field('id,share_price,detective_price,store_id,category_id,name,title,img,warehouse_num,points,sell_price,market_price,vip_price,coupon_id,end_time,update_time')->paginate(10);
  88. if($rel->isEmpty()){
  89. return enjson(204);
  90. }
  91. $shop = [];
  92. foreach ($rel as $key => $value) {
  93. $shop[$key] = $value;
  94. $shop[$key]['coupon_price'] = money(empty($value->coupon) ? 0 : AllwinShop::calculatePrice($value->sell_price,$value->coupon));
  95. $shop[$key]['img'] = $value->img.'?x-oss-process=style/640';
  96. $shop[$key]['tags'] = time()-$value->update_time <= 86400 ? '上新':'';
  97. $end_time = $value['warehouse_num'] ? (($value->end_time + 60 * 10) - time()) * 1000 : 0;
  98. $shop[$key]['end_time'] = $end_time ?: 0;
  99. }
  100. return enjson(200,'成功',['shop' => $shop,'special' => $cate]);
  101. }
  102. /**
  103. * 根据条件查询数据
  104. */
  105. public function goods(){
  106. $param['page'] = $this->request->param('page/d',1);
  107. $param['types'] = $this->request->param('types/d',0);
  108. $param['store_id']= $this->request->param('store_id/d',0);
  109. $param['keyword'] = $this->request->param('keyword','');
  110. $param['signkey'] = $this->request->param('signkey');
  111. $param['sign'] = $this->request->param('sign');
  112. $rel = $this->apiSign($param);
  113. if($rel['code'] != 200){
  114. return enjson($rel['code'],'签名验证失败');
  115. }
  116. $where = [];
  117. $where['is_del'] = 0;
  118. $where['is_sale'] = 1;
  119. $where['member_miniapp_id'] = $this->miniapp_id;
  120. if($param['types']){
  121. $where['types'] = $param['types'];
  122. }
  123. if($param['store_id']){
  124. $where['store_id'] = $param['store_id'];
  125. }
  126. $special = AllwinShop::where($where)->withSearch(['name'],['name' => $param['keyword']])->field('id,share_price,detective_price,store_id,category_id,name,title,img,warehouse_num,points,sell_price,market_price,vip_price,coupon_id,end_time,update_time')->paginate(10);
  127. if($special->isEmpty()){
  128. return enjson(204);
  129. }
  130. $data = [];
  131. foreach ($special as $key => $value) {
  132. $data[$key] = $value;
  133. $data[$key]['store_name'] = $value->store->name;
  134. $data[$key]['distance'] = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$value->store->longitude,$value->store->latitude);
  135. $data[$key]['tags'] = time()-$value->update_time <= 86400 ? '上新':'';
  136. $data[$key]['coupon_price'] = money(empty($value->coupon) ? 0 : AllwinShop::calculatePrice($value->sell_price,$value->coupon));
  137. $data[$key]['img'] = $value->img.'?x-oss-process=style/640';
  138. //计算价格
  139. $price = $value->sell_price-$value->vip_price-$data[$key]['coupon_price']-$value->points/100;
  140. $data[$key]['share_price'] = money($price*$value->share_price/100);
  141. $data[$key]['detective_price'] = money($price*$value->detective_price/100);
  142. $end_time = $value['warehouse_num'] ? (($value->end_time + 60 * 10) - time()) * 1000 : 0;
  143. $data[$key]['end_time'] = $end_time ?: 0;
  144. }
  145. return enjson(200,'成功',$data);
  146. }
  147. /**
  148. * 读取单个商品信息
  149. * @param integer $id 商品ID
  150. * @return void
  151. */
  152. public function item(){
  153. $param['id'] = $this->request->param('id/d',0);
  154. $param['signkey'] = $this->request->param('signkey');
  155. $param['sign'] = $this->request->param('sign');
  156. $rel = $this->apiSign($param);
  157. if($rel['code'] != 200){
  158. return enjson($rel['code'],'签名验证失败');
  159. }
  160. //查找商品SPU
  161. $item = AllwinShop::where(['is_sale'=>1,'id' => $param['id'],'member_miniapp_id' => $this->miniapp_id])
  162. ->field('id,store_id,category_id,warehouse_num,warehouse_sellnum,like,group_title,group_img,group_note,coupon_id,name,title,img,imgs,points,sell_price,market_price,end_time,content,notice,keyword,vip_price')
  163. ->find();
  164. if(empty($item)){
  165. return json(['code'=>403,'msg'=>'没有内容']);
  166. }
  167. $item->like += 1;
  168. $item->save();
  169. $store = [];
  170. if($item->store_id > 0){
  171. $store['img'] = $item->store['img']."?x-oss-process=style/w100";
  172. $imgs = empty($item->store['imgs']) ? [] :json_decode($item->store['imgs']);
  173. $imga = [];
  174. foreach ($imgs as $key => $img) {
  175. $imga[$key] = $img."?x-oss-process=style/w300";
  176. }
  177. $store['imgs'] = $imga;
  178. $store['create_time'] = util::ftime($item->store->create_time);
  179. $store['tags'] = explode(',',$item->store->tags);
  180. $store['name'] = $item->store->name;
  181. $store['address'] = $item->store->address;
  182. $store['tips'] = $item->store->tips;
  183. $store['distance'] = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$item->store->longitude,$item->store->latitude);
  184. $store['chains'] = [];
  185. if(!empty($item->store->chains)){
  186. $chains = $item->store->chains;
  187. $chain = [];
  188. foreach ($chains as $key => $value) {
  189. $chain[$key]['title'] = $value['title'];
  190. $chain[$key]['address'] = $value['address'];
  191. $chain[$key]['telphone'] = $value['telphone'];
  192. $chain[$key]['distance'] = getDistance($this->qqgps['lng'],$this->qqgps['lat'],$value->longitude,$value->latitude);
  193. }
  194. $store['chains'] = $chain;
  195. }
  196. }
  197. $item['content'] = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($item['content']));
  198. $item['notice'] = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($item['notice']));
  199. $item['market_price'] = money($item['market_price']);
  200. $item['sell_price'] = money($item['sell_price']);
  201. $item['img'] = $item['img'].'?x-oss-process=style/500';
  202. $item['imgs'] = json_decode($item['imgs'],true);
  203. $item['coupon_id'] = $item->coupon_id;
  204. $item['coupon_price'] = money(empty($item->coupon) ? 0 : AllwinShop::calculatePrice($item->sell_price,$item->coupon));
  205. $item['points_price'] = money($item->points/100);
  206. $item['tags'] = time()-$item->update_time <= 86400 ? '上新':'';
  207. $order_num = AllwinShopOrder::where(['shop_id' => $item->id,'paid_at'=>1])->count();
  208. $item['order_num'] = $item['warehouse_sellnum']+$order_num;
  209. $end_time = $item['warehouse_num'] ? (($item->end_time + 60 * 10) - time()) * 1000 : 0;
  210. $item['end_time'] = $end_time ?: 0;
  211. $data['imgs'] = $item['imgs'];
  212. $data['item'] = $item;
  213. $data['store'] = $store;
  214. unset($data['item']['store']);
  215. if(empty($data)){
  216. return json(['code'=>204,'msg'=>'没有内容']);
  217. }else{
  218. return json(['data'=>$data,'code'=>200,'msg'=>'成功']);
  219. }
  220. }
  221. /**
  222. * 单个商品下单用户列表
  223. */
  224. public function itemOrderUser(){
  225. $param['id'] = $this->request->param('id/d',0);
  226. $param['signkey'] = $this->request->param('signkey');
  227. $param['sign'] = $this->request->param('sign');
  228. $rel = $this->apiSign($param);
  229. if($rel['code'] != 200){
  230. return enjson($rel['code'],'签名验证失败');
  231. }
  232. $order = AllwinShopOrder::where(['shop_id' => $param['id']])->limit(6)->select();
  233. $data = [];
  234. if($order->isEmpty()){
  235. $user = SystemUser::where(['member_miniapp_id' => $this->miniapp_id])->field('face')->limit(6)->select()->toArray();
  236. $data = array_column($user,'face');
  237. }else{
  238. foreach ($order as $key => $value) {
  239. $data[$key] = $value->user->face;
  240. }
  241. }
  242. $new_order['id'] = 0;
  243. $new_order['create_time'] = 0;
  244. if($this->user){
  245. $info = AllwinShopOrder::where(['user_id'=> $this->user->id,'shop_id' =>$param['id'],'paid_at' => 0,'is_del' => 0])->field('id,create_time')->find();
  246. if($info){
  247. $create_time = (($info->create_time + 60 * 10) - time()) * 1000;
  248. if($create_time < 0){
  249. $info->is_del = 1;
  250. $info->save();
  251. }
  252. $new_order['id'] = $info->id;
  253. $new_order['create_time'] = $create_time;
  254. }
  255. }
  256. return enjson(200,'成功',['share' => $data,'order' => $new_order]);
  257. }
  258. }