gamebackend/database/migrations/20190302094849_admin_user.php

63 lines
2.7 KiB
PHP

<?php
/**
* 后台用户迁移文件
* @author yupoxiong<i@yufuping.com>
*/
use think\Db;
use think\migration\Migrator;
use think\migration\db\Column;
class AdminUser extends Migrator
{
public function change()
{
$table = $this->table('admin_user', ['comment' => '后台用户', 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci']);
$table
->addColumn('username', 'string', ['limit' => 30, 'default' => '', 'comment' => '用户名'])
->addColumn('invite', 'string', ['limit' => 10, 'default' => '', 'comment' => '邀请码'])
->addColumn('password', 'string', ['limit' => 255, 'default' => base64_encode(password_hash('_default__password_', 1)), 'comment' => '密码'])
->addColumn('nickname', 'string', ['limit' => 30, 'default' => '', 'comment' => '昵称'])
->addColumn('role', 'string', ['limit' => 200, 'default' => '', 'comment' => '角色'])
->addColumn('level', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '代理类型'])
->addColumn('parent_id', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '父级id'])
->addColumn('grandpa_id', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '祖级id'])
->addColumn('tax', 'integer', ['limit' => 10, 'default' => 0, 'comment' => '提成比例'])
->addColumn('money', 'decimal', ['limit' => [8,2], 'default' => 0, 'comment' => '未结余额'])
->addColumn('status', 'boolean', ['limit' => 1, 'default' => 1, 'comment' => '是否启用'])
->addColumn('create_time', 'integer', ['limit' => 10, 'default' => 0, 'comment' => '创建时间'])
->addColumn('update_time', 'integer', ['limit' => 10, 'default' => 0, 'comment' => '更新时间'])
->addColumn('delete_time', 'integer', ['limit' => 10, 'default' => 0, 'comment' => '删除时间'])
->addIndex(['username'], ['name' => 'index_username'])
->create();
$this->insertData();
}
protected function insertData()
{
$data = [
[
'id' => 1,
'username' => 'admins',
'nickname' => '超级管理员',
'password' => 'admins123',
'role' => [1]
]
];
$msg = '超级管理员创建成功.' . "\n" . '用户名:admins' . "\n" . '密码:admins123' . "\n";
Db::startTrans();
try {
foreach ($data as $item) {
\app\admin\model\AdminUser::create($item);
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
$msg = $e->getMessage();
}
print ($msg);
}
}