// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Helpers\Jwt; use App\Models\ActionLogModel; use App\Models\BalanceLogModel; use App\Models\MemberBankModel; use App\Models\MemberModel; use App\Models\OrderModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\MpService; use App\Services\RedisService; use App\Services\SmsService; use Illuminate\Support\Facades\DB; use phpQrcode\QRcode; /** * 会员管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class MemberService * @package App\Services\Api */ class MemberService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * MemberService constructor. */ public function __construct() { $this->model = new MemberModel(); } /** * 静态入口 * @return MemberService|static|null */ public static function make() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 账号登录 * @param $params * @return array|false */ public function login($params) { // 账号登录 $mobile = isset($params['mobile']) ? $params['mobile'] : ''; $password = isset($params['password']) ? $params['password'] : ''; if (empty($params) || empty($mobile) || empty($password)) { $this->error = 1041; return false; } // 验证是否注册,没有则注册 $data = $this->model->where(['mobile' => $mobile, 'mark' => 1])->select(['id', 'mobile', 'user_type', 'password', 'nickname', 'code', 'status'])->first(); $data = $data ? $data->toArray() : []; $userId = isset($data['id']) ? $data['id'] : 0; $status = isset($data['status']) ? $data['status'] : 0; $userPassword = isset($data['password']) ? $data['password'] : ''; if (empty($data) || $userId <= 0) { $this->error = 2014; return false; } if ($status == 3) { $this->error = 2050; return false; } if ($status != 1) { $this->error = 2015; return false; } // 验证登录密码 if (empty($userPassword) || $userPassword != get_password($password)) { $this->error = 2017; return false; } // 更新 if (!RedisService::get("caches:members:login_{$userId}")) { $system = isset($params['system']) ? $params['system'] : []; $system = $system && !is_array($system) ? json_decode($system, true) : $system; $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios'; $uuid = isset($system['uuid']) ? $system['uuid'] : ''; $version = isset($system['app_version']) ? $system['app_version'] : ''; $updateData = [ 'update_time' => time(), 'login_ip' => get_client_ip(), 'login_time' => time(), 'device_code' => $uuid, 'login_count' => DB::raw("login_count+1"), 'app_version' => $version, 'device' => $appSources == 'ios' ? 1 : 2, ]; $this->model->where(['id' => $userId])->update($updateData); RedisService::set("caches:members:login_{$userId}", $updateData, rand(300, 600)); } // 获取登录授权token $jwt = new Jwt('jwt_jd_app'); $token = $jwt->getToken($userId); // 结果返回 $result = [ 'access_token' => $token, 'info' => ['uid' => $userId, 'nickname' => $data['nickname']], ]; // 用户缓存信息 $this->error = 2019; $data['token'] = $token; unset($data['password']); unset($data['mobile']); RedisService::set("auths:info:{$userId}", $data, 24 * 3600); return $result; } /** * 授权登录 * @param $code * @param array $params * @return array|false */ public function mpAuth($code, $params = []) { // 账号登录 if (empty($code)) { $this->error = 1041; return false; } $system = isset($params['system']) ? $params['system'] : []; $system = $system && !is_array($system) ? json_decode($system, true) : $system; $platform = isset($system['platform']) && $system['platform']?$system['platform'] : 'mp'; // 获取授权信息 $userInfo = MpService::make()->getUserInfo($code, $platform); $openid = isset($userInfo['openid']) ? $userInfo['openid'] : ''; $unionid = isset($userInfo['unionid']) ? $userInfo['unionid'] : ''; $sessionKey = isset($userInfo['session_key']) ? $userInfo['session_key'] : ''; $accessToken = isset($userInfo['access_token']) ? $userInfo['access_token'] : ''; if (empty($userInfo)) { $this->error = MpService::make()->getError(); return false; } if (empty($openid)) { $this->error = 1042; return false; } // 小程序兼容解码获取unionId if(empty($unionid) && $platform == 'mp' && $sessionKey){ $loginInfo = isset($params['loginInfo'])?$params['loginInfo']:[]; $encryptedData = isset($loginInfo['encryptedData'])?$loginInfo['encryptedData']:''; $iv = isset($loginInfo['iv'])?$loginInfo['iv']:''; $loginInfo = MpService::make()->decryptData($encryptedData,$iv,$sessionKey); $unionid = isset($loginInfo->unionid)? $loginInfo->unionid : ''; } // 公众号获取用户信息 $nickname = ''; $avatar = ''; if($platform == 'wechat' && $accessToken){ $loginInfo = MpService::make()->getWechatUserInfo($accessToken, $openid); $nickname = isset($loginInfo['nickname'])? $loginInfo['nickname'] : ''; $avatar = isset($loginInfo['headimgurl'])? $loginInfo['headimgurl'] : ''; } // 关联账号 $data = []; if($unionid){ $data = $this->model->where(['unionid'=>$unionid]) ->select(['id', 'openid','unionid', 'mobile', 'user_type', 'nickname', 'avatar', 'code', 'status', 'mark']) ->first(); $data = $data ? $data->toArray() : []; } // 普通号 if(empty($data)){ $data = $this->model->where(['openid'=>$openid]) ->select(['id', 'openid','unionid', 'mobile','parent_id', 'user_type', 'nickname', 'avatar', 'code', 'status', 'mark']) ->first(); } RedisService::set("caches:member".date('His'),['data'=>$data,'unionid'=>$unionid,'openid'=>$openid,'info'=>$userInfo], 600); $userId = isset($data['id']) ? $data['id'] : 0; $userAvatar = isset($data['avatar']) ? $data['avatar'] : ''; $nickName = isset($data['nickname']) ? $data['nickname'] : ''; $mobile = isset($data['mobile']) ? $data['mobile'] : ''; $userUnionid = isset($data['unionid']) ? $data['unionid'] : ''; $status = isset($data['status']) ? $data['status'] : 0; $parentId = isset($data['parent_id']) ? $data['parent_id'] : 0; $mark = isset($data['mark']) ? $data['mark'] : 0; if ($data && $userId && $status != 1 && $mark == 1) { $this->error = 2011; return false; } $appSources = isset($system['app_sources']) && $system['app_sources'] ? $system['app_sources'] : 'ios'; $uuid = isset($system['uuid']) ? $system['uuid'] : ''; $version = isset($system['app_version']) ? $system['app_version'] : ''; // 推荐人 $rid = isset($params['rid']) ? intval($params['rid']) : 0; $parents = ''; if ($rid) { $inviteInfo = $this->model->where(['id' => $rid, 'mark' => 1]) ->select(['id', 'parent_id', 'parents', 'status']) ->first(); $parents = isset($inviteInfo['parents']) ? $inviteInfo['parents'] : ''; if ($inviteInfo) { $parents = $parents ? $parents . $rid . ',' : ",{$rid},"; }else{ $rid = 0; } } if (empty($data)) { $userId = $this->model->max('id') + 1; $data = [ 'nickname' => $nickname? $nickname : '用户' . $userId, 'openid' => $platform!='wechat'?$openid:'', 'wechat_openid' => $platform=='wechat'?$openid:'', 'unionid' => $unionid, 'mobile' => '', 'avatar' => $avatar, 'parent_id' => $rid, 'parents' => $parents, 'code' => get_random_code(9, 'S', $userId), 'password' => get_password('a123456'), 'login_ip' => get_client_ip(), 'create_time' => time(), 'login_time' => time(), 'login_count' => DB::raw("login_count+1"), 'app_version' => $version, 'app_uuid' => $uuid, 'device' => $appSources == 'ios' ? 1 : 2, ]; if (!$userId = $this->model->insertGetId($data)) { $this->error = 2012; return false; } } // 更新登录信息 else if ($mark == 0 || !RedisService::get("caches:members:login_{$userId}")) { $updateData = [ 'login_ip' => get_client_ip(), 'login_time' => time(), 'app_uuid' => $uuid, 'login_count' => DB::raw("login_count+1"), 'app_version' => $version, 'device' => $appSources == 'ios' ? 1 : 2, 'mark' => 1, ]; if ($openid && $platform=='wechat') { $updateData['wechat_openid'] = $openid; }else if ($openid) { $updateData['openid'] = $openid; } if($unionid) { $updateData['unionid'] = $unionid; } if(empty($userAvatar) && $avatar){ $updateData['avatar'] = $avatar; } if(empty($nickName) && $nickname){ $updateData['nickname'] = $nickname; } if($parentId<=0){ $updateData['parent_id'] = $rid; $updateData['parents'] = $parents; } if ($mark == 0) { $data['create_time'] = time(); $data['nickname'] = $nickname; $data['avatar'] = $avatar; $data['mobile'] = ''; $data['unionid'] = $unionid; $mobile = ''; if ($openid && $platform=='wechat') { $data['wechat_openid'] = $openid; }else if ($openid) { $data['openid'] = $openid; } } $this->model->where(['id' => $userId])->update($updateData); RedisService::set("caches:members:login_{$userId}", $updateData, rand(30, 60)); } // 获取登录授权token $jwt = new Jwt('jwt_lgx_app'); $token = $jwt->getToken($userId); // 结果返回 $result = [ 'access_token' => $token, 'info' => ['uid' => $userId,'unionid'=>$unionid, 'openid' => $openid,'nickname'=>$nickname, 'has_info' => $data['avatar'] && $data['nickname'] ? 1 : 0, 'mobile' => $mobile], ]; // 用户缓存信息 $this->error = 2013; $data['date'] = date('Y-m-d H:i:s'); $data['token'] = $token; unset($data['mobile']); RedisService::set("auths:info:{$userId}", $data, 24 * 3600); RedisService::set("auths:info:{$userId}_login", $result, 600); RedisService::clear("caches:storeId:id_{$userId}"); RedisService::clear("caches:storeId:uuid_{$uuid}"); RedisService::clear("caches:storeId:ip_".get_client_ip()); return $result; } /** * 完善资料 * @param $params * @return array|false */ public function setProfile($params) { $id = isset($params['id'])? $params['id'] : 0; $code = isset($params['code'])? $params['code'] : ''; $avatar = isset($params['avatar'])? $params['avatar'] : ''; $nickname = isset($params['nickname'])? $params['nickname'] : ''; $phone = isset($params['mobile'])? $params['mobile'] : ''; if($id<=0 || empty($code)){ $this->error = '授权参数错误,请刷新重试'; return false; } if(empty($avatar) || empty($nickname)){ $this->error = '请先获取用户授权信息'; return false; } $userInfo = $this->model->where(['id'=>$id,'mark'=>1]) ->select(['id as uid', 'nickname', 'openid','avatar']) ->first(); if(empty($userInfo)){ $this->error = '授权登录失败,请刷新重试'; return false; } // 获取手机号信息 if($code){ $phoneData = MpService::make()->getPhoneNumber($code); $phoneData = isset($phoneData['phone_info']) ? $phoneData['phone_info'] : []; $phone = isset($phoneData['phoneNumber']) ? $phoneData['phoneNumber'] : ''; if (empty($phone)) { $this->error = MpService::make()->getError(); return false; } } $avatar = save_base64_image($avatar, 'avatar'); if(!$this->model->where(['id'=>$id])->update(['mobile'=>$phone,'nickname'=>$nickname,'avatar'=>$avatar,'update_time'=>time()])){ $this->error = '获取授权信息失败'; return false; } $this->error = '登录成功'; // 获取登录授权token $jwt = new Jwt('jwt_lgx_app'); $token = $jwt->getToken($id); return [ 'access_token'=> $token, 'info'=> $userInfo ]; } /** * 重置密码 * @param $params * @return array|false */ public function forget($params) { // 账号登录 $mobile = isset($params['mobile']) ? trim($params['mobile']) : ''; $password = isset($params['password']) ? trim($params['password']) : ''; if (empty($params) || empty($mobile) || empty($password)) { $this->error = 1041; return false; } // 验证码验证 $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : ''; if (!SmsService::make()->check($mobile, $smsCode, 'reset_password')) { $this->error = SmsService::make()->getError(); return false; } // 验证是否注册 if (!$userId = $this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) { $this->error = 1038; return false; } if (!$this->model->where(['id' => $userId])->update(['password' => get_password($password), 'update_time' => time()])) { $this->error = 2030; return false; } // 操作日志 ActionLogModel::setRecord($userId, ['type' => 2, 'title' => '重置密码', 'content' => '重置登录密码', 'module' => 'member']); ActionLogModel::record(); $this->error = 2031; return true; } /** * 账号注册 * @param $params * @return array|false */ public function register($params) { // 账号登录 $mobile = isset($params['mobile']) ? trim($params['mobile']) : ''; $password = isset($params['password']) ? trim($params['password']) : ''; $nickname = isset($params['nockname']) ? trim($params['nockname']) : ''; if (empty($params) || empty($mobile) || empty($password)) { $this->error = 1041; return false; } // 验证码验证 $smsCode = isset($params['sms_code']) ? trim($params['sms_code']) : ''; if (!SmsService::make()->check($mobile, $smsCode, 'register')) { $this->error = SmsService::make()->getError(); return false; } // 验证是否注册 if ($this->model->where(['mobile' => $mobile, 'mark' => 1])->value('id')) { $this->error = 2002; return false; } // 驾驶证 $drivingLicense = isset($params['driving_license']) && $params['driving_license'] ? get_image_path($params['driving_license']) : ''; if (empty($drivingLicense)) { $this->error = 2007; return false; } // 行驶证 $driversLicense = isset($params['drivers_license']) && $params['drivers_license'] ? get_image_path($params['drivers_license']) : ''; if (empty($driversLicense)) { $this->error = 2008; return false; } $id = $this->model->max('id') + 1; $system = isset($params['system']) ? $params['system'] : []; $system = $system && !is_array($system) ? json_decode($system, true) : $system; $appSources = isset($system['app_sources']) ? $system['app_sources'] : ''; $uuid = isset($system['uuid']) ? $system['uuid'] : ''; $data = [ 'mobile' => $mobile, 'user_type' => 2, 'avatar' => '', 'nickname' => $nickname ? $nickname : 'DU' . rand(10, 99) . substr($mobile, -6, 6), 'realname' => isset($params['realname']) ? trim($params['realname']) : '', 'password' => get_password($password), 'car_number' => isset($params['car_number']) ? trim($params['car_number']) : '', 'car_type' => isset($params['car_type']) ? trim($params['car_type']) : '', 'driving_license' => $drivingLicense, 'drivers_license' => $driversLicense, 'code' => strtoupper(get_random_code(9, 'D', "{$id}")), 'app_uuid' => $uuid, 'device' => $appSources == 'android' ? 2 : 1, 'create_time' => time(), 'status' => 1, 'picker_status' => 1, 'confirm_remark' => '', 'confirm_status' => 2, 'balance' => 0, 'deposit' => 0, 'mark' => 1, 'login_ip' => get_client_ip(), ]; if (!$userId = $this->model->insertGetId($data)) { $this->error = 2003; return false; } // 获取登录授权token $jwt = new Jwt('jwt_jd_app'); $token = $jwt->getToken($userId); // 结果返回 $result = [ 'access_token' => $token, 'info' => ['uid' => $userId, 'nickname' => $data['nickname']], ]; // 注册成功 $this->error = 2004; $data['token'] = $token; unset($data['password']); unset($data['mobile']); RedisService::keyDel("caches:members:count*"); RedisService::set("auths:info:{$userId}", $data, 24 * 3600); return $result; } /** * 设置资料 * @param $userId * @param $params * @return bool */ public function setEntry($userId, $params) { $cacheLockKey = "caches:members:profile_{$userId}"; if (RedisService::get($cacheLockKey)) { $this->error = 1034; return false; } // 用户验证 RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3)); $info = $this->model->where(['id' => $userId, 'mark' => 1]) ->select(['id', 'password', 'status']) ->first(); if (!$info || $info['status'] != 1) { $this->error = 1043; RedisService::clear($cacheLockKey); return false; } // 获取头像 $avatar = ''; if (isset($params['avatar']) && $params['avatar']) { $avatar = save_base64_image($params['avatar'], 'avatar'); } // $data = [ 'avatar' => $avatar, 'nickname' => isset($params['nickname']) ? trim($params['nickname']) : '', 'update_time' => time() ]; if (isset($params['province']) && $params['city']) { $data['province'] = isset($params['province']) ? trim($params['province']) : ''; $data['city'] = isset($params['city']) ? trim($params['city']) : ''; $data['district'] = isset($params['district']) ? trim($params['district']) : ''; } if (!$this->model->where(['id' => $userId])->update($data)) { $this->error = 1020; RedisService::clear($cacheLockKey); return false; } $this->error = 1019; RedisService::clear($cacheLockKey); return true; } /** * 获取资料详情 * @param $where * @param array $field */ public function getInfo($where, array $field = [], $refresh = true) { if (empty($where)) { return false; } $fieldKey = $field ? '_' . md5(json_encode($field)) : ''; $cacheKey = "caches:members:info_" . (!is_array($where) ? $where . $fieldKey : md5(json_encode($where) . $fieldKey)); $info = RedisService::get($cacheKey); if ($info && !$refresh) { return $info; } $defaultField = ['id', 'user_type', 'realname', 'mobile', 'nickname', 'balance', 'code', 'openid', 'status', 'avatar']; $field = $field ? $field : $defaultField; if (is_array($where)) { $info = $this->model->with(['store', 'agent'])->where(['mark' => 1])->where($where)->select($field)->first(); } else { $info = $this->model->with(['store', 'agent'])->where(['mark' => 1])->where(['id' => (int)$where])->select($field)->first(); } $info = $info ? $info->toArray() : []; if ($info) { if (isset($info['avatar'])) { $info['avatar'] = $info['avatar'] ? get_image_url($info['avatar']) : ''; } if (isset($info['mobile'])) { $info['mobile_text'] = $info['mobile'] ? format_mobile($info['mobile']) : ''; } if (isset($info['create_time'])) { $info['create_at'] = datetime(strtotime($info['create_time'])); } $info['store'] = isset($info['store']) ? $info['store'] : []; $info['agent'] = isset($info['agent']) ? $info['agent'] : []; $info['agent_level'] = 0; $info['team_count'] = 0; $params = request()->all(); $type = isset($params['type'])?$params['type']:''; if ($type == 'agent' && $info['agent']) { $info['agent_level'] = $this->getAgentLevel($info['id']); $info['agent_level'] = $info['agent_level']>=2?2 : 1; $info['team_count'] = $this->getTeamCount($info['id']); } if($type == 'agent'){ $info['qrcode'] = MpService::make()->getMiniQrcode('pages/login/login',"rid={$info['id']}"); $info['qrcode'] = $info['qrcode']? get_image_url($info['qrcode']):''; }else if($type == 'center'){ $info['order1'] = OrderService::make()->getCountByStatus($info['id'], 1); $info['cart_count'] = CartService::make()->getCount($info['id']); $info['social_open'] = ConfigService::make()->getConfigByCode('social_open',0); } RedisService::set($cacheKey, $info, rand(30, 60)); } return $info; } /** * 收款账户 * @param $userId * @return array|mixed */ public function accountInfo($userId,$type=0) { $cacheKey = "caches:members:account:{$userId}_{$type}"; $datas = RedisService::get($cacheKey); if ($datas) { return $type?(isset($datas[$type-1])?$datas[$type-1]:[]):$datas; } $datas[0] = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'status'=>1,'mark'=>1]) ->select(['id','user_id','type','realname','account','account_remark','status']) ->first(); $datas[0] = $datas[0]? $datas[0] : ['id'=>0,'type'=>1]; $datas[1] = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'status'=>1,'mark'=>1]) ->select(['id','user_id','type','realname','account','account_remark','status']) ->first(); $datas[1] = $datas[1]? $datas[1] : ['id'=>0,'type'=>2]; if($datas){ RedisService::set($cacheKey, $datas, rand(5,10)); }else{ $datas = [['id'=>0,'type'=>1],['id'=>0,'type'=>2]]; } return $type?(isset($datas[$type-1])?$datas[$type-1]:[]):$datas; } /** * 绑定收款账户 * @param $userId * @return array|mixed */ public function bindAccount($userId, $params) { if($params['type']==1){ $alipay = MemberBankModel::where(['type'=>1,'user_id'=>$userId,'mark'=>1]) ->select(['id','user_id','type','realname','account','account_remark','status']) ->first(); $alipayId = isset($alipay['id'])?$alipay['id'] : 0; $data = [ 'type'=> 1, 'user_id'=> $userId, 'realname'=>$params['realname'], 'account'=>$params['account'], 'account_remark'=>isset($params['account_remark']) && $params['account_remark']?$params['account_remark']:'支付宝', 'status'=>1 ]; if($alipayId){ $data['update_time']=time(); MemberBankModel::where(['id'=>$alipayId])->update($data); }else { $data['create_time']=time(); MemberBankModel::insertGetId($data); } } else if($params['type']==2){ $banks = MemberBankModel::where(['type'=>2,'user_id'=>$userId,'mark'=>1]) ->select(['id','user_id','type','realname','account','account_remark','status']) ->first(); $bankId = isset($banks['id'])?$banks['id'] : 0; $data = [ 'type'=>2, 'user_id'=> $userId, 'realname'=>$params['realname'], 'account'=>$params['account'], 'account_remark'=>$params['account_remark'], 'status'=>1 ]; if($bankId){ $data['update_time']=time(); MemberBankModel::where(['id'=>$bankId])->update($data); }else { $data['create_time']=time(); MemberBankModel::insertGetId($data); } }else{ $this->error = '账号类型错误'; return false; } RedisService::keyDel("caches:members:account:{$userId}*"); $this->error = '绑定收款账号成功'; return true; } /** * 获取代理等级 * @param $uid * @return array|int|mixed */ public function getAgentLevel($uid) { $cacheKey = "caches:members:agentLevel:{$uid}"; $data = RedisService::get($cacheKey); if ($data) { return $data; } $data = $this->model->from('member as a') ->leftJoin('agents as b', function ($join) { $join->on('b.user_id', '=', 'a.id')->where(['b.status' => 1, 'b.mark' => 1]); }) ->where('b.id', '>', 0) ->where('a.parents', 'like', "%,{$uid},%") ->where(['a.status' => 1, 'a.mark' => 1]) ->select(['a.id', 'a.parents']) ->orderBy('a.parents', 'desc') ->first(); $data = $data ? $data->toArray() : []; $parents = isset($data['parents']) && $data['parents'] ? trim($data['parents'], ',') : ''; $parents = $parents ? explode(',', $parents) : []; $level = $parents ? count($parents) : 0; if($level){ RedisService::set($cacheKey, $level, rand(5,10)); } return $level; } /** * 团队人数 * @param $uid * @return array|int|mixed */ public function getTeamCount($uid) { $cacheKey = "caches:members:teamCount:{$uid}"; $data = RedisService::get($cacheKey); if ($data) { return $data; } $data = $this->model->from('member as a') ->where('a.parents', 'like', "%,{$uid},%") ->where(['a.status' => 1, 'a.mark' => 1]) ->count('id'); if($data){ RedisService::set($cacheKey, $data, rand(5,10)); } return $data; } /** * 生成普通参数二维码 * @param $str 参数 * @param bool $refresh 是否重新生成 * @return bool */ public function makeQrcode($str, $refresh = false, $size = 4, $margin = 2, $level = 2) { $basePath = base_path() . '/public'; $qrFile = '/images/qrcode/'; if (!is_dir($basePath . '/uploads' . $qrFile)) { @mkdir($basePath . '/uploads' . $qrFile, 0755, true); } $key = date('Ymd') . strtoupper(md5($str . '_' . $size . $margin . $level)); $qrFile = $qrFile . "C_{$key}.png"; $cacheKey = "caches:qrcodes:member_" . $key; if (RedisService::get($cacheKey) && is_file($basePath . '/uploads' . $qrFile) && !$refresh) { return $qrFile; } QRcode::png($str, $basePath . '/uploads' . $qrFile, $level, $size, $margin); if (!file_exists($basePath . '/uploads' . $qrFile)) { return false; } RedisService::set($cacheKey, ['str' => $str, 'qrcode' => $qrFile, 'date' => date('Y-m-d H:i:s')], 7 * 24 * 3600); return $qrFile; } /** * 修改头像 * @param $userId * @param $avatar * @return mixed */ public function saveAvatar($userId, $avatar) { $oldAvatar = $this->model->where(['id' => $userId])->value('avatar'); if ($this->model->where(['id' => $userId])->update(['avatar' => $avatar, 'update_time' => time()])) { if ($oldAvatar && file_exists(ATTACHMENT_PATH . $oldAvatar)) { @unlink(ATTACHMENT_PATH . $oldAvatar); } return true; } return false; } /** * 修改账号信息 * @param $userId * @param $params * @return bool */ public function modify($userId, $params) { $cacheLockKey = "caches:members:modify_{$userId}"; if (RedisService::get($cacheLockKey)) { $this->error = 1034; return false; } // 用户验证 RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(2, 3)); $info = $this->model->where(['id' => $userId, 'mark' => 1]) ->select(['id', 'password', 'status']) ->first(); $userPassword = isset($info['password']) ? $info['password'] : ''; if (!$info || $info['status'] != 1) { $this->error = 1029; RedisService::clear($cacheLockKey); return false; } // 密码校验 $data = ['update_time' => time()]; // 修改数据 $nickname = isset($params['nickname']) ? $params['nickname'] : ''; if (isset($params['nickname']) && $nickname) { $data['nickname'] = $nickname; } $mobile = isset($params['mobile']) ? $params['mobile'] : ''; if (isset($params['mobile']) && $mobile) { $data['mobile'] = $mobile; } $address = isset($params['address']) ? $params['address'] : ''; if (isset($params['address']) && $address) { $data['address'] = $address; } $password = isset($params['password']) ? $params['password'] : ''; $newPassword = isset($params['new_password']) ? $params['new_password'] : ''; if (isset($params['password']) && $password) { if ($userPassword != get_password($password)) { $this->error = 1038; RedisService::clear($cacheLockKey); return false; } if (empty($newPassword)) { $this->error = 1039; RedisService::clear($cacheLockKey); return false; } $data['password'] = get_password($newPassword); } // 头像 $avatar = isset($params['avatar']) ? $params['avatar'] : ''; if (isset($params['avatar']) && $avatar) { $data['avatar'] = get_image_path($avatar); } if (!$this->model->where(['id' => $userId])->update($data)) { $this->error = 1014; RedisService::clear($cacheLockKey); return false; } $oldAvatar = isset($info['avatar']) ? $info['avatar'] : ''; if ($avatar && $oldAvatar && ($avatar != $oldAvatar) && file_exists(ATTACHMENT_PATH . $oldAvatar)) { @unlink(ATTACHMENT_PATH . $oldAvatar); } $this->error = 1013; RedisService::clear($cacheLockKey); return true; } /** * 账号注销 * @param $userId * @return bool */ public function logOff($userId) { $info = $this->model->where(['id' => $userId, 'mark' => 1]) ->select(['id', 'password', 'status']) ->first(); $status = isset($info['status']) ? $info['status'] : 0; if (empty($info)) { $this->error = 2044; return false; } if ($status != 1) { $this->error = 2044; return false; } if (OrderModel::whereIn('status', [1, 2])->where(['user_id' => $userId, 'mark' => 1])->value('id')) { $this->error = 2045; return false; } if (DepositModel::where('refund_status', 1)->where(['user_id' => $userId, 'mark' => 1])->value('id')) { $this->error = 2046; return false; } if (BalanceLogModel::where('status', 1)->where(['user_id' => $userId, 'type' => 2, 'mark' => 1])->value('id')) { $this->error = 2047; return false; } if (!$this->model->where(['id' => $userId])->update(['status' => 3, 'update_time' => time()])) { $this->error = 2049; return false; } $this->error = 2048; RedisService::clear("auths:info:" . $userId); return true; } }