代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
PHP
】
邀请码的设计
作者:
Alex
/ 发布于
2015/6/25
/
565
<?php class InvitationCodeService { const CODE_LENGTH = 4; const WAIT_TIME = 10; //生成邀请码的间隔时间 const TIME_OUT = 3600; //半个小时 private $_obj = 1; //多个项目的情况下 private $_num = 100; private $_status = array(0 => "未激活", 1 => "激活的", - 1 => "已过期"); public function crateCode ($uid) { $lastCode = InvitationCodeModel::instance()->getCodeByUid($uid); if (time() - $lastCode['create_time'] < self::WAIT_TIME && ! empty( $lastCode)) { $msg = "生成间隔时间不能少于" . self::WAIT_TIME . "秒"; return array('status' => false, 'message' => $msg); } $code = $this->getRandCode(); $params = array('uid' => $uid, 'create_time' => time(), 'obj' => $this->_obj, 'code' => $code); $rs = InvitationCodeModel::instance()->addCode($params); if ($rs > 0) { return array('status' => true, 'data' => $code, 'message' => "生成成功"); } else { return array('status' => false, 'data' => $code, 'message' => "生成失败"); } } //激活码 public function activate ($sendee, $code) { $code = InvitationCodeModel::instance()->getCodeByCode($code); if (empty($code)) { return array('status' => false, 'message' => "授权码错误!"); } if ($code['status'] < 0) { $msg = $this->_status[$code['status']]; return array('status' => false, 'message' => $msg); } if ($code['status'] == 1 && $code['sendee'] != $sendee) { return array('status' => false, 'message' => "此邀请码已被使用"); } if ($code['status'] == 1 && $code['sendee'] == $sendee) { $istimeout = $this->isTimeout($code); if (! $istimeout['status']) { return $istimeout; } return array('status' => true); } $v = array('sendee' => $sendee, 'status' => 1, 'active_time' => time()); $rs = InvitationCodeModel::instance()->saveCodeById($v, $code['id']); if ($rs > 0) { return array('status' => true, 'data' => $code, 'message' => "激活成功"); } else { return array('status' => false, 'data' => $code, 'message' => "激活失败"); } } private function isTimeout ($code) { if (time() - $code['active_time'] > self::TIME_OUT) { $v = array('status' => - 1); InvitationCodeModel::instance()->saveCodeById($v, $code['id']); return array('status' => false, 'message' => "授权码已失效,请重新获取授权!"); } return array('status' => true); } //检查是否授权 public function authrize ($sendee) { $code = InvitationCodeModel::instance()->getCodeBySendee($sendee); if (empty($code)) { return array('status' => false, 'message' => "未被授权进入"); } if ($code['status'] == - 1) { return array('satatus' => false, 'message' => "授权码已失效,请重新获取授权!"); } if (time() - $code['active_time'] > self::TIME_OUT) { $v = array('status' => - 1); InvitationCodeModel::instance()->saveCodeById($v, $code['id']); return array('status' => false, 'message' => "授权码已失效,请重新获取授权!"); } $data = self::TIME_OUT - (time() - $code['active_time']); return array('status' => true, 'data' => $data); } //生成随机数 private function getRandCode () { $str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXXYZ'; $code = ""; for ($i = 0; $i < self::CODE_LENGTH; $i ++) { $code .= $str{rand(0, 33)}; } //检查是否重复 $isexsit = InvitationCodeModel::instance()->getCodeByCode($code); if (! empty($isexsit)) { return $this->getRandCode(); } return $code; } public function getCodePage ($p, $uid) { $where = array('uid' => $uid); $rs = InvitationCodeModel::instance()->getCodePage($p, $this->_num, $where); $member = new MemberService(); foreach ($rs as $k => $v) { $user = $member->getMemberByUid($v['sendee']); $rs[$k]['sendee'] = $user['real_name']; $rs[$k]['status'] = $this->_status[$v['status']]; } return $rs; } } class InvitationCodeModel extends Db { private $_code = 'd_invitation_code'; public function getCodePage ($start, $num, $where) { return $this->getPage($start, $num, $this->_code, null, $where, 'id DESC'); } public function addCode ($params) { return $this->add($this->_code, $params); } public function saveCodeById ($v, $id) { return $this->update($this->_code, $v, array('id' => $id)); } public function getCodeByCode ($code) { return $this->getOne($this->_code, array('code' => $code)); } public function getCodeByUid ($uid) { $sql = "SELECT * FROM $this->_code WHERE uid=$uid AND status=0 ORDER BY id DESC LIMIT 1"; return $this->fetch($sql); } public function getCodeBySendee ($sendee) { $sql = "SELECT * FROM $this->_code WHERE sendee=$sendee AND status=1 ORDER BY id DESC LIMIT 1"; return $this->fetch($sql); } /** * * @return InvitationCodeModel */ public static function instance () { return parent::_instance(__CLASS__); } }
试试其它关键字
邀请码
同语言下
.
用net匹配并替换iOS标准的emoji表情符号
.
处理带Emoji表情的的字符串
.
获取微信昵称时 过滤特殊字符
.
通过判断上传文件的头字符来判断文件的类型
.
模拟百度URL加密解密算法
.
以太坊检查地址是否合法
.
实现crontab解析类
.
获取每个月的开始和结束时间
.
图片上传工具类
.
APP手机应用信息采集
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Alex
贡献的其它代码
(
27
)
.
字典 Dictionary 遍历
.
读取http文件保存到本地
.
代码实现文件打包
.
用户登录过滤例子
.
HashMap中的内容进行迭代输出
.
根据银行卡账号获取所属银行
.
将HtmlTable内容导出到Excel,使用NPOI组件
.
判断页面滚动条是否到底部
.
把函数绑定到事件上
.
根据计算机硬件码生成注册码
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3