一.thinkphp 数据库迁移
1.创建迁移文件
php think migrate:create Users
2.编辑文件
public function change()
{
// create the table
$table = $this->table('users',array('engine'=>'InnoDB','comment' => '用户表'));
$table->addColumn('username', 'string',array('limit' => 32,'default'=>'','comment'=>'用户名,登陆使用'))
->addColumn('password', 'string',array('limit' => 32,'default'=>md5('123456'),'comment'=>'用户密码'))
->addColumn('name', 'string',array('limit' => 32,'default'=>'','comment'=>'真实姓名'))
->addColumn('phonenum', 'integer',array('limit' => 32,'default'=>110,'comment'=>'手机号'))
->addColumn('email', 'string',array('limit' => 32,'default'=>'[email protected]','comment'=>'邮箱'))
->addColumn('role_id', 'string',array('limit' => 32,'default'=>'[email protected]','comment'=>'角色'))
->addColumn('login_status', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'登陆状态'))
->addColumn('login_code', 'string',array('limit' => 32,'default'=>0,'comment'=>'排他性登陆标识'))
->addColumn('last_login_ip', 'integer',array('limit' => 11,'default'=>0,'comment'=>'最后登录IP'))
->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登录时间'))
->addColumn('is_delete', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'删除状态,1已删除'))
->addIndex(array('username'), array('unique' => true))
->addTimestamps()
->create();
}
public function up()
{
// add column type DOUBLE
$this->execute("ALTER TABLE third ADD COLUMN much_money DOUBLE(10,2) unsigned NOT NULL DEFAULT '0.00'");
}
public function down()
{
$this->dropTable('users');
}
3.执行文件迁移
php think migrate:run
4.执行回滚
php think migrate:rollback -t third
二.thinkphp 数据填充
1.创建填充
php think seed:create Third
2.编辑填充文件
public function run()
{
$data[] = [
'username' => $faker->userName,
'name' => $faker->name,
'phonenum' => $faker->phoneNumber,
'email' => $faker->email,
'role_id' => 3
];
}
3.开始进行数据库填充
a. php think seed:run
b.可指定一个或多个 seed
c. $ php think seed:run -s Third
$ php think seed:run -s Third -s member
4.安装faker类库
composer require fzaninotto/faker
public function run()
{
$faker = Factory::create('zh_CN');//选择中文库
$data = [];
for ($i = 0; $i < 100; $i++) {
$data[] = [
'username' => $faker->userName,
'name' => $faker->name,
'phonenum' => $faker->phoneNumber,
'email' => $faker->email,
'role_id' => 3
];
}
$this->table('users')->insert($data)->save();
}
或 https://blog.csdn.net/qq_54337367/article/details/117700542
tp5和微信小程序授权登录
html
Js
getUserProfile(e){
let that = this;
wx.getUserProfile({
desc: '展示用户信息',
success: (result) => {
// 接收名称和头像
let nickName = result.userInfo.nickName;
let avatarUrl = result.userInfo.avatarUrl;
wx.login({
timeout:10000,
success(res){
let code = res.code//获取code
wx.request({
url: 'http://www.day12.com/login',
method:'post',
data:{
code:code,
nickName:nickName,
avatarUrl:avatarUrl
},success: ret=>{
console.log(ret)
}
})
}
})
},
fail: (res) => {
console.log(res)
},
complete: (res) => {},
})
},
php
public function login()
{
$code = request()->param('code');
$nickName = request()->param('nickName');
$avatarUrl = request()->param('avatarUrl');
$appId = "";//appid
$appSecret = "";//填写你微信公众开发平台的appSecret
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appId}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
//获取openid和session_key
$userInfo = json_decode(file_get_contents($url),true);
$userData = [
'nick_name'=>$nickName,
'avatar_url'=>$avatarUrl,
'open_id'=>$userInfo['openid'],
'session_key'=>$userInfo['session_key']
];
$user = new User();
//根据openid查询用户数据判断用户数是否首次登陆
$res = User::get(['open_id'=>$userInfo['openid']]);
if($res)
{
//返回查询数据的id //更新用户昵称头像
$id = $res->id;
$user->save(['nick_name'=>$nickName,'avatar_url'=>$avatarUrl],['id'=>$id]);
}else{
$res = $user->save($userData);//新增
$id = $res;
}
Session::set('user_id',$id);
//jwt的创建与与验证
$token =Token::createToken($id);
$uid = Token::verifyToken($token);
if(!$uid)
{
return json(['code'=>200,'msg'=>'登录失败','data'=>[]]);
}
$data=[
'token'=>$token,
'id'=>$id
];
return json(['code'=>200,'msg'=>'success','data'=>$data]);
}
可参考 https://blog.csdn.net/weixin_58505056/article/details/122567938?spm=1001.2014.3001.5501