AuthApi.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. namespace app\ebapi\controller;
  3. use app\core\model\routine\RoutineFormId;//待完善
  4. use app\core\model\user\UserLevel;
  5. use service\JsonService;
  6. use app\core\util\SystemConfigService;
  7. use service\UtilService;
  8. use think\Request;
  9. use app\core\behavior\GoodsBehavior;//待完善
  10. use app\ebapi\model\store\StoreCouponUser;
  11. use app\ebapi\model\store\StoreOrder;
  12. use app\ebapi\model\store\StoreProductAttrValue;
  13. use app\ebapi\model\store\StoreCart;
  14. use app\ebapi\model\user\User;
  15. use app\ebapi\model\store\StorePink;
  16. use app\ebapi\model\store\StoreBargainUser;
  17. use app\ebapi\model\store\StoreBargainUserHelp;
  18. /**
  19. * 小程序 购物车,新增订单等 api接口
  20. * Class AuthApi
  21. * @package app\ebapi\controller
  22. *
  23. */
  24. class AuthApi extends AuthController
  25. {
  26. /**
  27. * 购物车
  28. * @return \think\response\Json
  29. */
  30. public function get_cart_list()
  31. {
  32. return JsonService::successful(StoreCart::getUserProductCartList($this->userInfo['uid']));
  33. }
  34. /*
  35. * 获取订单支付状态
  36. * @param string ordre_id 订单id
  37. * @return json
  38. * */
  39. public function get_order_pay_info($order_id = '')
  40. {
  41. if ($order_id == '') return JsonService::fail('缺少参数');
  42. return JsonService::successful(StoreOrder::tidyOrder(StoreOrder::where('order_id', $order_id)->find()));
  43. }
  44. /**
  45. * 订单页面
  46. * @param Request $request
  47. * @return \think\response\Json
  48. */
  49. public function confirm_order(Request $request)
  50. {
  51. $data = UtilService::postMore(['cartId'], $request);
  52. $cartId = $data['cartId'];
  53. if (!is_string($cartId) || !$cartId) return JsonService::fail('请提交购买的商品');
  54. $cartGroup = StoreCart::getUserProductCartList($this->userInfo['uid'], $cartId, 1);
  55. if (count($cartGroup['invalid'])) return JsonService::fail($cartGroup['invalid'][0]['productInfo']['store_name'] . '已失效!');
  56. if (!$cartGroup['valid']) return JsonService::fail('请提交购买的商品');
  57. $cartInfo = $cartGroup['valid'];
  58. $priceGroup = StoreOrder::getOrderPriceGroup($cartInfo);
  59. $other = [
  60. 'offlinePostage' => SystemConfigService::get('offline_postage'),
  61. 'integralRatio' => SystemConfigService::get('integral_ratio')
  62. ];
  63. $usableCoupon = StoreCouponUser::beUsableCoupon($this->userInfo['uid'], $priceGroup['totalPrice']);
  64. $cartIdA = explode(',', $cartId);
  65. if (count($cartIdA) > 1) $seckill_id = 0;
  66. else {
  67. $seckillinfo = StoreCart::where('id', $cartId)->find();
  68. if ((int)$seckillinfo['seckill_id'] > 0) $seckill_id = $seckillinfo['seckill_id'];
  69. else $seckill_id = 0;
  70. }
  71. $data['usableCoupon'] = $usableCoupon;
  72. $data['seckill_id'] = $seckill_id;
  73. $data['cartInfo'] = $cartInfo;
  74. $data['priceGroup'] = $priceGroup;
  75. $data['orderKey'] = StoreOrder::cacheOrderInfo($this->userInfo['uid'], $cartInfo, $priceGroup, $other);
  76. $data['offlinePostage'] = $other['offlinePostage'];
  77. $vipId=UserLevel::getUserLevel($this->uid);
  78. $this->userInfo['vip']=$vipId !==false ? true : false;
  79. if($this->userInfo['vip']){
  80. $this->userInfo['vip_id']=$vipId;
  81. $this->userInfo['discount']=UserLevel::getUserLevelInfo($vipId,'discount');
  82. }
  83. $data['userInfo']=$this->userInfo;
  84. $data['integralRatio'] = $other['integralRatio'];
  85. return JsonService::successful($data);
  86. }
  87. /*
  88. * 获取小程序订单列表统计数据
  89. *
  90. * */
  91. public function get_order_data()
  92. {
  93. return JsonService::successful(StoreOrder::getOrderData($this->uid));
  94. }
  95. /**
  96. * 过度查$uniqueId
  97. * @param string $productId
  98. * @param int $cartNum
  99. * @param string $uniqueId
  100. * @return \think\response\Json
  101. */
  102. public function unique()
  103. {
  104. $productId = $_GET['productId'];
  105. if (!$productId || !is_numeric($productId)) return JsonService::fail('参数错误');
  106. $uniqueId = StoreProductAttrValue::where('product_id', $productId)->value('unique');
  107. $data = $this->set_cart($productId, $cartNum = 1, $uniqueId);
  108. if ($data == true) {
  109. return JsonService::successful('ok');
  110. }
  111. }
  112. /**
  113. * 加入到购物车
  114. * @param string $productId
  115. * @param int $cartNum
  116. * @param string $uniqueId
  117. * @return \think\response\Json
  118. */
  119. public function set_cart($productId = '', $cartNum = 1, $uniqueId = '')
  120. {
  121. if (!$productId || !is_numeric($productId)) return JsonService::fail('参数错误');
  122. $res = StoreCart::setCart($this->userInfo['uid'], $productId, $cartNum, $uniqueId, 'product');
  123. if (!$res) return JsonService::fail(StoreCart::getErrorInfo());
  124. else return JsonService::successful('ok', ['cartId' => $res->id]);
  125. }
  126. /**
  127. * 拼团 秒杀 砍价 加入到购物车
  128. * @param string $productId
  129. * @param int $cartNum
  130. * @param string $uniqueId
  131. * @param int $combinationId
  132. * @param int $secKillId
  133. * @return \think\response\Json
  134. */
  135. public function now_buy($productId = '', $cartNum = 1, $uniqueId = '', $combinationId = 0, $secKillId = 0, $bargainId = 0)
  136. {
  137. if (!$productId || !is_numeric($productId)) return JsonService::fail('参数错误');
  138. if ($bargainId && StoreBargainUserHelp::getSurplusPrice($bargainId, $this->userInfo['uid'])) return JsonService::fail('请先砍价');
  139. $res = StoreCart::setCart($this->userInfo['uid'], $productId, $cartNum, $uniqueId, 'product', 1, $combinationId, $secKillId, $bargainId);
  140. if (!$res) return JsonService::fail(StoreCart::getErrorInfo());
  141. else return JsonService::successful('ok', ['cartId' => $res->id]);
  142. }
  143. /**
  144. * 获取购物车数量
  145. * @return \think\response\Json
  146. */
  147. public function get_cart_num()
  148. {
  149. return JsonService::successful('ok', StoreCart::getUserCartNum($this->userInfo['uid'], 'product'));
  150. }
  151. /**
  152. * 修改购物车产品数量
  153. * @param string $cartId
  154. * @param string $cartNum
  155. * @return \think\response\Json
  156. */
  157. public function change_cart_num($cartId = '', $cartNum = '')
  158. {
  159. if (!$cartId || !$cartNum || !is_numeric($cartId) || !is_numeric($cartNum)) return JsonService::fail('参数错误!');
  160. $res = StoreCart::changeUserCartNum($cartId, $cartNum, $this->userInfo['uid']);
  161. if ($res) return JsonService::successful();
  162. else return JsonService::fail(StoreCart::getErrorInfo('修改失败'));
  163. }
  164. /**
  165. * 删除购物车产品
  166. * @param string $ids
  167. * @return \think\response\Json
  168. */
  169. public function remove_cart($ids = '')
  170. {
  171. if (!$ids) return JsonService::fail('参数错误!');
  172. if(StoreCart::removeUserCart($this->userInfo['uid'], $ids))
  173. return JsonService::successful();
  174. else
  175. return JsonService::fail('清除失败!');
  176. }
  177. /**
  178. * 创建订单
  179. * @param string $key
  180. * @return \think\response\Json
  181. */
  182. public function create_order($key = '')
  183. {
  184. if (!$key) return JsonService::fail('参数错误!');
  185. if (StoreOrder::be(['order_id|unique' => $key, 'uid' => $this->userInfo['uid'], 'is_del' => 0]))
  186. return JsonService::status('extend_order', '订单已生成', ['orderId' => $key, 'key' => $key]);
  187. list($addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckill_id, $formId, $bargainId) = UtilService::postMore([
  188. 'addressId', 'couponId', 'payType', 'useIntegral', 'mark', ['combinationId', 0], ['pinkId', 0], ['seckill_id', 0], ['formId', ''], ['bargainId', '']
  189. ], Request::instance(), true);
  190. $payType = strtolower($payType);
  191. if ($bargainId) StoreBargainUser::setBargainUserStatus($bargainId, $this->userInfo['uid']); //修改砍价状态
  192. if ($pinkId) if (StorePink::getIsPinkUid($pinkId, $this->userInfo['uid'])) return JsonService::status('ORDER_EXIST', '订单生成失败,你已经在该团内不能再参加了', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $this->userInfo['uid'])]);
  193. if ($pinkId) if (StoreOrder::getIsOrderPink($pinkId, $this->userInfo['uid'])) return JsonService::status('ORDER_EXIST', '订单生成失败,你已经参加该团了,请先支付订单', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $this->userInfo['uid'])]);
  194. $order = StoreOrder::cacheKeyCreateOrder($this->userInfo['uid'], $key, $addressId, $payType, $useIntegral, $couponId, $mark, $combinationId, $pinkId, $seckill_id, $bargainId);
  195. $orderId = $order['order_id'];
  196. $info = compact('orderId', 'key');
  197. if ($orderId) {
  198. switch ($payType) {
  199. case "weixin":
  200. $orderInfo = StoreOrder::where('order_id', $orderId)->find();
  201. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  202. if ($orderInfo['paid']) exception('支付已支付!');
  203. //如果支付金额为0
  204. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  205. //创建订单jspay支付
  206. if (StoreOrder::jsPayPrice($orderId, $this->userInfo['uid'], $formId))
  207. return JsonService::status('success', '微信支付成功', $info);
  208. else
  209. return JsonService::status('pay_error', StoreOrder::getErrorInfo());
  210. } else {
  211. RoutineFormId::SetFormId($formId, $this->uid);
  212. try {
  213. $jsConfig = StoreOrder::jsPay($orderId); //创建订单jspay
  214. if(isset($jsConfig['package']) && $jsConfig['package']){
  215. $package=str_replace('prepay_id=','',$jsConfig['package']);
  216. for($i=0;$i<3;$i++){
  217. RoutineFormId::SetFormId($package, $this->uid);
  218. }
  219. }
  220. } catch (\Exception $e) {
  221. return JsonService::status('pay_error', $e->getMessage(), $info);
  222. }
  223. $info['jsConfig'] = $jsConfig;
  224. return JsonService::status('wechat_pay', '订单创建成功', $info);
  225. }
  226. break;
  227. case 'yue':
  228. if (StoreOrder::yuePay($orderId, $this->userInfo['uid'], $formId))
  229. return JsonService::status('success', '余额支付成功', $info);
  230. else {
  231. $errorinfo = StoreOrder::getErrorInfo();
  232. if (is_array($errorinfo))
  233. return JsonService::status($errorinfo['status'], $errorinfo['msg'], $info);
  234. else
  235. return JsonService::status('pay_error', $errorinfo);
  236. }
  237. break;
  238. case 'offline':
  239. RoutineFormId::SetFormId($formId, $this->uid);
  240. // RoutineTemplate::sendOrderSuccess($formId,$orderId);//发送模板消息
  241. return JsonService::status('success', '订单创建成功', $info);
  242. break;
  243. }
  244. } else return JsonService::fail(StoreOrder::getErrorInfo('订单生成失败!'));
  245. }
  246. /*
  247. * 再来一单
  248. *
  249. * */
  250. public function again_order($uni = ''){
  251. if(!$uni) return JsonService::fail('参数错误!');
  252. $order = StoreOrder::getUserOrderDetail($this->userInfo['uid'],$uni);
  253. if(!$order) return JsonService::fail('订单不存在!');
  254. $order = StoreOrder::tidyOrder($order,true);
  255. $res = array();
  256. foreach ($order['cartInfo'] as $v) {
  257. if($v['combination_id']) return JsonService::fail('拼团产品不能再来一单,请在拼团产品内自行下单!');
  258. else if($v['bargain_id']) return JsonService::fail('砍价产品不能再来一单,请在砍价产品内自行下单!');
  259. else if($v['seckill_id']) return JsonService::fail('秒杀产品不能再来一单,请在砍价产品内自行下单!');
  260. else $res[] = StoreCart::setCart($this->userInfo['uid'], $v['product_id'], $v['cart_num'], isset($v['productInfo']['attrInfo']['unique']) ? $v['productInfo']['attrInfo']['unique'] : '', 'product', 0, 0);
  261. }
  262. $cateId = [];
  263. foreach ($res as $v){
  264. if(!$v) return JsonService::fail('再来一单失败,请重新下单!');
  265. $cateId[] = $v['id'];
  266. }
  267. return JsonService::successful('ok',implode(',',$cateId));
  268. }
  269. //TODO 支付订单
  270. /**
  271. * 支付订单
  272. * @param string $uni
  273. * @return \think\response\Json
  274. */
  275. public function pay_order($uni = '', $paytype = 'weixin')
  276. {
  277. if (!$uni) return JsonService::fail('参数错误!');
  278. $order = StoreOrder::getUserOrderDetail($this->userInfo['uid'], $uni);
  279. if (!$order) return JsonService::fail('订单不存在!');
  280. if ($order['paid']) return JsonService::fail('该订单已支付!');
  281. if ($order['pink_id']) if (StorePink::isPinkStatus($order['pink_id'])) return JsonService::fail('该订单已失效!');
  282. $order['pay_type'] = $paytype; //重新支付选择支付方式
  283. switch ($order['pay_type']) {
  284. case 'weixin':
  285. try {
  286. $jsConfig = StoreOrder::jsPay($order); //订单列表发起支付
  287. if(isset($jsConfig['package']) && $jsConfig['package']){
  288. $package=str_replace('prepay_id=','',$jsConfig['package']);
  289. for($i=0;$i<3;$i++){
  290. RoutineFormId::SetFormId($package, $this->uid);
  291. }
  292. }
  293. } catch (\Exception $e) {
  294. return JsonService::fail($e->getMessage());
  295. }
  296. return JsonService::status('wechat_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
  297. break;
  298. case 'yue':
  299. if ($res = StoreOrder::yuePay($order['order_id'], $this->userInfo['uid']))
  300. return JsonService::successful('余额支付成功');
  301. else {
  302. $error = StoreOrder::getErrorInfo();
  303. return JsonService::fail(is_array($error) && isset($error['msg']) ? $error['msg'] : $error);
  304. }
  305. break;
  306. case 'offline':
  307. StoreOrder::createOrderTemplate($order);
  308. return JsonService::successful('订单创建成功');
  309. break;
  310. }
  311. }
  312. /*
  313. * 未支付的订单取消订单回退积分,回退优惠券,回退库存
  314. * @param string $order_id 订单id
  315. * */
  316. public function cancel_order($order_id = '')
  317. {
  318. if (StoreOrder::cancelOrder($order_id))
  319. return JsonService::successful('取消订单成功');
  320. else
  321. return JsonService::fail(StoreOrder::getErrorInfo());
  322. }
  323. /**
  324. * 申请退款
  325. * @param string $uni
  326. * @param string $text
  327. * @return \think\response\Json
  328. */
  329. public function apply_order_refund(Request $request)
  330. {
  331. $data = UtilService::postMore([
  332. ['text', ''],
  333. ['refund_reason_wap_img', ''],
  334. ['refund_reason_wap_explain', ''],
  335. ['uni', '']
  336. ], $request);
  337. $uni = $data['uni'];
  338. unset($data['uni']);
  339. if ($data['refund_reason_wap_img']) $data['refund_reason_wap_img'] = explode(',', $data['refund_reason_wap_img']);
  340. if (!$uni || $data['text'] == '') return JsonService::fail('参数错误!');
  341. $res = StoreOrder::orderApplyRefund($uni, $this->userInfo['uid'], $data['text'], $data['refund_reason_wap_explain'], $data['refund_reason_wap_img']);
  342. if ($res)
  343. return JsonService::successful();
  344. else
  345. return JsonService::fail(StoreOrder::getErrorInfo());
  346. }
  347. /**
  348. * 再来一单
  349. * @param string $uni
  350. */
  351. public function order_details($uni = '')
  352. {
  353. if (!$uni) return JsonService::fail('参数错误!');
  354. $order = StoreOrder::getUserOrderDetail($this->userInfo['uid'], $uni);
  355. if (!$order) return JsonService::fail('订单不存在!');
  356. $order = StoreOrder::tidyOrder($order, true);
  357. $res = array();
  358. foreach ($order['cartInfo'] as $v) {
  359. if ($v['combination_id']) return JsonService::fail('拼团产品不能再来一单,请在拼团产品内自行下单!');
  360. else $res[] = StoreCart::setCart($this->userInfo['uid'], $v['product_id'], $v['cart_num'], isset($v['productInfo']['attrInfo']['unique']) ? $v['productInfo']['attrInfo']['unique'] : '', 'product', 0, 0);
  361. }
  362. $cateId = [];
  363. foreach ($res as $v) {
  364. if (!$v) return JsonService::fail('再来一单失败,请重新下单!');
  365. $cateId[] = $v['id'];
  366. }
  367. return JsonService::successful('ok', implode(',', $cateId));
  368. }
  369. /**
  370. * 购物车库存修改
  371. * @param int $cartId
  372. * @param int $cartNum
  373. */
  374. public function set_buy_cart_num($cartId = 0, $cartNum = 0)
  375. {
  376. if (!$cartId) return JsonService::fail('参数错误');
  377. $res = StoreCart::edit(['cart_num' => $cartNum], $cartId);
  378. if ($res) return JsonService::successful();
  379. else return JsonService::fail('修改失败');
  380. }
  381. }