免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3172 | 回复: 0
打印 上一主题 下一主题

邀请码的设计(转) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-24 09:56 |只看该作者 |倒序浏览
这个只是思路
  1. <?php
  2. class InvitationCodeService
  3. {
  4.     const CODE_LENGTH = 4;
  5.     const WAIT_TIME = 10; //生成邀请码的间隔时间
  6.     const TIME_OUT = 3600; //半个小时
  7.     private $_obj = 1; //多个项目的情况下
  8.     private $_num = 100;
  9.     private $_status = array(0 => "未激活", 1 => "激活的", - 1 => "已过期");
  10.     public function crateCode ($uid)
  11.     {
  12.         $lastCode = InvitationCodeModel::instance()->getCodeByUid($uid);
  13.         if (time() - $lastCode['create_time'] < self::WAIT_TIME && ! empty(
  14.         $lastCode)) {
  15.             $msg = "生成间隔时间不能少于" . self::WAIT_TIME . "秒";
  16.             return array('status' => false, 'message' => $msg);
  17.         }
  18.         $code = $this->getRandCode();
  19.         $params = array('uid' => $uid, 'create_time' => time(),
  20.         'obj' => $this->_obj, 'code' => $code);
  21.         $rs = InvitationCodeModel::instance()->addCode($params);
  22.         if ($rs > 0) {
  23.             return array('status' => true, 'data' => $code, 'message' => "生成成功");
  24.         } else {
  25.             return array('status' => false, 'data' => $code,
  26.             'message' => "生成失败");
  27.         }
  28.     }
  29.     //激活码
  30.     public function activate ($sendee, $code)
  31.     {
  32.          
  33.         $code = InvitationCodeModel::instance()->getCodeByCode($code);
  34.         if (empty($code)) {
  35.             return array('status' => false, 'message' => "授权码错误!");
  36.         }
  37.         if ($code['status'] < 0) {
  38.             $msg = $this->_status[$code['status']];
  39.             return array('status' => false, 'message' => $msg);
  40.         }
  41.         if ($code['status'] == 1 && $code['sendee'] != $sendee) {
  42.             return array('status' => false, 'message' => "此邀请码已被使用");
  43.         }
  44.         if ($code['status'] == 1 && $code['sendee'] == $sendee) {
  45.             $istimeout = $this->isTimeout($code);
  46.             if (! $istimeout['status']) {
  47.                 return $istimeout;
  48.             }
  49.             return array('status' => true);
  50.         }
  51.         $v = array('sendee' => $sendee, 'status' => 1, 'active_time' => time());
  52.         $rs = InvitationCodeModel::instance()->saveCodeById($v, $code['id']);
  53.         if ($rs > 0) {
  54.             return array('status' => true, 'data' => $code, 'message' => "激活成功");
  55.         } else {
  56.             return array('status' => false, 'data' => $code,
  57.             'message' => "激活失败");
  58.         }
  59.     }
  60.     private function isTimeout ($code)
  61.     {
  62.         if (time() - $code['active_time'] > self::TIME_OUT) {
  63.             $v = array('status' => - 1);
  64.             InvitationCodeModel::instance()->saveCodeById($v, $code['id']);
  65.             return array('status' => false, 'message' => "授权码已失效,请重新获取授权!");
  66.         }
  67.         return array('status' => true);
  68.     }
  69.     //检查是否授权
  70.     public function authrize ($sendee)
  71.     {
  72.         $code = InvitationCodeModel::instance()->getCodeBySendee($sendee);
  73.         if (empty($code)) {
  74.             return array('status' => false, 'message' => "未被授权进入");
  75.         }
  76.         if ($code['status'] == - 1) {
  77.             return array('satatus' => false, 'message' => "授权码已失效,请重新获取授权!");
  78.         }
  79.         if (time() - $code['active_time'] > self::TIME_OUT) {
  80.             $v = array('status' => - 1);
  81.             InvitationCodeModel::instance()->saveCodeById($v, $code['id']);
  82.             return array('status' => false, 'message' => "授权码已失效,请重新获取授权!");
  83.         }
  84.         $data = self::TIME_OUT - (time() - $code['active_time']);
  85.         return array('status' => true, 'data' => $data);
  86.     }
  87.     //生成随机数
  88.     private function getRandCode ()
  89.     {
  90.         $str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXXYZ';
  91.         $code = "";
  92.         for ($i = 0; $i < self::CODE_LENGTH; $i ++) {
  93.             $code .= $str{rand(0, 33)};
  94.         }
  95.         //检查是否重复
  96.         $isexsit = InvitationCodeModel::instance()->getCodeByCode($code);
  97.         if (! empty($isexsit)) {
  98.             return $this->getRandCode();
  99.         }
  100.         return $code;
  101.     }
  102.     public function getCodePage ($p, $uid)
  103.     {
  104.         $where = array('uid' => $uid);
  105.         $rs = InvitationCodeModel::instance()->getCodePage($p, $this->_num,
  106.         $where);
  107.         $member = new MemberService();
  108.         foreach ($rs as $k => $v) {
  109.             $user = $member->getMemberByUid($v['sendee']);
  110.             $rs[$k]['sendee'] = $user['real_name'];
  111.             $rs[$k]['status'] = $this->_status[$v['status']];
  112.         }
  113.         return $rs;
  114.     }
  115. }
  116. class InvitationCodeModel extends Db
  117. {
  118.     private $_code = 'd_invitation_code';
  119.     public function getCodePage ($start, $num, $where)
  120.     {
  121.         return $this->getPage($start, $num, $this->_code, null, $where,
  122.         'id DESC');
  123.     }
  124.     public function addCode ($params)
  125.     {
  126.         return $this->add($this->_code, $params);
  127.     }
  128.     public function saveCodeById ($v, $id)
  129.     {
  130.         return $this->update($this->_code, $v, array('id' => $id));
  131.     }
  132.     public function getCodeByCode ($code)
  133.     {
  134.         return $this->getOne($this->_code, array('code' => $code));
  135.     }
  136.     public function getCodeByUid ($uid)
  137.     {
  138.         $sql = "SELECT * FROM $this->_code WHERE uid=$uid AND status=0 ORDER BY id DESC LIMIT 1";
  139.         return $this->fetch($sql);
  140.     }
  141.     public function getCodeBySendee ($sendee)
  142.     {
  143.         $sql = "SELECT * FROM $this->_code WHERE sendee=$sendee AND status=1 ORDER BY id DESC LIMIT 1";
  144.         return $this->fetch($sql);
  145.     }
  146.     /**
  147.      *
  148.      * @return InvitationCodeModel
  149.      */
  150.     public static function instance ()
  151.     {
  152.         return parent::_instance(__CLASS__);
  153.     }
  154. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP