| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Helpers\Jwt;
- use App\Models\ActionLogModel;
- use App\Models\MemberModel;
- use App\Models\UserModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 会员-服务类
- * Class MemberService
- * @package App\Services\Api
- */
- class MemberService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @since 2020/11/10
- * LoginService constructor.
- */
- public function __construct()
- {
- $this->model = new MemberModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if(!self::$instance){
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 获取资料详情
- * @param $where
- * @param array $field
- */
- public function getInfo($where, array $field=[])
- {
- if(is_array($where)){
- $info = $this->model->where($where)->select($field)->first();
- }else{
- $info = $this->model->getInfo((int)$where);
- }
- return $info;
- }
- /**
- * 用户注册
- * @param $params
- * @return bool
- */
- public function register($params)
- {
- // 检测账号是否存在
- if($this->checkExists('username', $params['username'])){
- $this->error = '2005';
- return false;
- }
- $username = isset($params['username'])? trim($params['username']) : '';
- $password = isset($params['password'])? trim($params['password']) : '123456';
- $avatar = isset($params['avatar'])? trim($params['avatar']) : '';
- $data = [
- 'username'=> $username,
- 'password'=> get_password($password . $username),
- 'mobile'=> isPhone($username)? $username : '',
- 'avatar'=> $avatar? $avatar : '',
- 'status'=> 1,
- 'mark'=> 1,
- 'create_time'=> time(),
- ];
- if($this->model->insert($data)){
- return true;
- }
- $this->error = 2007;
- return false;
- }
- /**
- * 用户登录
- * @param $params
- * @return array|false
- */
- public function login($params)
- {
- $username = isset($params['username'])? $params['username'] : '';
- $password = isset($params['password'])? $params['password'] : '';
- if(empty($username) || empty($password)){
- $this->error = 1013;
- return false;
- }
- // 用户验证
- $info = $this->model->getOne([['username', '=', $username]]);
- if (!$info) {
- $this->error = 2001;
- return false;
- }
- // 密码校验
- $password = get_password($password . $username);
- if ($password != $info['password']) {
- $this->error = 2002;
- return false;
- }
- // 使用状态校验
- if ($info['status'] != 1) {
- $this->error = 2009;
- return false;
- }
- // 设置日志标题
- ActionLogModel::setTitle("会员登录APP");
- ActionLogModel::record($info);
- // JWT生成token
- $jwt = new Jwt('jwt_app');
- $token = $jwt->getToken($info['id']);
- RedisService::set("stores:auths:info:{$info['id']}", $info, 5, 10);
- // 登录数据
- return [
- 'token' => $token,
- 'user_id' => $info['id'],
- 'user_type' => $info['user_type'],
- ];
- }
- }
|