zilong 4 vuotta sitten
vanhempi
commit
0ce4dece3d

+ 1 - 1
app/Http/Controllers/Api/V1/DocterController.php

xqd
@@ -13,7 +13,7 @@ use App\Models\Organization;
 use App\Models\Schedule;
 use App\Models\SchedulePeriod;
 use App\Models\TimePeriod;
-use Illuminate\Support\Facades\DB;
+use DB;
 
 class DocterController extends AuthController
 {

+ 11 - 0
app/Http/Controllers/Api/V1/OrderController.php

xqd xqd
@@ -55,6 +55,7 @@ class OrderController extends AuthController
                 'total_amount' => $req['total_amount'],
                 'payment_amount' => $payment_amount,
                 'discount_amount' => $discount_amount,
+                'patient_id' => $req['patient_id'],
             ]);
             $order_sn = build_sn($order['id']);
             Order::where('id', $order['id'])->update(['order_sn' => $order_sn]);
@@ -113,4 +114,14 @@ class OrderController extends AuthController
 
         return out($config);
     }
+
+    public function orderList()
+    {
+        $req = request()->post();
+        $this->validate(request(), [
+            'product_type' => 'require|integer',
+            'order_status' => 'require|integer',
+            ''
+        ]);
+    }
 }

+ 60 - 0
app/Http/Controllers/Api/V1/PatientController.php

xqd
@@ -0,0 +1,60 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zilongs
+ * Date: 20-10-2
+ * Time: 下午8:46
+ */
+
+namespace App\Http\Controllers\Api\v1;
+
+use App\Models\Order;
+use App\Models\Patient;
+
+class PatientController extends AuthController
+{
+    public function createPatient()
+    {
+        $req = request()->post();
+        $this->validate(request(), [
+            'name' => 'required|max:50',
+            'sex' => 'required|in:1,2',
+            'avatar' => 'required|url',
+            'birthday' => 'required|date',
+            'relationship_type' => 'required|integer',
+            'info' => 'max:1000',
+            'card_type' => 'required|in:1,2',
+            'card_number' => 'required|max:50',
+        ]);
+        $user = $this->user;
+
+        $req['user_id'] = $user['id'];
+        Patient::create($req);
+
+        return out();
+    }
+
+    public function patientList()
+    {
+        $user = $this->user;
+        $data = Patient::where('user_id', $user['id'])->orderBy('id', 'desc')->paginate();
+        return out($data);
+    }
+
+    public function patientDetail()
+    {
+        $req = request()->post();
+        $this->validate(request(), [
+            'patient_id' => 'required|integer',
+        ]);
+        $user = $this->user;
+
+        $data = [];
+        $data['patient'] = Patient::where('user_id', $user['id'])->where('patient_id', $req['patient_id'])->first();
+
+        $data['orders']['cases'] = Order::with(['docter' => ['office', 'qualification']])->where('user_id', $user['id'])->where('patient_id', $req['patient_id'])->whereIn('order_status', [2,3])->where('product_type', '<', 6)->orderBy('id', 'desc')->get();
+        $data['orders']['service_packs'] = Order::with('orderPack')->where('user_id', $user['id'])->where('patient_id', $req['patient_id'])->whereIn('order_status', [2,3])->where('product_type', 6)->orderBy('id', 'desc')->get();
+
+        return out($data);
+    }
+}

+ 83 - 0
app/Http/Controllers/Api/V1/PayCallbackController.php

xqd
@@ -0,0 +1,83 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zilongs
+ * Date: 20-10-2
+ * Time: 下午8:27
+ */
+
+namespace App\Http\Controllers\Api\v1;
+
+use App\Http\Controllers\Controller;
+use App\Models\Docter;
+use App\Models\Order;
+use App\Models\Payment;
+use DB;
+use EasyWeChat\Factory;
+
+class PayCallbackController extends Controller
+{
+    public function wechatPayNotify()
+    {
+        $app = Factory::payment(config('config.wechat_pay'));
+        $response = $app->handlePaidNotify(function($message, $fail){
+            // 使用通知里的 "交易单号" 去自己的数据库找到订单
+            $payment = Payment::where('trade_sn', $message['out_trade_no'])->first();
+            // 如果订单不存在 或者 订单已经支付过了
+            if (empty($payment) || $payment['status'] != 1) {
+                return true; // 告诉微信,我已经处理完了或者订单没找到,别再通知我了
+            }
+            //使用通知里的 "微信支付订单号" 去自己的数据库找是否已经存在
+            if(Payment::where('online_sn', $message['transaction_id'])->exists()){
+                return true; // 告诉微信,我已经处理过这单了,别再通知我了
+            }
+
+            //建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付
+
+            // return_code 表示通信状态,不代表支付状态
+            if ($message['return_code'] === 'SUCCESS') {
+                // 用户是否支付成功
+                if ($message['result_code'] === 'SUCCESS') {
+                    // 启动事务
+                    DB::beginTransaction();
+                    try {
+                        //更新payment
+                        Payment::where('id', $payment['id'])->update(['online_sn' => $message['transaction_id'], 'status' => 2, 'payment_time' => time()]);
+                        //更新订单
+                        Order::where('id', $payment['order_id'])->update([
+                            'order_status' => 2,
+                            'payment_status' => 2,
+                            'payment_time' => time(),
+                        ]);
+
+                        $order = Order::select(['docter_id'])->where('id', $payment['order_id'])->first();
+                        //更新医生的服务人数
+                        Docter::where('id', $order['docter_id'])->increment('service_persons');
+
+                        // 提交事务
+                        DB::commit();
+                    } catch (\Exception $e) {
+                        // 回滚事务
+                        DB::rollback();
+                        trace(['微信支付回调错误' => $e->getMessage(), '错误数据' => $payment], 'error');
+                        return $fail('内部服务失败,请稍后再通知我');
+                    }
+                }
+                elseif ($message['result_code'] === 'FAIL') {
+                    $err_code = $message['err_code'] ?? '';
+                    $err_code_des = $message['err_code_des'] ?? '';
+                    $remark = $err_code.':'.$err_code_des;
+                    Payment::where('id', $payment['id'])->update(['remark' => $remark, 'status' => 3, 'payment_time' => time()]);
+                }
+            }
+            else {
+                trace(['微信支付回调通知数据错误,返回码return_code:' => $message['return_code'], '错误数据' => $payment], 'error');
+                return $fail('通信失败,请稍后再通知我');
+            }
+
+            return true; // 返回处理完成
+        });
+
+        return $response;
+    }
+}

+ 8 - 0
app/Models/Order.php

xqd
@@ -10,5 +10,13 @@ namespace App\Models;
 
 class Order extends BaseModel
 {
+    public function docter()
+    {
+        return $this->belongsTo(Docter::class);
+    }
 
+    public function orderPack()
+    {
+        return $this->hasMany(OrderPack::class);
+    }
 }

+ 14 - 0
app/Models/OrderPack.php

xqd
@@ -0,0 +1,14 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: zilongs
+ * Date: 20-10-2
+ * Time: 下午10:57
+ */
+
+namespace App\Models;
+
+class OrderPack extends BaseModel
+{
+
+}