StoreOrderController.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. <?php
  2. namespace app\api\controller\order;
  3. use app\admin\model\system\{
  4. SystemAttachment, ShippingTemplates
  5. };
  6. use app\admin\model\user\User;
  7. use app\models\routine\RoutineFormId;
  8. use app\models\user\UserBill;
  9. use crmeb\repositories\OrderRepository;
  10. use app\models\store\{
  11. StoreBargainUser,
  12. StoreCart,
  13. StoreCoupon,
  14. StoreCouponIssue,
  15. StoreCouponUser,
  16. StoreOrder,
  17. StoreOrderCartInfo,
  18. StoreOrderStatus,
  19. StorePink,
  20. StoreProductReply
  21. };
  22. use app\models\system\SystemStore;
  23. use app\models\order\Line;
  24. use app\models\user\UserAddress;
  25. use app\models\user\UserLevel;
  26. use app\Request;
  27. use crmeb\services\{
  28. CacheService,
  29. ExpressService,
  30. SystemConfigService,
  31. UtilService
  32. };
  33. use think\facade\Db;
  34. use crmeb\utils\Line as LineJob;
  35. /**
  36. * 订单类
  37. * Class StoreOrderController
  38. * @package app\api\controller\order
  39. */
  40. class StoreOrderController
  41. {
  42. /**
  43. * 订单确认
  44. * @param Request $request
  45. * @return mixed
  46. */
  47. public function confirm(Request $request)
  48. {
  49. $temp = ShippingTemplates::get(1);
  50. if (!$temp) return app('json')->fail('默认模板未配置,无法下单');
  51. list($cartId) = UtilService::postMore(['cartId'], $request, true);
  52. if (!is_string($cartId) || !$cartId) return app('json')->fail('请提交购买的商品');
  53. $uid = $request->uid();
  54. $cartGroup = StoreCart::getUserProductCartList($uid, $cartId, 1);
  55. if (count($cartGroup['invalid'])) {
  56. // return app('json')->fail($cartGroup['invalid'][0]['productInfo']['store_name'] . '已失效!');
  57. return app('json')->fail($cartGroup['invalid']);
  58. }
  59. if (!$cartGroup['valid']) return app('json')->fail('请提交购买的商品');
  60. $cartInfo = $cartGroup['valid'];
  61. $addr = UserAddress::where('uid', $uid)->where('is_default', 1)->find();
  62. $priceGroup = StoreOrder::getOrderPriceGroup($cartInfo, $addr);
  63. if ($priceGroup === false) {
  64. return app('json')->fail(StoreOrder::getErrorInfo('运费模板不存在'));
  65. }
  66. $other = [
  67. 'offlinePostage' => sys_config('offline_postage'),
  68. 'integralRatio' => sys_config('integral_ratio')
  69. ];
  70. $usableCoupons = StoreCouponUser::getUsableCouponList($uid, $cartGroup, $priceGroup['totalPrice']);
  71. $usableCoupon = isset($usableCoupons[0]) ? $usableCoupons[0] : null;
  72. $cartIdA = explode(',', $cartId);
  73. $seckill_id = 0;
  74. $combination_id = 0;
  75. $bargain_id = 0;
  76. $activity_id = 0;
  77. $live_id = 0;
  78. $is_gift = 0;
  79. if (count($cartIdA) == 1) {
  80. $seckill_id = StoreCart::where('id', $cartId)->value('seckill_id');
  81. $combination_id = StoreCart::where('id', $cartId)->value('combination_id');
  82. $bargain_id = StoreCart::where('id', $cartId)->value('bargain_id');
  83. $activity_id = StoreCart::where('id', $cartId)->value('activity_id');
  84. $live_id = StoreCart::where('id', $cartId)->value('live_id');
  85. $is_gift = StoreCart::where('id', $cartId)->value('is_gift');
  86. }
  87. $use_integral_pink = Db::name('store_combination')->alias('c')->join('store_product p', 'c.product_id=p.id')->where('c.id', '=', $combination_id)->value('cost_integral');
  88. $data['use_integral_pink'] = $use_integral_pink; //拼团商品的消耗的默认积分
  89. $data['deduction'] = $seckill_id || $combination_id || $bargain_id;
  90. $data['usableCoupon'] = $usableCoupon;
  91. $data['addressInfo'] = UserAddress::getUserDefaultAddress($uid);
  92. $data['seckill_id'] = $seckill_id;
  93. $data['combination_id'] = $combination_id;
  94. $data['bargain_id'] = $bargain_id;
  95. $data['activity_id'] = $activity_id;
  96. $data['live_id'] = $live_id;
  97. $data['cartInfo'] = $cartInfo;
  98. $data['priceGroup'] = $priceGroup;
  99. $data['orderKey'] = StoreOrder::cacheOrderInfo($uid, $cartInfo, $priceGroup, $other);
  100. $data['offlinePostage'] = $other['offlinePostage'];
  101. $vipId = UserLevel::getUserLevel($uid);
  102. $user = $request->user();
  103. if (isset($user['pwd'])) unset($user['pwd']);
  104. $user['vip'] = $vipId !== false ? true : false;
  105. if ($user['vip']) {
  106. $user['vip_id'] = $vipId;
  107. $user['discount'] = UserLevel::getUserLevelInfo($vipId, 'discount');
  108. }
  109. $data['is_gift'] = $is_gift;
  110. $data['userInfo'] = $user;
  111. $data['integralRatio'] = $other['integralRatio'];
  112. $data['offline_pay_status'] = (int)sys_config('offline_pay_status') ?? (int)2;
  113. $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
  114. $data['system_store'] = ($res = SystemStore::getStoreDispose()) ? $res : [];//门店信息
  115. return app('json')->successful($data);
  116. }
  117. /**
  118. * 计算订单金额
  119. * @param Request $request
  120. * @param $key
  121. * @return mixed
  122. * @throws \think\Exception
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. * @throws \think\exception\DbException
  126. */
  127. public function computedOrder(Request $request, $key)
  128. {
  129. // $priceGroup = StoreOrder::getOrderPriceGroup($cartInfo);
  130. if (!$key) return app('json')->fail('参数错误!');
  131. $uid = $request->uid();
  132. if (StoreOrder::be(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0]))
  133. return app('json')->status('extend_order', '订单已生成', ['orderId' => $key, 'key' => $key]);
  134. list($addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckill_id, $formId, $bargainId, $activity_id, $live_id, $shipping_type, $use_integral_pink,$is_num) = UtilService::postMore([
  135. 'addressId', 'couponId', ['payType', 'yue'], ['useIntegral', 0], 'mark', ['combinationId', 0], ['pinkId', 0], ['seckill_id', 0], ['formId', ''], ['bargainId', ''], ['activity_id', 0], ['live_id', 0], ['shipping_type', 1], ['use_integral_pink', sys_config('pink_cost_integral')],['is_num',0]], $request, true);
  136. $payType = strtolower($payType);
  137. // if ($combinationId) {
  138. // $userinfo = $request->user()->toArray();
  139. // if ($use_integral_pink + $useIntegral > $userinfo['integral']) {
  140. // return app('json')->fail('抱歉:您的积分不够');
  141. // }
  142. // }
  143. if ($bargainId) {
  144. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $uid);//TODO 获取用户参与砍价表编号
  145. if (!$bargainUserTableId)
  146. return app('json')->fail('砍价失败');
  147. $status = StoreBargainUser::getBargainUserStatusEnd($bargainUserTableId);
  148. if ($status == 3)
  149. return app('json')->fail('砍价已支付');
  150. StoreBargainUser::setBargainUserStatus($bargainId, $uid); //修改砍价状态
  151. }
  152. if ($pinkId) {
  153. if (StorePink::getIsPinkUid($pinkId, $request->uid()))
  154. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经在该团内不能再参加了', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  155. if (StoreOrder::getIsOrderPink($pinkId, $request->uid()))
  156. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经参加该团了,请先支付订单', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  157. }
  158. if ($activity_id) {
  159. //todo
  160. }
  161. if ($live_id) {
  162. //todo
  163. }
  164. $priceGroup = StoreOrder::cacheKeyCreateOrder($request->uid(), $key, $addressId, $payType, (int)$useIntegral, $couponId, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, $activity_id, $live_id, true, 0, $shipping_type,$use_integral_pink,$is_num);
  165. if ($priceGroup)
  166. return app('json')->status('NONE', 'ok', $priceGroup);
  167. else
  168. return app('json')->fail(StoreOrder::getErrorInfo('计算失败'));
  169. }
  170. /**
  171. * 订单创建
  172. * @param Request $request
  173. * @param $key
  174. * @return mixed
  175. * @throws \think\Exception
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\ModelNotFoundException
  178. * @throws \think\exception\DbException
  179. */
  180. public function create(Request $request, $key)
  181. {
  182. if (!$key) return app('json')->fail('参数错误!');
  183. $uid = $request->uid();
  184. if (StoreOrder::be(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0]))
  185. return app('json')->status('extend_order', '订单已生成', ['orderId' => $key, 'key' => $key]);
  186. list($addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckill_id, $formId, $bargainId, $activity_id, $live_id, $from, $shipping_type, $real_name, $phone, $storeId, $use_integral_pink, $isAutoPink,$is_gift,$spread,$is_num,$pink_num,$line_id,$is_wdc) = UtilService::postMore([
  187. 'addressId', 'couponId', 'payType', ['useIntegral', 0], 'mark', ['combinationId', 0], ['pinkId', 0], ['seckill_id', 0], ['formId', ''], ['bargainId', ''], ['activity_id', 0], ['live_id', 0], ['from', 'weixin'], ['shipping_type', 1], ['real_name', ''], ['phone', ''], ['store_id', 0], ['use_integral_pink', sys_config('pink_cost_integral')], ['is_auto_pink', 0],['is_gift',0],['spread',0],['is_num',0],['pink_num',1],['line_id',0],['is_wdc',0]
  188. ], $request, true);
  189. $payType = strtolower($payType);
  190. // if ($combinationId) {
  191. // $userinfo = $request->user()->toArray();
  192. // if ($use_integral_pink + $useIntegral > $userinfo['integral']) {
  193. // return app('json')->fail('抱歉:您的积分不够');
  194. // }
  195. // }
  196. if ($bargainId) {
  197. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $uid);//TODO 获取用户参与砍价表编号
  198. if (!$bargainUserTableId)
  199. return app('json')->fail('砍价失败');
  200. $status = StoreBargainUser::getBargainUserStatusEnd($bargainUserTableId);
  201. if ($status == 3)
  202. return app('json')->fail('砍价已支付');
  203. StoreBargainUser::setBargainUserStatus($bargainId, $uid); //修改砍价状态
  204. }
  205. $top_num=sys_config('pink_suceess_num');
  206. $num=StorePink::where('uid',$uid)->where('is_refund',0)->where('status',2)->whereTime('stop_time','today')->count();
  207. //查找 有无还未结束的自动拼团订单
  208. if($combinationId && $isAutoPink){
  209. if($num>=$top_num) return app('json')->fail('今日拼团已达上限');
  210. $auto_pink=StoreOrder::where('uid',$uid)->where('combination_id','<>',0)->where('is_del',0)->where('is_ok',0)->find();
  211. if($auto_pink){
  212. return app('json')->fail('拼团正在进行');
  213. }
  214. }
  215. if ($pinkId) {
  216. $people = StorePink::where("id", $pinkId)->value('people');
  217. // $count = StorePink::where("id|k_id", $pinkId)->count();
  218. $count = StoreOrder::where("pink_id={$pinkId} and is_del=0")->count();
  219. if ($count >= $people) {
  220. return app('json')->fail('有人在您前面付款,请稍后再试!' . $people . '-' . $count);
  221. }
  222. // if (StorePink::where('id', $pinkId)->value('status') != 1) {
  223. // return app('json')->fail('拼团已经完成或结束,无法参团');
  224. // }
  225. if (StorePink::getIsPinkUid($pinkId, $request->uid()))
  226. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经在该团内不能再参加了', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  227. if (StoreOrder::getIsOrderPink($pinkId, $request->uid()))
  228. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经参加该团了,请先支付订单', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  229. if (StorePink::isPinkStatus($pinkId))
  230. return app('json')->fail('拼团已经完成,无法支付!');
  231. }
  232. if ($activity_id) {
  233. //todo
  234. }
  235. if ($live_id) {
  236. //todo
  237. }
  238. $isChannel = 1;
  239. if ($from == 'weixin')
  240. $isChannel = 0;
  241. elseif ($from == 'weixinh5')
  242. $isChannel = 2;
  243. elseif ($from == 'integral')
  244. $isChannel = 3;
  245. elseif ($from == 'wdc')
  246. $isChannel = 4;
  247. $order = StoreOrder::cacheKeyCreateOrder($request->uid(), $key, $addressId, $payType, (int)$useIntegral, $couponId, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, $activity_id, $live_id, false, $isChannel, $shipping_type, $real_name, $phone, $storeId, intval($use_integral_pink), $isAutoPink,$is_gift,$spread,$is_num,$pink_num,$line_id,$is_wdc);
  248. if ($order === false) return app('json')->fail(StoreOrder::getErrorInfo('订单生成失败'));
  249. $orderId = $order['order_id'];
  250. $info = compact('orderId', 'key');
  251. if ($orderId) {
  252. event('OrderCreated', [$order]); //订单创建成功事件
  253. event('ShortMssageSend', [$orderId, 'AdminPlaceAnOrder']);//发送管理员通知
  254. switch ($payType) {
  255. case "weixin":
  256. $orderInfo = StoreOrder::where('order_id', $orderId)->find();
  257. if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  258. $orderInfo = $orderInfo->toArray();
  259. if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  260. //支付金额为0
  261. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  262. //创建订单jspay支付
  263. $payPriceStatus = StoreOrder::jsPayPrice($orderId, $uid, $formId);
  264. if ($payPriceStatus)//0元支付成功
  265. return app('json')->status('success', '微信支付成功', $info);
  266. else
  267. return app('json')->status('pay_error', StoreOrder::getErrorInfo());
  268. } else {
  269. try {
  270. if ($from == 'routine') {
  271. $jsConfig = OrderRepository::jsPay($orderId); //创建订单jspay
  272. } else if ($from == 'weixinh5') {
  273. $jsConfig = OrderRepository::h5Pay($orderId);
  274. } else {
  275. $jsConfig = OrderRepository::wxPay($orderId);
  276. }
  277. } catch (\Exception $e) {
  278. return app('json')->status('pay_error', $e->getMessage(), $info);
  279. }
  280. $info['jsConfig'] = $jsConfig;
  281. if ($from == 'weixinh5') {
  282. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  283. } else {
  284. return app('json')->status('wechat_pay', '订单创建成功', $info);
  285. }
  286. }
  287. break;
  288. case 'yue':
  289. if (StoreOrder::yuePay($orderId, $request->uid(), $formId))
  290. return app('json')->status('success', '余额支付成功', $info);
  291. else {
  292. $errorinfo = StoreOrder::getErrorInfo();
  293. if (is_array($errorinfo))
  294. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  295. else
  296. return app('json')->status('pay_error', $errorinfo);
  297. }
  298. break;
  299. case 'integral':
  300. if (StoreOrder::integralPay($orderId, $request->uid(), $formId))
  301. return app('json')->status('success', '积分支付成功', $info);
  302. else {
  303. $errorinfo = StoreOrder::getErrorInfo();
  304. if (is_array($errorinfo))
  305. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  306. else
  307. return app('json')->status('pay_error', $errorinfo);
  308. }
  309. break;
  310. case 'wdc':
  311. if (StoreOrder::wdcPay($orderId, $request->uid(), $formId))
  312. return app('json')->status('success', 'wdc支付成功', $info);
  313. else {
  314. $errorinfo = StoreOrder::getErrorInfo();
  315. if (is_array($errorinfo))
  316. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  317. else
  318. return app('json')->status('pay_error', $errorinfo);
  319. }
  320. break;
  321. case 'offline':
  322. return app('json')->status('success', '订单创建成功', $info);
  323. break;
  324. }
  325. } else return app('json')->fail(StoreOrder::getErrorInfo('订单生成失败!'));
  326. }
  327. /**
  328. * 订单 再次下单
  329. * @param Request $request
  330. * @return mixed
  331. */
  332. public function again(Request $request)
  333. {
  334. list($uni) = UtilService::postMore([
  335. ['uni', ''],
  336. ], $request, true);
  337. if (!$uni) return app('json')->fail('参数错误!');
  338. $order = StoreOrder::getUserOrderDetail($request->uid(), $uni);
  339. if (!$order) return app('json')->fail('订单不存在!');
  340. $order = StoreOrder::tidyOrder($order, true);
  341. $res = [];
  342. foreach ($order['cartInfo'] as $v) {
  343. if ($v['combination_id']) return app('json')->fail('拼团产品不能再来一单,请在拼团产品内自行下单!');
  344. else if ($v['bargain_id']) return app('json')->fail('砍价产品不能再来一单,请在砍价产品内自行下单!');
  345. else if ($v['seckill_id']) return app('json')->ail('秒杀产品不能再来一单,请在秒杀产品内自行下单!');
  346. else if ($v['activity_id']) return app('json')->ail('活动产品不能再来一单,请在秒杀产品内自行下单!');
  347. else $res[] = StoreCart::setCart($request->uid(), $v['product_id'], $v['cart_num'], isset($v['productInfo']['attrInfo']['unique']) ? $v['productInfo']['attrInfo']['unique'] : '', 'product', 0, 0);
  348. }
  349. $cateId = [];
  350. foreach ($res as $v) {
  351. if (!$v) return app('json')->fail('再来一单失败,请重新下单!');
  352. $cateId[] = $v['id'];
  353. }
  354. event('OrderCreateAgain', implode(',', $cateId));
  355. return app('json')->successful('ok', ['cateId' => implode(',', $cateId)]);
  356. }
  357. /**
  358. * 订单支付
  359. * @param Request $request
  360. * @return mixed
  361. */
  362. public function pay(Request $request)
  363. {
  364. list($uni, $paytype, $from) = UtilService::postMore([
  365. ['uni', ''],
  366. ['paytype', 'weixin'],
  367. ['from', 'weixin']
  368. ], $request, true);
  369. if (!$uni) return app('json')->fail('参数错误!');
  370. $order = StoreOrder::getUserOrderDetail($request->uid(), $uni);
  371. if (!$order)
  372. return app('json')->fail('订单不存在!');
  373. if ($order['paid'])
  374. return app('json')->fail('该订单已支付!');
  375. if ($order['pink_id']) if (StorePink::isPinkStatus($order['pink_id']))
  376. return app('json')->fail('该订单已失效!');
  377. if ($order['pink_id']) {
  378. $data = StorePink::where('id', $order['pink_id'])->field("k_id,people")->find();
  379. $count = StoreOrder::where("pink_id", $order['pink_id'])->where("is_del=0")->where("uid!={$request->uid()}")->count();
  380. if ($count >= $data['people']) {
  381. return app('json')->fail('有人在您前面付款,请稍后再试!');
  382. }
  383. if (StorePink::isPinkStatus($order['pink_id']))
  384. return app('json')->fail('拼团已经完成,无法支付!');
  385. }
  386. if ($from == 'weixin') {//0
  387. if (in_array($order->is_channel, [1, 2,3,4]))
  388. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  389. }
  390. if ($from == 'weixinh5') {//2
  391. if (in_array($order->is_channel, [0, 1,3,4]))
  392. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  393. }
  394. if ($from == 'routine') {//1
  395. if (in_array($order->is_channel, [0, 2,3,4]))
  396. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  397. }
  398. if ($from == 'integral') {//1
  399. if (in_array($order->is_channel, [0, 2,1,4]))
  400. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  401. }
  402. if ($from == 'wdc') {//1
  403. if (in_array($order->is_channel, [0, 2,1,3]))
  404. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  405. }
  406. $order['pay_type'] = $paytype; //重新支付选择支付方式
  407. switch ($order['pay_type']) {
  408. case 'weixin':
  409. try {
  410. if ($from == 'routine') {
  411. $jsConfig = OrderRepository::jsPay($order); //订单列表发起支付
  412. } else if ($from == 'weixinh5') {
  413. $jsConfig = OrderRepository::h5Pay($order);
  414. } else {
  415. $jsConfig = OrderRepository::wxPay($order);
  416. }
  417. } catch (\Exception $e) {
  418. return app('json')->fail($e->getMessage());
  419. }
  420. if ($from == 'weixinh5') {
  421. return app('json')->status('wechat_h5_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
  422. } else {
  423. return app('json')->status('wechat_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
  424. }
  425. break;
  426. case 'yue':
  427. if (StoreOrder::yuePay($order['order_id'], $request->uid()))
  428. return app('json')->status('success', '余额支付成功');
  429. else {
  430. $error = StoreOrder::getErrorInfo();
  431. return app('json')->fail(is_array($error) && isset($error['msg']) ? $error['msg'] : $error);
  432. }
  433. break;
  434. case 'integral':
  435. if (StoreOrder::integralPay($order['order_id'], $request->uid()))
  436. return app('json')->status('success', '余额支付成功');
  437. else {
  438. $error = StoreOrder::getErrorInfo();
  439. return app('json')->fail(is_array($error) && isset($error['msg']) ? $error['msg'] : $error);
  440. }
  441. break;
  442. case 'wdc':
  443. if (StoreOrder::wdcPay($order['order_id'], $request->uid()))
  444. return app('json')->status('success', '余额支付成功');
  445. else {
  446. $error = StoreOrder::getErrorInfo();
  447. return app('json')->fail(is_array($error) && isset($error['msg']) ? $error['msg'] : $error);
  448. }
  449. break;
  450. case 'offline':
  451. StoreOrder::createOrderTemplate($order);
  452. if (StoreOrder::setOrderTypePayOffline($order['order_id']))
  453. return app('json')->status('success', '订单创建成功');
  454. else
  455. return app('json')->status('success', '支付失败');
  456. break;
  457. }
  458. return app('json')->fail('支付方式错误');
  459. }
  460. /**
  461. * 订单列表
  462. * @param Request $request
  463. * @return mixed
  464. */
  465. public function lst(Request $request)
  466. {
  467. list($type, $page, $limit, $search) = UtilService::getMore([
  468. ['type', ''],
  469. ['page', 0],
  470. ['limit', ''],
  471. ['search', ''],
  472. ], $request, true);
  473. return app('json')->successful(StoreOrder::getUserOrderSearchList($request->uid(), $type, $page, $limit, $search));
  474. }
  475. /**
  476. * 订单详情
  477. * @param Request $request
  478. * @param $uni
  479. * @return mixed
  480. */
  481. public function detail(Request $request, $uni)
  482. {
  483. if (!strlen(trim($uni))) return app('json')->fail('参数错误');
  484. $order = StoreOrder::getUserOrderDetail($request->uid(), $uni);
  485. if (!$order) return app('json')->fail('订单不存在');
  486. $order = $order->toArray();
  487. //是否开启门店自提
  488. $store_self_mention = sys_config('store_self_mention');
  489. //关闭门店自提后 订单隐藏门店信息
  490. if ($store_self_mention == 0) $order['shipping_type'] = 1;
  491. if ($order['verify_code']) {
  492. $verify_code = $order['verify_code'];
  493. $verify[] = substr($verify_code, 0, 4);
  494. $verify[] = substr($verify_code, 4, 4);
  495. $verify[] = substr($verify_code, 8);
  496. $order['_verify_code'] = implode(' ', $verify);
  497. }
  498. $order['add_time_y'] = date('Y-m-d', $order['add_time']);
  499. $order['add_time_h'] = date('H:i:s', $order['add_time']);
  500. $order['system_store'] = SystemStore::getStoreDispose($order['store_id']);
  501. if ($order['shipping_type'] === 2 && $order['verify_code']) {
  502. $name = $order['verify_code'] . '.jpg';
  503. $imageInfo = SystemAttachment::getInfo($name, 'name');
  504. $siteUrl = sys_config('site_url');
  505. if (!$imageInfo) {
  506. $imageInfo = UtilService::getQRCodePath($order['verify_code'], $name);
  507. if (is_array($imageInfo)) {
  508. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  509. $url = $imageInfo['dir'];
  510. } else
  511. $url = '';
  512. } else $url = $imageInfo['att_dir'];
  513. if (isset($imageInfo['image_type']) && $imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  514. $order['code'] = $url;
  515. }
  516. $order['mapKey'] = sys_config('tengxun_map_key');
  517. return app('json')->successful('ok', StoreOrder::tidyOrder($order, true, true));
  518. }
  519. /**
  520. * 订单删除
  521. * @param Request $request
  522. * @return mixed
  523. */
  524. public function del(Request $request)
  525. {
  526. list($uni) = UtilService::postMore([
  527. ['uni', ''],
  528. ], $request, true);
  529. if (!$uni) return app('json')->fail('参数错误!');
  530. $res = StoreOrder::removeOrder($uni, $request->uid());
  531. if ($res)
  532. return app('json')->successful();
  533. else
  534. return app('json')->fail(StoreOrder::getErrorInfo());
  535. }
  536. /**
  537. * 订单收货
  538. * @param Request $request
  539. * @return mixed
  540. */
  541. public function take(Request $request)
  542. {
  543. list($uni) = UtilService::postMore([
  544. ['uni', ''],
  545. ], $request, true);
  546. if (!$uni) return app('json')->fail('参数错误!');
  547. $res = StoreOrder::takeOrder($uni, $request->uid());
  548. if ($res) {
  549. $order_info = StoreOrder::where('order_id', $uni)->find();
  550. $gain_integral = intval($order_info['gain_integral']);
  551. $gain_coupon = StoreCouponIssue::alias('a')
  552. ->join('store_coupon b', 'a.cid = b.id')
  553. ->where('a.status', 1)
  554. ->where('a.is_full_give', 1)
  555. ->where('a.is_del', 0)
  556. ->where('a.full_reduction', '<=', $order_info['total_price'])
  557. ->sum('b.coupon_price');
  558. return app('json')->successful(['gain_integral' => $gain_integral, 'gain_coupon' => $gain_coupon]);
  559. } else
  560. return app('json')->fail(StoreOrder::getErrorInfo());
  561. }
  562. /**
  563. * 订单 查看物流
  564. * @param Request $request
  565. * @param $uni
  566. * @return mixed
  567. */
  568. public function express(Request $request, $uni)
  569. {
  570. if (!$uni || !($order = StoreOrder::getUserOrderDetail($request->uid(), $uni))) return app('json')->fail('查询订单不存在!');
  571. if ($order['delivery_type'] != 'express' || !$order['delivery_id']) return app('json')->fail('该订单不存在快递单号!');
  572. $cacheName = $uni . $order['delivery_id'];
  573. $result = CacheService::get($cacheName, null);
  574. if ($result === NULL) {
  575. $result = ExpressService::query($order['delivery_id']);
  576. if (is_array($result) &&
  577. isset($result['result']) &&
  578. isset($result['result']['deliverystatus']) &&
  579. $result['result']['deliverystatus'] >= 3)
  580. $cacheTime = 0;
  581. else
  582. $cacheTime = 1800;
  583. CacheService::set($cacheName, $result, $cacheTime);
  584. }
  585. $orderInfo = [];
  586. $cartInfo = StoreOrderCartInfo::where('oid', $order['id'])->column('cart_info', 'unique') ?? [];
  587. $info = [];
  588. $cartNew = [];
  589. foreach ($cartInfo as $k => $cart) {
  590. $cart = json_decode($cart, true);
  591. $cartNew['cart_num'] = $cart['cart_num'];
  592. $cartNew['truePrice'] = $cart['truePrice'];
  593. $cartNew['productInfo']['image'] = $cart['productInfo']['image'];
  594. $cartNew['productInfo']['store_name'] = $cart['productInfo']['store_name'];
  595. $cartNew['productInfo']['unit_name'] = $cart['productInfo']['unit_name'] ?? '';
  596. array_push($info, $cartNew);
  597. unset($cart);
  598. }
  599. $orderInfo['delivery_id'] = $order['delivery_id'];
  600. $orderInfo['delivery_name'] = $order['delivery_name'];
  601. $orderInfo['delivery_type'] = $order['delivery_type'];
  602. $orderInfo['cartInfo'] = $info;
  603. return app('json')->successful(['order' => $orderInfo, 'express' => $result ? $result : []]);
  604. }
  605. /**
  606. * 订单评价
  607. * @param Request $request
  608. * @return mixed
  609. * @throws \think\db\exception\DataNotFoundException
  610. * @throws \think\db\exception\ModelNotFoundException
  611. * @throws \think\exception\DbException
  612. */
  613. public function comment(Request $request)
  614. {
  615. $group = UtilService::postMore([
  616. ['unique', ''], ['comment', ''], ['pics', ''], ['product_score', 5], ['service_score', 5]
  617. ], $request);
  618. $unique = $group['unique'];
  619. unset($group['unique']);
  620. if (!$unique) return app('json')->fail('参数错误!');
  621. $cartInfo = StoreOrderCartInfo::where('unique', $unique)->find();
  622. $uid = $request->uid();
  623. $user_info = User::get($uid);
  624. $group['nickname'] = $user_info['nickname'];
  625. $group['avatar'] = $user_info['avatar'];
  626. if (!$cartInfo) return app('json')->fail('评价产品不存在!');
  627. $orderUid = StoreOrder::getOrderInfo($cartInfo['oid'], 'uid')['uid'];
  628. if ($uid != $orderUid) return app('json')->fail('评价产品不存在!');
  629. if (StoreProductReply::be(['oid' => $cartInfo['oid'], 'unique' => $unique]))
  630. return app('json')->fail('该产品已评价!');
  631. $group['comment'] = htmlspecialchars(trim($group['comment']));
  632. if ($group['product_score'] < 1) return app('json')->fail('请为产品评分');
  633. else if ($group['service_score'] < 1) return app('json')->fail('请为商家服务评分');
  634. if ($cartInfo['cart_info']['combination_id']) $productId = $cartInfo['cart_info']['product_id'];
  635. else if ($cartInfo['cart_info']['seckill_id']) $productId = $cartInfo['cart_info']['product_id'];
  636. else if ($cartInfo['cart_info']['bargain_id']) $productId = $cartInfo['cart_info']['product_id'];
  637. else $productId = $cartInfo['product_id'];
  638. if ($group['pics']) $group['pics'] = json_encode(is_array($group['pics']) ? $group['pics'] : explode(',', $group['pics']));
  639. $group = array_merge($group, [
  640. 'uid' => $uid,
  641. 'oid' => $cartInfo['oid'],
  642. 'unique' => $unique,
  643. 'product_id' => $productId,
  644. 'add_time' => time(),
  645. 'reply_type' => 'product'
  646. ]);
  647. StoreProductReply::beginTrans();
  648. $res = StoreProductReply::reply($group, 'product');
  649. if (!$res) {
  650. StoreProductReply::rollbackTrans();
  651. return app('json')->fail('评价失败!');
  652. }
  653. try {
  654. StoreOrder::checkOrderOver($cartInfo['oid']);
  655. } catch (\Exception $e) {
  656. StoreProductReply::rollbackTrans();
  657. return app('json')->fail($e->getMessage());
  658. }
  659. StoreProductReply::commitTrans();
  660. event('UserCommented', $res);
  661. event('AdminNewPush');
  662. return app('json')->successful();
  663. }
  664. /**
  665. * 订单统计数据
  666. * @param Request $request
  667. * @return mixed
  668. */
  669. public function data(Request $request)
  670. {
  671. return app('json')->successful(StoreOrder::getOrderData($request->uid()));
  672. }
  673. /**
  674. * 订单退款理由
  675. * @return mixed
  676. */
  677. public function refund_reason()
  678. {
  679. $reason = sys_config('stor_reason') ?: [];//退款理由
  680. $reason = str_replace("\r\n", "\n", $reason);//防止不兼容
  681. $reason = explode("\n", $reason);
  682. return app('json')->successful($reason);
  683. }
  684. /**
  685. * 订单退款审核
  686. * @param Request $request
  687. * @return mixed
  688. */
  689. public function refund_verify(Request $request)
  690. {
  691. $data = UtilService::postMore([
  692. ['text', ''],
  693. ['refund_reason_wap_img', ''],
  694. ['refund_reason_wap_explain', ''],
  695. ['uni', '']
  696. ], $request);
  697. $uni = $data['uni'];
  698. unset($data['uni']);
  699. if ($data['refund_reason_wap_img']) $data['refund_reason_wap_img'] = explode(',', $data['refund_reason_wap_img']);
  700. if (!$uni || $data['text'] == '') return app('json')->fail('参数错误!');
  701. $res = StoreOrder::orderApplyRefund($uni, $request->uid(), $data['text'], $data['refund_reason_wap_explain'], $data['refund_reason_wap_img']);
  702. if ($res)
  703. return app('json')->successful('提交申请成功');
  704. else
  705. return app('json')->fail(StoreOrder::getErrorInfo());
  706. }
  707. /**
  708. * 订单取消 未支付的订单回退积分,回退优惠券,回退库存
  709. * @param Request $request
  710. * @return mixed
  711. * @throws \think\db\exception\DataNotFoundException
  712. * @throws \think\db\exception\ModelNotFoundException
  713. * @throws \think\exception\DbException
  714. */
  715. public function cancel(Request $request)
  716. {
  717. list($id) = UtilService::postMore([['id', 0]], $request, true);
  718. if (!$id) return app('json')->fail('参数错误');
  719. if (StoreOrder::cancelOrder($id, $request->uid()))
  720. return app('json')->successful('取消订单成功');
  721. return app('json')->fail(StoreOrder::getErrorInfo('取消订单失败'));
  722. }
  723. /**
  724. * 订单产品信息
  725. * @param Request $request
  726. * @return mixed
  727. * @throws \think\db\exception\DataNotFoundException
  728. * @throws \think\db\exception\ModelNotFoundException
  729. * @throws \think\exception\DbException
  730. */
  731. public function product(Request $request)
  732. {
  733. list($unique) = UtilService::postMore([['unique', '']], $request, true);
  734. if (!$unique || !StoreOrderCartInfo::be(['unique' => $unique]) || !($cartInfo = StoreOrderCartInfo::where('unique', $unique)->find())) return app('json')->fail('评价产品不存在!');
  735. $cartInfo = $cartInfo->toArray();
  736. $cartProduct = [];
  737. $cartProduct['cart_num'] = $cartInfo['cart_info']['cart_num'];
  738. $cartProduct['productInfo']['image'] = isset($cartInfo['cart_info']['productInfo']['image']) ? $cartInfo['cart_info']['productInfo']['image'] : '';
  739. $cartProduct['productInfo']['price'] = isset($cartInfo['cart_info']['productInfo']['price']) ? $cartInfo['cart_info']['productInfo']['price'] : 0;
  740. $cartProduct['productInfo']['store_name'] = isset($cartInfo['cart_info']['productInfo']['store_name']) ? $cartInfo['cart_info']['productInfo']['store_name'] : '';
  741. if (isset($cartInfo['cart_info']['productInfo']['attrInfo'])) {
  742. $cartProduct['productInfo']['attrInfo']['product_id'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['product_id']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['product_id'] : '';
  743. $cartProduct['productInfo']['attrInfo']['suk'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['suk']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['suk'] : '';
  744. $cartProduct['productInfo']['attrInfo']['price'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['price']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['price'] : '';
  745. $cartProduct['productInfo']['attrInfo']['image'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['image']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['image'] : '';
  746. }
  747. $cartProduct['product_id'] = isset($cartInfo['cart_info']['product_id']) ? $cartInfo['cart_info']['product_id'] : 0;
  748. $cartProduct['combination_id'] = isset($cartInfo['cart_info']['combination_id']) ? $cartInfo['cart_info']['combination_id'] : 0;
  749. $cartProduct['seckill_id'] = isset($cartInfo['cart_info']['seckill_id']) ? $cartInfo['cart_info']['seckill_id'] : 0;
  750. $cartProduct['bargain_id'] = isset($cartInfo['cart_info']['bargain_id']) ? $cartInfo['cart_info']['bargain_id'] : 0;
  751. $cartProduct['order_id'] = StoreOrder::where('id', $cartInfo['oid'])->value('order_id');
  752. return app('json')->successful($cartProduct);
  753. }
  754. /**
  755. * 首页获取未支付订单
  756. */
  757. public function get_noPay(Request $request)
  758. {
  759. return app('json')->successful(StoreOrder::getUserOrderSearchList($request->uid(), 0, 0, 0, ''));
  760. }
  761. /**
  762. * 快递物流查询接口
  763. * @param Request $request
  764. * @return mixed
  765. */
  766. public function express_query(Request $request)
  767. {
  768. $param = UtilService::getMore([
  769. 'id'
  770. ], $request);
  771. $res = Db::name('store_order')->alias('o')->join('express e', 'o.delivery_name=e.name')->field("o.delivery_name as delivery_name,o.delivery_id as delivery_id,e.code as code")->where("o.id", $param['id'])->select()->toArray();
  772. if (empty($res)) {
  773. return app('json')->fail('找不到订单');
  774. }
  775. if (!$res[0]['delivery_id'] || !$res[0]['code']) {
  776. return app('json')->fail('快递单号不存在或找不到该快递公司');
  777. }
  778. //参数设置
  779. $key = 'YOBlimCZ5185'; //客户授权key
  780. $customer = '5B24735E58787695A978711842716651'; //查询公司编号
  781. $param = array(
  782. 'com' => $res[0]['code'], //快递公司编码
  783. 'num' => $res[0]['delivery_id'], //快递单号
  784. 'phone' => '', //手机号
  785. 'from' => '', //出发地城市
  786. 'to' => '', //目的地城市
  787. 'resultv2' => '1' //开启行政区域解析
  788. );
  789. //请求参数
  790. $post_data = array();
  791. $post_data["customer"] = $customer;
  792. $post_data["param"] = json_encode($param);
  793. $sign = md5($post_data["param"] . $key . $post_data["customer"]);
  794. $post_data["sign"] = strtoupper($sign);
  795. $url = 'http://poll.kuaidi100.com/poll/query.do'; //实时查询请求地址
  796. $params = "";
  797. foreach ($post_data as $k => $v) {
  798. $params .= "$k=" . urlencode($v) . "&"; //默认UTF-8编码格式
  799. }
  800. $post_data = substr($params, 0, -1);
  801. // echo '请求参数<br/>' . $post_data;
  802. //发送post请求
  803. $ch = curl_init();
  804. curl_setopt($ch, CURLOPT_POST, 1);
  805. curl_setopt($ch, CURLOPT_HEADER, 0);
  806. curl_setopt($ch, CURLOPT_URL, $url);
  807. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  808. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  809. $result = curl_exec($ch);
  810. $data = str_replace("\"", '"', $result);
  811. $data = json_decode($data, true);
  812. if ($data['status'] != 200) {
  813. return app('json')->fail($data['message']);
  814. }
  815. switch ($data['state']) {
  816. case 0:
  817. $data['state'] = '在途';
  818. break;
  819. case 1:
  820. $data['state'] = '揽收';
  821. break;
  822. case 2:
  823. $data['state'] = '疑难';
  824. break;
  825. case 3:
  826. $data['state'] = '签收';
  827. break;
  828. case 4:
  829. $data['state'] = '退签';
  830. break;
  831. case 5:
  832. $data['state'] = '派件';
  833. break;
  834. case 6:
  835. $data['state'] = '退回';
  836. break;
  837. case 7:
  838. $data['state'] = '转单';
  839. break;
  840. case 10:
  841. $data['state'] = '待清关';
  842. break;
  843. case 11:
  844. $data['state'] = '清关中';
  845. break;
  846. case 12:
  847. $data['state'] = '已清关';
  848. break;
  849. case 13:
  850. $data['state'] = '清关异常';
  851. break;
  852. case 14:
  853. $data['state'] = '收件人拒签';
  854. break;
  855. default:
  856. $data['state'] = '未知';
  857. break;
  858. }
  859. unset($data['message']);
  860. unset($data['status']);
  861. unset($data['ischeck']);
  862. $data['com'] = $res[0]['delivery_name'];
  863. unset($data['condition']);
  864. return app('json')->success('ok', $data);
  865. }
  866. //添加排队
  867. public function line(Request $request)
  868. {
  869. $line_num=sys_config('line_num');//获取每天排队的次数
  870. $find_line_num=Line::where('uid',$request->uid())->whereTime('add_time', 'today')->count();
  871. if($find_line_num>=$line_num) return app('json')->fail('当日参加排队次数已达上限');
  872. //$id=LineJob::create([$request->uid()]);
  873. //@file_put_contents('258.txt',json_encode($id));
  874. $id=line::createLevelOrder($request->uid());
  875. if($id){
  876. return app('json')->successful($id->toArray());
  877. }else{
  878. return app('json')->fail('排队失败');
  879. }
  880. }
  881. //我的排队订单
  882. public function line_order(Request $request){
  883. list($type,$page,$limit) = UtilService::postMore([['type', ''],['page',1],['limit',20]], $request, true);
  884. $arr=Line::my_line($type,$page,$limit,$request->uid());
  885. return app('json')->successful($arr->toArray());
  886. }
  887. //排队挂售首页
  888. public function line_list(Request $request){
  889. list($page,$limit) = UtilService::postMore([['page',1],['limit',20]], $request, true);
  890. $info = $request->user()->toArray();
  891. $arr['max']=sys_config('line_num');
  892. $arr['low']=Line::where('uid',$request->uid())->whereTime('add_time','today')->count();
  893. //排队成功多少佣金
  894. $arr['suc_bo_pd']=sys_config('suc_bo_pd');
  895. //参加排队多少酒币
  896. $arr['del_wdc']=sys_config('del_wdc');
  897. //邀请多少人结束排队
  898. $arr['invite_num_pd']=sys_config('invite_num_pd');
  899. //剩余酒必
  900. $arr['wdc']=$info['wdc'];
  901. //全部人
  902. $arr['all_line_sum']=Line::where('uid',$request->uid())->count();
  903. //排队中
  904. $arr['now_line_sum']=Line::where('uid',$request->uid())->where('status',0)->count();
  905. //已完成
  906. $arr['over_line_sum']=Line::where('uid',$request->uid())->where('status',1)->count();
  907. $find=Line::where('status',1)->order('id desc')->page($page,$limit)->select();
  908. foreach($find as $key=>&$value){
  909. $value['key']=$key+1;
  910. $value['info']=User::where('uid',$value['uid'])->find();
  911. if($value['info']) $value['info']= $value['info']->toArray();
  912. $value['time']=date('Y-m-d h:i:s',$value['suc_time']);
  913. }
  914. $arr['info']=$find;
  915. return app('json')->successful($arr);
  916. }
  917. //立即结束
  918. public function js_line(Request $request){
  919. list($order_id) = UtilService::postMore([['order_id', '']], $request, true);
  920. $arr=Line::js_line($order_id);
  921. if($arr){
  922. return app('json')->successful('成功');
  923. }else{
  924. return app('json')->fail('失败');
  925. }
  926. }
  927. }