2020-11-04 16:47:17 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
|
|
use App\Model\User;
|
2020-11-04 17:36:52 +08:00
|
|
|
use App\Model\ArticleClass;
|
2020-11-04 22:58:49 +08:00
|
|
|
use Hyperf\DbConnection\Db;
|
2020-11-04 16:47:17 +08:00
|
|
|
|
|
|
|
class UserService extends BaseService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 登录逻辑
|
|
|
|
*
|
|
|
|
* @param string $mobile 手机号
|
|
|
|
* @param string $password 登录密码
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
|
|
|
public function login(string $mobile,string $password){
|
|
|
|
$user = User::where('mobile',$mobile)->first();
|
|
|
|
|
|
|
|
if(!$user){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!password_verify($password,$user->password)){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user->toArray();
|
|
|
|
}
|
2020-11-04 17:36:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 账号注册逻辑
|
|
|
|
*
|
|
|
|
* @param array $data 用户数据
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function register(array $data)
|
|
|
|
{
|
|
|
|
try {
|
2020-11-04 22:58:49 +08:00
|
|
|
$data['password'] = create_password($data['password']);
|
2020-11-04 17:36:52 +08:00
|
|
|
$data['created_at'] = date('Y-m-d H:i:s');
|
2020-11-04 22:58:49 +08:00
|
|
|
|
2020-11-04 17:36:52 +08:00
|
|
|
$result = User::create($data);
|
|
|
|
|
|
|
|
// 创建用户的默认笔记分类
|
|
|
|
ArticleClass::create([
|
|
|
|
'user_id' => $result->id,
|
|
|
|
'class_name' => '我的笔记',
|
|
|
|
'is_default' => 1,
|
|
|
|
'sort' => 1,
|
|
|
|
'created_at' => time()
|
|
|
|
]);
|
2020-11-04 22:58:49 +08:00
|
|
|
} catch (\Exception $e) {
|
2020-11-04 17:36:52 +08:00
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result ? true : false;
|
|
|
|
}
|
2020-11-04 16:47:17 +08:00
|
|
|
}
|