TencentImFriendService.php 9.3 KB

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