| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- namespace App\Api\Util;
- use Illuminate\Support\Facades\Cache;
- use Qiniu\Http\Client;
- use Tencent\TLSSigAPIv2;
- trait IM
- {
- use MyUtil;
- private function getIM(){
- $data['IDBefore']=config('love.UCLOUD_CODE');
- if (Cache::has('MYIM')) {
- $im=Cache::get('MYIM');
- }else{
- $im=$this->synchronization('IM')['data'];
- $data['IMSdkAppID']=$im['SecretId'];
- Cache::put('MYIM', $im, 86400);
- }
- $data['SecretId']=$im['SecretId'];
- $data['SecretKey']=$im['SecretKey'];
- return $data;
- }
- /**
- * im 接口请求通用
- * @param $service_name
- * @param $cmd_name
- * @param $identifier
- * @param $usersig
- * @param $req_data
- * @param bool $print_flag
- * @return mixed
- */
- private function api($service_name, $cmd_name,$req_data)
- {
- $seting=$this->getIM();
- $parameter = http_build_query([
- 'usersig' => $this->getUserSig('admin'),
- 'identifier' => 'admin',
- 'sdkappid' => $seting['SecretId'],
- 'contenttype' => 'json',
- 'random' => time(),
- ]);
- $url = 'https://console.tim.qq.com/v4/' . $service_name . '/' . $cmd_name . '?' . $parameter;
- $ret = Client::post($url, $req_data);
- return $ret;
- }
- /**
- * 修改用户资料
- * @param $From_Account 用户
- * @param $profileArray Tag_Profile_IM_Level 表示VIP Tag_Profile_IM_Role 红娘/月老 Tag_Profile_IM_SelfSignature 王牌否
- */
- public function portrait_set($From_Account,$profileArray){
- $seting=$this->getIM();
- $msgcount['From_Account']=$seting['IDBefore'].$From_Account;
- if(count($profileArray,1)==2){
- $msgcount['ProfileItem']=[];
- array_push($msgcount['ProfileItem'], $profileArray);
- }else{
- $msgcount['ProfileItem']=$profileArray;
- }
- $req_data = json_encode($msgcount);
- $ret=$this->api('profile','portrait_set',$req_data);
- return json_decode($ret->body,true);
- }
- /**
- * 发送文本消息
- * @param $from
- * @param $to
- * @param $msg
- * @param string $MsgType TIMTextElem 文本 TIMFaceElem 表情 TIMLocationElem 位置 TIMCustomElem自定义
- * @param int $syncOtherMachine 1消息同步到发送端 2不同步
- */
- public function imsendmassage($from,$to,$msg,$MsgType='TIMTextElem',$syncOtherMachine=1,$Ext='',$image=''){
- $seting=$this->getIM();
- $msg_content = [];
- $msg_content_elem = [
- 'MsgType' => $MsgType, // 文本类型
- 'MsgContent' => [
- "Text"=>$msg
- ]
- ];
- array_push($msg_content, $msg_content_elem);
- $msgcount = [
- 'SyncOtherMachine' => $syncOtherMachine,
- 'MsgLifeTime' => 3600,
- 'To_Account' =>$seting['IDBefore'].$to,
- 'MsgSeq' => rand(1, 65535),
- 'MsgRandom' => rand(1, 65535),
- 'MsgTimeStamp' => time(),
- 'MsgBody' => $msg_content,
- ];
- if($from!='admin'){
- $msgcount['From_Account']=$seting['IDBefore'].$from;
- $msgcount['OfflinePushInfo']=[
- 'PushFlag'=>0,
- 'Desc'=>$msg,
- 'Ext'=>$Ext,
- 'AndroidInfo'=>['Sound'=>'android.mp3'],
- 'ApnsInfo'=>['Sound'=>'apns.mp3','BadgeMode'=>1,'Title'=>$msg,'SubTitle'=>$msg,'Image'=>!$image?:config('app.APP_URL').'/logo.png'],
- ];
- }else{
- $msgcount['SyncOtherMachine']=2;
- }
- $req_data = json_encode($msgcount);
- $ret=$this->api('openim','sendmsg',$req_data);
- // info(json_encode($ret->body));
- // info($req_data);
- }
- /**
- * 创建群
- * @param $owner_Account 群主
- * @param $type 类型
- * @param $name 名
- * @param $groupId 名
- * @param $introduction 介绍
- * @param $notification 公告
- * @param $faceUrl 图
- * @param $admin 管理员
- */
- public function createGroup($owner_Account,$type,$name,$groupId,$introduction,$notification,$faceUrl='',$admin=''){
- $seting=$this->getIM();
- $content = [
- 'Owner_Account' => $seting['IDBefore'].$owner_Account,
- 'Type' => $type,
- 'Name' =>$name,
- 'GroupId' =>$seting['IDBefore'].$groupId,
- 'Introduction' =>$introduction,
- 'Notification' => $notification,
- ];
- if($faceUrl){
- $content['FaceUrl']=$faceUrl;
- }
- if($type=='Public'){
- $content['ApplyJoinOption']='FreeAccess';
- }
- if($admin){
- $content['MemberList']= [];
- $member= [
- "Member_Account"=>$seting['IDBefore'].$admin, // 成员(必填)
- "Role"=>"Admin"
- ];
- array_push($content['MemberList'], $member);
- }
- $req_data = json_encode($content);
- $ret=$this->api('group_open_http_svc','create_group',$req_data);
- return json_decode($ret->body,true);
- }
- /**
- * getUserSig
- * $is_identifier 是否管理员默认不是
- * @return array
- */
- public function getUserSig($uid='admin', $expire=15552000){
- $sig = Cache::remember('UserSig'.$uid,$expire-100,function() use ($uid,$expire){
- $seting=$this->getIM();
- $api= new TLSSigAPIv2($seting['SecretId'],$seting['SecretKey']);
- if($uid!='admin'){
- $usrsign= $api->genSig($seting['IDBefore'].$uid,$expire);
- }else{
- $usrsign= $api->genSig($uid) ;
- }
- return $usrsign;
- });
- return $sig;
- }
- /**
- * @param $owner_Account
- * @return mixed
- */
- public function closeGroup($groupId,$type=0){
- $seting=$this->getIM();
- if($type){
- $content = [
- // 'GroupId' =>$seting['IDBefore'].$groupId.'_'.$type
- 'GroupId' =>$seting['IDBefore'].$groupId
- ];
- }else{
- $content = [
- 'GroupId' =>$seting['IDBefore'].$groupId
- ];
- }
- $req_data = json_encode($content);
- $ret=$this->api('group_open_http_svc','destroy_group',$req_data);
- return json_decode($ret->body,true);
- }
- /**
- * 获取用户好友
- * @param $groupId
- * @param $uid
- * @return mixed
- */
- public function friend_get($uid){//没开发完 暂时不用
- $seting=$this->getIM();
- $content = [
- 'GroupId' =>$seting['IDBefore'].$groupId,
- 'Silence'=>1,
- 'MemberList'=>[]
- ];
- array_push($content['MemberList'], ['Member_Account'=>$seting['IDBefore'].$uid]);
- $req_data = json_encode($content);
- $ret=$this->api('sns','friend_get',$req_data);
- return json_decode($ret->body,true);
- }
- /**
- * 增加群成员
- * @param $groupId
- * @param $uid
- * @return mixed
- */
- public function add_group_member($groupId,$uid){
- $seting=$this->getIM();
- $content = [
- 'GroupId' =>$seting['IDBefore'].$groupId,
- 'Silence'=>1,
- 'MemberList'=>[]
- ];
- array_push($content['MemberList'], ['Member_Account'=>$seting['IDBefore'].$uid]);
- $req_data = json_encode($content);
- $ret=$this->api('group_open_http_svc','add_group_member',$req_data);
- return json_decode($ret->body,true);
- }
- /**
- * 退群
- * @param $groupId 群id
- * @param $uid 人
- */
- public function delete_group_member($groupId,$uid){
- $seting=$this->getIM();
- $content = [
- 'GroupId' => $seting['IDBefore'].$groupId,
- 'Silence' => 0,
- 'MemberToDel_Account'=>[]
- ];
- array_push($content['MemberToDel_Account'], $seting['IDBefore'].$uid);
- $req_data = json_encode($content);
- $ret=$this->api('group_open_http_svc','delete_group_member',$req_data);
- return json_decode($ret->body,true);
- }
- public function friend_check($From_Account,$to_Account){
- $seting=$this->getIM();
- $msgcount['From_Account']=$seting['IDBefore'].$From_Account;
- $msgcount['CheckType']='CheckResult_Type_Single';
- $msgcount['To_Account']=[$seting['IDBefore'].$to_Account];
- $req_data = json_encode($msgcount);
- $ret=$this->api('sns','friend_check',$req_data);
- return json_decode($ret->body,true);
- }
- public function friend_add($From_Account,$to_Account){
- $seting=$this->getIM();
- $msgcount['From_Account']=$seting['IDBefore'].$From_Account;
- $msgcount['AddType']='Add_Type_Single';
- $msgcount['AddFriendItem']=[];
- $profileArray['To_Account']=$seting['IDBefore'].$to_Account;
- $profileArray['AddSource']='AddSource_Type_app';
- $profileArray['AddWording']='我觉得你很赞,认识一下吧';
- array_push($msgcount['AddFriendItem'], $profileArray);
- $req_data = json_encode($msgcount);
- $ret=$this->api('sns','friend_add',$req_data);
- return json_decode($ret->body,true);
- }
- }
|