xiaogang 4 سال پیش
والد
کامیت
7b30b9c991
1فایلهای تغییر یافته به همراه14 افزوده شده و 0 حذف شده
  1. 14 0
      app/Http/Controllers/Api/ChatController.php

+ 14 - 0
app/Http/Controllers/Api/ChatController.php

@@ -4,9 +4,11 @@
 namespace App\Http\Controllers\Api;
 namespace App\Http\Controllers\Api;
 
 
 
 
+use App\Models\User;
 use App\Services\TencentImAccountService;
 use App\Services\TencentImAccountService;
 use App\Services\TencentImFriendService;
 use App\Services\TencentImFriendService;
 use Illuminate\Http\Request;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
 use PHPUnit\Util\Exception;
 use PHPUnit\Util\Exception;
 
 
 class ChatController extends Controller
 class ChatController extends Controller
@@ -19,11 +21,16 @@ class ChatController extends Controller
      * @throws \GuzzleHttp\Exception\GuzzleException
      * @throws \GuzzleHttp\Exception\GuzzleException
      */
      */
     public function add_friend(Request $request){
     public function add_friend(Request $request){
+        DB::beginTransaction();
         try {
         try {
             $user = auth('api')->user();
             $user = auth('api')->user();
             if(empty($request->tencent_im_user_id)){
             if(empty($request->tencent_im_user_id)){
                 throw new Exception("参数错误");
                 throw new Exception("参数错误");
             }
             }
+            if(!$toUser = User::query()->where(['tencent_im_user_id'=>$request->tencent_im_user_id])->first()){
+                throw new Exception("对应用户不存在");
+            }
+
             $tencent_account =new TencentImAccountService();
             $tencent_account =new TencentImAccountService();
             $tencent_friend =new TencentImFriendService();
             $tencent_friend =new TencentImFriendService();
             //检测是否已经导入IM账号
             //检测是否已经导入IM账号
@@ -32,7 +39,14 @@ class ChatController extends Controller
                 throw new Exception("对方账户错误");
                 throw new Exception("对方账户错误");
             }
             }
             $res = $tencent_friend->friendAddItem($user->tencent_im_user_id,$request->tencent_im_user_id);
             $res = $tencent_friend->friendAddItem($user->tencent_im_user_id,$request->tencent_im_user_id);
+            DB::table("chat_list")->firstOrCreate([
+                'user_id'=>$user->id,
+                'to_user_id'=>$toUser->id,
+                'atime'=>date('Y-m-d H:i:s'),
+            ]);
+            DB::commit();
         }catch (\Exception $exception){
         }catch (\Exception $exception){
+            DB::rollBack();
             return $this->response->errorForbidden($exception->getMessage());
             return $this->response->errorForbidden($exception->getMessage());
         }
         }
         return response()->json($res);
         return response()->json($res);