123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- declare(strict_types=1);
- namespace App\Model\Arts;
- use App\Model\Model;
- use App\Utils\Common;
- use Hyperf\DbConnection\Db;
- class UserModel extends Model
- {
- /**
- * The table associated with the model.
- *
- * @var ?string
- */
- protected ?string $table = 'user';
- protected ?string $dateFormat = 'U';
- public bool $timestamps = false;
- /**
- * 默认查询字段
- *
- * @var array|string[]
- */
- public array $select = [
- '*'
- ];
- protected int $is_status_search = 1;// 默认使用 status = 1 筛选
- protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
- /**
- * 中间件中获取用户信息
- * @param int $user_id
- * @return array
- */
- public function searchMobileAttribute($query, $value, array $params): mixed
- {
- if (!isset($value)) {
- return $query;
- }
- return $query->where('mobile', $value);
- }
- public function searchIdsAttribute($query, $value, array $params): mixed
- {
- if (!isset($value)) {
- return $query;
- }
- return $query->whereIn('id', $value);
- }
- public function authUserInfo(int $user_id)
- {
- return (new UserModel())->getDetail(params: ['id' => $user_id]);
- }
- public function getByMobile(string $mobile)
- {
- $this->is_status_search = 0;
- return $this->getDetail(params: ['mobile' => $mobile]);
- }
- /**
- * 注册
- * @param string $mobile
- * @return bool
- */
- public function register(string $mobile)
- {
- $time = time();
- $params = [
- 'avatar' => '',
- 'nickname' => Common::getRandNickName(),
- 'mobile' => $mobile,
- 'status' => 1,
- 'createtime' => $time
- ];
- //账号注册时需要开启事务,避免出现垃圾数据
- Db::beginTransaction();
- if (!$user_id = $this->query()->insertGetId($params)){
- Db::rollBack();
- return $this->error('注册失败');
- }
- //注册钱包
- if (!Db::table('user_wallet')->insert(['user_id'=>$user_id])){
- Db::rollBack();
- return $this->error('注册失败');
- }
- Db::commit();
- return $this->success('注册成功',array_merge($params,[
- 'id' => $user_id
- ]));
- }
- // 开播点赞
- public function roomLike()
- {
- return $this->hasOne(LiveRoomLogLikeModel::class, 'user_id', 'id');
- }
- }
|