IM.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace App\Api\Util;
  3. use Illuminate\Support\Facades\Cache;
  4. use Qiniu\Http\Client;
  5. use Tencent\TLSSigAPIv2;
  6. trait IM
  7. {
  8. use MyUtil;
  9. private function getIM(){
  10. $data['IDBefore']=config('love.UCLOUD_CODE');
  11. if (Cache::has('MYIM')) {
  12. $im=Cache::get('MYIM');
  13. }else{
  14. $im=$this->synchronization('IM')['data'];
  15. $data['IMSdkAppID']=$im['SecretId'];
  16. Cache::put('MYIM', $im, 86400);
  17. }
  18. $data['SecretId']=$im['SecretId'];
  19. $data['SecretKey']=$im['SecretKey'];
  20. return $data;
  21. }
  22. /**
  23. * im 接口请求通用
  24. * @param $service_name
  25. * @param $cmd_name
  26. * @param $identifier
  27. * @param $usersig
  28. * @param $req_data
  29. * @param bool $print_flag
  30. * @return mixed
  31. */
  32. private function api($service_name, $cmd_name,$req_data)
  33. {
  34. $seting=$this->getIM();
  35. $parameter = http_build_query([
  36. 'usersig' => $this->getUserSig('admin'),
  37. 'identifier' => 'admin',
  38. 'sdkappid' => $seting['SecretId'],
  39. 'contenttype' => 'json',
  40. 'random' => time(),
  41. ]);
  42. $url = 'https://console.tim.qq.com/v4/' . $service_name . '/' . $cmd_name . '?' . $parameter;
  43. $ret = Client::post($url, $req_data);
  44. return $ret;
  45. }
  46. /**
  47. * 修改用户资料
  48. * @param $From_Account 用户
  49. * @param $profileArray Tag_Profile_IM_Level 表示VIP Tag_Profile_IM_Role 红娘/月老 Tag_Profile_IM_SelfSignature 王牌否
  50. */
  51. public function portrait_set($From_Account,$profileArray){
  52. $seting=$this->getIM();
  53. $msgcount['From_Account']=$seting['IDBefore'].$From_Account;
  54. if(count($profileArray,1)==2){
  55. $msgcount['ProfileItem']=[];
  56. array_push($msgcount['ProfileItem'], $profileArray);
  57. }else{
  58. $msgcount['ProfileItem']=$profileArray;
  59. }
  60. $req_data = json_encode($msgcount);
  61. $ret=$this->api('profile','portrait_set',$req_data);
  62. return json_decode($ret->body,true);
  63. }
  64. /**
  65. * 发送文本消息
  66. * @param $from
  67. * @param $to
  68. * @param $msg
  69. * @param string $MsgType TIMTextElem 文本 TIMFaceElem 表情 TIMLocationElem 位置 TIMCustomElem自定义
  70. * @param int $syncOtherMachine 1消息同步到发送端 2不同步
  71. */
  72. public function imsendmassage($from,$to,$msg,$MsgType='TIMTextElem',$syncOtherMachine=1,$Ext='',$image=''){
  73. $seting=$this->getIM();
  74. $msg_content = [];
  75. $msg_content_elem = [
  76. 'MsgType' => $MsgType, // 文本类型
  77. 'MsgContent' => [
  78. "Text"=>$msg
  79. ]
  80. ];
  81. array_push($msg_content, $msg_content_elem);
  82. $msgcount = [
  83. 'SyncOtherMachine' => $syncOtherMachine,
  84. 'MsgLifeTime' => 3600,
  85. 'To_Account' =>$seting['IDBefore'].$to,
  86. 'MsgSeq' => rand(1, 65535),
  87. 'MsgRandom' => rand(1, 65535),
  88. 'MsgTimeStamp' => time(),
  89. 'MsgBody' => $msg_content,
  90. ];
  91. if($from!='admin'){
  92. $msgcount['From_Account']=$seting['IDBefore'].$from;
  93. $msgcount['OfflinePushInfo']=[
  94. 'PushFlag'=>0,
  95. 'Desc'=>$msg,
  96. 'Ext'=>$Ext,
  97. 'AndroidInfo'=>['Sound'=>'android.mp3'],
  98. 'ApnsInfo'=>['Sound'=>'apns.mp3','BadgeMode'=>1,'Title'=>$msg,'SubTitle'=>$msg,'Image'=>!$image?:config('app.APP_URL').'/logo.png'],
  99. ];
  100. }else{
  101. $msgcount['SyncOtherMachine']=2;
  102. }
  103. $req_data = json_encode($msgcount);
  104. $ret=$this->api('openim','sendmsg',$req_data);
  105. // info(json_encode($ret->body));
  106. // info($req_data);
  107. }
  108. /**
  109. * 创建群
  110. * @param $owner_Account 群主
  111. * @param $type 类型
  112. * @param $name 名
  113. * @param $groupId 名
  114. * @param $introduction 介绍
  115. * @param $notification 公告
  116. * @param $faceUrl 图
  117. * @param $admin 管理员
  118. */
  119. public function createGroup($owner_Account,$type,$name,$groupId,$introduction,$notification,$faceUrl='',$admin=''){
  120. $seting=$this->getIM();
  121. $content = [
  122. 'Owner_Account' => $seting['IDBefore'].$owner_Account,
  123. 'Type' => $type,
  124. 'Name' =>$name,
  125. 'GroupId' =>$seting['IDBefore'].$groupId,
  126. 'Introduction' =>$introduction,
  127. 'Notification' => $notification,
  128. ];
  129. if($faceUrl){
  130. $content['FaceUrl']=$faceUrl;
  131. }
  132. if($type=='Public'){
  133. $content['ApplyJoinOption']='FreeAccess';
  134. }
  135. if($admin){
  136. $content['MemberList']= [];
  137. $member= [
  138. "Member_Account"=>$seting['IDBefore'].$admin, // 成员(必填)
  139. "Role"=>"Admin"
  140. ];
  141. array_push($content['MemberList'], $member);
  142. }
  143. $req_data = json_encode($content);
  144. $ret=$this->api('group_open_http_svc','create_group',$req_data);
  145. return json_decode($ret->body,true);
  146. }
  147. /**
  148. * getUserSig
  149. * $is_identifier 是否管理员默认不是
  150. * @return array
  151. */
  152. public function getUserSig($uid='admin', $expire=15552000){
  153. $sig = Cache::remember('UserSig'.$uid,$expire-100,function() use ($uid,$expire){
  154. $seting=$this->getIM();
  155. $api= new TLSSigAPIv2($seting['SecretId'],$seting['SecretKey']);
  156. if($uid!='admin'){
  157. $usrsign= $api->genSig($seting['IDBefore'].$uid,$expire);
  158. }else{
  159. $usrsign= $api->genSig($uid) ;
  160. }
  161. return $usrsign;
  162. });
  163. return $sig;
  164. }
  165. /**
  166. * @param $owner_Account
  167. * @return mixed
  168. */
  169. public function closeGroup($groupId,$type=0){
  170. $seting=$this->getIM();
  171. if($type){
  172. $content = [
  173. // 'GroupId' =>$seting['IDBefore'].$groupId.'_'.$type
  174. 'GroupId' =>$seting['IDBefore'].$groupId
  175. ];
  176. }else{
  177. $content = [
  178. 'GroupId' =>$seting['IDBefore'].$groupId
  179. ];
  180. }
  181. $req_data = json_encode($content);
  182. $ret=$this->api('group_open_http_svc','destroy_group',$req_data);
  183. return json_decode($ret->body,true);
  184. }
  185. /**
  186. * 获取用户好友
  187. * @param $groupId
  188. * @param $uid
  189. * @return mixed
  190. */
  191. public function friend_get($uid){//没开发完 暂时不用
  192. $seting=$this->getIM();
  193. $content = [
  194. 'GroupId' =>$seting['IDBefore'].$groupId,
  195. 'Silence'=>1,
  196. 'MemberList'=>[]
  197. ];
  198. array_push($content['MemberList'], ['Member_Account'=>$seting['IDBefore'].$uid]);
  199. $req_data = json_encode($content);
  200. $ret=$this->api('sns','friend_get',$req_data);
  201. return json_decode($ret->body,true);
  202. }
  203. /**
  204. * 增加群成员
  205. * @param $groupId
  206. * @param $uid
  207. * @return mixed
  208. */
  209. public function add_group_member($groupId,$uid){
  210. $seting=$this->getIM();
  211. $content = [
  212. 'GroupId' =>$seting['IDBefore'].$groupId,
  213. 'Silence'=>1,
  214. 'MemberList'=>[]
  215. ];
  216. array_push($content['MemberList'], ['Member_Account'=>$seting['IDBefore'].$uid]);
  217. $req_data = json_encode($content);
  218. $ret=$this->api('group_open_http_svc','add_group_member',$req_data);
  219. return json_decode($ret->body,true);
  220. }
  221. /**
  222. * 退群
  223. * @param $groupId 群id
  224. * @param $uid 人
  225. */
  226. public function delete_group_member($groupId,$uid){
  227. $seting=$this->getIM();
  228. $content = [
  229. 'GroupId' => $seting['IDBefore'].$groupId,
  230. 'Silence' => 0,
  231. 'MemberToDel_Account'=>[]
  232. ];
  233. array_push($content['MemberToDel_Account'], $seting['IDBefore'].$uid);
  234. $req_data = json_encode($content);
  235. $ret=$this->api('group_open_http_svc','delete_group_member',$req_data);
  236. return json_decode($ret->body,true);
  237. }
  238. public function friend_check($From_Account,$to_Account){
  239. $seting=$this->getIM();
  240. $msgcount['From_Account']=$seting['IDBefore'].$From_Account;
  241. $msgcount['CheckType']='CheckResult_Type_Single';
  242. $msgcount['To_Account']=[$seting['IDBefore'].$to_Account];
  243. $req_data = json_encode($msgcount);
  244. $ret=$this->api('sns','friend_check',$req_data);
  245. return json_decode($ret->body,true);
  246. }
  247. public function friend_add($From_Account,$to_Account){
  248. $seting=$this->getIM();
  249. $msgcount['From_Account']=$seting['IDBefore'].$From_Account;
  250. $msgcount['AddType']='Add_Type_Single';
  251. $msgcount['AddFriendItem']=[];
  252. $profileArray['To_Account']=$seting['IDBefore'].$to_Account;
  253. $profileArray['AddSource']='AddSource_Type_app';
  254. $profileArray['AddWording']='我觉得你很赞,认识一下吧';
  255. array_push($msgcount['AddFriendItem'], $profileArray);
  256. $req_data = json_encode($msgcount);
  257. $ret=$this->api('sns','friend_add',$req_data);
  258. return json_decode($ret->body,true);
  259. }
  260. }