请先配置appKey和appSecret 本demo实现了服务端和web的联调(系统通知)
服务端参考文档:开发者中心
WEB参考文档:开发者中心
目录
3. HTML 测试demo页面 js文件请在官方网站自行下载
1.公共类 Common.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2022/1/25
* Time: 14:11
*/
namespace app\nim\controller;
use \think\Controller;
class Common extends Controller{
public $appKey="";//开发者平台分配的 appkey
public $appSecret="";//开发者平台分配的 appSecret
public $accid;//IM 账号
public $nim_name;//redis键名
public $input;
public $nonce;//随机数(最大长度128个字符)
public $checkSum;//SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写)
public $time;//当前时间戳
public function _initialize()
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$this->input=input('');
$this->accid=session_id();//账户 用于测试
$this->nim_name='nim_token_'.$this->accid;
$this->nonce=$this->rand_code(16);
$this->time=time();
$this->checkSum=$this->nim_hearder_init($this->time,$this->nonce);
}
/**
* Notes:生成 checkSum 请求头初始化
* User: ZHOU WEI YUN
* Date: 2022/1/24 0024
* Time: 下午 2:04
* @param $curtime int 当前UTC时间戳
* @param $nonce string 随机数(最大长度128个字符)
* @return string
*/
public function nim_hearder_init($curtime,$nonce){
$appSecret=$this->appSecret;
return strtolower(sha1($appSecret.$nonce.$curtime,false));
}
/* 随机字符串
* $strlen 随机数长度
*/
public function rand_code($strlen=4){
$result='';
$str='abcdefghijklmnopqrstuvwxyz0123456789';//ABCDEFGHIJKLMNOPQRSTUVWXYZ
for($i=0;$i<$strlen;$i++){
$result.=substr($str,mt_rand(0,strlen($str)-$i),1);
//$result.=$str[random_int(0,strlen($str)-$i)];
}
return $result;
}
public function post_url($url,$urlencodedStr)
{
$arr=[
'Content-type:application/x-www-form-urlencoded',
'AppKey:'. $this->appKey,
'Nonce:'. $this->nonce,
'CurTime:'. $this->time,
'CheckSum:'. $this->checkSum,
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($urlencodedStr));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//去掉https验证
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}
2. Nim.php 需要继承Common
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/8/30
* Time: 14:10
*/
namespace app\nim\controller;
class Nim extends Common
{
//https://doc.yunxin.163.com/docs/TM5MzM5Njk/DEwMTE3NzQ?platformId=60353 文档
//产生网易云通信ID
public function create_accid()
{
$return=[
'code'=>400,
'msg'=>'请求失败',
'data'=>[
'appkey'=>$this->appKey,
'token'=>'',
'account'=>$this->accid
],
];
$redis=get_redis();
if(!$redis){
return $return['msg']='redis连接失败';
}
$token=$redis->get($this->nim_name);
if(empty($token)){
$url = "https://api.netease.im/nimserver/user/create.action";//获取
$urlencodedStr_arr=array();
$urlencodedStr_arr['accid']=$this->accid;//自定义(传入即可)
$re=json_decode($this->post_url($url,$urlencodedStr_arr),true);
if($re['code']===200){
$return['msg']='请求成功';
$return['code']=200;
$return['data']['token']=$re['info']['token'];
$redis->set($this->nim_name,$re['info']['token'],600);
}else{
return $this->refresh_token();//重置token
}
}else{
$return['msg']='请求成功';
$return['code']=200;
$return['data']['token']=$token;
}
return $return;
}
/**
* Notes:主动更新 token
* User: ZHOU WEI YUN
* Date: 2022/1/24 0024
* Time: 下午 3:43
* @param array
* @return array|string
*/
public function refresh_token()
{
$return=[
'code'=>400,
'msg'=>'请求失败',
'dat