样机小程序
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\traits\crud\CrudLogicTrait;
|
||||
use app\common\model\User;
|
||||
use app\common\service\WechatService;
|
||||
use support\exception\BusinessException;
|
||||
|
||||
class UserLogic
|
||||
{
|
||||
use CrudLogicTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = User::class;
|
||||
|
||||
|
||||
/**
|
||||
* 构建 JWT 令牌有效载荷
|
||||
*
|
||||
* @param User $user
|
||||
* @return array
|
||||
*/
|
||||
public function buildJwtPayload(User $user): array
|
||||
{
|
||||
$now = time();
|
||||
return [
|
||||
'sub' => 'user_' . $user->id,
|
||||
'aud' => 'bsh.yw.hnzhwlkj.cn',
|
||||
'iat' => $now,
|
||||
'nbf' => $now,
|
||||
'exp' => $now + config('jwt.jwt_expiration'),
|
||||
'typ' => 'access',
|
||||
'user_id' => $user->id,
|
||||
'name' => $user->name,
|
||||
'meta' => $user->toArray(),
|
||||
'uid' => $user->id,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户信息
|
||||
*/
|
||||
public function userInfo(int $uid)
|
||||
{
|
||||
$user = User::find($uid);
|
||||
|
||||
if (!$user) {
|
||||
throw new BusinessException('账户不存在', 404);
|
||||
}
|
||||
|
||||
if ($user->status != 1) {
|
||||
throw new BusinessException('账号已禁用', 403);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测用户是否存在
|
||||
*
|
||||
* @param string $openid
|
||||
* @param string $unionid
|
||||
* @return User|bool
|
||||
*/
|
||||
public function checkUser(string $openid, string $unionid): User|bool
|
||||
{
|
||||
$uinfo = User::where('status', 1)
|
||||
->where('openid', $openid)
|
||||
->where('unionid', $unionid)
|
||||
->first();
|
||||
return $uinfo === null ? false : $uinfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定用户
|
||||
*
|
||||
* @param array $data
|
||||
* @return User|bool
|
||||
*/
|
||||
public function bindUser(array $data): User|bool
|
||||
{
|
||||
$uinfo = User::where('status', 1)
|
||||
->where('name', $data['name'])
|
||||
->where('mobile', $data['mobile'])
|
||||
->where('sn', $data['sn'])
|
||||
->first();
|
||||
|
||||
if ($uinfo !== null) {
|
||||
$uinfo->update([
|
||||
'openid' => $data['openid'],
|
||||
'unionid' => $data['unionid'],
|
||||
'nickname' => $data['nickname'],
|
||||
'avatar' => $data['avatar'],
|
||||
'password' => password_hash($data['password'], PASSWORD_DEFAULT)
|
||||
]);
|
||||
return $uinfo;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建用户
|
||||
*
|
||||
* @param array $data
|
||||
* @return User
|
||||
*/
|
||||
public function createUser(array $data): User
|
||||
{
|
||||
return User::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信access_token
|
||||
*
|
||||
* @param string $code
|
||||
* @param string $state
|
||||
* @return array
|
||||
*/
|
||||
public function getAccessToken(string $code, string $state): array
|
||||
{
|
||||
$wechat = new WechatService();
|
||||
return $wechat->wechatWebAccessToken($code, $state);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信用户信息
|
||||
*
|
||||
* @param array $token
|
||||
* @return array
|
||||
*/
|
||||
public function getWechatUserInfo(array $token)
|
||||
{
|
||||
$wechat = new WechatService();
|
||||
return $wechat->wechatWebUserInfo($token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户列表
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
$list = User::where('status', 1)->orderBy('id', 'desc')->get();
|
||||
$arr = [];
|
||||
foreach ($list->toArray() as $item) {
|
||||
$arr[] = [
|
||||
'name' => $item['name'],
|
||||
'id' => $item['id'],
|
||||
];
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
*/
|
||||
public function savePassword(int $uid, array $data)
|
||||
{
|
||||
$password = password_hash($data['password_new'], PASSWORD_DEFAULT);
|
||||
$res = User::where('id',$uid)->update(['password' => $password]);
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
//---------------------------------------------小程序------------------------------------------------------------
|
||||
/**
|
||||
* 获取微信小程序access_token
|
||||
*
|
||||
* @param string $code
|
||||
* @param string $state
|
||||
* @return array
|
||||
*/
|
||||
public function getMiniAccessToken(): array
|
||||
{
|
||||
$wechat = new WechatService();
|
||||
return $wechat->getMiniAccessToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 openid / session_key
|
||||
*
|
||||
* @param string $code
|
||||
* @param string $state
|
||||
* @return array
|
||||
*/
|
||||
public function miniCode2Session($loginCode): array
|
||||
{
|
||||
$wechat = new WechatService();
|
||||
return $wechat->miniCode2Session($loginCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 openid / session_key
|
||||
*
|
||||
* @param string $code
|
||||
* @param string $state
|
||||
* @return array
|
||||
*/
|
||||
public function getMiniPhoneNumber($access_token, $phone_code): array
|
||||
{
|
||||
$wechat = new WechatService();
|
||||
return $wechat->getMiniPhoneNumber($access_token, $phone_code);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user