zilong 4 年 前
コミット
0a2e52dff2

+ 30 - 0
app/Helpers/functions.php

xqd xqd
@@ -6,6 +6,7 @@
  * Time: 上午10:56
  */
 
+use EasyWeChat\Factory;
 use Illuminate\Support\Facades\Log;
 use App\Exceptions\ExitOutException;
 use App\Models\SystemConfig;
@@ -529,3 +530,32 @@ if (!function_exists('numDays')){
         return $Days;
     }
 }
+
+//发送微信订阅消息
+if (!function_exists('send_wechat_message')) {
+    function send_wechat_message($user_id, $type)
+    {
+        try {
+            $app = Factory::miniProgram(config('config.wechat_small_program'));
+            $data = [
+                'template_id' => 'bDmywsp2oEHjwAadTGKkUJ-eJEiMiOf7H-dZ7wjdw80', // 所需下发的订阅模板id
+                'touser' => 'oSyZp5OBNPBRhG-7BVgWxbiNZm',     // 接收者(用户)的 openid
+                'page' => '',       // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
+                'data' => [         // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
+                    'date01' => [
+                        'value' => '2019-12-01',
+                    ],
+                    'number01' => [
+                        'value' => 10,
+                    ],
+                ],
+            ];
+
+            $ret = $app->subscribe_message->send($data);
+        } catch (Exception $e) {
+            trace(['发送微信订阅消息失败' => $e->getMessage(), '请求参数' => $data ?? '', '返回数据' => $ret ?? ''], 'error');
+        }
+
+        return $ret ?? false;
+    }
+}

+ 17 - 12
app/Http/Controllers/Api/V1/OrderController.php

xqd xqd
@@ -693,7 +693,7 @@ class OrderController extends AuthController
         if ($order['order_status'] == 4) {
             return out(null, 10001, '订单已完成,不能取消了');
         }
-        if ($order['order_status'] == 5) {
+        if ($order['order_status'] == 5 || $order['order_status'] == 6) {
             return out(null, 10002, '订单已取消了,请勿重复操作');
         }
 
@@ -701,20 +701,25 @@ class OrderController extends AuthController
             if ($order['order_patient']['appoint_start_time'] - 1800 < time()) {
                 return out(null, 10003, '预约时间临近,不能取消订单了');
             }
-
-            DB::beginTransaction();
-            try {
-                Order::orderCancel($req['order_id']);
-
-                DB::commit();
-            } catch (Exception $e) {
-                DB::rollBack();
-
-                return out(null, 500, '取消订单失败,请稍后重试', $e->getMessage());
+        }
+        elseif (in_array($order['product_type'], [1, 2])) {
+            if ($order['order_status'] == 3) {
+                return out(null, 10004, '医生已接单,不能再取消订单了');
             }
         }
         else {
-            return out(null, 10001, '暂时只支持取消门诊预约的订单');
+            return out(null, 10001, '改订单类型不支持取消订单');
+        }
+
+        DB::beginTransaction();
+        try {
+            Order::orderCancel($req['order_id'], '用户取消订单');
+
+            DB::commit();
+        } catch (Exception $e) {
+            DB::rollBack();
+
+            return out(null, 500, '取消订单失败,请稍后重试', $e->getMessage());
         }
 
         return out();

+ 14 - 6
app/Models/Order.php

xqd
@@ -167,22 +167,30 @@ class Order extends BaseModel
     }
 
     //取消订单,建议是在事务里面去调用
-    public static function orderCancel($order_id)
+    public static function orderCancel($order_id, $order_notes = '')
     {
         $order = Order::with(['orderPatient'])->where('id', $order_id)->first();
         //改变订单状态
-        Order::where('id', $order_id)->update(['order_status' => 5]);
-        //退钱到余额
-        if (!empty($order['payment_amount'])) {
-            User::changeBalance($order_id, $order['payment_amount'], 4, $order['id'], '取消订单退款');
+        $updateOrder = ['order_status' => 5, 'order_notes' => $order_notes];
+        if ($order['payment_status'] > 1) {
+            //退钱到余额
+            if (!empty($order['payment_amount'])) {
+                User::changeBalance($order_id, $order['payment_amount'], 4, $order['id'], '取消订单退款');
+            }
+            $updateOrder['refund_time'] = time();
+            $updateOrder['payment_status'] = 4;
         }
+        Order::where('id', $order_id)->update($updateOrder);
+
         //如果距离预约时间还有半个小时以上,那么预约时间段的订单数减1
         if ($order['order_patient']['appoint_start_time'] - 1800 > time()) {
             $schedule_date = date('Y-m-d', $order['order_patient']['appoint_start_time']);
             SchedulePeriod::where('docter_id', $order['docter_id'])->where('organization_id', $order['organization_id'])->where('time_period_id', $order['order_patient']['time_period_id'])->where('schedule_date', $schedule_date)->decrement('order_num');
         }
         //更新医生的服务人数
-        Docter::where('id', $order['docter_id'])->decrement('service_persons');
+        if ($order['payment_status'] > 1) {
+            Docter::where('id', $order['docter_id'])->decrement('service_persons');
+        }
 
         return true;
     }

+ 18 - 7
config/config.php

xqd xqd
@@ -34,13 +34,7 @@ return [
         'key' =>  env('WECHAT_KEY','c1891122765718911227657189112276'),
         'notify_url' => env('API_HOST', '').'/api/v1/payCallback/wechatPayNotify'
     ],
-    'aly_sms' => [
-        'access_key' => 'LTAI4FgJdnfwsj5Bb6ioWSD9',
-        'access_secret' => 'S4Tp2eNVKHROuNOLjTvAICTnjqXFFR',
-        'sign_name' => '思维定制',
-        'verify_template_code' => 'SMS_185242509',
-        'sms_verify_code_expire' => 360,
-    ],
+
     'axb'   =>[
         'appid' => 'LTAI4GGMyBKstgVLLckoPxtf',
         'appscret' => 'goLufn8Qe0Mfip5PBR8R0DfbYYYkDF',
@@ -78,4 +72,21 @@ return [
     'no_auth_function_map' => ['docter/docterList', 'docter/docterDetail'],
 
     'pack_label_map' => [1 => '图文', 2 => '电话', 3 => '门诊', 4 => '计免', 5 => '儿保'],
+
+    'wechat_subscribe_message_template' => [
+        // 用户下单提醒(图文咨询/电话咨询)
+        1 => [
+            'template_id' => 'bDmywsp2oEHjwAadTGKkUJ-eJEiMiOf7H-dZ7wjdw80', // 所需下发的订阅模板id
+            'touser' => 'oSyZp5OBNPBRhG-7BVgWxbiNZm',     // 接收者(用户)的 openid
+            'page' => '',       // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
+            'data' => [         // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
+                'date01' => [
+                    'value' => '2019-12-01',
+                ],
+                'number01' => [
+                    'value' => 10,
+                ],
+            ],
+        ],
+    ]
 ];

+ 4 - 0
upgrade.md

xqd
@@ -0,0 +1,4 @@
+###重构订单状态逻辑的更新
+- 执行以下sql:
+    ALTER TABLE `bm_orders` 
+    ADD COLUMN `refund_time` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '退款时间' AFTER `receiving_time`;