-
Notifications
You must be signed in to change notification settings - Fork 27
/
chat.php
590 lines (474 loc) · 15.1 KB
/
chat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
/**
* 鸿宇多用户商城 在线客服聊天系统-前台
* ============================================================================
* * 版权所有 2008-2015 鸿宇多用户商城科技有限公司,并保留所有权利。
* 网站地址: http://bbs.hongyuvip.com;
* ----------------------------------------------------------------------------
* 仅供学习交流使用,如需商用请购买正版版权。鸿宇不承担任何法律责任。
* 踏踏实实做事,堂堂正正做人。
* ============================================================================
* $Author: 鸿宇多用户商城 $
* $Id: category.php 17217 2015-02-10 06:29:08Z 鸿宇多用户商城 $
*/
define('IN_ECS', true);
require ('includes/init.php');
require ('includes/lib_chat.php');
/* 载入语言文件 */
require_once (ROOT_PATH . 'languages/' . $_CFG['lang'] . '/user.php');
$action = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'chat';
/* 检查用户是否已登录 */
if(empty($_SESSION['user_id']) && $action != 'act_login' && $action != 'check_login')
{
// $captcha = intval($_CFG['captcha']);
// if(($captcha & CAPTCHA_LOGIN) && (! ($captcha & CAPTCHA_LOGIN_FAIL) || (($captcha & CAPTCHA_LOGIN_FAIL) && $_SESSION['login_fail'] > 2)) && gd_version() > 0)
// {
// $GLOBALS['smarty']->assign('enabled_captcha', 1);
// $GLOBALS['smarty']->assign('rand', mt_rand());
// }
// 如果未登录跳转到登录页面
/**
$smarty->assign('lang', $_LANG);
$smarty->assign('back_act', '');
$smarty->assign('action', 'login');
$smarty->display('chat_passport.dwt');
**/
show_message('您还未登录系统,请先登录!', array('登录', '返回上一页'), array('user.php?act=login', 'index.php'), 'info');
return;
}
//路由
$function_name = 'action_' . $action;
if(function_exists($function_name))
{
call_user_func($function_name);
}
else
{
exit('函数' . $function_name . '不存在');
}
/**
* 检查用户是否登录
*/
function action_check_login()
{
$is_login = empty($_SESSION['user_id']) ? 'false' : 'true';
exit($is_login);
}
/**
* 处理会员登录
*/
function action_act_login ()
{
$user_id = $_SESSION['user_id'];
$smarty = get_smarty();
$ecs = get_ecs();
$db = get_database();
/* 处理会员的登录 */
$username = isset($_POST['username']) ? trim($_POST['username']) : '';
$password = isset($_POST['password']) ? trim($_POST['password']) : '';
$back_act = isset($_POST['back_act']) ? trim($_POST['back_act']) : '';
$captcha = intval($_CFG['captcha']);
if(($captcha & CAPTCHA_LOGIN) && (! ($captcha & CAPTCHA_LOGIN_FAIL) || (($captcha & CAPTCHA_LOGIN_FAIL) && $_SESSION['login_fail'] > 2)) && gd_version() > 0)
{
if(empty($_POST['captcha']))
{
$smarty->assign('lang', $_LANG);
$smarty->assign('action', 'login');
$smarty->assign('error', $_LANG['invalid_captcha']);
$smarty->display('chat_passport.dwt');
return;
}
/* 检查验证码 */
include_once ('includes/cls_captcha.php');
$validator = new captcha();
$validator->session_word = 'captcha_login';
if(! $validator->check_word($_POST['captcha']))
{
$smarty->assign('lang', $_LANG);
$smarty->assign('action', 'login');
$smarty->assign('error', $_LANG['invalid_captcha']);
$smarty->display('chat_passport.dwt');
return;
}
}
if(is_email($username))
{
$sql = "select user_name from " . $ecs->table('users') . " where email='" . $username . "'";
$username_e = $db->getOne($sql);
if($username_e)
$username = $username_e;
}
if(is_telephone($username))
{
$sql = "select user_name from " . $ecs->table('users') . " where mobile_phone='" . $username . "'";
$username_res = $db->query($sql);
$kkk = 0;
while($username_row = $db->fetchRow($username_res))
{
$username_e = $username_row['user_name'];
$kkk = $kkk + 1;
}
if($kkk > 1)
{
$smarty->assign('lang', $_LANG);
$smarty->assign('action', 'login');
$smarty->assign('error', '本网站有多个会员ID绑定了和您相同的手机号,请使用其他登录方式,如:邮箱或用户名。');
$smarty->display('chat_passport.dwt');
return;
}
if($username_e)
{
$username = $username_e;
}
}
if($GLOBALS['user']->login($username, $password, isset($_POST['remember'])))
{
update_user_info();
recalculate_price();
// 登录成功
$ucdata = isset($user->ucdata) ? $user->ucdata : '';
// show_message($_LANG['login_success'] . $ucdata ,
// array($_LANG['back_up_page'], $_LANG['profile_lnk']),
// array($back_act,'user.php'), 'info');
// 刷新user_id
$user_id = $_SESSION['user_id'];
header('Location: chat.php?act=chat');
}
else
{
$_SESSION['login_fail'] ++;
$smarty->assign('lang', $_LANG);
$smarty->assign('action', 'login');
$smarty->assign('error', $_LANG['login_failure']);
$smarty->display('chat_passport.dwt');
return;
}
}
/* ------------------------------------------------------ */
// -- 在线客服聊天 --> 请求聊天
// 聊天窗口右侧默认展示最近订单,如果想要展示商品、订单、店铺则需要在当前页面中设置隐藏域,name必须为 chat_goods_id,
// chat_order_id, chat_supp_id
/* ------------------------------------------------------ */
function action_chat ()
{
$user_id = $_SESSION['user_id'];
$smarty = get_smarty();
$ecs = get_ecs();
$db = get_database();
/**
* 判断当前用户是为聊天系统的注册用户
*/
$exist = check_of_username_exist($user_id);
// 获取用户头像
if(! empty($user_id))
{
$sql = "select password, headimg from " . $ecs->table('users') . " where user_id = '$user_id'";
$row = $db->getRow($sql);
$headimg = $row['headimg'];
$password = $row['password'];
$smarty->assign('headimg', $headimg);
}
if(! $exist)
{
// 查询ECShop内用户信息
$sql = 'select a.user_id, a.password, a.email, a.user_name from ' . $ecs->table('users') . ' AS a where a.user_id = "' . $user_id . '"';
$user = $GLOBALS['db']->getRow($sql);
if(empty($user))
{
// 根据user_id未查找到任何用户信息
}
// 用户不存在,创建用户信息
$username = $user_id;
$password = $user['password'];
$name = $user['user_name'];
$email = $user['email'];
$type = 10;
$shop_id = - 1;
$result = create_of_user($username, $password, $name, $email, $type, $shop_id);
if($result)
{
// 创建成功
}
else
{
// 创建失败
}
}
// 获取前端传来的商品编号、订单编号、店铺编号等
// 商品编号则显示商品信息
// 订单编号则显示订单信息
// 店铺编号则显示店铺信息
$goods_id = null;
$supp_id = - 1;
$order_id = null;
$customers = null;
// 获取客服信息
$tab_items = array();
// 客服类型
$cus_types = CUSTOMER_SERVICE;
// 记录需要发给客服的URL
if(! empty($_REQUEST['chat_goods_id']))
{
/* 咨询商品信息 */
$goods_id = $_REQUEST['chat_goods_id'];
$goods = goods_info($goods_id);
$smarty->assign('chat_goods', $goods);
$smarty->assign('chat_goods_id', $goods_id);
$tab_items[] = array(
"id" => "chat_goods","name" => "咨询商品"
);
// 客服+售前
$cus_types = CUSTOMER_SERVICE . ',' . CUSTOMER_PRE;
}
if(! empty($_REQUEST['chat_order_id']))
{
/* 咨询订单信息 */
require ('includes/lib_order.php');
$order_id = $_REQUEST['chat_order_id'];
// 获取商品和店铺信息
$goods_id = null;
$order = order_info($order_id);
$supp_id = $order['supplier_id'];
$order['order_status_text'] = $GLOBALS['_LANG']['os'][$order['order_status']] . ',' . $GLOBALS['_LANG']['ps'][$order['pay_status']] . ',' . $GLOBALS['_LANG']['ss'][$order['shipping_status']];
$order['goods_list'] = order_goods($order_id);
$smarty->assign('chat_order', $order);
$smarty->assign('chat_order_id', $order_id);
$smarty->assign('chat_order_sn', $order['order_sn']);
$tab_items[] = array(
"id" => "chat_order","name" => "咨询订单"
);
// 客服+售后
$cus_types = CUSTOMER_SERVICE . ',' . CUSTOMER_AFTER;
}
if(! empty($_REQUEST['chat_supp_id']) && $_REQUEST['chat_supp_id'] != 0)
{
/* 店铺信息 */
$supp_id = $_REQUEST['chat_supp_id'];
$supp_info = get_dianpu_baseinfo($supp_id);
$smarty->assign('supp_info', $supp_info);
$smarty->assign('chat_supp_id', $supp_id);
$tab_items[] = array(
"id" => "chat_supp", "name" => "店铺信息"
);
// 客服+售前
$cus_types = CUSTOMER_SERVICE . ',' . CUSTOMER_PRE;
}
if(true)
{
/* 最近订单列表 */
require ('includes/lib_transaction_1.php');
// 获取用户最近的5条订单列表
$order_list = get_user_orders_1($user_id, 5, 0);
// 所有客服忙碌状态,提示web端
$smarty->assign('order_list', $order_list);
$smarty->assign('order_count', count($order_list));
$tab_items[] = array(
"id" => "chat_order_list","name" => "最近订单"
);
// 客服
$cus_types = CUSTOMER_SERVICE;
}
// 获取客服信息
$customers = get_customers($cus_types, $supp_id);
// 转换为JSON数据
$smarty->assign('tab_items', json_encode($tab_items));
$to = null;
// 客服获取策略:0-顺序、1-随机、2-竞争
if(! empty($customers))
{
// 暂时采用随机策略
$poliy = 1;
if($poliy == 0)
{
foreach($customers as $customer)
{
$status = $customer['status'];
if($status == '在线' || $status == '空闲')
{
$to = $customer;
break;
// if(isset($customer['cus_status']) && count($customers) > 1)
// {
// if(time() - $customer['chat_time'] > 5*60)
// {
// set_customer_status($customer['cus_id'], 0);
// $customer['cus_status'] = 0;
// }
// if($customer['cus_status'] == 0)
// {
// $to = $customer;
// break;
// }
// }
// else
// {
// $to = $customer;
// break;
// }
}
}
}
else if($poliy == 1)
{
/* 随进策略 */
$onlines = array();
foreach($customers as $customer)
{
$status = $customer['status'];
if($status == '在线' || $status == '空闲')
{
$onlines[] = $customer;
}
}
if(count($onlines) > 0)
{
$min = 1;
$max = count($onlines);
$i = mt_rand($min, $max);
$to = $onlines[$i - 1];
}
}
else
{
}
if(empty($to))
{
if($supp_id == -1){
// 所有客服忙碌状态,提示web端
$smarty->assign('system_notice', '当前客服忙碌,请稍后联系!');
}else{
// 所有客服忙碌状态,提示web端
$smarty->assign('system_notice', '当前店铺客服忙碌,请稍后联系!');
}
}
else
{
$xmpp_domain = get_xmpp_domain();
$_SESSION['OF_FROM'] = $user_id . '@' . $xmpp_domain;
$_SESSION['OF_TO'] = $to['of_username'] . '@' . $xmpp_domain;
$smarty->assign('from', $_SESSION['OF_FROM']);
$smarty->assign('password', $password);
// $smarty->assign('password', "123456");
$smarty->assign('to', '==to==');
$smarty->assign('username', $_SESSION['user_name']);
$smarty->assign('customername', $to['cus_name']);
// 存储在Session中方便其他地方使用
// 所有客服忙碌状态,提示web端
$smarty->assign('system_notice', '客服<span class="kf_name">' . $to['cus_name'] . '</span>已加入会话!');
}
}
else
{
// 所有客服忙碌状态,提示web端
$smarty->assign('system_notice', '当前客服忙碌,请稍后联系!');
}
// 打开聊天页面
$smarty->display('chat.dwt');
}
/* ------------------------------------------------------ */
// -- 在线客服聊天 --> 认证失败,重新设置聊天系统的用户密码
// 聊天窗口右侧默认展示最近订单,如果想要展示商品、订单、店铺则需要在当前页面中设置隐藏域,name必须为 chat_goods_id,
// chat_order_id, chat_supp_id
/* ------------------------------------------------------ */
function action_authfail ()
{
$user_id = $_SESSION['user_id'];
$sql = "select user_name, password, email from " . $GLOBALS['ecs']->table('users') . " where user_id = '$user_id'";
$row = $db->getRow($sql);
$success = create_of_user($user_id, $row['password'], $row['user_name'], $row['email'], 10, - 1);
if($success)
{
$result = array(
'error' => 1,'message' => '可能由于网络原因,发生错误!请点击 <a href="chat.php?act=chat"><strong>重试</strong></a> ,重新连接...','content' => ''
);
}
else
{
$result = array(
'error' => 1,'message' => '由于网络原因,发生错误!请点击 <a href="chat.php?act=chat"><strong>重试</strong></a> ,重新连接...','content' => ''
);
}
$result = json_encode($result);
exit($result);
}
/**
* 用户离线
*/
function action_off_line()
{
// 用户超过5分钟未发言则视为自动离线,更新客服状态
}
function is_telephone ($phone)
{
$chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/";
if(preg_match($chars, $phone))
{
return true;
}
}
/**
* 获取db对象
*
* @return unknown
*/
function get_database ()
{
return $GLOBALS['db'];
}
/**
* 获取smarty对象
*
* @return unknown
*/
function get_smarty ()
{
return $GLOBALS[smarty];
}
/**
* 获取ecs对象
*
* @return unknown
*/
function get_ecs ()
{
return $GLOBALS['ecs'];
}
/*
* 获取商品所对应店铺的店铺基本信息
* @param int $suppid 店铺id
* @param int $suppinfo 入驻商的信息
*/
function get_dianpu_baseinfo ($suppid = 0)
{
if(intval($suppid) <= 0)
{
return;
}
$sql_supplier = "SELECT s.supplier_id,s.supplier_name,s.add_time,sr.rank_name FROM " . $GLOBALS['ecs']->table("supplier") . " as s left join " . $GLOBALS['ecs']->table("supplier_rank") . " as sr ON s.rank_id=sr.rank_id WHERE s.supplier_id=" . $suppid . " AND s.status=1";
$supp_info = $GLOBALS['db']->getRow($sql_supplier);
$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('supplier_shop_config') . " WHERE supplier_id = " . $suppid;
$shopinfo = $GLOBALS['db']->getAll($sql);
$config = array();
foreach($shopinfo as $value)
{
$config[$value['code']] = $value['value'];
}
$shop_info = array();
$shop_info['ghs_css_path'] = 'themes/' . $config['template'] . '/images/ghs/css/ghs_style.css'; // 入驻商所选模板样式路径
$shoplogo = empty($config['shop_logo']) ? 'themes/' . $config['template'] . '/images/dianpu.jpg' : $config['shop_logo'];
$shop_info['shoplogo'] = $shoplogo; // 商家logo
$shop_info['shopname'] = htmlspecialchars($config['shop_name']); // 店铺名称
$shop_info['suppid'] = $suppid; // 商家名称
$shop_info['suppliername'] = htmlspecialchars($supp_info['supplier_name']); // 商家名称
$shop_info['userrank'] = htmlspecialchars($supp_info['rank_name']); // 商家等级
$shop_info['region'] = get_province_city($config['shop_province'], $config['shop_city']);
$shop_info['address'] = $config['shop_address'];
$shop_info['serviceqq'] = $config['qq'];
$shop_info['serviceww'] = $config['ww'];
$shop_info['serviceemail'] = $config['service_email'];
$shop_info['servicephone'] = $config['service_phone'];
$shop_info['createtime'] = gmdate('Y-m-d', $config['add_time']); // 商家创建时间
$suppid = (intval($suppid) > 0) ? intval($suppid) : intval($_GET['suppId']);
return $shop_info;
}
?>