Cart.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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\popupshop\controller\api\v1;
  9. use app\popupshop\controller\api\Base;
  10. use app\popupshop\model\Item;
  11. use app\popupshop\model\Fare;
  12. use app\popupshop\model\Order;
  13. use app\popupshop\model\OrderCache;
  14. use app\common\facade\WechatPay;
  15. use app\common\model\SystemMemberPayment;
  16. use app\common\model\SystemUserAddress;
  17. use think\facade\Request;
  18. use filter\Filter;
  19. class Cart extends Base{
  20. public function initialize() {
  21. parent::initialize();
  22. if(!$this->user){
  23. exit(json_encode(['code'=>401,'msg'=>'用户认证失败']));
  24. }
  25. }
  26. /**
  27. * 购物车编辑
  28. */
  29. public function edit(){
  30. $data['item_id'] = Request::param('item_id/d',0);
  31. $data['buy_num'] = Request::param('buy_num/d',1);
  32. $data['sign'] = Request::param('sign');
  33. $rel = $this->apiSign($data);
  34. if($rel['code'] == 200){
  35. if($data['buy_num'] > 5){
  36. return enjson(403,'超过最大购买商品数');
  37. }
  38. //检测SPU是否存在或下架
  39. $spu = Item::where(['id' => $data['item_id'],'is_sale'=>2])->find();
  40. if(empty($spu)){
  41. return enjson(403,'商品已下降');
  42. }
  43. $cart[$data['item_id']] = $data['buy_num'];
  44. return enjson(200,'成功',$cart);
  45. }
  46. return enjson($rel['code'],'签名验证失败');
  47. }
  48. /**
  49. * 读取购物车产品列表
  50. */
  51. public function cartItem(){
  52. if(request()->isPost()){
  53. $cart = Request::param('cart/a',[]);
  54. if (empty($cart)) {
  55. return enjson(204,'没有宝贝');
  56. }
  57. $ids = ids(array_keys($cart),true);
  58. //查找商品SPU
  59. $item = Item::where(['is_sale'=>2,'id' => $ids,'member_miniapp_id' => $this->miniapp_id])
  60. ->field('id,category_id,name,sell_price,market_price,points,repoints,weight,img')
  61. ->select();
  62. if(empty($item)){
  63. return enjson(403,'没有宝贝');
  64. }else{
  65. $data = [];
  66. foreach ($item as $value) {
  67. $num = abs(intval($cart[$value['id']]));
  68. $num = $num <= 0 ? 1: $num;
  69. $data[$value['id']] = [
  70. 'id' => $value['id'],
  71. 'num' => $num,
  72. 'amount' => money($value['sell_price'] * $num), //单个商品价格的总价
  73. 'market_price' => money($value['market_price']),
  74. 'sell_price' => money($value['sell_price']),
  75. 'weight' => $value['weight'],
  76. 'points' => $value['points'],
  77. 'repoints' => $value['repoints'],
  78. 'name' => $value['name'],
  79. 'img' => $value['img']
  80. ];
  81. }
  82. $amount = Fare::realAmount($data,$this->miniapp_id);
  83. return enjson(200,'成功',['item' => $data,'amount' => $amount]);
  84. }
  85. }
  86. }
  87. /**
  88. * ################################
  89. * 判断是否支付了
  90. */
  91. public function isPay($order_no){
  92. $order_no = Filter::filter_escape($order_no);
  93. $order = Order::where(['order_no' => $order_no,'paid_at' => 0])->field('id,order_no,paid_no,order_amount')->find();
  94. if(empty($order)){
  95. return enjson(204);
  96. }
  97. return enjson();
  98. }
  99. /**
  100. * 微信商城支付
  101. * @param string $no
  102. * @return void
  103. */
  104. public function doPay(){
  105. $rule = [
  106. 'address' => Request::param('address/d',0),
  107. 'ids' => Request::param('ids/s','','htmlspecialchars_decode'),
  108. 'ucode' => Request::param('ucode'),
  109. 'sign' => Request::param('sign')
  110. ];
  111. $validate = $this->validate($rule,'Cart.add_order');
  112. if(true !== $validate){
  113. return enjson(403,$validate);
  114. }
  115. $rel = $this->apiSign($rule);
  116. if($rel['code'] != 200){
  117. return enjson(403,'签名失败');
  118. }
  119. $cart = (array)ids(json_decode($rule['ids'],true),true); //购买的产品ID和数量
  120. $ids = ids(array_keys($cart),true); //购买的产品ID
  121. if (empty($ids)){
  122. return enjson(403,'购物车是空的');
  123. }
  124. //读取发货地址
  125. $address = SystemUserAddress::where(['user_id'=>$this->user->id,'id' =>$rule['address']])->find();
  126. if(empty($address)){
  127. return enjson(403,'请选择收货地址');
  128. }
  129. //支付接口
  130. $payment = SystemMemberPayment::where(['apiname'=>'wepay','member_miniapp_id'=>$this->miniapp_id])->field('id')->find();
  131. if(empty($payment)){
  132. return enjson(403,'未开通微信支付功能');
  133. }
  134. //读取订单
  135. $item = Item::where(['is_sale'=>2,'id' => $ids,'member_miniapp_id' => $this->miniapp_id])
  136. ->field('id,sell_price,market_price,points,repoints,weight,name,img')
  137. ->select();
  138. if(empty($item)){
  139. return enjson(403,'没有宝贝');
  140. }
  141. $data = [];
  142. foreach ($item as $value) {
  143. $num = abs(intval($cart[$value['id']]));
  144. $num = $num <= 0 ? 1: $num;
  145. $data[$value['id']] = [
  146. 'id' => $value['id'],
  147. 'num' => $num,
  148. 'amount' => money($value['sell_price'] * $num), //单个商品价格的总价
  149. 'market_price' => money($value['market_price']),
  150. 'sell_price' => money($value['sell_price']),
  151. 'weight' => $value['weight'],
  152. 'points' => $value['points'],
  153. 'repoints' => $value['repoints'],
  154. 'name' => $value['name'],
  155. 'img' => $value['img'],
  156. ];
  157. }
  158. $order_no = $this->user->invite_code.order_no(); //生成的订单号
  159. $amount = Fare::realAmount($data,$this->miniapp_id);
  160. //创建订单
  161. $order['payment_id'] = $payment['id']; //支付ID
  162. $order['express_name'] = $address['name'];
  163. $order['express_phone'] = $address['telphone'];
  164. $order['express_address'] = $address['address'];
  165. $order['order_amount'] = $amount['order_amount'];
  166. $order['real_amount'] = $amount['real_amount'];
  167. $order['real_freight'] = $amount['real_freight'];
  168. $order['order_no'] = $order_no;
  169. $order['member_miniapp_id'] = $this->miniapp_id;
  170. $order['user_id'] = $this->user->id;
  171. $order['order_starttime'] = time();
  172. $order_id = Order::insertGetId($order);
  173. if(empty($order_id)){
  174. return enjson(403,'创建订单失败');
  175. }
  176. //保存订单产品到缓存数据表
  177. foreach ($data as $key => $value) {
  178. $item_data[$key]['order_id'] = $order_id ;
  179. $item_data[$key]['order_no'] = $order_no;
  180. $item_data[$key]['item_id'] = $value['id'];
  181. $item_data[$key]['buy_price'] = $value['sell_price'];
  182. $item_data[$key]['buy_nums'] = $value['num'];
  183. $item_data[$key]['name'] = $value['name'];
  184. $item_data[$key]['img'] = $value['img'];
  185. }
  186. OrderCache::insertAll($item_data);
  187. //去请求微信支付接口
  188. $payparm = [
  189. 'openid' => $this->user->miniapp_uid,
  190. 'miniapp_id' => $this->miniapp_id,
  191. 'name' => $this->miniapp->appname.'购买商品',
  192. 'order_no' => $order_no,
  193. 'total_fee' => $amount['order_amount']*100,
  194. 'notify_url' => api(1,'popupshop/notify/shop',$this->miniapp_id),
  195. ];
  196. $ispay = WechatPay::orderPay($payparm);
  197. if($ispay['code'] == 0){
  198. return enjson(403,$ispay['msg']);
  199. }
  200. return enjson(200,'成功',$ispay['data']);
  201. }
  202. /**
  203. * 重新支付
  204. * @param string $no
  205. * @return void
  206. */
  207. public function retrypay(){
  208. $rule = [
  209. 'order_no' => Request::param('order_no'),
  210. 'sign' => Request::param('sign')
  211. ];
  212. $rel = $this->apiSign($rule);
  213. if($rel['code'] == 200){
  214. $order = Order::where(['order_no' => $rule['order_no'],'member_miniapp_id' => $this->miniapp_id,'paid_at' => 0])->find();
  215. if(empty($order)){
  216. return enjson(403,'未找到当前订单信息');
  217. }
  218. $payorder = [
  219. 'openid' => $this->user->miniapp_uid,
  220. 'miniapp_id' => $this->miniapp_id,
  221. 'name' => $this->miniapp->appname.'购买商品',
  222. 'order_no' => $order->order_no,
  223. 'total_fee' => $order->order_amount*100,
  224. 'notify_url' => api(1,'popupshop/notify/shop',$this->miniapp_id),
  225. ];
  226. $ispay = WechatPay::orderPay($payorder);
  227. if($ispay['code'] == 0){
  228. return enjson(403,$ispay['msg']);
  229. }
  230. return enjson(200,'成功',$ispay['data']);
  231. }
  232. return enjson(204,'签名失败');
  233. }
  234. /**
  235. * 关闭订单
  236. */
  237. public function closeorder(){
  238. $rule = [
  239. 'order_no' => Request::param('order_no'),
  240. 'sign' => Request::param('sign')
  241. ];
  242. $rel = $this->apiSign($rule);
  243. if($rel['code'] == 200){
  244. $condition['order_no'] = $rule['order_no'];
  245. $condition['user_id'] = $this->user->id;
  246. $condition['is_del'] = 0;
  247. $rel = Order::where($condition)->update(['is_del' => 1]);
  248. if(empty($rel)){
  249. return enjson(403,'关闭失败');
  250. }
  251. return enjson(200,'成功关闭');
  252. }
  253. return enjson(204,'签名失败');
  254. }
  255. /**
  256. * 签收订单
  257. */
  258. public function signOrder($order_no){
  259. $rule = [
  260. 'order_no' => Request::param('order_no'),
  261. 'sign' => Request::param('sign')
  262. ];
  263. $rel = $this->apiSign($rule);
  264. if($rel['code'] == 200){
  265. $condition['order_no'] = Filter::filter_escape($order_no);
  266. $condition['user_id'] = $this->user->id;
  267. $condition['is_del'] = 0;
  268. $condition['paid_at'] = 1;
  269. $condition['express_status'] = 1;
  270. $rel = Order::where($condition)->update(['status' => 1]);
  271. if(empty($rel)){
  272. return enjson(403,'未找到当前订单');
  273. }
  274. return enjson(200,'订单签收成功');
  275. }
  276. return enjson(204,'签名失败');
  277. }
  278. /**
  279. * ######################################
  280. * 小程序端用户的订单列表
  281. * 我的产品列表
  282. */
  283. public function order(){
  284. $rule = [
  285. 'types' => Request::param('types',0),
  286. 'page' => Request::param('page',0),
  287. 'sign' => Request::param('sign')
  288. ];
  289. $rel = $this->apiSign($rule);
  290. if($rel['code'] == 200){
  291. $order = Order::getUserOrderList($this->user->id,$rule['types']);
  292. if(empty($order)) {
  293. return enjson(204,'没有订单');
  294. }
  295. return enjson(200,'成功',Order::order_data($order));
  296. }
  297. return enjson(204,$rel['msg']);
  298. }
  299. /**
  300. * 读取购物车预览
  301. */
  302. public function review(){
  303. $rule = [
  304. 'order_no' => Request::param('order_no',0),
  305. 'sign' => Request::param('sign')
  306. ];
  307. $rel = $this->apiSign($rule);
  308. if($rel['code'] == 200){
  309. $order = Order::getOrder($rule['order_no'],$this->user->id);
  310. if(empty($order)) {
  311. return enjson(204,'没有订单');
  312. }
  313. return enjson(200,'成功',Order::order_data($order));
  314. }
  315. return enjson(204,$rel['msg']);
  316. }
  317. }