| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994 |
- <?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\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(),
- 'create_time' => time(),
- '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['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;
- }
- }
|