| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- /**
- * 兑换码仓库.
- * @author ningque
- * @date 2020-02-26 10:10:06
- */
- declare(strict_types=1);
- namespace App\Repositories;
- use App\Model\VipCodeModel;
- use Carbon\Carbon;
- use KZ2\Repositories\Model\Repository;
- class VipCodeRepository extends Repository
- {
- public function model()
- {
- return VipCodeModel::class;
- }
- /**
- * 生成兑换码
- * @return bool
- */
- public function makeCode($data)
- {
- $num = 0;
- $insert = [];
- while ($num < $data['count']) {
- $insert[$num] = [
- 'title' => $data['title'],
- 'remark' => $data['remark'],
- 'code' => strtoupper(random(6)),
- 'day' => $data['day'],
- 'type' => $data['type'],
- 'limit' => $data['limit'],
- 'expire' => $data['expire'],
- 'created_at' => Carbon::now()->toDateTimeString(),
- 'updated_at' => Carbon::now()->toDateTimeString(),
- ];
- $num++;
- }
- try {
- // 可能有重复的code,用try catch过滤.
- return VipCodeModel::query()->insert($insert);
- } catch (\Exception $e) {
- unset($insert);
- $this->makeCode($data);
- }
- }
- }
|