生成卡号

/**
 * 生成一个16位卡号
 * @param int $num
 * @return \Illuminate\Http\JsonResponse
 */
public function applyCardNum($num = 1)
{
    switch ($num) {
        case '1':
            //大学生卡
            $type = '01';
            break;
        case '2':
            //普卡
            $type = '02';
            break;
        case '3':
            //银卡
            $type = '03';
            break;
        case '4':
            //金卡
            $type = '04';
            break;
        case '5':
            //白金卡
            $type = '05';
            break;
        case '6':
            //黑金卡
            $type = '06';
            break;
        case '7':
            //钻石卡
            $type = '07';
            break;
        case '8':
            //商户卡
            $type = '08';
            break;
        default:
            //普卡
            $type = '02';
    }
    return $this->card($type, $num);
}

/**
 * 生成16位卡号
 * @param $type
 * @return string
 */
public function card($type, $num)
{
    $card = substr(date('Y'), 2, 2);
    $card = $card . $type;
    $card = $card . rand(1000, 9999) . rand(1000, 9999) . rand(1000, 9999);

    $z = '/(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){3,}|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){3,})\d/';
    $z1 = '/([\d])\1{2,}/';
    if ($num > 5) {
        ;
        $chars = [0, 1, 2, 3, 5, 6, 7, 8, 9];
        $card = substr($card, 1, 4) . substr($card, -4) . str_replace(4, $chars[rand(0, 9)], substr($card, 5));
    }
    if (preg_match($z, $card)) {
        return $this->card($type, $num);
    } elseif (preg_match($z1, $card)) {
        return $this->card($type, $num);
    }
    if (CreditCardApplys::where(['card_num' => $card])->whereIn('application_results', [0, 1, 2, 4, 5, 6])->exists()|| User::where(['username' => $card])->exists()) {
        return $this->card($type, $num);
    } else {
        return $card;
    }
}

转载于:https://my.oschina.net/vioyangx/blog/2247031

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值