TencentImFriendService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace App\Services;
  3. use App\Exceptions\TencentImFriendException;
  4. use App\Models\User;
  5. use App\Models\UserFriendApplyRecord;
  6. use App\Traits\TencentIm;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Facades\DB;
  9. class TencentImFriendService
  10. {
  11. use TencentIm;
  12. const ADD_FRIEND_TYPE_SINGLE = 'Add_Type_Single'; //表示单向加好友
  13. const ADD_FRIEND_TYPE_BOTH = 'Add_Type_Both'; //表示双向加好友
  14. const IM_IDENTIFIER_PREFIX = 'IM_USER_';
  15. const CHECK_FRIEND_TYPE_SINGLE = 'CheckResult_Type_Single'; //只会检查 From_Account 的好友表中是否有 To_Account,不会检查 To_Account 的好友表中是否有 From_Account
  16. const CHECK_FRIEND_TYPE_BOTH = 'CheckResult_Type_Both'; //既会检查 From_Account 的好友表中是否有 To_Account,也会检查 To_Account 的好友表中是否有 From_Account
  17. const ADD_FRIEND_ADD_SOURCE_TYPE_ADDRESS_BOOK = 'AddSource_Type_100'; //加好友来源 通讯录
  18. const TENCENT_REST_APIS = [
  19. 'friendAddItem' => 'v4/sns/friend_add', //单个添加好友,
  20. 'friendGet' => 'v4/sns/friend_get', //拉取好友,
  21. 'friendCheck' => 'v4/sns/friend_check', //校验好友
  22. 'del_friend' => 'v4/sns/friend_delete', //删除好友
  23. 'black_add' => 'v4/sns/black_list_add', //拉黑好友
  24. 'black_del' => 'v4/sns/black_list_delete', //删除拉黑好友
  25. ];
  26. static public function verifyUserApplyFriendExists(User $fromUser, User $toUser)
  27. {
  28. }
  29. //添加好友记录
  30. public function addApplyFriendRecord(User $user, array $options = [])
  31. {
  32. try {
  33. DB::beginTransaction();
  34. $toUser = User::find($options['to_user_id']);
  35. if (!$toUser) {
  36. throw new TencentImFriendException('好友账号不存在');
  37. }
  38. //检测是否已经有记录
  39. $friendApplyRecordModel = UserFriendApplyRecord::query()
  40. ->where(['from_user_id' => $user->id, 'to_user_id' => $toUser->id])
  41. ->latest()
  42. ->first();
  43. if ($friendApplyRecordModel) {
  44. if (UserFriendApplyRecord::APPLY_STATUS_100 === $friendApplyRecordModel->apply_status) {
  45. throw new TencentImFriendException('已发出好友添加申请,请勿重复操作');
  46. }
  47. if (UserFriendApplyRecord::APPLY_STATUS_101 === $friendApplyRecordModel->apply_status) {
  48. throw new TencentImFriendException('已经是好友关系,请勿重复操作');
  49. }
  50. if (UserFriendApplyRecord::APPLY_STATUS_101 === $friendApplyRecordModel->apply_status) {
  51. throw new TencentImFriendException('已经是好友关系,请勿重复操作');
  52. }
  53. }
  54. if (!$friendApplyRecordModel) {
  55. $friendApplyRecordModel = new UserFriendApplyRecord();
  56. }
  57. //添加发起申请记录
  58. $applyData = [
  59. 'from_user_id' => $user->id,
  60. 'to_user_id' => $toUser->id,
  61. 'type' => UserFriendApplyRecord::APPLY_TYPE_100,
  62. 'apply_status' => UserFriendApplyRecord::APPLY_STATUS_102,
  63. 'apply_raw_data' => [
  64. 'remark' => $options['remark'] ?? '',
  65. 'group' => $options['group_id'] ?? '',
  66. ],
  67. ];
  68. $friendApplyRecordModel->fill($applyData);
  69. //添加接收
  70. if (!$friendApplyRecordModel->save()) {
  71. throw new \Exception('写入好友申请记录失败');
  72. }
  73. DB::commit();
  74. return $friendApplyRecordModel;
  75. } catch (\Exception $e) {
  76. DB::rollBack();
  77. return ['error' => $e->getMessage()];
  78. } catch (TencentImFriendException $e) {
  79. DB::rollBack();
  80. return ['error' => $e->getMessage()];
  81. }
  82. }
  83. /**
  84. * 发起添加 IM 好友 (单个用户)
  85. * @param string $fromAccount
  86. * @param string $toAccount
  87. * @param array $options
  88. * @return \Psr\Http\Message\ResponseInterface
  89. * @throws TencentImFriendException
  90. * @throws \App\Exceptions\TencentImAccountException
  91. * @throws \App\Exceptions\TencentImException
  92. * @throws \GuzzleHttp\Exception\GuzzleException
  93. */
  94. public function friendAddItem(string $fromAccount, string $toAccount, array $options = [])
  95. {
  96. //TODO 请求添加好友前先拉取好友状态
  97. $this->restApiName = self::TENCENT_REST_APIS['friendAddItem'];
  98. $baseApiHost = $this->getTencentImRestApiBaseHost();
  99. $params = self::prepareFriendAddItemOptions($fromAccount, $toAccount, $options);
  100. $apiResult = $this->requestApi($baseApiHost, $params);
  101. self::verifyApiResult($apiResult);
  102. if ($apiResult['ResultItem'][0]['ResultInfo'] || ($apiResult['ResultItem'][0]['ResultCode'] != 0)) {
  103. throw new TencentImFriendException(trans('api.ADD_FRIEND_ERROR').': ' . $apiResult['ResultItem'][0]['ResultInfo']);
  104. }
  105. return $apiResult;
  106. }
  107. //拉取好友
  108. public function friendGet(string $fromAccount, int $pageStartIndex = 0)
  109. {
  110. $this->restApiName = self::TENCENT_REST_APIS['friendGet'];
  111. $baseApiHost = $this->getTencentImRestApiBaseHost();
  112. $params = [
  113. 'From_Account' => $fromAccount,
  114. 'StartIndex' => $pageStartIndex,
  115. 'StandardSequence' => $StandardSequence ?? 0,
  116. 'CustomSequence' => $CustomSequence ?? 0
  117. ];
  118. $apiResult = $this->requestApi($baseApiHost, $params);
  119. dd($apiResult);
  120. }
  121. //检验好友
  122. public function friendCheck(string $fromAccount, array $toAccounts, string $checkType = self::ADD_FRIEND_TYPE_BOTH)
  123. {
  124. $this->restApiName = self::TENCENT_REST_APIS['friendCheck'];
  125. $baseApiHost = $this->getTencentImRestApiBaseHost();
  126. $params = [
  127. 'From_Account' => $fromAccount,
  128. 'To_Account' => $toAccounts,
  129. 'CheckType' => 'CheckResult_Type_Both'
  130. ];
  131. $apiResult = $this->requestApi($baseApiHost, $params);
  132. self::verifyApiResult($apiResult);
  133. return $apiResult;
  134. }
  135. /**
  136. * 构建添加好友api 请求数组
  137. * @param string $fromAccount 需要添加好友的用户IM id
  138. * @param string $toAccount 被申请添加好友的用户IM id
  139. * @param array $options
  140. * @param string $options ['AddType'] 加好友方式
  141. * @param string $options ['AddSource'] 好友来源
  142. * @param string $options ['ForceAddFlags'] 管理员强制加好友标记:1表示强制加好友,0表示常规加好友方式
  143. * @param string $options ['Remark'] 好友备注
  144. * @param string $options ['AddWording'] 形成好友关系时的附言信息
  145. * @param string $options ['GroupName'] 分组信息,添加好友时只允许设置一个分组
  146. * @return array
  147. */
  148. static public function prepareFriendAddItemOptions(string $fromAccount, string $toAccount, array $options)
  149. {
  150. // string $addType = self::ADD_FRIEND_TYPE_BOTH,
  151. // string $addSource = self::ADD_FRIEND_ADD_SOURCE_TYPE_ADDRESS_BOOK,
  152. // string $remark = '',
  153. // bool $forceAddFlags = false
  154. $result = [
  155. 'From_Account' => $fromAccount,
  156. // 'AddSource' => $options['AddSource'] ?? self::ADD_FRIEND_ADD_SOURCE_TYPE_ADDRESS_BOOK, //好友来源
  157. 'AddType' => $options['AddType'] ?? self::ADD_FRIEND_TYPE_BOTH, //加好友方式
  158. 'ForceAddFlags' => empty($options['ForceAddFlags']) ? 0 : ($options['ForceAddFlags'] ? 1 : 0), //管理员强制加好友标记:1表示强制加好友,0表示常规加好友方式
  159. ];
  160. $AddFriendItem = [];
  161. $AddFriendItem['To_Account'] = $toAccount;
  162. //好友来源
  163. $AddFriendItem['AddSource'] = $options['AddSource'] ?? self::ADD_FRIEND_ADD_SOURCE_TYPE_ADDRESS_BOOK;
  164. //好友备注
  165. if (!empty($options['Remark'])) {
  166. $AddFriendItem['Remark'] = $options['Remark'];
  167. }
  168. //形成好友关系时的附言信息
  169. if (!empty($options['AddWording'])) {
  170. $AddFriendItem['AddWording'] = $options['AddWording'];
  171. }
  172. //分组信息,添加好友时只允许设置一个分组
  173. if (!empty($options['GroupName'])) {
  174. $AddFriendItem['GroupName'] = $options['GroupName'] ? 1 : 0;
  175. }
  176. $result['AddFriendItem'][] = $AddFriendItem;
  177. unset($options);
  178. unset($AddFriendItem);
  179. return $result;
  180. }
  181. //拉黑
  182. public function friend_black_add(string $fromAccount, string $toAccount)
  183. {
  184. $this->restApiName = self::TENCENT_REST_APIS['black_add'];
  185. $baseApiHost = $this->getTencentImRestApiBaseHost();
  186. $params = [
  187. 'From_Account' => $fromAccount,
  188. 'To_Account' => array($toAccount),
  189. ];
  190. $apiResult = $this->requestApi($baseApiHost, $params);
  191. return $apiResult;
  192. }
  193. //取消拉黑
  194. public function friend_black_del(string $fromAccount, string $toAccount)
  195. {
  196. $this->restApiName = self::TENCENT_REST_APIS['black_del'];
  197. $baseApiHost = $this->getTencentImRestApiBaseHost();
  198. $params = [
  199. 'From_Account' => $fromAccount,
  200. 'To_Account' => array($toAccount),
  201. ];
  202. $apiResult = $this->requestApi($baseApiHost, $params);
  203. return $apiResult;
  204. }
  205. //删除好友
  206. public function del_friend(string $fromAccount, string $toAccount)
  207. {
  208. $this->restApiName = self::TENCENT_REST_APIS['del_friend'];
  209. $baseApiHost = $this->getTencentImRestApiBaseHost();
  210. $params = [
  211. 'From_Account' => $fromAccount,
  212. 'To_Account' => array($toAccount),
  213. ];
  214. $apiResult = $this->requestApi($baseApiHost, $params);
  215. return $apiResult;
  216. }
  217. }