Jelajahi Sumber

支付完订单,发送im自定义消息

lizhen_gitee 10 bulan lalu
induk
melakukan
3c2d334ca6

+ 16 - 3
application/api/controller/Demo.php

@@ -4,6 +4,7 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use app\common\library\Easemob;
+use app\common\library\Tenim;
 /**
  * 示例接口
  */
@@ -15,7 +16,7 @@ class Demo extends Api
     //如果接口已经设置无需登录,那也就无需鉴权了
     //
     // 无需登录的接口,*表示全部
-    protected $noNeedLogin = ['test', 'test1'];
+    protected $noNeedLogin = ['*'];
     // 无需鉴权的接口,*表示全部
     protected $noNeedRight = ['test2'];
 
@@ -52,7 +53,8 @@ class Demo extends Api
      */
     public function test1()
     {
-        $this->success('返回成功', ['action' => 'test1']);
+        $tenim = new Tenim();
+        $rs = $tenim->register('user'. 1, 'nickname', '');
     }
 
     /**
@@ -61,7 +63,18 @@ class Demo extends Api
      */
     public function test2()
     {
-        $this->success('返回成功', ['action' => 'test2']);
+        $tenim = new Tenim();
+
+        $message = [
+            'businessID' => 'order_status',
+            'name' => '待接单',
+            'status' => '10',
+            'id' => 16,
+            'content' => '已通知医生尽快接诊,超时自动取消订单并退款',
+        ];
+
+        $rs = $tenim->sendCustomMessageToUser('user7','doctor7',$message);
+        dump($rs);
     }
 
     /**

+ 19 - 0
application/api/controller/Notify.php

@@ -4,6 +4,7 @@ namespace app\api\controller;
 use app\common\controller\Api;
 use think\Db;
 use addons\epay\library\Service;
+use app\common\library\Tenim;
 /**
  * 订单支付回调
  */
@@ -164,6 +165,7 @@ class Notify extends Api
             'pay_out_trade_no' => $out_trade_no,
             'pay_type' => $orderInfo['pay_type'],
             'pay_time' => $nowtime,
+            'status'   => 10,
         ];
         $update_rs = Db::name('wenzhen_order')->where('id',$orderInfo['table_id'])->update($order_update);
         if($update_rs===false)
@@ -182,6 +184,23 @@ class Notify extends Api
 
         //默认提交
         Db::commit();
+
+
+        //发送im消息给医生
+        $wenzhen_order = Db::name('wenzhen_order')->where('id',$orderInfo['table_id'])->find();
+        if($wenzhen_order['order_type'] == 1){
+            $tenim = new Tenim();
+            $message = [
+                'businessID' => 'order_status',
+                'name' => '待接单',
+                'status' => '10',
+                'id' => $wenzhen_order['id'],
+                'content' => '已通知医生尽快接诊,超时自动取消订单并退款',
+            ];
+            $rs = $tenim->sendCustomMessageToUser('user'.$wenzhen_order['user_id'],'doctor'.$wenzhen_order['doctor_id'],$message);
+        }
+
+
         return true;
     }
 

+ 16 - 1
application/api/controller/Wenzhen.php

@@ -5,6 +5,7 @@ namespace app\api\controller;
 use app\common\controller\Api;
 use think\Db;
 use addons\epay\library\Service;
+use app\common\library\Tenim;
 /**
  * 问诊订单
  */
@@ -126,7 +127,7 @@ class Wenzhen extends Api
         if($pay_type == 'wallet'){
             Db::startTrans();
 
-            //扣钱
+            //用户扣钱
             $logtype = $wenzhen_order['ordertype'] == 1 ? 11 : 12;
             $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,'money',$wenzhen_order['total_fee'],$logtype,'图文问诊'.$wenzhen_order['order_no'],'wenzhen_order',$order_id);
             if($rs_wallet['status'] == false){
@@ -146,6 +147,20 @@ class Wenzhen extends Api
 
             Db::commit();
 
+            //发送im消息给医生
+            if($wenzhen_order['order_type'] == 1){
+                $tenim = new Tenim();
+                $message = [
+                    'businessID' => 'order_status',
+                    'name' => '待接单',
+                    'status' => '10',
+                    'id' => $order_id,
+                    'content' => '已通知医生尽快接诊,超时自动取消订单并退款',
+                ];
+                $rs = $tenim->sendCustomMessageToUser('user'.$this->auth->id,'doctor'.$wenzhen_order['doctor_id'],$message);
+            }
+
+            //输出
             $result = [
                 'pay_type' => $pay_type,
                 'pay_params' => '',

+ 52 - 1
application/common/library/Tenim.php

@@ -51,7 +51,58 @@ class Tenim
                 "Text"=> $message
             ],
         ];
-        $tencentObj->toSend($data);
+        $res = $tencentObj->toSend($data);
+
+        if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
+            $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
+            return $error;
+        }
+
+        return true;
+
+    }
+
+    /**
+     * 发送自定义消息给某人
+     */
+    public function sendCustomMessageToUser($from_user,$to_user,$message) {
+
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/openim/sendmsg";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+
+        $data = [];
+        $data["SyncOtherMachine"] = 1;//1=消息同步至发送方,2=消息不同步至发送方
+        $data["From_Account"] = (string)$from_user;
+        $data["To_Account"] = (string)$to_user;
+        $data["MsgRandom"] = rand(1000000,9999999);
+        $data["MsgTimeStamp"] = time();
+
+        $data["MsgBody"][] = [
+            "MsgType" => "TIMCustomElem",
+            "MsgContent" => [
+                "Data" => json_encode($message),
+                "Desc" => $message['name'],
+                "Ext"  => $message['name'],
+                "Sound"=> '',
+            ],
+        ];
+        $res = $tencentObj->toSend($data);
+
+        if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
+            $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
+            return $error;
+        }
+
+        return true;
 
     }