<?php
/**
 * Created by PhpStorm.
 * User: EricPan
 * Date: 2020/4/20
 * Time: 15:45
 */

namespace App\Repository;


class CreateCodeRepository
{
//控制区内的私有方法调用模型内方法
    public function getcode($model,$key ='code') {
        $code = $this->CreateCode();
        //把接收的邀请码再次返回给模型
        if ($this->recode($model,$code,$key)) {
            //不重复 返回验证码
            return $code;
        } else {
            //重复 再次生成
            while(true) {
                $this->getcode($model);
            }
        }
    }

    /**
     * 模型内生成邀请码 并返回控制器
     * @param $uid
     * @return string
     */
    private function CreateCode() {
        $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $rand = $code[rand(0,25)]
            .strtoupper(dechex(date('m')))
            .date('d').substr(time(),-5)
            .substr(microtime(),2,5)
            .sprintf('%02d',rand(0,99));
        for($a = md5( $rand, true ), $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV', $d = '', $f = 0; $f < 10;
            $g = ord( $a[ $f ] ),
            $d .= $s[ ( $g ^ ord( $a[ $f + 4 ] ) ) - $g & 0x1F ],
            $f++
        );
        return $d;
    }

    /**
     * 判断验证码是否存在数据库中
     */
    private function recode($model,$code,$key) {
        if ($model->where($key,'=',$code)->first()) {
            return false;
        }
        return true;
    }
}
Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐