OrderController.php 29 KB

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