| xqd
@@ -4,9 +4,11 @@
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
|
+use App\Models\User;
|
|
|
use App\Services\TencentImAccountService;
|
|
|
use App\Services\TencentImFriendService;
|
|
|
use Illuminate\Http\Request;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
use PHPUnit\Util\Exception;
|
|
|
|
|
|
class ChatController extends Controller
|
| xqd
@@ -19,11 +21,16 @@ class ChatController extends Controller
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
*/
|
|
|
public function add_friend(Request $request){
|
|
|
+ DB::beginTransaction();
|
|
|
try {
|
|
|
$user = auth('api')->user();
|
|
|
if(empty($request->tencent_im_user_id)){
|
|
|
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_friend =new TencentImFriendService();
|
|
|
//检测是否已经导入IM账号
|
| xqd
@@ -32,7 +39,14 @@ class ChatController extends Controller
|
|
|
throw new Exception("对方账户错误");
|
|
|
}
|
|
|
$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){
|
|
|
+ DB::rollBack();
|
|
|
return $this->response->errorForbidden($exception->getMessage());
|
|
|
}
|
|
|
return response()->json($res);
|