OrderController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-30
  6. * Time: 下午10:58
  7. */
  8. namespace App\Http\Controllers\Api\V1;
  9. use App\Models\Nurse;
  10. use App\Models\Order;
  11. use App\Models\OrderNurse;
  12. use App\Models\OrderPack;
  13. use App\Models\OrderPatient;
  14. use App\Models\OrderVaccine;
  15. use App\Models\Patient;
  16. use App\Models\Payment;
  17. use App\Models\ServicePack;
  18. use App\Models\Team;
  19. use App\Models\TimePeriod;
  20. use App\Models\User;
  21. use App\Models\UserCoupon;
  22. use App\Models\Vaccine;
  23. use EasyWeChat\Factory;
  24. use DB;
  25. class OrderController extends AuthController
  26. {
  27. public function consultPlaceOrder()
  28. {
  29. $req = request()->post();
  30. $this->validate(request(), [
  31. 'payment_type' => 'required|in:1,2,3',
  32. 'product_type' => 'required|in:1,2',
  33. 'docter_id' => 'required|integer',
  34. 'patient_id' => 'required|integer',
  35. 'total_amount' => 'required|integer',
  36. 'user_coupon_id' => 'integer',
  37. 'phone' => 'required_if:product_type,1',
  38. 'phone_minutes' => 'required_if:product_type,1|integer',
  39. 'symptoms' => 'required_if:product_type,2|max:2000',
  40. 'medical_imgs' => 'required_if:product_type,2|json|max:3000',
  41. 'order_pack_id' => 'required_if:payment_type,3|integer',
  42. 'pay_password|支付密码' => 'integer',
  43. ]);
  44. $user = $this->user;
  45. if (!empty($req['pay_password'])) {
  46. if (empty($user['pay_password'])) {
  47. return out(null, 60010, '未设置支付密码');
  48. }
  49. if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
  50. return out(null, 10001, '密码错误');
  51. }
  52. }
  53. $discount_amount = 0;
  54. if (!empty($req['user_coupon_id'])) {
  55. //计算优惠金额
  56. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $req['product_type']);
  57. }
  58. $payment_amount = $req['total_amount'] - $discount_amount;
  59. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  60. if (in_array($req['payment_type'], [2,3]) && $payment_amount > 0 && empty($req['pay_password'])) {
  61. return out(null, 10011, '请输入支付密码');
  62. }
  63. if ($req['payment_type'] == 2) {
  64. if ($user['balance'] < $payment_amount) {
  65. return out(null, 601, '余额不足');
  66. }
  67. }
  68. if ($req['payment_type'] == 3) {
  69. OrderPack::checkUserServicePack($req['order_pack_id'], $user['id'], $req['payment_type'], $payment_amount);
  70. }
  71. $order_status = $payment_status = 1;
  72. $payment_time = 0;
  73. if ($payment_amount == 0 || in_array($req['payment_type'], [2,3])) {
  74. $order_status = $payment_status = 2;
  75. $payment_time = time();
  76. }
  77. $config = null;
  78. DB::beginTransaction();
  79. try {
  80. //保存订单数据
  81. $create = [
  82. 'user_id' => $user['id'],
  83. 'payment_type' => $req['payment_type'],
  84. 'product_type' => $req['product_type'],
  85. 'docter_id' => $req['docter_id'],
  86. 'total_amount' => $req['total_amount'],
  87. 'payment_amount' => $payment_amount,
  88. 'discount_amount' => $discount_amount,
  89. 'patient_id' => $req['patient_id'],
  90. 'order_status' => $order_status,
  91. 'payment_status' => $payment_status,
  92. 'payment_time' => $payment_time,
  93. ];
  94. if ($req['payment_type'] == 3) {
  95. $create['pay_order_pack_id'] = $req['order_pack_id'];
  96. }
  97. $order = Order::create($create);
  98. $order_sn = build_sn($order['id']);
  99. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  100. //保存订单患者信息
  101. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url', 'email', 'phone as patient_phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'])->where('id', $req['patient_id'])->first()->getOriginal();
  102. $addPatient['order_id'] = $order['id'];
  103. $addPatient['patient_id'] = $req['patient_id'];
  104. if ($req['product_type'] == 1) {
  105. $addPatient['phone'] = $req['phone'];
  106. $addPatient['phone_minutes'] = $req['phone_minutes'];
  107. }
  108. elseif ($req['product_type'] == 2) {
  109. $addPatient['symptoms'] = $req['symptoms'];
  110. $addPatient['medical_imgs'] = $req['medical_imgs'];
  111. }
  112. OrderPatient::create($addPatient);
  113. //判断是微信支付
  114. if ($req['payment_type'] == 1) {
  115. //生成支付交易单
  116. if ($payment_amount > 0) {
  117. $trade_sn = build_sn($order['id'], 3, 'T');
  118. $payBody = '超级宝妈-'.config('config.product_type_map')[$req['product_type']];
  119. Payment::create([
  120. 'user_id' => $user['id'],
  121. 'order_id' => $order['id'],
  122. 'trade_sn' => $trade_sn,
  123. 'amount' => $payment_amount,
  124. 'remark' => $payBody,
  125. ]);
  126. //请求支付
  127. $payment = Factory::payment(config('config.wechat_pay'));
  128. $result = $payment->order->unify([
  129. 'body' => $payBody,
  130. 'out_trade_no' => $trade_sn,
  131. 'total_fee' => $payment_amount,
  132. 'trade_type' => 'JSAPI',
  133. 'openid' => $user['openid'],
  134. ]);
  135. if (empty($result['prepay_id'])) {
  136. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  137. return out(null, 702, $errorMsg);
  138. }
  139. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  140. }
  141. }
  142. //判断是余额支付
  143. elseif ($req['payment_type'] == 2) {
  144. if ($payment_amount > 0) {
  145. //改变用户余额
  146. $change_amount = 0 - $payment_amount;
  147. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '咨询订单消费');
  148. }
  149. }
  150. if ($payment_amount == 0 || in_array($req['payment_type'], [2, 3])) {
  151. Order::payCompletedHandle($order['id']);
  152. }
  153. DB::commit();
  154. } catch (\Exception $e) {
  155. DB::rollBack();
  156. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  157. }
  158. return out($config);
  159. }
  160. public function appointPlaceOrder()
  161. {
  162. $req = request()->post();
  163. $this->validate(request(), [
  164. 'payment_type' => 'required|in:1,2,3',
  165. 'product_type' => 'required|in:3,4,5',
  166. 'patient_id' => 'required|integer',
  167. 'organization_id' => 'required|integer',
  168. 'schedule_date' => 'required|date',
  169. 'time_period_id' => 'required|integer',
  170. 'total_amount' => 'required|integer',
  171. 'user_coupon_id' => 'integer',
  172. 'docter_id' => 'required_if:product_type,3|integer',
  173. 'vaccine_id' => 'required_if:product_type,4|integer',
  174. 'nurse_ids' => 'required_if:product_type,5|json',
  175. 'order_pack_id' => 'required_if:payment_type,3|integer',
  176. 'pay_password|支付密码' => 'integer',
  177. ]);
  178. $user = $this->user;
  179. if (!empty($req['pay_password'])) {
  180. if (empty($user['pay_password'])) {
  181. return out(null, 60010, '未设置支付密码');
  182. }
  183. if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
  184. return out(null, 10001, '密码错误');
  185. }
  186. }
  187. $product_type = $req['product_type'];
  188. if ($req['product_type'] == 4) {
  189. $vaccine = Vaccine::select(['type'])->where('id', $req['vaccine_id'])->first();
  190. if ($vaccine['type'] == 2) {
  191. if (empty($req['total_amount'])) {
  192. return out(null, 10001, '总价不能为0');
  193. }
  194. }
  195. }
  196. $discount_amount = 0;
  197. if (!empty($req['user_coupon_id'])) {
  198. //计算优惠金额
  199. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], $product_type);
  200. }
  201. $payment_amount = $req['total_amount'] - $discount_amount;
  202. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  203. if (in_array($req['payment_type'], [2,3]) && $payment_amount > 0 && empty($req['pay_password'])) {
  204. return out(null, 10011, '请输入支付密码');
  205. }
  206. if ($req['payment_type'] == 2) {
  207. if ($user['balance'] < $payment_amount) {
  208. return out(null, 601, '余额不足');
  209. }
  210. }
  211. if ($req['payment_type'] == 3) {
  212. OrderPack::checkUserServicePack($req['order_pack_id'], $user['id'], $req['payment_type'], $payment_amount);
  213. }
  214. $order_status = $payment_status = 1;
  215. $payment_time = 0;
  216. if ($payment_amount == 0 || in_array($req['payment_type'], [2,3])) {
  217. $order_status = 3;
  218. $payment_status = 2;
  219. $payment_time = time();
  220. }
  221. $config = null;
  222. DB::beginTransaction();
  223. try {
  224. //保存订单数据
  225. $create = [
  226. 'user_id' => $user['id'],
  227. 'docter_id' => $req['docter_id'] ?? 0,
  228. 'patient_id' => $req['patient_id'],
  229. 'organization_id' => $req['organization_id'],
  230. 'payment_type' => $req['payment_type'],
  231. 'product_type' => $product_type,
  232. 'order_status' => $order_status,
  233. 'payment_status' => $payment_status,
  234. 'total_amount' => $req['total_amount'],
  235. 'payment_amount' => $payment_amount,
  236. 'discount_amount' => $discount_amount,
  237. 'payment_time' => $payment_time,
  238. ];
  239. if ($req['payment_type'] == 3) {
  240. $create['pay_order_pack_id'] = $req['order_pack_id'];
  241. }
  242. $order = Order::create($create);
  243. $order_sn = build_sn($order['id']);
  244. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  245. //保存订单患者信息
  246. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url', 'email', 'phone as patient_phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'])->where('id', $req['patient_id'])->first()->getOriginal();
  247. $addPatient['order_id'] = $order['id'];
  248. $addPatient['patient_id'] = $req['patient_id'];
  249. $addPatient['organization_id'] = $req['organization_id'];
  250. $addPatient['time_period_id'] = $req['time_period_id'];
  251. $timePeriod = TimePeriod::where('id', $req['time_period_id'])->first();
  252. $addPatient['appoint_start_time'] = strtotime($req['schedule_date'].' '.$timePeriod['start_time_period'].':00');
  253. $addPatient['appoint_end_time'] = strtotime($req['schedule_date'].' '.$timePeriod['end_time_period'].':00');
  254. $orderPatient = OrderPatient::create($addPatient);
  255. //保存订单疫苗信息
  256. if ($req['product_type'] == 4) {
  257. $addVaccine = Vaccine::select(['id as vaccine_id', 'type as vaccine_type', 'price as vaccine_price', 'name as vaccine_name', 'remark as vaccine_remark', 'supplier as vaccine_supplier'])->where('id', $req['vaccine_id'])->first()->getOriginal();
  258. $addVaccine['order_id'] = $order['id'];
  259. $addVaccine['order_patient_id'] = $orderPatient['id'];
  260. OrderVaccine::create($addVaccine);
  261. }
  262. //保存儿保订单信息
  263. if ($req['product_type'] == 5) {
  264. $nurse_ids = json_decode($req['nurse_ids'], true);
  265. foreach ($nurse_ids as $k => $v) {
  266. $addNurse = Nurse::select(['id as nurse_id', 'price as nurse_price', 'name as nurse_name', 'remark as nurse_remark'])->where('id', $v)->first()->getOriginal();
  267. $addNurse['order_id'] = $order['id'];
  268. $addNurse['order_patient_id'] = $orderPatient['id'];
  269. OrderNurse::create($addNurse);
  270. }
  271. }
  272. //判断是微信支付
  273. if ($req['payment_type'] == 1) {
  274. //生成支付交易单
  275. if ($payment_amount > 0) {
  276. $trade_sn = build_sn($order['id'], 3, 'T');
  277. $payBody = '超级宝妈-'.config('config.product_type_map')[$product_type];
  278. Payment::create([
  279. 'user_id' => $user['id'],
  280. 'order_id' => $order['id'],
  281. 'trade_sn' => $trade_sn,
  282. 'amount' => $payment_amount,
  283. 'remark' => $payBody,
  284. ]);
  285. //请求支付
  286. $payment = Factory::payment(config('config.wechat_pay'));
  287. $result = $payment->order->unify([
  288. 'body' => $payBody,
  289. 'out_trade_no' => $trade_sn,
  290. 'total_fee' => $payment_amount,
  291. 'trade_type' => 'JSAPI',
  292. 'openid' => $user['openid'],
  293. ]);
  294. if (empty($result['prepay_id'])) {
  295. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  296. return out(null, 702, $errorMsg);
  297. }
  298. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  299. }
  300. }
  301. //判断是余额支付
  302. elseif ($req['payment_type'] == 2) {
  303. if ($payment_amount > 0) {
  304. //改变用户余额
  305. $change_amount = 0 - $payment_amount;
  306. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '预约订单消费');
  307. }
  308. }
  309. if ($payment_amount == 0 || in_array($req['payment_type'], [2, 3])) {
  310. Order::payCompletedHandle($order['id']);
  311. }
  312. DB::commit();
  313. } catch (\Exception $e) {
  314. DB::rollBack();
  315. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  316. }
  317. return out($config);
  318. }
  319. public function packPlaceOrder()
  320. {
  321. $req = request()->post();
  322. $this->validate(request(), [
  323. 'payment_type' => 'required|in:1,2',
  324. 'patient_id' => 'required|integer',
  325. 'total_amount' => 'required|integer',
  326. 'user_coupon_id' => 'integer',
  327. 'service_pack_id' => 'required|integer',
  328. 'is_security' => 'required|in:0,1',
  329. 'guardian_name' => 'required|max:50',
  330. 'relationship_type' => 'required|integer',
  331. 'pay_password|支付密码' => 'integer',
  332. ]);
  333. $user = $this->user;
  334. if (!empty($req['pay_password'])) {
  335. if (empty($user['pay_password'])) {
  336. return out(null, 60010, '未设置支付密码');
  337. }
  338. if (sha1(md5($req['pay_password'])) !== $user['pay_password']) {
  339. return out(null, 10001, '密码错误');
  340. }
  341. }
  342. $discount_amount = 0;
  343. if (!empty($req['user_coupon_id'])) {
  344. //计算优惠金额
  345. $discount_amount = UserCoupon::getDiscountAmount($req['user_coupon_id'], $user['id'], $req['total_amount'], 6);
  346. }
  347. $addPatient = Patient::select(['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'info', 'card_type', 'card_number', 'card_img_url', 'card_back_img_url', 'email', 'phone as patient_phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'])->where('id', $req['patient_id'])->first()->getOriginal();
  348. if (empty($addPatient['card_img_url'])) {
  349. return out(null, 70011, '该患者未上传身份证');
  350. }
  351. $payment_amount = $req['total_amount'] - $discount_amount;
  352. $payment_amount = $payment_amount < 0 ? 0 : $payment_amount;
  353. if ($req['payment_type'] == 2 && $payment_amount > 0 && empty($req['pay_password'])) {
  354. return out(null, 10011, '请输入支付密码');
  355. }
  356. if ($req['payment_type'] == 2) {
  357. if ($user['balance'] < $payment_amount) {
  358. return out(null, 601, '余额不足');
  359. }
  360. }
  361. $order_status = $payment_status = 1;
  362. $payment_time = 0;
  363. if ($payment_amount == 0 || $req['payment_type'] == 2) {
  364. $order_status = 3;
  365. $payment_status = 2;
  366. $payment_time = time();
  367. }
  368. $config = null;
  369. DB::beginTransaction();
  370. try {
  371. //保存订单数据
  372. $order = Order::create([
  373. 'user_id' => $user['id'],
  374. 'patient_id' => $req['patient_id'],
  375. 'payment_type' => $req['payment_type'],
  376. 'product_type' => 6,
  377. 'total_amount' => $req['total_amount'],
  378. 'payment_amount' => $payment_amount,
  379. 'discount_amount' => $discount_amount,
  380. 'order_status' => $order_status,
  381. 'payment_status' => $payment_status,
  382. 'payment_time' => $payment_time,
  383. ]);
  384. $order_sn = build_sn($order['id']);
  385. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  386. //保存订单患者信息
  387. $addPatient['order_id'] = $order['id'];
  388. $addPatient['patient_id'] = $req['patient_id'];
  389. OrderPatient::create($addPatient);
  390. //保存订单服务包表
  391. $addPack = ServicePack::select(['id as service_pack_id', 'name as pack_name', 'intro as pack_intro', 'price as pack_price', 'insurance_policy', 'insurance_img_url', 'team_id', 'phone_minutes', 'chat_num', 'appoint_num', 'vaccine_limit_amount', 'nurses_limit_amount', 'effective_days', 'label'])->where('id', $req['service_pack_id'])->first()->getOriginal();
  392. $addPack['user_id'] = $user['id'];
  393. $addPack['order_id'] = $order['id'];
  394. $addPack['is_security'] = $req['is_security'];
  395. $addPack['guardian_name'] = $req['guardian_name'];
  396. $addPack['relationship_type'] = $req['relationship_type'];
  397. $addPack['start_time'] = time();
  398. $addPack['end_time'] = time() + $addPack['effective_days']*24*3600;
  399. $addPack['total_phone_minutes'] = $addPack['phone_minutes'];
  400. $addPack['total_chat_num'] = $addPack['chat_num'];
  401. $addPack['total_appoint_num'] = $addPack['appoint_num'];
  402. $addPack['total_vaccine_limit_amount'] = $addPack['vaccine_limit_amount'];
  403. $addPack['total_nurses_limit_amount'] = $addPack['nurses_limit_amount'];
  404. OrderPack::create($addPack);
  405. //判断是微信支付
  406. if ($req['payment_type'] == 1) {
  407. //生成支付交易单
  408. if ($payment_amount > 0) {
  409. $trade_sn = build_sn($order['id'], 3, 'T');
  410. $payBody = '超级宝妈-'.config('config.product_type_map')[6];
  411. Payment::create([
  412. 'user_id' => $user['id'],
  413. 'order_id' => $order['id'],
  414. 'trade_sn' => $trade_sn,
  415. 'amount' => $payment_amount,
  416. 'remark' => $payBody,
  417. ]);
  418. //请求支付
  419. $payment = Factory::payment(config('config.wechat_pay'));
  420. $result = $payment->order->unify([
  421. 'body' => $payBody,
  422. 'out_trade_no' => $trade_sn,
  423. 'total_fee' => $payment_amount,
  424. 'trade_type' => 'JSAPI',
  425. 'openid' => $user['openid'],
  426. ]);
  427. if (empty($result['prepay_id'])) {
  428. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  429. return out(null, 702, $errorMsg);
  430. }
  431. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  432. }
  433. }
  434. //判断是余额支付
  435. elseif ($req['payment_type'] == 2) {
  436. if ($payment_amount > 0) {
  437. //改变用户余额
  438. $change_amount = 0 - $payment_amount;
  439. User::changeBalance($user['id'], $change_amount, 1, $order['id'], '购买服务包');
  440. Order::payCompletedHandle($order['id']);
  441. }
  442. }
  443. DB::commit();
  444. } catch (\Exception $e) {
  445. DB::rollBack();
  446. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  447. }
  448. return out($config);
  449. }
  450. public function orderList()
  451. {
  452. $req = request()->post();
  453. $this->validate(request(), [
  454. 'list_type' => 'required|in:0,1,2',
  455. 'product_type' => 'integer',
  456. 'order_status' => 'integer',
  457. 'time_sort' => 'in:0,1',
  458. 'is_pack_expire' => 'in:0,1,2',
  459. ]);
  460. $user = $this->user;
  461. $builder = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter', 'suggest'])->where('user_id', $user['id']);
  462. if (!empty($req['product_type'])) {
  463. $builder->where('product_type', $req['product_type']);
  464. }
  465. else {
  466. if (!empty($req['list_type'])) {
  467. if ($req['list_type'] == 1) {
  468. $builder->whereIn('product_type', [1,2]);
  469. }
  470. elseif ($req['list_type'] == 2) {
  471. $builder->whereIn('product_type', [3,4,5]);
  472. }
  473. }
  474. else {
  475. $builder->where('product_type', '<', 7);
  476. }
  477. }
  478. if (!empty($req['order_status'])) {
  479. $builder->where('order_status', $req['order_status']);
  480. }
  481. if (!empty($req['is_pack_expire'])) {
  482. $tmpBuilder = OrderPack::join('orders as o', 'o.id', '=', 'order_packs.order_id')->where('o.user_id', $user['id']);
  483. if ($req['is_pack_expire'] == 1) {
  484. $tmpBuilder->where('order_packs.end_time', '<', time());
  485. }
  486. else {
  487. $tmpBuilder->where('order_packs.end_time', '>=', time());
  488. }
  489. $order_ids = $tmpBuilder->pluck('o.id')->toArray();
  490. $builder->whereIn('id', $order_ids);
  491. }
  492. if (!empty($req['time_sort'])) {
  493. $builder->orderBy('id', 'desc');
  494. }
  495. else {
  496. $builder->orderBy('id', 'asc');
  497. }
  498. $data = $builder->paginate();
  499. return out($data);
  500. }
  501. public function orderDetail()
  502. {
  503. $req = request()->post();
  504. $this->validate(request(), [
  505. 'order_id' => 'required|integer'
  506. ]);
  507. $user = $this->user;
  508. $data = Order::with(['docter.office', 'docter.qualification', 'orderPatient', 'orderPack', 'orderNurse', 'orderVaccine', 'organization.docter', 'suggest'])->where('id', $req['order_id'])->where('user_id', $user['id'])->first()->toArray();
  509. if (!empty($data['order_pack'])) {
  510. $data['order_pack']['team'] = [];
  511. if (!empty($data['order_pack']['team_id'])) {
  512. $data['order_pack']['team'] = Team::with(['docter.office', 'docter.qualification'])->whereIn('id', $data['order_pack']['team_id'])->get()->toArray();
  513. }
  514. }
  515. return out($data);
  516. }
  517. public function topup()
  518. {
  519. $req = request()->post();
  520. $this->validate(request(), [
  521. 'amount' => 'required|integer',
  522. ]);
  523. $user = $this->user;
  524. DB::beginTransaction();
  525. try {
  526. //保存订单数据
  527. $order = Order::create([
  528. 'user_id' => $user['id'],
  529. 'product_type' => 7,
  530. 'total_amount' => $req['amount'],
  531. 'payment_amount' => $req['amount'],
  532. ]);
  533. $order_sn = build_sn($order['id']);
  534. Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
  535. //生成支付交易单
  536. $trade_sn = build_sn($order['id'], 3, 'T');
  537. $payBody = '超级宝妈-'.config('config.product_type_map')[7];
  538. Payment::create([
  539. 'user_id' => $user['id'],
  540. 'order_id' => $order['id'],
  541. 'trade_sn' => $trade_sn,
  542. 'amount' => $req['amount'],
  543. 'remark' => $payBody,
  544. ]);
  545. //请求支付
  546. $payment = Factory::payment(config('config.wechat_pay'));
  547. $result = $payment->order->unify([
  548. 'body' => $payBody,
  549. 'out_trade_no' => $trade_sn,
  550. 'total_fee' => $req['amount'],
  551. 'trade_type' => 'JSAPI',
  552. 'openid' => $user['openid'],
  553. ]);
  554. if (empty($result['prepay_id'])) {
  555. $errorMsg = !empty($result['err_code_des']) ? $result['err_code_des'] : $result['return_msg'];
  556. return out(null, 702, $errorMsg);
  557. }
  558. $config = $payment->jssdk->bridgeConfig($result['prepay_id'], false);
  559. DB::commit();
  560. } catch (\Exception $e) {
  561. DB::rollBack();
  562. return out(null, 500, '下单失败,请稍后重试', $e->getMessage());
  563. }
  564. return out($config);
  565. }
  566. }