123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- namespace app\common\helpers;
- use app\modules\ucenter\models\User;
- use app\modules\ucenter\models\UserGroup;
- use app\modules\ucenter\models\UserGroupLevel;
- use app\modules\ucenter\models\UserLoginLog;
- use app\modules\ucenter\models\Message;
- use app\modules\ucenter\models\MessageRead;
- use app\modules\ucenter\models\ShareLog;
- use app\common\components\Wallet;
- use app\models\ContentModel;
- use Yii;
- //前台用户COOKIE生成,更新,销毁身份认证类
- class Identify
- {
- //传入用户对象,生成用户身份状态cookie
- public static function doLogin($user,$expire=0)
- {
- $identityInfo = array();
- $identityInfo['user_id'] = $user->user_id;
- $identityInfo['user_name'] = $user->user_name;
- $identityInfo['cookieHash'] = sys_auth($user->user_name);
- //上一次登录时间
- if($user->last_login_time)
- {
- $lastLoginLog = UserLoginLog::find()->where("user_id=".$user->user_id."")->orderBy(['id' => SORT_DESC])->limit(1)->one();
- $identityInfo['last_login_time'] = $lastLoginLog->login_time;
- }
- $identityInfo = sys_auth(array2string($identityInfo));
- if(!empty($expire))
- {
- Cookie::getInitCls()->setCookie(Yii::$app->params['userCookieName'],$identityInfo,$expire);
- }
- else
- {
- Cookie::getInitCls()->setCookie(Yii::$app->params['userCookieName'],$identityInfo);
- }
- $user->last_login_time=TIMESTAMP;
- $user->last_login_ip = MYIP;
- $user->save();
- //写登录日志
- $loginLog = new UserLoginLog();
- $loginLog->user_id = $user->user_id;
- $loginLog->login_ip = MYIP;
- $loginLog->login_time = TIMESTAMP;
- $loginLog->request_from = REQUEST_FROM;
- $loginLog->create_time = TIMESTAMP;
- $loginLog->update_time = TIMESTAMP;
- $loginLog->save();
- //更新用户关联数据
- \app\modules\ucenter\models\User::refreshData($user->user_id);
- return true;
- }
- //获取用户信息
- public static function getUserInfo($user_id=NULL,$key=NULL)
- {
- if(empty($user_id))
- {
- $identityInfo = Cookie::getInitCls()->getCookie(Yii::$app->params['userCookieName']);
- if(empty($identityInfo))
- {
- return null;
- }
- else
- {
- $identityInfo = string2array(sys_auth($identityInfo,'DECODE'));
- $user = User::findOne($identityInfo['user_id']);
- }
- }
- else
- {
- $identityInfo = [];
- $user = User::findOne($user_id);
- if(empty($user))
- {
- return null;
- }
- }
- $identityInfo['user_id'] = intval($user->user_id);
- $identityInfo['user_name'] = $user->user_name;
- $identityInfo['group_id'] = intval($user->group_id);
- $identityInfo['user_group_level'] = intval($user->user_group_level);
- $identityInfo['email']=$user->email;
- $identityInfo['mobile']=$user->mobile;
- $identityInfo['nick_name']=$user->nick_name;
- $identityInfo['real_name']=$user->real_name;
- $identityInfo['sex'] = $user->sex;
- $identityInfo['birthday'] = $user->birthday;
- $identityInfo['signature'] = $user->signature;
- $identityInfo['qq'] = $user->qq;
- $identityInfo['weixin'] = $user->weixin;
- $identityInfo['region_id']=$user->region_id;
- $identityInfo['address']=$user->address;
- $identityInfo['money'] = $user->money;
- $identityInfo['coin'] = $user->coin;
- $identityInfo['point'] = $user->point;
- $identityInfo['allpoint'] = $user->allpoint;
- $identityInfo['amount_money_income'] = $user->amount_money_income;
- $identityInfo['coin_income'] = $user->coin_income;
- $identityInfo['amount_coin_income'] = $user->amount_coin_income;
- $identityInfo['content_model_id'] = intval($user->content_model_id);
- $identityInfo['avatar'] = $user->avatar;
- $identityInfo['weixin_img'] = $user->weixin_img;
- $identityInfo['vip_info'] = $user?$user->vipInfo():null;
- $identityInfo['vip_settings'] = $user?$user->vipSettings():null;
- $identityInfo['org_vip_settings'] = $user?$user->vipSettings('org_vip_settings'):null;
- $identityInfo['vip_name'] = $user?$user->vipName():null;
- $identityInfo['org_vip_name'] = $user?$user->vipName('org_vip_settings'):null;
- $identityInfo['open_home'] = $user->open_home;
- $identityInfo['open_upload'] = $user->open_upload;
- $identityInfo['cart_num'] = 0;
- $identityInfo['referer_id'] = $user->referer_id;
- $identityInfo['referer_ids'] = $user->referer_ids;
- $identityInfo['is_lock'] = $user->is_lock;
- $identityInfo['is_delete'] = $user->is_delete;
- if($user)
- {
- //附表信息
- $tablename = userDetailTable($user->content_model_id);
- $userDetail = Yii::$app->db->createCommand("select * from $tablename where user_id=".$user->user_id)->queryOne();
- $identityInfo = array_merge($identityInfo,$userDetail);
- //用户组
- $identityInfo['group_name'] = $user->group?$user->group->group_name:'';
- $identityInfo['group_level_name'] = $user->grouplevel?$user->grouplevel->name:'';
- //获取用户等级基础权限
- $identityInfo['group_rights'] = string2array($user->grouplevel->settings);
- //获取未读消息数量
- $readIds = [];
- $readedMsgList = MessageRead::find()->where("user_id=".$user->user_id)->all();
- if(is_array($readedMsgList))foreach($readedMsgList as $readMsg)
- {
- $readIds[] = $readMsg->m_id;
- }
- $sysCount = Message::find()->where("message_type=1")->count();
- $sysCount = $sysCount-count($readIds);
- $commonCount = Message::find()->where("to_user=".$user->user_id." and have_read=0 and to_delete=0")->count();
- $identityInfo['msgcount'] = ($sysCount+$commonCount)>0?($sysCount+$commonCount):0;
- //获取关联机构信息
- $store = Yii::$app->db->createCommand("select * from {{%shopping_store}} where user_id=".$user->user_id)->queryOne();
- if($store)
- {
- $identityInfo['store'] = $store;
- $identityInfo['store_id'] = $store['store_id'];
- }
- //认证信息
- $certList = getSysconfigValue('user_cert_type');
- if(is_array($certList))foreach($certList as $k=>$name)
- {
- $identityInfo['cert_'.$k.'_name'] = $identityInfo['cert_'.$k]==1?$name:'';
- }
- }
- return !is_null($key)?$identityInfo[$key]:$identityInfo;
- }
- //判断用户是否登录,并验证信息是否合法
- public static function hasLogined()
- {
- $userInfo = self::getUserInfo();
- if(!empty($userInfo)&&sys_auth($userInfo['cookieHash'],'DECODE') == $userInfo['user_name'])
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //注销用户登录信息
- public static function logout()
- {
- Cookie::getInitCls()->deleteCookie(Yii::$app->params['userCookieName']);
- return true;
- }
- public static function test()
- {
- print_r(Yii::$app->controller->module->pointconfig);
- exit;
- }
- //创建新用户
- public static function createUser($userOpenAuth,$contentModelId)
- {
- //系统默认值
- $baseconfigResult = \app\modules\admin\models\Config::find()->where("name='baseconfig'")->one();
- $baseconfig = string2array($baseconfigResult->value);
- $pointconfigResult = \app\modules\admin\models\Config::find()->where("name='point'")->one();
- $pointconfig = string2array($pointconfigResult->value);
- if(empty($baseconfig['username_pre']))exit('请设置用户名前缀');
- $default_user_group = getDefaultUserGroup($contentModelId);
- if(empty($default_user_group))exit('请设置默认用户组');
- $default_user_group_level = getDefaultUserGroupLevel($contentModelId);
- if(empty($default_user_group_level))exit('请设置默认用户等级');
- //写用户
- $userInfo = string2array(base64_decode($userOpenAuth->user_info));
- $max_user = User::find()->orderBy(['user_id'=>SORT_DESC])->one();
- $user = new User();
- $user->request_from = (strpos($userOpenAuth->scene_id,'pc_')!==false||strpos($userOpenAuth->scene_id,'mplogin_')!==false)?1:REQUEST_FROM;
- $user->user_id = $max_user->user_id+1;
- $user->user_name = $baseconfig['username_pre'].str_pad($max_user->user_id+1,10,'0',STR_PAD_LEFT);
- $user->nick_name = $userInfo['nick_name']?$userInfo['nick_name']:Yii::$app->params['defaultNickName'];
- $user->avatar = $userInfo['avatar']?$userInfo['avatar']:'';
- $user->mobile = $userInfo['mobile']?$userInfo['mobile']:'';
- $user->sex = intval($userInfo['sex']);
- //用于分销的参数
- $share_no = \app\common\helpers\Cookie::getCookie(Yii::$app->params['shareArgName']);
- if(!empty($share_no))
- {
- $shareResult = getReferIdsByShareNo($share_no);
- $user->referer_id = $shareResult['referer_id'];
- $user->referer_ids = $shareResult['referer_ids'];
- }
- else
- {
- $user->referer_id = 0;
- }
- $user->register_time = TIMESTAMP;
- $user->user_group_level = $default_user_group_level;//默认用户等级
- $user->group_id = $default_user_group;//默认用户组
- $user->content_model_id = $contentModelId;
- if($user->save())
- {
- //邀请注册积分奖励
- if($pointconfig['invite_register_prize']>0&&$user->referer_id>0)
- {
- Wallet::pointChange($user->referer_id,$pointconfig['invite_register_prize'],'邀请注册',1,'point');
- }
- $tableName = userDetailTable($contentModelId);
- $sql = "insert into $tableName set user_id=".$user->user_id."";
- Yii::$app->db->createCommand($sql)->execute();
- //百度竞价跟踪
- $bd_vid = \app\common\helpers\Cookie::getCookie('bd_vid');
- if(!empty($bd_vid))
- {
- $higherconfigResult = \app\modules\admin\models\Config::find()->where("name='higherconfig'")->one();
- $higherconfig = string2array($higherconfigResult->value);
- $token = $higherconfig['reg_cpc_token'];
- if(!empty($token))
- {
- $cv = array(
- 'logidUrl' => APP_URL.'?bd_vid='.$bd_vid, // 您的落地页url
- 'newType' => 49 // 转化类型请按实际情况填写
- );
- $conversionTypes = array($cv);
- $baiducpc = new \app\common\components\BaiduCpc();
- $baiducpc->sendConvertData($token, $conversionTypes);
- }
- }
- return $user;
- }
- else
- {
- return false;
- }
- }
- }
|