注册

用户登录我的网站,如何让webIM在页面自动登录?

用户登录我的网站,如何让webIM在页面自动登录,必须把用户id和密码发到前端页面,然后再用js调用登录?
已邀请:

lizg - ……

首先登陆你的网站,然后在登陆环信,这个可以同步登陆的,登陆网址之后再登陆你为用户注册的环信id,不让用户感知就行
用户token跟管理token不一样,
获取用户token
private $app_key = 'xxxxx#ddddd';
    private $client_id = 'ccccccccc';
    private $client_secret = 'dddddddddd';
    private $url = "https://a1.easemob.com/xiaoyaosheng/webiminset";
    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
        );
    }
 
 //通过用户名密码获取用户token
public 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);
    }
}

lizg - ……

是的,admin token是通过client_id和client_secret获取的, 用户token 是通过用户名密码获取的
请教大牛 设置自动登录一直报错websdk-1.4.11.js:10421 Uncaught TypeError: conn.onError is not a function
at error (websdk-1.4.11.js:10421)
at XMLHttpRequest.xhr.onreadystatechange (websdk-1.4.11.js:2403)
error @ websdk-1.4.11.js:10421
xhr.onreadystatechange @ websdk-1.4.11.js:2403
XMLHttpRequest.send (async)
ajax @ websdk-1.4.11.js:2462
connection.login @ websdk-1.4.11.js:10446
connection.open @ websdk-1.4.11.js:10368
(anonymous) @ index.html:129
j @ jquery.min.js:2
fireWith @ jquery.min.js:2
ready @ jquery.min.js:2
I @ jquery.min.js:2
websdk-1.4.11.js:2462 POST http://a1.easemob.com/1195180614228952/zkyy/users 400 (Bad Request)
 
 
 
自动注册可以的    跪谢各位大佬

要回复问题请先登录注册