<?php
//配置文件
return [
'APP_KEY' => 'e0x9wycfxxx5q',
'APP_SECRET' => 'F7sI8rkLtv'
]; 他们给的key好短啊有没有,继续吧。我下载了官方给的SDK,已经引入到extend文件下了。可以直接调用了。开始主要的部分吧,聊天页面的主方法:<?php
namespace app\index\controller;
use rongyun\ServerAPI;
use think\Controller;
class Index extends Controller
{
public function _initialize()
{
if( empty( cookie('uid') ) ){
$this->redirect( url('login/index') );
}
}
//聊天主方法
public function index()
{
$appKey = config('APP_KEY');
$appSecret = config('APP_SECRET');
$rongYun = new ServerAPI( $appKey, $appSecret );
$tx = "http://www.tk.com/static/images/1.jpg";
if( 2 == cookie('uid') ){
$tx = "http://www.tk.com/static/images/2.jpg";
}
$token = $rongYun->getToken( cookie('uid'), cookie('uname'), $tx );
$token = json_decode( $token, true )['token'];
$this->assign([
'token' => $token
]);
return $this->fetch();
}
//所有的用户信息
public function userInfo()
{
$return['userlist'] = [
['id' => 1, 'name' => '张三', 'portraitUri' => 'http://www.tk.com/static/images/1.jpg'],
['id' => 2, 'name' => '李四', 'portraitUri' => 'http://www.tk.com/static/images/2.jpg']
];
return json( $return );
}
//登录用户信息
public function onLine()
{
$return['data'] = [
['id' => '1', 'status' => true],
['id' => '2', 'status' => true]
];
return json( $return );
}
}各种代码我都写死了,主要是演示效果的。好了,其余的代码,可以去我的github上去下载自己去官网对照这个看吧。我主要演示一下具体怎么跑起来。看一下login.php吧:<?php
namespace app\index\controller;
use think\Controller;
class Login extends Controller
{
public function index()
{
return $this->fetch();
}
public function doLogin()
{
$param = input('param.');
if( '张三' == $param['uname'] ){
cookie('uid', 1);
cookie('uname', '张三');
}else if( '李四' == $param['uname'] ){
cookie('uid', 2);
cookie('uname', '李四');
}
$this->redirect( url('index/index') );
}
} 哈哈,我的登录只是为了区分用户(真的做项目可不是这样!)。可以看到吧,一个用户是张三,一个用户是李四。密码乱输就行。看一下演示效果吧:ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。