123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\services\user;
- use app\model\user\User;
- use app\services\system\SmsRecordServices;
- use Exception;
- use Psr\SimpleCache\InvalidArgumentException;
- use qiniu\basic\BaseModel;
- use qiniu\basic\BaseServices;
- use qiniu\services\CacheService;
- use qiniu\services\sms\Sms;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\ValidateException;
- use think\facade\Config;
- use think\Model;
- /**
- *
- * Class LoginServices
- * @package app\services\user
- * @mixin User
- */
- class LoginServices extends BaseServices
- {
- /**
- * LoginServices constructor.
- * @param User $model
- */
- public function __construct(User $model)
- {
- $this->model = $model;
- }
- /**
- * @param $account
- * @param $password
- * @param int $spread_uid
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function login($account, $login_type, $password, $spread_uid = 0)
- {
- if ($login_type != 'phone') $login_type = 'account';
- $user = $this->getOne([$login_type => $account], 'uid,pwd,status,salt');
- if ($user) {
- if ($user->pwd !== password((string)$password, $user->salt)[0])
- throw new ValidateException('账号或密码错误');
- } else {
- throw new ValidateException('账号或密码错误');
- }
- if (!$user['status'])
- throw new ValidateException('已被禁止,请联系管理员');
- $uid = (int)$user['uid'];
- //更新用户信息
- $token = $this->createToken($uid, 'api', $user->pwd);
- if ($token) {
- // 用户登录成功事件
- $this->updateUserLastLogin($uid);
- if ($spread_uid > 0) {
- /** @var UserSpreadServices $spreadService */
- $spreadService = app()->make(UserSpreadServices::class);
- $spreadService->setSpread($uid, $spread_uid);
- }
- return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
- } else
- throw new ValidateException('登录失败');
- }
- /**
- * 更新用户信息
- * @param int $uid
- * @return bool
- */
- public function updateUserLastLogin(int $uid): bool
- {
- $data['last_time'] = time();
- $data['last_ip'] = app()->request->ip();
- if (!$this->update($uid, $data, 'uid')) {
- throw new ValidateException('修改信息失败');
- }
- return true;
- }
- /**
- * 更新用户信息
- * @param int $uid
- * @param array $wechatInfo
- * @param array $userInfo
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function updateUserWechatLogin(int $uid, array $wechatInfo, $userInfo = []): bool
- {
- if (!$userInfo) {
- $userInfo = $this->get($uid);
- if (!$userInfo) {
- throw new ValidateException('数据不存在');
- }
- }
- $data = [];
- if (isset($wechatInfo['nickname']) && $wechatInfo['nickname']) {
- $data['nickname'] = $wechatInfo['nickname'];
- }
- if (isset($wechatInfo['headimgurl']) && $wechatInfo['headimgurl']) {
- $data['avatar'] = $wechatInfo['headimgurl'];
- }
- if (isset($wechatInfo['phone']) && $wechatInfo['phone'] && !$this->be(['account|phone' => $wechatInfo['phone']]) && !$userInfo['phone']) {
- $data['phone'] = $wechatInfo['phone'];
- $data['account'] = $wechatInfo['phone'];
- }
- $data['last_time'] = time();
- $data['last_ip'] = app()->request->ip();
- if (!$this->update($uid, $data, 'uid')) {
- throw new ValidateException('修改信息失败');
- }
- if (($wechatInfo['spread_uid'] ?? 0) > 0) {
- /** @var UserSpreadServices $spreadService */
- $spreadService = app()->make(UserSpreadServices::class);
- $spreadService->setSpread($uid, $wechatInfo['spread_uid']);
- }
- return true;
- }
- /**
- * 发送验证码
- * @param $phone
- * @param $type
- * @param $time
- * @param $ip
- * @return int
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function verify($phone, $type, $time, $ip)
- {
- if ($this->getOne(['phone' => $phone]) && ($type == 'register' || $type == 'change_new')) {
- throw new ValidateException('手机号已注册');
- }
- $default = Config::get('sms.default', 'qiniu');
- $defaultMaxPhoneCount = Config::get('sms.maxPhoneCount', 10);
- $defaultMaxIpCount = Config::get('sms.maxIpCount', 50);
- $maxPhoneCount = Config::get('sms.stores.' . $default . '.maxPhoneCount', $defaultMaxPhoneCount);
- $maxIpCount = Config::get('sms.stores.' . $default . '.maxIpCount', $defaultMaxIpCount);
- /** @var SmsRecordServices $smsRecord */
- $smsRecord = app()->make(SmsRecordServices::class);
- if ($smsRecord->getCount(['phone' => $phone, 'add_ip' => $ip, 'time' => 'today']) >= $maxPhoneCount) {
- throw new ValidateException('您今日发送短信次数已经达到上限');
- }
- if ($smsRecord->getCount(['add_ip' => $ip, 'time' => 'today']) >= $maxIpCount) {
- throw new ValidateException('此IP今日发送短信次数已经达到上限');
- }
- if (CacheService::get('code_' . $phone))
- throw new ValidateException('验证码' . $time . '分钟内有效');
- mt_srand();
- $code = rand(100000, 999999);
- $data['code'] = $code;
- $data['time'] = $time;
- $res = app()->make(Sms::class)->send($phone, 'VERIFICATION_CODE_TIME', $data);
- if (!$res)
- throw new ValidateException('短信平台验证码发送失败');
- return $code;
- }
- /**
- * H5用户注册
- * @param $phone
- * @param $password
- * @param $spread_uid
- * @param string $user_type
- * @param string $nickname
- * @return Model|BaseModel
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function register($phone, $password, $spread_uid, string $user_type = 'h5', string $nickname = '')
- {
- if ($this->getOne(['phone' => $phone])) {
- throw new ValidateException('该手机号已使用');
- }
- $data['account'] = create_account();
- [$data['pwd'], $data['salt']] = password((string)$password);
- $data['phone'] = $phone;
- $data['real_name'] = '';
- $data['birthday'] = 0;
- $data['user_type'] = $user_type;
- $data['add_time'] = time();
- $data['add_ip'] = app('request')->ip();
- $data['last_time'] = time();
- $data['last_ip'] = app('request')->ip();
- $data['nickname'] = $nickname ?: substr(md5($phone . time()), 0, 12);
- $data['avatar'] = sys_config('h5_avatar');
- $data['status'] = 1;
- $data['is_promoter'] = ((sys_config('brokerage_func_status', 0) && sys_config('store_brokerage_statu', 1) == 2) ? 1 : 0);
- if (!$re = $this->create($data)) {
- throw new ValidateException('注册失败');
- } else {
- if ($spread_uid > 0) {
- /** @var UserSpreadServices $spreadService */
- $spreadService = app()->make(UserSpreadServices::class);
- $spreadService->setSpread($re->uid, $spread_uid);
- }
- return $re;
- }
- }
- /**
- * 重置密码
- * @param $account
- * @param $password
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function reset($account, $password)
- {
- $user = $this->getOne(['phone' => $account]);
- if (!$user) {
- throw new ValidateException('账号不存在');
- }
- [$pwd, $salt] = password((string)$password);
- if (!$this->update($user['uid'], ['pwd' => $pwd, 'salt' => $salt], 'uid')) {
- throw new ValidateException('修改密码失败');
- }
- return true;
- }
- /**
- * 重置交易密码
- * @param $account
- * @param $password
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function trade_reset($account, $password)
- {
- $user = $this->getOne(['phone' => $account]);
- if (!$user) {
- throw new ValidateException('账号不存在');
- }
- [$pwd, $salt] = password((string)$password);
- if (!$this->update($user['uid'], ['trade_pwd' => $pwd, 'trade_salt' => $salt], 'uid')) {
- throw new ValidateException('修改交易密码失败');
- }
- return true;
- }
- /**
- * 手机号登录
- * @param $phone
- * @param $spread_uid
- * @param string $user_type
- * @return array
- * @throws Exception
- */
- public function mobile($phone, $spread_uid, $user_type = 'h5')
- {
- //数据库查询
- $user = $this->getOne(['phone' => $phone]);
- if (!$user) {
- $user = $this->register($phone, '123456', $spread_uid, $user_type);
- if (!$user) {
- throw new ValidateException('用户登录失败,无法生成新用户,请稍后再试!');
- }
- }
- if (!$user->status)
- throw new ValidateException('已被禁止,请联系管理员');
- $uid = (int)$user['uid'];
- $token = $this->createToken($uid, 'api', $user->pwd);
- if ($token) {
- // 用户登录成功事件
- $this->updateUserLastLogin($uid);
- if ($spread_uid > 0) {
- /** @var UserSpreadServices $spreadService */
- $spreadService = app()->make(UserSpreadServices::class);
- $spreadService->setSpread($uid, $spread_uid);
- }
- return ['token' => $token['token'], 'expires_time' => $token['params']['exp']];
- } else {
- throw new ValidateException('登录失败');
- }
- }
- }
|