coupon-admin/app/repository/AccountRepository.php

106 lines
2.6 KiB
PHP
Raw Normal View History

2021-11-18 09:57:04 +00:00
<?php
namespace app\repository;
use app\exception\RepositoryException;
2021-12-15 07:04:20 +00:00
use app\model\Account;
2021-11-18 09:57:04 +00:00
use app\service\Repository;
2021-12-10 10:25:14 +00:00
use app\traits\account\ApplyStaffTrait;
2021-12-02 10:34:44 +00:00
use app\traits\account\BusinessFlowTrait;
2021-11-19 11:06:22 +00:00
use app\traits\CommentTrait;
2021-11-23 09:13:55 +00:00
use app\traits\CouponBillTrait;
2021-11-19 11:06:22 +00:00
use app\traits\CouponTrait;
2021-11-18 09:57:04 +00:00
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;
/**
* 账户域 相关操作(客户账号)
*
* Class AccountRepository
* @package app\repository
* @method self getInstance(Model $model = null) static
*/
class AccountRepository extends Repository
{
2021-12-02 10:34:44 +00:00
use CommentTrait;
use BusinessFlowTrait;
2021-11-19 11:06:22 +00:00
use CouponTrait;
2021-11-23 09:13:55 +00:00
use CouponBillTrait;
2021-12-10 10:25:14 +00:00
use ApplyStaffTrait;
2021-11-18 09:57:04 +00:00
/**
* 获取指定账户记录By手机号
*
* @param string $phone
* @param array $fields
* @return Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function infoByPhone(string $phone, array $fields = []): ?Model
{
$where[] = ['mobile', '=', $phone];
return $this->findOneByWhere($where, $fields);
}
/**
* 获取指定账户记录By用户名
*
* @param string $username
* @param array $fields
* @return Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function infoByUsername(string $username, array $fields = []): ?Model
{
$where[] = ['username', '=', $username];
return $this->findOneByWhere($where, $fields);
}
/**
* 通过微信小程序的openID查询
*
* @param string $openID
* @return array|Model|null
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function findByOpenID(string $openID)
{
2021-11-24 08:19:54 +00:00
return $this->model->where('open_id', $openID)->find();
2021-11-18 09:57:04 +00:00
}
/**
* 修改密码
*
* @param int $accountId
* @param string $oldPwd
* @param string $newPwd
* @return bool
* @throws RepositoryException
*/
public function modifyPwd(int $accountId, string $oldPwd, string $newPwd): bool
{
if (!$user = $this->findById($accountId)) {
throw new RepositoryException('用户不存在');
}
if ($user['password'] != md5($oldPwd)) {
throw new RepositoryException('原密码错误');
}
$user->password = md5($newPwd);
return $user->save();
}
}