Sale.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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\popupshop\controller\api\v1;
  9. use app\popupshop\controller\api\Base;
  10. use app\common\facade\WechatPay;
  11. use app\common\model\SystemMemberPayment;
  12. use app\common\model\SystemUserAddress;
  13. use app\common\model\SystemUserLevel;
  14. use app\common\model\SystemMemberBank;
  15. use app\popupshop\model\Sale as AppSale;
  16. use app\popupshop\model\SaleHouse;
  17. use app\popupshop\model\SaleOrder;
  18. use app\popupshop\model\SaleOrderCache;
  19. use app\popupshop\model\SaleUser;
  20. use app\popupshop\model\Config;
  21. use think\facade\Request;
  22. use util\Util;
  23. class Sale extends Base{
  24. /**
  25. * 获得首页
  26. */
  27. public function index(){
  28. $param['signkey'] = Request::param('signkey');
  29. $param['sign'] = Request::param('sign');
  30. $rel = $this->apiSign($param);
  31. if($rel['code'] != 200){
  32. return enjson(204,'签名失败');
  33. }
  34. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  35. $condition[] = ['is_sale','=',1];
  36. $condition[] = ['is_pay','=',0];
  37. $condition[] = ['is_out','=',0];
  38. $info = AppSale::with(['User'=> function($query) {
  39. $query->field('face,nickname,id');
  40. }])
  41. ->with(['house'=> function($query) {
  42. $query->field('id,title,name,note,sell_price,img');
  43. }])
  44. ->where($condition)->field('id,store_id,user_id,house_id,user_cost_price,user_entrust_price,user_sale_price,gift,update_time')->order('id desc')->limit(5)->select();
  45. if($info->isEmpty()){
  46. return enjson(204,'空内容');
  47. }
  48. $data = [];
  49. foreach ($info as $key => $value) {
  50. $data[$key] = $value;
  51. $data[$key]['user'] = empty($value->user) ? [] : $value->user;
  52. $data[$key]['store'] = empty($value->store) ? [] : $value->store;
  53. $data[$key]['house'] = $value->house;
  54. $house_ids = array_column(json_decode($value->gift),'house_id');
  55. $gift = [];
  56. foreach ($house_ids as $i => $id) {
  57. $gift[$i] = SaleHouse::where(['id' => $id])->field('id,title,name,note,sell_price,img')->find()->toArray();
  58. }
  59. $data[$key]['gift'] = $gift;
  60. }
  61. $rel['sale'] = $data;
  62. //统计每日数量
  63. $rel['num'] = AppSale::where($condition)->count();
  64. return enjson(200,'成功',$rel);
  65. }
  66. /**
  67. * 获得列表
  68. */
  69. public function lists(){
  70. $param['page'] = Request::param('page/d',1);
  71. $param['sign'] = Request::param('sign');
  72. $rel = $this->apiSign($param);
  73. if($rel['code'] != 200){
  74. return enjson(204,'签名失败');
  75. }
  76. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  77. $condition[] = ['is_sale','=',1];
  78. $condition[] = ['is_pay','=',0];
  79. $condition[] = ['is_out','=',0];
  80. //读取我的推荐人
  81. $condition_user = [];
  82. if($this->user){
  83. $level_user = SystemUserLevel::where(['user_id' => $this->user->id,'level'=>1])->field('parent_id')->find();
  84. if(!empty($level_user)){
  85. $condition_user[] = ['user_id','=',$level_user->parent_id];
  86. $condition_user[] = ['user_id','<>',$this->user->id];
  87. }
  88. }
  89. //数据
  90. $field = 'id,store_id,user_id,house_id,user_cost_price,user_entrust_price,user_sale_price,gift,update_time';
  91. $info = AppSale::with(['User'=> function($query) {
  92. $query->field('face,nickname,id');
  93. }])->with(['house'=> function($query) {
  94. $query->field('id,title,name,note,sell_price,img');
  95. }])->where($condition)->where($condition_user)->field($field)->order('id desc')->paginate(10);
  96. if($info->isEmpty()){
  97. if($param['page'] == 1){
  98. $info = AppSale::with(['User' => function($query) {
  99. $query->field('face,nickname,id');
  100. }])->with(['house'=> function($query) {
  101. $query->field('id,title,name,note,sell_price,img');
  102. }])->where($condition)->field($field)->order('create_time asc')->limit(2)->select();
  103. }else{
  104. $info = [];
  105. }
  106. }
  107. $data = [];
  108. foreach ($info as $key => $value) {
  109. $data[$key] = $value;
  110. $data[$key]['user'] = empty($value->user) ? [] : $value->user;
  111. $data[$key]['is_store'] = empty($value->user) ? 0 : 1;
  112. $data[$key]['store'] = empty($value->store) ? [] : $value->store;
  113. $data[$key]['house'] = $value->house;
  114. $house_ids = array_column(json_decode($value->gift),'house_id');
  115. $gift = [];
  116. foreach ($house_ids as $i => $id) {
  117. $gift[$i] = SaleHouse::where(['id' => $id])->field('id,title,name,note,sell_price,img')->find()->toArray();
  118. }
  119. $data[$key]['gift'] = $gift;
  120. }
  121. return enjson(200,'成功',$data);
  122. }
  123. /**
  124. * 获取某个产品
  125. */
  126. public function item(){
  127. $param['id'] = Request::param('id/d',1);
  128. $param['sign'] = Request::param('sign');
  129. $rel = $this->apiSign($param);
  130. if($rel['code'] != 200){
  131. return enjson(204,'签名失败');
  132. }
  133. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  134. $condition[] = ['is_sale','=',1];
  135. $condition[] = ['is_pay','=',0];
  136. $condition[] = ['is_out','=',0];
  137. $condition[] = ['id','=',$param['id']];
  138. $info = AppSale::with(['User'=> function($query) {
  139. $query->field('face,nickname,id');
  140. }])
  141. ->with(['house'=> function($query) {
  142. $query->field('id,title,name,note,sell_price,img,imgs,content');
  143. }])
  144. ->where($condition)->field('id,store_id,user_id,house_id,user_entrust_price,user_sale_price,gift,update_time')->order('id desc')->find();
  145. if(empty($info)){
  146. return enjson(204,'空内容');
  147. }
  148. $data = $info->toArray();
  149. $data['user'] = empty($info->user) ? [] : $info->user;
  150. $data['store'] = empty($info->store) ? [] : $info->store;
  151. $data['is_store'] = empty($info->user) ? 0 : 1;
  152. $data['house'] = $info->house;
  153. $data['house']['imgs'] = json_decode($info->house->imgs,true);
  154. $house_ids = array_column(json_decode($info->gift),'house_id');
  155. $gift = [];
  156. foreach ($house_ids as $i => $id) {
  157. $gift[$i] = SaleHouse::where(['id' => $id])->field('id,title,name,note,sell_price,img')->find()->toArray();
  158. }
  159. $data['gift'] = $gift;
  160. return enjson(200,'成功',$data);
  161. }
  162. /**
  163. * 查看是否允许下单
  164. * @param integer 读取ID
  165. * @return json
  166. */
  167. public function isOrder(){
  168. $this->isUserAuth();
  169. $param['id'] = Request::param('id/d',1);
  170. $param['sign'] = Request::param('sign');
  171. $rel = $this->apiSign($param);
  172. if($rel['code'] != 200){
  173. return enjson(204,'签名失败');
  174. }
  175. $condition[] = ['id','=',$param['id']];
  176. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  177. $condition[] = ['is_sale','=',1];
  178. $condition[] = ['is_pay','=',0];
  179. $condition[] = ['is_out','=',0];
  180. $sale = AppSale::where($condition)->count();
  181. if (empty($sale)) {
  182. return enjson(403,'宝贝已下架');
  183. }
  184. return enjson(200);
  185. }
  186. /**
  187. * 查看是否允许下单
  188. * @param integer 读取ID
  189. * @return json
  190. */
  191. public function cartItem(){
  192. $this->isUserAuth();
  193. $param['cart'] = Request::param('cart/d',0);
  194. $param['sign'] = Request::param('sign');
  195. $rel = $this->apiSign($param);
  196. if($rel['code'] != 200){
  197. return enjson(204,'签名失败');
  198. }
  199. $condition[] = ['id','=',$param['cart']];
  200. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  201. $condition[] = ['is_sale','=',1];
  202. $condition[] = ['is_pay','=',0];
  203. $condition[] = ['is_out','=',0];
  204. $info = AppSale::with(['house'=> function($query) {
  205. $query->field('id,title,name,note,sell_price,cost_price,img');
  206. }])
  207. ->where($condition)->field('id,store_id,user_id,house_id,user_entrust_price,user_sale_price,gift,update_time')->find();
  208. if(empty($info)){
  209. return enjson(204,'空内容');
  210. }
  211. $data = $info->toArray();
  212. $data['house'] = $info->house;
  213. $data['amount']['order_amount'] = $info->user_sale_price;
  214. $data['amount']['real_amount'] = $info->user_sale_price;
  215. $data['amount']['real_freight'] = 0;
  216. $house_ids = array_column(json_decode($info->gift),'house_id');
  217. $gift = [];
  218. foreach ($house_ids as $i => $id) {
  219. $gift[$i] = SaleHouse::where(['id' => $id])->field('id,title,name,note,sell_price,img')->find()->toArray();
  220. }
  221. $data['gift'] = $gift;
  222. return enjson(200,'成功',$data);
  223. }
  224. /**
  225. * 微信商城支付
  226. * @param string $no
  227. * @return void
  228. */
  229. public function doPay(){
  230. $this->isUserAuth();
  231. $rule = [
  232. 'address' => Request::param('address/d'),
  233. 'cart' => Request::param('cart/d'),
  234. 'ucode' => Request::param('ucode'),
  235. 'sign' => Request::param('sign')
  236. ];
  237. $validate = $this->validate($rule,'Cart.sale_order');
  238. if(true !== $validate){
  239. return enjson(403,$validate);
  240. }
  241. $rel = $this->apiSign($rule);
  242. if($rel['code'] != 200){
  243. return enjson(403,'签名失败');
  244. }
  245. $sale_id = $rule['cart'];
  246. if(empty($sale_id)){
  247. return enjson(204,'购物车空');
  248. }
  249. $config = Config::where(['member_miniapp_id' => $this->miniapp_id])->find();
  250. //下单次数
  251. if($config->num_referee_people > 0){
  252. $peple_num = SystemUserLevel::where(['parent_id' => $this->user->id,'level'=>1])->count();
  253. $sale_user = SaleUser::where(['member_miniapp_id' => $this->miniapp_id,'user_id' => $this->user->id,'is_rebate' => 0,'is_out' => 0])->field('user_id,order_no,is_rebate')->select();
  254. $entrust_num = count(Util::unique_array($sale_user->toArray()));
  255. $num = ceil($peple_num/$config->num_referee_people);
  256. if($num <= $entrust_num && $num){
  257. return enjson(403,"还有[{$entrust_num}]单未成交,暂时不能购买。");
  258. }
  259. }
  260. //读取发货地址
  261. $address = SystemUserAddress::where(['user_id'=>$this->user->id,'id' => $rule['address']])->find();
  262. if(empty($address)){
  263. return enjson(204,'请重新选择收货地址');
  264. }
  265. //支付接口
  266. $payment = SystemMemberPayment::where(['apiname'=>'wepay','member_miniapp_id'=>$this->miniapp_id])->field('id')->find();
  267. if(empty($payment)){
  268. return enjson(204,'未开通微信支付功能');
  269. }
  270. //读取订单
  271. $condition[] = ['id','=',$sale_id];
  272. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  273. $condition[] = ['is_sale','=',1];
  274. $condition[] = ['is_pay','=',0];
  275. $condition[] = ['is_out','=',0];
  276. $item = AppSale::where($condition)->find();
  277. if(empty($item)){
  278. return enjson(204,'空内容');
  279. }
  280. //查询应用帐号余额
  281. if(SystemMemberBank::moneyJudge($this->miniapp->member_id,$item->user_sale_price*0.05)){
  282. return enjson(204,'官方帐号余额不足,请联系管理员');
  283. }
  284. $house_ids = array_column(json_decode($item->gift),'house_id');
  285. $gift = [];
  286. foreach ($house_ids as $i => $id) {
  287. $gift[$i] = SaleHouse::where(['id' => $id])->field('id,title,name,note,sell_price,img')->find()->toArray();
  288. }
  289. $item->gift = $gift;
  290. $order_no = $this->user->invite_code.order_no(); //生成的订单号
  291. $order['order_no'] = $order_no;
  292. $order['member_miniapp_id'] = $this->miniapp_id;
  293. $order['user_id'] = $this->user->id;
  294. $order['order_amount'] = $item->user_sale_price;
  295. $order['real_amount'] = $item->user_sale_price;
  296. $order['sale_id'] = $item->id;
  297. $order['payment_id'] = $payment['id']; //支付ID
  298. $order['express_name'] = $address['name'];
  299. $order['express_phone'] = $address['telphone'];
  300. $order['express_address'] = $address['address'];
  301. $order['real_freight'] = 0;
  302. $order['order_starttime'] = time();
  303. $order_id = SaleOrder::insertGetId($order);
  304. if(empty($order_id)){
  305. return enjson(204,'创建订单失败');
  306. }
  307. //主商品
  308. $house_data = [
  309. 'sale_order_id' => $order_id,
  310. 'order_no' => $order_no,
  311. 'house_id' => $item->house_id,
  312. 'sale_price' => $item->house->sell_price,
  313. 'name' => $item->house->name,
  314. 'img' => $item->house->img,
  315. 'is_sales' => 1,
  316. 'is_entrust' => 0
  317. ];
  318. //保存订单产品到缓存数据表
  319. $order_cache = [];
  320. foreach ($item->gift as $key => $value) {
  321. $order_cache[$key]['sale_order_id'] = $order_id;
  322. $order_cache[$key]['order_no'] = $order_no;
  323. $order_cache[$key]['house_id'] = $value['id'];
  324. $order_cache[$key]['sale_price'] = $value['sell_price'];
  325. $order_cache[$key]['name'] = $value['name'];
  326. $order_cache[$key]['img'] = $value['img'];
  327. $order_cache[$key]['is_sales'] = 0;
  328. $order_cache[$key]['is_entrust'] = 0;
  329. }
  330. array_unshift($order_cache,$house_data);
  331. $rel = SaleOrderCache::insertAll($order_cache); //批量插入订单记录
  332. //去请求微信支付接口
  333. $payparm = [
  334. 'openid' => $this->user->miniapp_uid,
  335. 'miniapp_id' => $this->miniapp_id,
  336. 'name' => $this->miniapp->appname.'购买套装',
  337. 'total_fee' => $item->user_sale_price*100,
  338. 'order_no' => $order_no,
  339. 'notify_url' => api(1,'popupshop/notify/sale',$this->miniapp_id),
  340. ];
  341. $ispay = WechatPay::orderPay($payparm);
  342. if($ispay['code'] == 0){
  343. return enjson(403,$ispay['msg']);
  344. }
  345. return enjson(200,'成功',$ispay['data']);
  346. }
  347. }