问题描述
用户登录我的网站,如何让webIM在页面自动登录,必须把用户id和密码发到前端页面,然后再用js调用登录?
解决方案
首先登陆你的网站,然后在登陆环信,这个可以同步登陆的,登陆网址之后再登陆你为用户注册的环信id,不让用户感知就行
解决方案二:
用户token跟管理token不一样,获取用户tokenprivate $app_key = 'xxxxx#ddddd'; private $client_id = 'ccccccccc'; private $client_secret = 'dddddddddd'; private $url = "https://a1.easemob.com/xiaoyao ... 3B%3B private $token=''; private $header=array();/* * 获取APP管理员Token */ public function __construct(){ //设置缓存 $cache_temp_file='./cache.json'; if(!file_exists($cache_temp_file)){ touch($cache_temp_file); } $this->token=file_get_contents($cache_temp_file); $time=filemtime($cache_temp_file)+(3600*24); //判断文件是否超时(缓存24小时) if( $time < time() || $this->token==''){ $url = $this->url . "/token"; $data = array( 'grant_type' => 'client_credentials', 'client_id' => $this->client_id, 'client_secret' => $this->client_secret ); $rs=$this->curl($url, $data); $this->token = $rs['access_token']; file_put_contents($cache_temp_file,$this->token); } //请求头 $this->header = array( 'Content-Type: application/json', 'Authorization: Bearer ' . $this->token ); } //通过用户名密码获取用户tokenpublic function getUserToken($username, $password){ $url = $this->url . "/token"; $data = array( 'grant_type' => 'password', 'username' => $username, 'password' => $password ); return $this->curl($url, $data, array($this->header[0]), "POST"); } /* * * curl请求 */ private function curl($url, $data, $header = false, $method = "POST"){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($header) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); if ($data) { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $ret = curl_exec($ch); return json_decode($ret,true); }}
解决方案三:
是的,admin token是通过client_id和client_secret获取的, 用户token 是通过用户名密码获取的