PHP 邀请码类
来源:互联网 发布:淘宝客怎样推广店铺 编辑:程序博客网 时间:2024/04/29 01:49
<?phpclass 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__); }}
0 0
- PHP 邀请码类
- php 生成邀请码
- 邀请码工具类
- 邀请码
- jhost邀请码 JHOST免费JSP、PHP主页空间
- Dreamhost邀请码
- jhost邀请码
- JHost邀请码
- JHost 邀请码
- jhost邀请码
- JHOST邀请码
- jhost邀请码 最新
- 关于cl邀请码
- 最新 jhost 邀请码
- jHost邀请码
- jhost邀请码
- jhost邀请码
- Google+ 邀请码
- 求二进制数中1的个数
- solr core 创建的另外一种方法
- 猜拳游戏GuessGame源码
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
- OC特有语法—Block
- PHP 邀请码类
- MS DOS 命令大全
- use sleep() to gain thread predictability
- iOS 视图封装及适配
- Java性能优化
- Optimal ArrayMultiplication Sequence
- Android开发之设置60s倒计时
- CocoaPods操作
- 纯css3实现的圆形进度条