初始化项目

This commit is contained in:
2026-09-01 22:15:50 +08:00
commit b5a26a8848
11217 changed files with 1710987 additions and 0 deletions
@@ -0,0 +1,3 @@
/vendor
.idea
composer.lock
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Dashing.Li
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,126 @@
# think-generator
**thinkphp6 随机生成手机号、身份证号、邮箱地址、昵称等测试数据**
**特此申明:本类的初衷是生成测试数据,不可进行任何违法行为,不可进行任何违法行为,不可进行任何违法行为!!!**
## Installation
通过在composer.json中配置安装:
```php
composer require dashingunique/think-generator
```
or
```php
// composer.json
composer install
// composer update
composer update
```
## Usage
**
## 生成手机号码
**
```php
$generator = (new Generator($this->app))->generator('mobile');
//随机生成一个手机号
$generator->get(); //13611111111
//随机生成一个号段为182的手机号
$generator->prefix(182)->get();//18211111111
//允许13,14,15,17,18,19号段的手机号生成(从中生成一个号码段)
$generator->allow([3, 4, 5, 7, 8, 9])->get();//13011111111
//获取多个手机号
$generator->limit(10)->get();
//若同时存在prefix 和 allow prefix强制覆盖allow
$generator->prefix(182)->allow([3, 4, 5, 7, 8, 9])->limit(10)->get();//18211111111
```
## 生成邮箱地址
**
```php
$generator = (new Generator($this->app))->generator('email');
//随机生成一个邮箱地址
$generator->get();//12345678@qq.com
//随机生成一个前缀为123456789的邮箱
$generator->prefix(123456789)->get();//123456789@163.com
//随机生成一个后缀为@qq.com的邮箱
$generator->suffix('@qq.com')->get();//12345678@qq.com
//生成一个指定长度的邮箱地址
$generator->length(6)->get();//123456@qq.com
//获取多个邮箱
$generator->limit(10)->get();
```
### 生成身份证号码
```php
$generator = (new Generator($this->app))->generator('identity');
//随机生成一个身份证号码
$generator->get();
//随机生成一个指定性别的身份证号码(1男性 2女性)
$generator->sex(1)->get();
//随机生成一个北京市的身份证号码
$generator->province('北京市')->get();
//随机生成一个省份城市的身份证号码
$generator->city('成都市')->get();
//随机生成一个地区的身份证号码
$generator->city('高新区')->get();
//随机生成一个指定出生日期的身份证号码
$generator->birthday('2000-01-01')->get();
//获取多个身份证号码
$generator->limit(10)->get();
```
## 生成昵称
```php
$generator = (new Generator($this->app))->generator('nickname');
//随机生成一个用户昵称
$generator->get();
//随机生成一个指定姓氏的用户昵称
$generator->surname('张')->get();//张三
//随机生成一个指定姓氏类型的用户昵称(1单姓 2复姓)
$generator->compound(2)->get();//上官三
//随机生成一个指定性别的用户昵称(1男性 2女性)
$generator->sex(2)->get();//张美
//获取一个指定长度的用户昵称
$generator->length(3)->get();//张大麻子
//获取多个用户昵称
$generator->limit(10)->get();
```
```
## 后续
增加银行卡号的生成
验证生成的数据为一性
增加数据驱动类型:本地文件、Redis、数据库
@@ -0,0 +1,26 @@
{
"name": "dashingunique/think-generator",
"description": "thinkphp6 random generation of mobile phone number, ID number, mailbox address, nickname and other test data.",
"type": "src",
"license": "MIT",
"authors": [
{
"name": "dashing unique",
"email": "1107842285@qq.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=7.0"
},
"autoload": {
"files": [
"src/generator/Helper.php"
],
"psr-4": {
"dashing\\": "src"
}
},
"extra": {
}
}
@@ -0,0 +1,155 @@
<?php
namespace dashing\generator;
abstract class BaseUtil
{
const MAX = 100; //最大生成条数
const MALE = 1; //性别:1男
const FEMALE = 2; //性别:2女
/**
* @var int 限制条数
*/
protected $limit = 1;
/**
* @var array 最近参数
*/
protected $padding = [];
/**
* @var array 属性信息
*/
protected $attributes;
/**
* @var mixed 性别编号
*/
protected $sex;
/**
* 生成条数
* @param $limit
* @return $this
*/
public function limit($limit)
{
$limit = (int)$limit ?: 1;
$limit = ($limit >= self::MAX) ? self::MAX : $limit;
$this->limit = $limit;
return $this;
}
/**
* @return mixed
*/
public function getLimit()
{
return $this->limit;
}
/**
* 获取生成的省份证号码
* @return array|string
*/
public function get()
{
$limit = $this->getLimit();
if ($limit > 1) {
$data = [];
for ($i = 0; $i < $this->getLimit(); $i++) {
$data[] = $this->generate();
}
return $data;
} else {
return $this->generate();
}
}
/**
* @return mixed 生成信息
*/
public abstract function generate();
/**
* @param $name
* @param $arguments
* @return $this
*/
public function __call($name, $arguments)
{
if (!is_null($arguments) && isset($arguments[0])) {
$this->setAttributes($name, $arguments[0]);
}
return $this;
}
/**
* @param $key
* @param $value
* @return $this
*/
protected function setAttributes($key, $value)
{
if ($this->isPadding($key)) {
$this->attributes[$key] = $value;
}
return $this;
}
/**
* 是否是额外参数
* @param $value
* @return bool
*/
public function isPadding($value): bool
{
if (in_array($value, $this->getPadding())) {
return false;
}
return true;
}
/**
* @return array
*/
public function getPadding(): array
{
return $this->padding;
}
/**
* 获取性别信息
* @return float|int
*/
public function getSex()
{
$this->setSex();
return $this->sex;
}
/**
* 设置性别
* @return $this
*/
protected function setSex()
{
// sex is null , random 1 - 8
if (!isset($this->attributes['sex'])) {
$sex = random_int(1, 8);
} elseif ($this->attributes['sex'] == self::MALE) {
// sex is male
$sex = 2 * random_int(1, 4) - 1;
} else {
// sex is female
$sex = 2 * random_int(1, 4);
}
$this->sex = $sex;
return $this;
}
}
@@ -0,0 +1,35 @@
<?php
/**
* | Author: dashingUnique <https://github.com/dashingunique>
* +----------------------------------------------------------------------
* | Description: 随机生成器
* +----------------------------------------------------------------------
*/
namespace dashing\generator;
use think\Manager;
class Generator extends Manager
{
protected $namespace = '\\dashing\\generator\\util\\';
/**
* 生成器
* @param string $name
* @return mixed
*/
public function generator(string $name)
{
return $this->driver(ucfirst($name));
}
/**
* 默认驱动
* @return string|null
*/
public function getDefaultDriver()
{
return null;
}
}
@@ -0,0 +1,12 @@
<?php
if (!function_exists('generatorPath')) {
/**
* 获取generator路径
* @return string
*/
function generatorPath() {
return __DIR__;
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
@@ -0,0 +1,36 @@
[
"北京市",
"天津市",
"河北省",
"山西省",
"内蒙古自治区",
"辽宁省",
"吉林省",
"黑龙江省",
"上海市",
"江苏省",
"浙江省",
"安徽省",
"福建省",
"江西省",
"山东省",
"河南省",
"湖北省",
"湖南省",
"广东省",
"广西壮族自治区",
"海南省",
"重庆市",
"四川省",
"贵州省",
"云南省",
"西藏自治区",
"陕西省",
"甘肃省",
"青海省",
"宁夏回族自治区",
"新疆维吾尔自治区",
"台湾省",
"香港特别行政区",
"澳门特别行政区"
]
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,145 @@
<?php
/**
* | Author: dashingUnique <https://github.com/dashingunique>
* +----------------------------------------------------------------------
* | Description: 邮箱地址生成器
* +----------------------------------------------------------------------
*/
namespace dashing\generator\util;
use dashing\generator\BaseUtil;
use dashing\library\Collection;
class Email extends BaseUtil
{
/**
* @var string 邮箱前缀
*/
protected $prefix;
// 字符集合
protected $codeSet = '0123456789abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
// 位数
protected $length = 8;
/**
* @var string[] 常用邮箱后缀
*/
protected $mailboxSuffix = [
'@qq.com',
'@163.com',
'@sina.com',
'@sohu.com',
'@gmail.com',
'@yahoo.com',
'@msn.com',
'@hotmail.com',
'@aol.com',
'@ask.com',
'@live.com',
'@0355.net',
'@163.net',
'@263.net',
'@3721.net',
'@yeah.net',
'@googlemail.com',
'@mail.com',
'@sina.com',
'@21cn.com',
'@yahoo.com.cn',
'@etang.com',
'@eyou.com',
'@56.com',
'@x.cn',
'@chinaren.com',
'@sogou.com',
'@citiz.com'
];
/**
* @var string 邮箱后缀
*/
protected $suffix;
/**
* 获取邮箱前缀
* @return string
*/
public function getPrefix()
{
$this->setPrefix();
return $this->prefix;
}
/**
* 设置邮箱前缀
* @return $this
*/
public function setPrefix()
{
$regexp = "/^[a-zA-Z0-9]*$/";
if (isset($this->attributes['prefix']) && preg_match($regexp, $this->attributes['prefix'])) {
$this->prefix = $this->attributes['prefix'];
} else {
$characters = str_split($this->codeSet);
$prefix = '';
for ($i = 0; $i < $this->getLength(); $i++) {
$value = $characters[rand(0, count($characters) - 1)];
if (empty($i) && empty($value)) {
$prefix .= rand(1,9);
} else {
$prefix .= $value;
}
}
$this->prefix = $prefix;
}
return $this;
}
/**
* 获取邮箱前缀长度
* @return mixed
*/
public function getLength()
{
if (isset($this->attributes['length'])) {
$this->length = $this->attributes['length'];
}
return $this->length;
}
/**
* 获取邮箱后缀
* @return string
*/
public function getSuffix()
{
$this->setSuffix();
return $this->suffix;
}
/**
* 设置邮箱后缀
* @return $this
*/
public function setSuffix()
{
$mailboxSuffix = new Collection($this->mailboxSuffix);
if (isset($this->attributes['suffix']) && in_array($this->attributes['suffix'], $mailboxSuffix->all())) {
$this->suffix = $this->attributes['suffix'];
} else {
$this->suffix = $mailboxSuffix->random();
}
return $this;
}
/**
* @return mixed 生成信息
*/
public function generate()
{
return $this->getPrefix() . $this->getSuffix();
}
}
@@ -0,0 +1,264 @@
<?php
/**
* | Author: dashingUnique <https://github.com/dashingunique>
* +----------------------------------------------------------------------
* | Description: 身份证号码生成器
* +----------------------------------------------------------------------
*/
namespace dashing\generator\util;
use dashing\generator\BaseUtil;
use dashing\library\Collection;
use InvalidArgumentException;
class Identity extends BaseUtil
{
/**
* 省份集合
* @var Collection
*/
protected $region;
/**
* 城市集合
* @var Collection
*/
protected $cities;
/**
* @var mixed 性别编号
*/
protected $sex;
/**
* @var string 生日编号
*/
protected $birthday;
/**
* @var mixed 省份
*/
protected $province;
/**
* @var mixed 市区
*/
protected $city;
/**
* @var array|Collection|mixed
*/
private $district;
public function __construct()
{
parent::__construct();
$this->loadRegion();
}
/**
* 获取省份
* @return array|bool|int|Collection|mixed
*/
public function getProvince()
{
$this->setProvince();
return $this->province;
}
/**
* 获取城市
* @return int|mixed
* @throws \Exception
*/
public function getCity()
{
$this->setCity();
return $this->city;
}
/**
* @return mixed
* @throws \Exception
*/
public function getDistrict()
{
$this->setDistrict();
return $this->district;
}
/**
* 获取生日日期编号
* @return string
*/
protected function getBirthday()
{
$this->setBirthday();
return $this->birthday;
}
/**
* 加载省份信息
*/
protected function loadRegion()
{
if (empty($this->region)) {
$file = file_get_contents(generatorPath().'/data/region.json');
$this->region = uniqueCollection(json_decode($file,
true));
}
return $this;
}
/**
* @return $this
*/
protected function setBirthday()
{
//random Datatime
if (!isset($this->attributes['birthday'])) {
$startDate = mktime(0, 0, 0, 1, 1, 1920);
$year = date('Y');
$month = date('m');
$day = date('d');
$endDate = mktime(0, 0, 0, $month, $day, $year);
$birth = mt_rand($startDate, $endDate);
$datetime = date('Ymd', $birth);
} else {
list($year, $month, $day) = explode('-', $this->attributes['birthday']);
if (!checkdate($month, $day, $year)) {
throw new InvalidArgumentException('Invalided datetime');
}
$datetime = $year.$month.$day;
}
$this->birthday = $datetime;
return $this;
}
/**
* 设置省份信息
* @return $this
*/
protected function setProvince()
{
if (isset($this->attributes['province'])) {
$province = $this->region->where('name', $this->attributes['province'])->where('level', 1)->first();
} else {
$province = $this->region->where('level', 1)->random();
}
$this->province = $province;
return $this;
}
/**
* 设置城市信息
* @return $this
*/
protected function setCity()
{
$province = $this->getProvince();
try {
$list = $this->region->where('parent_id', $province['id'])->where('level', 2);
if (isset($this->attributes['city'])) {
$city = $list->where('name', $this->attributes['city'])->first();
} else {
$city = $list->random();
}
} catch (\Exception $exception) {
$city = $province;
}
$this->city = $city;
return $this;
}
/**
* 设置区域信息
* @return $this
* @throws \Exception
*/
protected function setDistrict()
{
$city = $this->getCity();
try {
$list = $this->region->where('parent_id', $city['id'])->where('level', 3);
if (isset($this->attributes['district'])) {
$district = $list->where('name', $this->attributes['district'])->first();
} else {
$district = $list->random();
}
} catch (\Exception $exception) {
$district = $city;
}
$this->district = $district;
return $this;
}
/**
* 计算省份证最后一位
* @param $base
* @return string
*/
protected function getSuffixD($base)
{
if (strlen($base) <> 17) {
die('Invalid Length');
}
// 权重
$factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
$sums = 0;
for ($i = 0; $i < 17; $i++) {
$sums += substr($base, $i, 1) * $factor[$i];
}
$mods = $sums % 11; //10X98765432
$return = '';
switch ($mods) {
case 0:
$return = '1';
break;
case 1:
$return = '0';
break;
case 2:
$return = 'X';
break;
case 3:
$return = '9';
break;
case 4:
$return = '8';
break;
case 5:
$return = '7';
break;
case 6:
$return = '6';
break;
case 7:
$return = '5';
break;
case 8:
$return = '4';
break;
case 9:
$return = '3';
break;
case 10:
$return = '2';
break;
}
return $return;
}
/**
* 生成身份证号码
* @return string
*/
public function generate()
{
$districtCode = $this->getDistrict()['adcode'];
$baseCode = $districtCode.$this->getBirthday().mt_rand(0, 9).mt_rand(0, 9).$this->getSex();
return $baseCode.$this->getSuffixD($baseCode);
}
}
@@ -0,0 +1,91 @@
<?php
/**
* | Author: dashingUnique <https://github.com/dashingunique>
* +----------------------------------------------------------------------
* | Description: 手机号码生成器
* +----------------------------------------------------------------------
*/
namespace dashing\generator\util;
use dashing\generator\BaseUtil;
use think\helper\Arr;
class Mobile extends BaseUtil
{
/**
* @var string 手机号前缀:例如136
*/
protected $prefix = '';
/**
* @var array 允许的号码段
*/
protected $allowPrefix = [];
/**
* 获取手机号前缀
* @return string
*/
public function getPrefix()
{
$this->setPrefix();
return $this->prefix;
}
/**
* 设置手机号前缀
* @return $this
*/
public function setPrefix()
{
if (isset($this->attributes['prefix'])) {
$this->prefix = $this->attributes['prefix'];
} else {
$allowPrefix = $this->getAllowPrefix();
$this->prefix = 1 . Arr::random($allowPrefix) . mt_rand(0, 9);
}
return $this;
}
/**
* 获取允许的号码段(手机号第二位)
* @return array
*/
public function getAllowPrefix(): array
{
$this->setAllowPrefix();
return $this->allowPrefix;
}
/**
* 设置允许的号码段(手机号第二位)
* @param array $allow
* @return $this
*/
public function setAllowPrefix(array $allow = [])
{
if (isset($this->attributes['allow'])) {
if (is_string($this->attributes['allow']) && strpos($this->attributes['allow'], ',')) {
$this->allowPrefix = explode(',', $this->attributes['allow']);
} elseif (is_array($this->attributes['allow'])) {
$this->allowPrefix = $this->attributes['allow'];
}
} else {
if (empty($allow)) {
$allow = [3, 4, 5, 7, 8, 9];
}
$this->allowPrefix = $allow;
}
return $this;
}
/**
* 生成手机号码
* @return string
*/
public function generate()
{
return $this->getPrefix() . str_pad(rand(0,99999999),8,0,STR_PAD_LEFT);
}
}
@@ -0,0 +1,136 @@
<?php
/**
* | Author: dashingUnique <https://github.com/dashingunique>
* +----------------------------------------------------------------------
* | Description: 名称生成器
* +----------------------------------------------------------------------
*/
namespace dashing\generator\util;
use dashing\generator\BaseUtil;
class Nickname extends BaseUtil
{
/**
* @var string 姓氏
*/
protected $surname;
/**
* @var string 名字
*/
protected $lastName;
/**
* @var int 姓氏类型:1单姓 2复姓
*/
protected $compound;
/**
* @var int 名长
*/
protected $nameLength;
/**
* 获取复姓长度
* @return int
*/
public function getCompound()
{
if (isset($this->attributes['compound']) && $this->attributes['compound'] <= 2) {
$this->compound = $this->attributes['compound'];
} else {
$this->compound = mt_rand(1, 2);
}
return $this->compound;
}
/**
* @return string
*/
public function getSurname()
{
$this->setSurname();
return $this->surname;
}
/**
* @return $this
*/
public function setSurname()
{
if (isset($this->attributes['surname'])) {
$this->surname = $this->attributes['surname'];
} else {
$file = file_get_contents(generatorPath().'/data/surname.json');
$surnameData = uniqueCollection(json_decode($file, true));
$surname = $surnameData->where('length', $this->getCompound())->random();
$this->surname = $surname['surname'];
}
return $this;
}
/**
* 获取名字
* @return string
*/
public function getName()
{
$this->setName();
return $this->lastName;
}
/**
* @return Nickname
*/
protected function setSex(): Nickname
{
if (isset($this->attributes['sex'])) {
$this->sex = $this->attributes['sex'];
} else {
$this->sex = mt_rand(self::MALE, self::FEMALE);
}
return $this;
}
/**
* 设置名字信息
* @return $this
*/
public function setName()
{
$file = file_get_contents(generatorPath().'/data/name.json');
$nameData = uniqueCollection(json_decode($file, true));
$nameCollect = $nameData->where('sex', $this->getSex());
$name = '';
for ($i = 0; $i < $this->getLength(); $i++) {
$nameItem = $nameCollect->random();
$name .= $nameItem['name'];
}
$this->lastName = $name;
return $this;
}
/**
* 获取名字长度
* @return $this
*/
public function getLength()
{
if (isset($this->attributes['length'])) {
$this->nameLength = $this->attributes['length'];
} else {
$this->nameLength = mt_rand(1, 2);
}
return $this->nameLength;
}
/**
* @return mixed 生成信息
*/
public function generate()
{
return $this->getSurname().$this->getName();
}
}