예제 #1
0
/**
 * 添加/编辑客服信息的提交
 */
function action_insert_update()
{
    $user = $GLOBALS['user'];
    $_CFG = $GLOBALS['_CFG'];
    $_LANG = $GLOBALS['_LANG'];
    $smarty = $GLOBALS['smarty'];
    $db = $GLOBALS['db'];
    $ecs = $GLOBALS['ecs'];
    $user_id = $_SESSION['user_id'];
    $user_id = intval($_POST['user_id']);
    /* 取得客服id */
    $cus_id = intval($_POST['cus_id']);
    $customer = array('supp_id' => -1, 'user_id' => $_POST['user_id'], 'of_username' => $_POST['of_username'], 'cus_name' => $_POST['cus_name'], 'cus_password' => $_POST['cus_password'], 'cus_type' => $_POST['cus_type'], 'cus_enable' => $_POST['cus_enable'], 'cus_desc' => $_POST['cus_desc']);
    // 判断密码是否为空
    if (empty($customer['of_username'])) {
        sys_msg($_LANG['error_of_username_empty']);
    }
    // 判断客服名称是否为空
    if (empty($customer['cus_name'])) {
        sys_msg($_LANG['error_cus_name_empty']);
    }
    // 检查聊天系统用户名是否已经绑定了其他管理员账户
    if (check_of_username_binding($customer['of_username'], $customer['user_id'])) {
        sys_msg($_LANG['error_of_username_binding']);
    } else {
        // 用户不存在则需要判断密码是否为空
        if (!check_of_username_exist($customer['of_username'])) {
            // 判断密码是否为空
            if (empty($customer['cus_password'])) {
                sys_msg($_LANG['error_password_empty']);
            }
        }
        // 创建活更新聊天系统用户
        $create_success = create_of_user($customer['of_username'], $customer['cus_password'], $customer['cus_name'], null, 10, -1);
        if (!$create_success) {
            sys_msg($_LANG['error_create_of_user']);
        }
    }
    if (empty($_POST['cus_id'])) {
        // 检查管理员账户是否存在
        if (check_user_id_exist($user_id)) {
            sys_msg($_LANG['error_user_id_exist']);
        }
        $customer['add_time'] = gmtime();
        /* insert */
        $db->autoExecute($ecs->table('chat_customer'), $customer, 'INSERT');
        /* log */
        admin_log(addslashes($customer['of_username']), 'add', 'chat_customer');
        /* 提示信息 */
        $links = array(array('href' => 'customer.php?act=add', 'text' => $_LANG['continue_add']), array('href' => 'customer.php?act=list', 'text' => $_LANG['back_list']));
        sys_msg($_LANG['add_success'], 0, $links);
    } else {
        // 检查管理员账户是否存在
        if (check_user_id_exist($user_id, $cus_id)) {
            sys_msg($_LANG['error_user_id_exist']);
        }
        /* update */
        $db->autoExecute($ecs->table('chat_customer'), $customer, 'UPDATE', "cus_id = '{$cus_id}'");
        /* log */
        admin_log(addslashes($customer['of_username']) . '[' . $cus_id . ']', 'edit', 'chat_customer');
        /* 提示信息 */
        $links = array(array('href' => 'customer.php?act=list&' . list_link_postfix(), 'text' => $_LANG['back_list']));
        sys_msg($_LANG['edit_success'], 0, $links);
    }
    /* 显示客服列表页面 */
    assign_query_info();
    $smarty->display('customer_info.htm');
}
예제 #2
0
/**
 * 创建用户信息,如果用户信息存在则更新
 * 
 * @param string $username 用户名
 * @param string $password 密码
 * @param string $name 昵称
 * @param string $email 邮箱
 * @param string $type 用户类型
 * @param string $shop_id 店铺ID
 * @return boolean
 */
function create_of_user($username = null, $password = null, $name = null, $email = null, $type = 10, $shop_id = -1)
{
    $_CFG = $GLOBALS['_CFG'];
    $of_username = $_CFG['chat_server_admin_username'];
    $of_password = $_CFG['chat_server_admin_password'];
    $of_ip = $_CFG['chat_server_ip'];
    $of_port = $_CFG['chat_server_port'];
    $of_url = get_of_url($of_ip, $of_port);
    if ($username == null || strlen($username) == 0) {
        return false;
    }
    // 判断用户是否已经存在
    $exist = check_of_username_exist($username);
    if ($exist) {
        if ($password == null || strlen($password) == 0) {
            $password = null;
        }
        $url = $of_url . '/plugins/userService/users/' . $username;
        $method = 'PUT';
    } else {
        if ($password == null || strlen($password) == 0) {
            return false;
        }
        $url = $of_url . '/plugins/userService/users';
        $method = 'POST';
    }
    $user = new User();
    $user->username = $username;
    $user->password = $password;
    $user->name = $name;
    $user->email = $email;
    $user->properties = array(new Property('type', $type), new Property('shop_id', $shop_id));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    // 设置HTTP头
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
    // 授权验证
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $of_username . ":" . $of_password);
    // 设置可以读取返回值
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // post提交方式
    // 	curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    // 提交的数据
    // curl_setopt ( $ch, CURLOPT_POSTFIELDS, array('username'=>$username, 'password'=>$password, 'name'=>$name, 'email'=>$email) );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $user->asXML());
    // 运行curl
    $result = trim(curl_exec($ch));
    // 关闭
    curl_close($ch);
    if (strpos($result, '201 Created') >= 0) {
        return true;
    } else {
        if (strpos($result, '400 Bad Request') >= 0) {
            return false;
        } else {
            if (strpos($result, 'UserAlreadyExistsException') >= 0) {
                return true;
            } else {
                return false;
            }
        }
    }
}
예제 #3
0
파일: chat.php 프로젝트: seanguo166/yinoos
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);
        // 获取店铺信息
        $supp_id = null;
        $tab_items[] = array("id" => "chat_goods", "name" => "咨询商品");
        // 客服+售前
        $cus_types = CUSTOMER_SERVICE . ',' . CUSTOMER_PRE;
    }
    if (!empty($_REQUEST['chat_supp_id'])) {
        /* 店铺信息 */
        $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 (!empty($_REQUEST['chat_order_id'])) {
        /* 咨询订单信息 */
        require 'includes/lib_order.php';
        $order_id = $_REQUEST['chat_order_id'];
        // 获取商品和店铺信息
        $goods_id = null;
        $supp_id = null;
        $order = order_info($order_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 (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');
}