generated from liutk/owl-admin-base
104 lines
2.8 KiB
PHP
104 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Api;
|
|
|
|
use App\Models\User;
|
|
use App\Models\UserGift;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class UserService
|
|
{
|
|
public static function make(): static
|
|
{
|
|
return new static;
|
|
}
|
|
|
|
public function register($miniOpenid)
|
|
{
|
|
if(! $user = User::where('mini_openid', $miniOpenid)->first()){
|
|
$user = new User();
|
|
$user->mini_openid = $miniOpenid;
|
|
$user->save();
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function login(User $user)
|
|
{
|
|
//记录当前登录时间、IP
|
|
$ip = request()->getClientIp();
|
|
$user->last_login_ip = ip2long($ip);
|
|
$user->last_login_at = now();
|
|
$user->save();
|
|
|
|
//撤销当前所有令牌;
|
|
$user->tokens()->delete();
|
|
|
|
return $user->createToken(
|
|
name: 'api',
|
|
expiresAt: now()->addDay(),
|
|
)->plainTextToken;
|
|
}
|
|
|
|
public function bindPhone(User $user, $phone)
|
|
{
|
|
$res = [
|
|
'status' => false,
|
|
'message'=> ''
|
|
];
|
|
|
|
if(User::where('phone', $phone)->where('id', '<>', $user->id)->exists()){
|
|
$res['message'] = '该手机号已被其他微信号绑定,请更换手机号绑定';
|
|
return $res;
|
|
}
|
|
|
|
$user->update([
|
|
'phone' => $phone,
|
|
'bind_phone_at' => now()
|
|
]);
|
|
|
|
$res['status'] = true;
|
|
|
|
return $res;
|
|
}
|
|
|
|
public function receiveGift(UserGift $userGift, $params)
|
|
{
|
|
$userGift->update([
|
|
'consignee' => $params['consignee'],
|
|
'phone' => $params['phone'],
|
|
'address' => $params['address'],
|
|
'state' => 1
|
|
]);
|
|
}
|
|
|
|
protected function saveFile($path, $file = null)
|
|
{
|
|
if (gettype($file) == 'object') {
|
|
//获取文件大小
|
|
if($size = $file->getSize() > 2*1024*1024){//大于2M
|
|
return false;
|
|
}
|
|
$type = $file->getClientOriginalExtension();
|
|
if (in_array($type, array('jpeg', 'jpg', 'bmp', 'png'))) {
|
|
$file = Storage::disk(Admin::config('admin.upload.disk'))->putFile($path, $file);
|
|
}else{
|
|
return false;
|
|
}
|
|
} else if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $file, $result)) {
|
|
$type = $result[2];
|
|
if (in_array($type, array('jpeg', 'jpg', 'bmp', 'png'))) {
|
|
$savePath = $path . '/' . uniqid() . '.' . $type;
|
|
Storage::disk(Admin::config('admin.upload.disk'))->put($savePath, base64_decode(str_replace($result[1], '', $file)));
|
|
$file = $savePath;
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
return $file;
|
|
}
|
|
|
|
} |