373 lines
13 KiB
PHP
373 lines
13 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 首页控制器.
|
|
*/
|
|
|
|
namespace app\index\controller;
|
|
|
|
use think\Db;
|
|
|
|
class Index extends Base
|
|
{
|
|
// 模拟用户
|
|
public $userInfo = [
|
|
'user' => [
|
|
'user_id' => 1234,
|
|
'user_name' => '游客1234',
|
|
'expire_time' => 10000,
|
|
'avatar_url' => 'http://wx2.sinaimg.cn/mw690/5db11ff4gy1flxmew7edlj203d03wt8n.jpg',
|
|
],
|
|
'type' => '1',
|
|
];
|
|
|
|
/**
|
|
* 首页.
|
|
*
|
|
* @return [type] [description]
|
|
*/
|
|
// public function index()
|
|
// {
|
|
// $this->assign([
|
|
// 'url' => 'http://'.config('socket_host').'/index/index/chat?group=',
|
|
// ]);
|
|
|
|
// return $this->fetch();
|
|
// }
|
|
|
|
// pc客户端
|
|
public function chat()
|
|
{
|
|
// 跳转到移动端
|
|
if (request()->isMobile()) {
|
|
$param = http_build_query([
|
|
'mytoken' => input('param.mytoken'),
|
|
'group' => input('param.group'),
|
|
]);
|
|
$this->redirect('/index/index/mobile?' . $param);
|
|
}
|
|
|
|
session_start();
|
|
if (!isset($_SESSION['token'])) {
|
|
$token = $this->getTokenInfo();
|
|
$_SESSION['token'] = $token;
|
|
}
|
|
|
|
$this->assign([
|
|
'socket' => config('socket_host') . ':' . config('socket_port'),
|
|
'token' => $_SESSION['token'],
|
|
'avatar' => json_decode($_SESSION['token'], true)['user']['avatar_url'],
|
|
'group' => input('param.group'),
|
|
]);
|
|
|
|
return $this->fetch();
|
|
}
|
|
|
|
// 移动客户端
|
|
public function mobile()
|
|
{
|
|
$this->assign([
|
|
'socket' => config('socket_url'),
|
|
'port' => config('socket_port'),
|
|
'mytoken' => input('param.mytoken'),
|
|
'uid' => 123,
|
|
'name' => 'aaa',
|
|
'avatar' => 'https://customer.doudoubei.com/uploads/chatimg/kf/20190903/032c82f4cff3b6d727f4ce1d4cae95f8.jpg',
|
|
'group' => 1,
|
|
'kf_type'=>1
|
|
]);
|
|
|
|
return $this->fetch();
|
|
}
|
|
|
|
/**
|
|
* 客服列表.
|
|
*/
|
|
public function getkflist()
|
|
{
|
|
$ip = config('socket_host');
|
|
$uid = $this->userInfo['user']['user_id'];
|
|
// $token = input("get.token");
|
|
// dump($token);
|
|
$groupId = input('param.group');
|
|
if(empty($groupId)){
|
|
return json(['code' => 400, 'msg' => '平台ID不能为空', 'data' =>[] ]);
|
|
}
|
|
$type = input('param.type');
|
|
$where = [
|
|
'status' => 1,
|
|
'group_id' => $groupId
|
|
];
|
|
if ($type) {
|
|
$where['kf_type'] = intval($type);
|
|
}
|
|
$dialogue_list = db('dialogue_list');
|
|
$url_prefix = config('img_take_prefix');
|
|
$kfList = db('users')->field('CONCAT("KF",id) as id, user_name, user_avatar, kf_type, group_id,rank_num,rate_num')
|
|
->where($where)->select();
|
|
|
|
// 获取客服的支付方式
|
|
foreach ($kfList as &$item) {
|
|
$id = self::kf2Num($item['id']);
|
|
$item['pay_list'] = db('payment')->alias('a')->join('payment_type b', 'a.payment_type=b.id')->field('b.alias_id')->where(['kf_id' => $id, 'is_use' => 1, 'a.status' => '1'])->select();
|
|
// 检测用户是否与当前客服有交流
|
|
$check_log = $dialogue_list->field('id')->where(['user_id' => $uid, 'kf_id' => $id])->find();
|
|
$item['check_log'] = $check_log ? true : false;
|
|
}
|
|
|
|
// 获取对话查询
|
|
$chat_log = $dialogue_list->where(['user_id' => $uid])->order('ping_time desc')->limit(10)->select();
|
|
foreach ($chat_log as &$vo) {
|
|
if (!$vo['ping_time']) {
|
|
continue;
|
|
}
|
|
// 检测对话时间是否超过5分钟
|
|
$diff = (time() - $vo['ping_time']) / 60;
|
|
if ($diff >= 5 && $vo['status'] != 1) {
|
|
$vo['status'] = -1;
|
|
// 更新数据库的状态
|
|
$dialogue_list->where('id', $vo['id'])->update(['status' => -1]);
|
|
}
|
|
}
|
|
// dump($kfList);return;
|
|
$this->assign(['data' => $kfList, 'ip' => $ip, 'chat_log' => $chat_log]);
|
|
return $this->fetch('getkflist');
|
|
}
|
|
|
|
/**
|
|
* 客服列表数据
|
|
*/
|
|
public function getkfdata()
|
|
{
|
|
$ip = config('socket_host');
|
|
$uid = $this->userInfo['user']['user_id'];
|
|
// $token = input("get.token");
|
|
// dump($token);
|
|
$groupId = input('param.group');
|
|
$type = input('param.type');
|
|
if(empty($groupId)){
|
|
return json(['code' => 400, 'msg' => '平台ID不能为空', 'data' =>[] ]);
|
|
}
|
|
$where = [
|
|
'status' => 1,
|
|
'group_id' => $groupId
|
|
];
|
|
if ($type) {
|
|
$where['kf_type'] = intval($type);
|
|
}
|
|
$dialogue_list = db('dialogue_list');
|
|
$url_prefix = config('img_take_prefix');
|
|
$kfList = db('users')->field('CONCAT("KF",id) as id, user_name, user_avatar, kf_type, group_id,rank_num,rate_num')
|
|
->where($where)->select();
|
|
|
|
// 获取客服的支付方式
|
|
foreach ($kfList as &$item) {
|
|
$id = self::kf2Num($item['id']);
|
|
$item['pay_list'] = db('payment')->alias('a')->join('payment_type b', 'a.payment_type=b.id')->field('b.alias_id')->where(['kf_id' => $id, 'is_use' => 1, 'a.status' => '1'])->select();
|
|
// 检测用户是否与当前客服有交流
|
|
$check_log = $dialogue_list->field('id')->where(['user_id' => $uid, 'kf_id' => $id])->find();
|
|
$item['check_log'] = $check_log ? true : false;
|
|
}
|
|
|
|
// 获取对话查询
|
|
$chat_log = $dialogue_list->where(['user_id' => $uid])->order('ping_time desc')->limit(10)->select();
|
|
foreach ($chat_log as &$vo) {
|
|
if (!$vo['ping_time']) {
|
|
continue;
|
|
}
|
|
// 检测对话时间是否超过5分钟
|
|
$diff = (time() - $vo['ping_time']) / 60;
|
|
if ($diff >= 5 && $vo['status'] != 1) {
|
|
$vo['status'] = -1;
|
|
// 更新数据库的状态
|
|
$dialogue_list->where('id', $vo['id'])->update(['status' => -1]);
|
|
}
|
|
}
|
|
// dump($kfList);return;
|
|
return json_encode(['data' => $kfList, 'ip' => $ip, 'chat_log' => $chat_log]);
|
|
}
|
|
|
|
/**
|
|
* 提取客服id.
|
|
*/
|
|
private function kf2Num($kfId)
|
|
{
|
|
if (!is_numeric($kfId) && strpos($kfId, 'KF') === 0) {
|
|
$kfId = substr($kfId, 2);
|
|
}
|
|
|
|
return $kfId;
|
|
}
|
|
|
|
public function getkflist1()
|
|
{
|
|
$ip = config('socket_host');
|
|
// $groupId = input('param.group');
|
|
// if(empty($groupId)){
|
|
// return json(['code' => 400, 'msg' => '平台ID不能为空', 'data' =>[] ]);
|
|
// }
|
|
$url_prefix = config('img_take_prefix');
|
|
$kfList = db('users')->field('CONCAT("KF",id) as id, user_name, user_avatar,rank_num,rate_num')
|
|
->where(['status' => 1, 'group_id' => 2])->select();
|
|
// $payment = Db::query('SELECT alias_id FROM ws_users INNER JOIN ws_payment on ws_users.id=ws_payment.kf_id INNER JOIN ws_payment_type on ws_payment.payment_type=ws_payment_type.id');
|
|
// // dump($kfList);die;
|
|
// $this->assign(['data'=>$kfList,'ip'=>$ip,'payment'=>$payment]);
|
|
return $kfList;
|
|
}
|
|
|
|
/**
|
|
* 聊天.
|
|
*/
|
|
public function kfchat()
|
|
{
|
|
// 订单db
|
|
$dialogue_list = db('dialogue_list');
|
|
$user_id = input('user_id/d', '12345');
|
|
if ($user_id) {
|
|
|
|
}
|
|
//模拟游客信息
|
|
$userInfo = [
|
|
'user' => [
|
|
'user_id' => $user_id,
|
|
'user_name' => '游客1234',
|
|
'expire_time' => time() + 100,
|
|
'avatar_url' => 'http://wx2.sinaimg.cn/mw690/5db11ff4gy1flxmew7edlj203d03wt8n.jpg',
|
|
],
|
|
'type' => '1',
|
|
];
|
|
//客服信息
|
|
$id = input('id'); //客服id
|
|
$dialogue_id = input('dialogue_id'); //对话记录id
|
|
$order_num = 'JXJY' . random_int(100000, 999999);
|
|
$start_time = time();
|
|
$kf_id = substr($id, 2);
|
|
// 检测是否生产订单, 否的话生成
|
|
$uid = $userInfo['user']['user_id'];
|
|
$check_order = $dialogue_list->where(['user_id' => $uid, 'kf_id' => $kf_id, 'status' => 0])->find();
|
|
if (!$dialogue_id && empty($check_order)) {
|
|
$array = [
|
|
'kf_id' => $kf_id,
|
|
'order_num' => $order_num,
|
|
'start_time' => $start_time,
|
|
'ping_time' => $start_time,
|
|
'user_id' => $uid,
|
|
];
|
|
$dialogue_list->insert($array);
|
|
} else {
|
|
// 更新ping字段
|
|
$where = !$dialogue_id ? ['user_id' => $uid, 'kf_id' => $kf_id, 'id' => $dialogue_id] : ['user_id' => $uid, 'kf_id' => $kf_id, 'status' => 0];
|
|
// $update_data = ['ping_time' => time(), 'status' => 0];
|
|
$update_data = ['ping_time' => time()];
|
|
$update_ping = $dialogue_list->where($where)->update($update_data);
|
|
}
|
|
|
|
$data = db('users')->field('id as id, user_name, user_avatar,rank_num,rate_num, group_id, kf_type')
|
|
->where(['status' => 1, 'id' => $kf_id])->find();
|
|
$this->assign([
|
|
'uid' => $id,
|
|
'name' => $data['user_name'],
|
|
'avatar' => $data['user_avatar'],
|
|
'socket' => config('socket_url'),
|
|
'port' => config('socket_port'),
|
|
// 'group' => input('param.group'),
|
|
'group' => $data['group_id'],
|
|
'kf_type' => $data['kf_type'],
|
|
'token' => json_encode($userInfo),
|
|
]);
|
|
|
|
return $this->fetch();
|
|
}
|
|
|
|
/**
|
|
* 对话查询.
|
|
*/
|
|
public function dialogQuery()
|
|
{
|
|
$data = db('dialogue_list')->order('id desc')->limit(10)->select();
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 获取客服的支付方式
|
|
* @return [type] [description]
|
|
*/
|
|
public function getKfPayWay()
|
|
{
|
|
$id = self::kf2Num(input('post.id'));
|
|
if (!$id) {
|
|
return ['code' => 2, 'msg' => '参数错误'];
|
|
}
|
|
$data = db('payment')->alias('a')->join('payment_type b', 'a.payment_type=b.id')->field('b.type_name')->where(['kf_id' => $id, 'is_use' => 1, 'a.status' => '1'])->select();
|
|
return ['code' => 1, 'msg' => '获取成功', 'data' => $data];
|
|
}
|
|
|
|
/**
|
|
* 获取token信息.
|
|
*/
|
|
private function getTokenInfo()
|
|
{
|
|
$userId = random_int(1000, 9999);
|
|
$userInfo = [
|
|
'user' => [
|
|
'user_id' => $userId,
|
|
'user_name' => '游客' . $userId,
|
|
'expire_time' => time() + 100,
|
|
'avatar_url' => 'http://wx2.sinaimg.cn/mw690/5db11ff4gy1flxmew7edlj203d03wt8n.jpg',
|
|
],
|
|
'type' => '1',
|
|
];
|
|
|
|
return json_encode($userInfo);
|
|
}
|
|
|
|
public function test()
|
|
{
|
|
|
|
$param = input('param.');
|
|
// var_dump($param);exit;
|
|
$ts = self::getMsectime();
|
|
unset($param['sign'], $param['ts']);
|
|
$data = '';
|
|
foreach ($param as $key => $value) {
|
|
$data .= $key . $value;
|
|
}
|
|
// $data = json_encode($param);
|
|
$sign = md5('U8HpGv1ry0yigghDfMp3O6' . ';' . $data . ';' . $ts);
|
|
// var_dump($sign);
|
|
// var_dump('U8HpGv1ry0yigghDfMp3O6'.';'.$data.';'.$ts);
|
|
// $url = "https://customer.doudoubei.com/api/game/putKfRate?sign=$sign&ts=$ts";
|
|
// $url = "https://customer.doudoubei.com/api/game/getKfRate?kf_id=KF6&order_id=asdiofoasd4541as65df1asdf&sign=$sign&ts=$ts";
|
|
// $url = "https://customer.doudoubei.com/api/game/getKfList?group_id=1&sign=$sign&ts=$ts";
|
|
// $url = "https://customer.doudoubei.com/api/game/getKfList?group_id=1&sign=bdd91502d3239feb5e6bb20871e0dc01&ts=$ts";
|
|
|
|
// echo $this->curl_post($url,$data);
|
|
echo curl_get($url);
|
|
}
|
|
public function curl_post($url, $data, $timeout = 5)
|
|
{
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
curl_setopt(
|
|
$ch,
|
|
CURLOPT_HTTPHEADER,
|
|
array(
|
|
"Content-Type: application/json; charset=utf-8",
|
|
"Content-Length: " . strlen($data)
|
|
)
|
|
);
|
|
$res = curl_exec($ch);
|
|
curl_close($ch);
|
|
return $res;
|
|
}
|
|
public static function getMsectime()
|
|
{
|
|
return (float) sprintf('%.0f', microtime(true) * 1000);
|
|
}
|
|
} |