public function add() { if (false === $this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $num = $params['num']; if (!is_numeric($num)) { # 请输入数字 $this->error('请输入数字'); } if ($num <= 0) { # 数量必须大于0 $this->error('数量必须大于0'); } if ($num > 1000){ $this->error('一次批量生成1000条'); } $cardLog = Db::name('card_log')->order("card DESC")->find(); if ($cardLog){ $startNumber = $cardLog['card'] + 1; // 起始数字 }else{ $startNumber = 10000000; // 起始数字 } $increment = 1; //增量 $numbersBatch = $this->generateUniqueNumbers($startNumber, $increment, $num); foreach ($numbersBatch as $v){ $data[] = [ 'name' => $v, 'key' => getCardKey(), 'status' => 1, 'createtime' => time(), ]; } $this->model->insertAll($data); Db::name('card_log')->insert(['card' => $data[count($data)-1]['name'],'createtime' => time()]); $this->success("成功"); } function generateUniqueNumbers($startNumber, $increment, $count) { $uniqueNumbers = array(); for ($i = 0; $i < $count; $i++) { $number = $startNumber + $i * $increment; $uniqueNumbers[] = str_pad($number, 8, '0', STR_PAD_LEFT); } return $uniqueNumbers; }
例如:10000001,10000002,10000003
标签:name,卡密,重复,num,startNumber,increment,php,data,card From: https://www.cnblogs.com/ixiangang06/p/18385269