花了一年時間作了個網盤下載站點,有興趣的朋友能夠看看站名:去轉盤網
其中有個需求是想模擬百度網盤的邀請好友註冊功能,想了好久果真
"皇天不負有心人"
,終於整理出個思路,而且附上代碼,但願各位網友多多吐槽。個人思路大致是用網絡範圍內惟一的字符串uuid爲標識去邀請好友,邀請者給個狀態位
1
表明邀請者,並給他綁定個惟一uuid。被邀請者給個標識位
0
表明被邀請者,若是他註冊成功,則能夠根據邀請連接得到uuid去查詢數據庫找到邀請者,找到後給予相應積分便可,代碼以下:
java
public String invite(){ int invteOrBeInted=1;//邀請人爲1,被邀請爲0 String username=null; String temp=getParameter("username"); if(temp!=null){ try { username = new String(temp.getBytes("ISO-8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } //username=((User)session.get(CodeConst.USERINSESSION)).getUsername(); if(username==null){ message="沒有權限邀請用戶"; return ERROR; } RegisterService registerService = ServiceFactory.getRegisterService(); UserDao userDao=null; try { userDao = registerService.getUserDao(); } catch (Exception e1) { e1.printStackTrace(); } @SuppressWarnings("rawtypes") java.util.List list=null; User u=null; try { list=userDao.findByUserName(username); } catch (Exception e) { e.printStackTrace(); } if(list!=null&&list.size()>0){ u=(User)list.get(0); } if(u.getInviteCode()!=null&&u.getInvitedOrBeInvited()==1){ //已經邀請了 inviteCode=u.getInviteCode(); } else{ inviteCode=UUID.randomUUID().toString();//uuid的惟一性 } String goodNews="網盤分享,網盤下載,這裏是去轉盤網。趕快點擊下面連接註冊吧,海量網盤資源與你共享"; String url=goodNews+getBase()+"registerManager/inviteRegister.action?inviteCode="+inviteCode; //註釋,這裏getBase()函數請本身實現,是得到 getHttpRequest().setAttribute("url",url);//發送給前臺 try { registerService.updateInviteMan(username, inviteCode,invteOrBeInted); //更新邀請人信息,注意inviteCode爲uuid是惟一的 //當用戶註冊成功以後根據inviteCode爲邀請人加分 } catch (Exception e) { e.printStackTrace(); } return SUCCESS; }