首页  >  文章  >  php教程  >  邀请码的设计

邀请码的设计

WBOY
WBOY原创
2016-06-06 19:38:391701浏览

无详细内容 无 ?phpclass InvitationCodeService{ const CODE_LENGTH = 4; const WAIT_TIME = 10; //生成邀请码的间隔时间 const TIME_OUT = 3600; //半个小时 private $_obj = 1; //多个项目的情况下 private $_num = 100; private $_status = array(0 = "未

<?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__);
    }
}
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn