第一部分 了解网易云信im
根据上图所示,在我们接入网易云信im时,我们所需要做的就是蓝色部分,并且客户端网易提供了一个完整的demo,我们的开发完全可以基于这个demo来改造,然后就是应用服务器的开发,由于官网提供了大量的api接口来实现基层功能,使得我们可以专心于我们的业务开发。App_server = 应用服务器,App = 客户端.
在上图,我们知道,我们的第一步就是创建账号,在官方提供的demo中已经可以完整的使用im功能,但我总不能使用官方提供的账号系统吧,所以我们就需要开发我们的应用服务器来创建属于我们的账号系统。接下来就是代码实战阶段。
第二部分 代码实战
2.1 创建一个全新的lumen项目,并添加guzzle包,因为要发请求
2.2创建im工具类
<?php
/**
* Created by PhpStorm.
* User: jinmi
* Date: 2019-04-13
* Time: 20:37
*/
namespace App\Tools;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
class IMTool
{
private $AppKey;
private $AppSecret;
private $Nonce;
private $CurTime;
private $CheckSum;
const HEX_DIGITS = "0123456789abcdef";
public function __construct()
{
$this->AppKey = env('IM_APP_KEY'); //这里要替换成你的app_key
$this->AppSecret = env('IM_APP_SCRET'); //这里要替换成你的app_scret
$this->client = new Client([
'timeout' => 5
]);
$this->checkSumBuilder();
}
/**
* API checksum校验生成
* @param void
* @return $CheckSum(对象私有属性)
*/
protected function checkSumBuilder()
{
//此部分生成随机字符串
$hex_digits = self::HEX_DIGITS;
$this->Nonce;
for ($i = 0; $i < 128; $i++) { //随机字符串最大128个字符,也可以小于该数
$this->Nonce .= $hex_digits[rand(0, 15)];
}
$this->CurTime = (string)(time()); //当前时间戳,以秒为单位
$join_string = $this->AppSecret . $this->Nonce . $this->CurTime;
$this->CheckSum = sha1($join_string);
}
/**
* 发送数据
* @param $url
* @param $data
* @return array
* @throws
* User: jinmi
* Date: 2019-04-13
*/
protected function postData($url, $data)
{
$code = 500;
$msg = 'ok';
$response = null;
$body = null;
try {
$response = $this->client->request('post', $url, [
'form_params' => $data,
'headers' => [
'AppKey' => $this->AppKey,
'Nonce' => $this->Nonce,
'CurTime' => $this->CurTime,
'CheckSum' => $this->CheckSum,
'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
]
]);
} catch (\Exception $e){
$msg = $e->getMessage();
}
if ($response instanceof ResponseInterface) {
$statusCode = $response->getStatusCode();
$body = $response->getBody();
$data = json_decode($body, true);
if (is_array($data)) {
return $data;
} else {
$data = $body;
$code = $statusCode;
}
}
return [$code, $msg, $data];
}
/**
* 创建网易云id
* @param $accid 自定义id
* @param $name 昵称
* @param $token token不指定,网易会自动生成
* @return array
* User: jinmi
* Date: 2019-04-13
*/
public function createID($accid, $name, $token)
{
return $this->postData('https://api.netease.im/nimserver/user/create.action', [
'accid' => $accid,
'name' => $name,
'token' => $token
]);
}
}
以上的checksum方法是根据 网易云信IMweb版集成实现(PHP直接上码)
2.3 创建登录和注册接口
<?php
namespace App\Http\Controllers;
use App\Tools\IMTool;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;
class Controller extends BaseController
{
//登录
public function login(Request $request){
$account = $request->get('account','');
$pwd = $request->get('pwd','');
// 经过一系列验证操作,然后验证成功后返回token
return response()->json([
'code' => 200,
'info' => [
'accid' => $account,
'name' => $account,
'token' => $account,
]
]);
}
//注册
public function register(Request $request){
$account = $request->get('account','');
$pwd = $request->get('pwd','');
$name = $request->get('name','');
// 现在数据库创建账号
// 创建网易id
$result = (new IMTool())->createID($account,$account,$account);
return response()->json($result);
}
}
2.4 修改官方demo,修改登录逻辑和配置
这里是借鉴 网易云信Web IM入门(二)java
第三部分 注册账号并且登录测试
3.1 注册测试账号
3.2 运行官方demo登录并且加上好友聊天