12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Models\ChatList;
- 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
- {
- /**
- * 添加好友
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|void
- * @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("对应用户不存在");
- }
- //if(!ChatList::query()->where(['user_id'=>$user->id,'to_user_id'=>$toUser->id])->first()){
- $tencent_account =new TencentImAccountService();
- $tencent_friend =new TencentImFriendService();
- //检测是否已经导入IM账号
- $check_result = $tencent_account->accountCheck([$request->tencent_im_user_id]);
- if(!isset($check_result) || (isset($check_result['AccountStatus'])&&$check_result['AccountStatus']=="NotImported")){
- throw new Exception("对方账户错误");
- }
- $res = $tencent_friend->friendAddItem($user->tencent_im_user_id,$request->tencent_im_user_id);
- ChatList::query()->firstOrCreate([
- 'user_id'=>$user->id,
- 'to_user_id'=>$toUser->id,
- 'atime'=>date('Y-m-d H:i:s'),
- ]);
- ChatList::query()->firstOrCreate([
- 'user_id'=>$toUser->id,
- 'to_user_id'=>$user->id,
- 'atime'=>date('Y-m-d H:i:s'),
- ]);
- // }else{
- // $res = [
- // "ErrorCode"=>0,
- // "ActionStatus"=>"OK",
- // "ErrorInfo"=>""
- // ];
- // }
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return $this->response->errorForbidden($exception->getMessage());
- }
- return response()->json($res);
- }
- /**
- * 获取好友关系
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|void
- */
- public function get_friend(Request $request){
- try {
- $user = auth('api')->user();
- $tencent_friend =new TencentImFriendService();
- $res = $tencent_friend->friendGet($user->tencent_im_user_id,($request->page-1)*20);
- }catch (\Exception $exception){
- return $this->response->errorForbidden($exception->getMessage());
- }
- return response()->json($res);
- }
- }
|