/**
* 处理好友邀请
* @UserFunction(method = GET|POST)
* @CheckLogin
*/
function process_request_friend(Integer $id, Boolean $is_access)
{
    $currentUserId = getCurrentUserId();
    $db = new DataBase(DB_DNS, DB_USER, DB_PASSWORD);
    $my_friend_status = $db->fetch('SELECT `status` FROM `friend` WHERE `user_id` = ? AND `friend_id` = ?', $currentUserId, $id);
    $other_friend_status = $db->fetch('SELECT `status` FROM `friend` WHERE `user_id` = ? AND `friend_id` = ?', $id, $currentUserId);
    if ($my_friend_status && $other_friend_status && $my_friend_status['status'] == 3 && $other_friend_status['status'] == 2) {
        if ($is_access->val) {
            $db->exec('UPDATE `friend` SET `status` = 1 WHERE `user_id`=? AND `friend_id`=?;', $currentUserId, $id);
            $db->exec('UPDATE `friend` SET `status` = 1 WHERE `user_id`=? AND `friend_id`=?;', $id, $currentUserId);
            $user_name_1 = $db->fetchColumn('SELECT `username` FROM `user` WHERE `id` = ?', $currentUserId);
            $user_name_2 = $db->fetchColumn('SELECT `username` FROM `user` WHERE `id` = ?', $id);
            //向融云IM server发送消息
            //$other_id = $id->val;
            ServerAPI::getInstance()->messagePublish($currentUserId, array($id->val), 'RC:InfoNtf', json_encode(array('message' => '你已添加了' . $user_name_1 . ',现在可以开始聊天了。')));
            ServerAPI::getInstance()->messagePublish($id->val, array($currentUserId), 'RC:InfoNtf', json_encode(array('message' => '你已添加了' . $user_name_2 . ',现在可以开始聊天了。')));
        } else {
            $db->exec('DELETE FROM `friend` WHERE `user_id`=? AND `friend_id`=?;', $currentUserId, $id);
            $db->exec('UPDATE `friend` SET `status` = 4 WHERE `user_id`=? AND `friend_id`=?;', $id, $currentUserId);
        }
    } else {
        throw new ProException('unknow error', 306);
    }
}
/**
* 获取token
* @UserFunction(method = GET)
* @CheckLogin
*/
function token()
{
    $db = new DataBase(DB_DNS, DB_USER, DB_PASSWORD);
    $user = $db->fetch('SELECT `id`, `email`, `username`, `portrait` FROM `user` WHERE `id` = ?', getCurrentUserId());
    if ($user) {
        $token = ServerAPI::getInstance()->getToken($user['id'], $user['username'], $user['portrait']);
        if (!$token) {
            throw new Exception('API Server Error');
        }
        if ($token->code != 200) {
            throw new ProException($token->errorMessage, $token->code);
        } else {
            unset($token->code);
            unset($token->userId);
            return $token;
        }
    } else {
        throw new ProException('user not found', 108);
    }
}