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