贴码了

来源:互联网 发布:龙江网络客服中心电话 编辑:程序博客网 时间:2024/05/17 04:20

使用了  nitrogen. erlang.

可用作内部交流沟通使用

login.erl: 未做重复用户名判断,

%% -*- mode: nitrogen -*-
-module (login).
-compile(export_all).
-include_lib("nitrogen_core/include/wf.hrl").

main() -> #template { file="./site/templates/login.html" }.

title() ->
    case wf:user() of
        undefined ->
            ok;
        _ ->
            wf:wire(#alert { text="已经登录" }),
            wf:redirect("coffee")
    end,
    "苦味".
username() ->
    wf:wire("obj('username').focus(); obj('username').select();"),
    [
    #textbox {id=username, text="" }
    ].
login()->
    wf:wire(loginButton, username, #validate { validators=[
        #is_required { text="请输入用户名" }
    ]}),
    [#button {id=loginButton,text="登录",postback=click}].
    
event(click)->
    case wf:user() of
        undefined ->
            wf:clear_user(),
            wf:user(wf:q(username));
        _ ->
            ok
    end,
    wf:redirect("coffee");
event(_) ->
    ok.


%% -*- mode: nitrogen -*-
-module (coffee).
-compile(export_all).
-include_lib("nitrogen_core/include/wf.hrl").

main() -> #template { file="./site/templates/CoffeeTime.html" }.

title() ->
    check(),
    wf:comet_global(fun() -> chat_loop() end, chatroom),
    wf:wire(#api { name=apiImage, tag=f1 }),
    ["苦味"].
users()->
    Elements = [
        "online: ",
        #span {text=wf:user()},
        "<br/><div id=user_names>",
        #panel { id=uu,text="",style="height:0px"},
        "</div>"
    ],   
    Elements.

check()->    
     case wf:user() of
        undefined ->
            wf:wire(#alert { text="未登录" }),
            wf:redirect("login");
        _ ->
            ok
    end.    
hr()->
[
    #hr{}
].
input()->
    wf:wire(sendmessage,messageTextBox,#validate { validators=[
        #is_required { text="请输入消息" }
    ]}),
    [
        #textarea { id=messageTextBox,actions=#event { type=enterkey, postback=click },text="",style="width:630px;height=30px;resize:none"}
    ].
file()->
[
    #flash{}
].

message()->
    wf:wire("obj('messageTextBox').focus(); obj('messageTextBox').select();"),
    [
        #button{id=sendmessage,text="发送消息",postback=click}
    ].
send()->
    [
    #upload{tag=sendfile,show_button=false}
    ].

faces()->
    [
        #link {body=#image {image="images/coffice.png"},postback=face,style="width:10px,height:10px"}
    ].

chat() ->
    [
    #panel {id=chats}
    ].
    
api_event(apiImage,_,Image)->
    Terms=["<img src=",$",Image,$","/><br/>"],
    wf:send_global(chatroom, {img,wf:user(),Terms});
api_event(_,_,_)->
    ok.
event(face)->
    wf:wire("faces();")
    ;
event(click) ->
    check(),
    Username = wf:user(),
    Message = wf:q(messageTextBox),
    case string:len(string:strip(Message))>0 of
        true ->
            wf:send_global(chatroom, {message, Username, Message}),
            wf:wire("obj('messageTextBox').focus();obj('messageTextBox').value='';");
        _ ->
            ok
    end;
event(_)->
    ok.
chat_loop() ->
    receive
        {message, Username, Message} ->
            {H,M,S}=time(),
            Terms = [
                #span { text=Username },"  ",
                #span { text=H},":",
                #span { text=M},":",
                #span { text=S},"  ",
                #span { text=Message ,class=message},"<br/>"
            ],    
            wf:insert_bottom(chats, Terms),
            wf:wire("var div = document.getElementById('chat');div.scrollTop = div.scrollHeight;"),
            wf:wire(".message:last",#effect { effect=highlight, speed=10000, options=[{color, "#ff9900"}] }),
            wf:wire("newmessage();"),
            Ch=wf:f("checkonline('~s');", [Username]),
            wf:wire(Ch),
            wf:flush();
        {file,Url} ->
            wf:flash(wf:f("~s",[Url])),        
            wf:wire("newmessage();"),
            wf:flush();
        {img,Username,Url} ->
            {H,M,S}=time(),
              Terms = [
                #span { text=Username },"  ",
                #span { text=H},":",
                #span { text=M},":",
                #span { text=S},"  ",
                Url,"<br/>"
            ],
            wf:insert_bottom(chats,Terms),
            wf:flush(),
            wf:wire("var div = document.getElementById('chat');div.scrollTop = div.scrollHeight;"),
            %%wf:wire("chats",#effect { effect=highlight, speed=1000, options=[{color, "#ff0000"}] }),
            wf:wire("newmessage();"),
            wf:flush()
    end,
    chat_loop().

start_upload_event(sendfile) ->
    ok.
finish_upload_event(_Tag, undefined, _, _) ->
    ok;
finish_upload_event(_Tag, FileName, LocalFileData, _) ->
    

    Ex=filename:extension(FileName),
    NewName=[LocalFileData,Ex],
    file:rename(LocalFileData,NewName),
    OkName=filename:basename(NewName),
    {ok,Myip}=inet:getiflist(),
    case check_image:image_type(FileName) of
        yes ->
            Terms=["<img src=",$","http://",hd(Myip),":8080/",OkName,$","/><br/>"],
            wf:send_global(chatroom, {img,wf:user(),Terms});
        no ->
            Terms=["<a href=",$","http://",hd(Myip),":8080/",OkName,$"," target=",$","_blank",$",">",FileName,"</a><br/>"],
            wf:send_global(chatroom, {file,Terms})
    end,
    ok.



部分javascript代码:

<script type="text/javascript">
    var step=0; // 后台输出是否有新信息数据
    var b=false;
    var _title=document.title;
    var timer=null;
    function blink()
     {
      step++
      if (step==3) {step=1}
      if (step==1) {document.title=_title}
      if (step==2) {document.title='【新信息】'}
    }
    function newmessage(){
        if(b==false){
            timer1 = setInterval(blink,500);
            b=true;
        }        
    }
    function clr()
    {
        clearInterval(timer1);
        document.title=_title;
        b=false;
    }
    function faces()
    {    
        var obj=document.getElementById("mfaces");
        if (obj.style.zIndex>1)
        {
        obj.style.zIndex=-1;}
        else
        {
        obj.style.zIndex=2;
        }
    }
    function clickimages(img)
    {
        faces();
        page.apiImage(img.src);        
    }
    function checkonline(username)
    {
        if(navigator.appName.indexOf("Explorer")>-1)  
            {
            vartext=document.getElementById("user_names").innerText;
            
            if (vartext.indexOf(username)==-1)
            {
                var b= document.createElement("b");
                b.innerText=username;
                document.getElementById("user_names").appendChild(b);
                var br = document.createElement("br");        
                document.getElementById("user_names").appendChild(br);    
            }
            
            }
        else  
            {
            vartext=document.getElementById("user_names").textContent;
            
            if (vartext.indexOf(username)==-1)
            {
                var b= document.createElement("b");
                b.textContent=username;
                document.getElementById("user_names").appendChild(b);
                var br = document.createElement("br");        
                document.getElementById("user_names").appendChild(br);    
            }
            
            }
      }
</script>


最后上两张运行图


附上csdn下载地址:

点击下载

原创粉丝点击