Identify.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace app\common\helpers;
  3. use app\modules\ucenter\models\User;
  4. use app\modules\ucenter\models\UserGroup;
  5. use app\modules\ucenter\models\UserGroupLevel;
  6. use app\modules\ucenter\models\UserLoginLog;
  7. use app\modules\ucenter\models\Message;
  8. use app\modules\ucenter\models\MessageRead;
  9. use app\modules\ucenter\models\ShareLog;
  10. use app\common\components\Wallet;
  11. use app\models\ContentModel;
  12. use Yii;
  13. //前台用户COOKIE生成,更新,销毁身份认证类
  14. class Identify
  15. {
  16. //传入用户对象,生成用户身份状态cookie
  17. public static function doLogin($user,$expire=0)
  18. {
  19. $identityInfo = array();
  20. $identityInfo['user_id'] = $user->user_id;
  21. $identityInfo['user_name'] = $user->user_name;
  22. $identityInfo['cookieHash'] = sys_auth($user->user_name);
  23. //上一次登录时间
  24. if($user->last_login_time)
  25. {
  26. $lastLoginLog = UserLoginLog::find()->where("user_id=".$user->user_id."")->orderBy(['id' => SORT_DESC])->limit(1)->one();
  27. $identityInfo['last_login_time'] = $lastLoginLog->login_time;
  28. }
  29. $identityInfo = sys_auth(array2string($identityInfo));
  30. if(!empty($expire))
  31. {
  32. Cookie::getInitCls()->setCookie(Yii::$app->params['userCookieName'],$identityInfo,$expire);
  33. }
  34. else
  35. {
  36. Cookie::getInitCls()->setCookie(Yii::$app->params['userCookieName'],$identityInfo);
  37. }
  38. $user->last_login_time=TIMESTAMP;
  39. $user->last_login_ip = MYIP;
  40. $user->save();
  41. //写登录日志
  42. $loginLog = new UserLoginLog();
  43. $loginLog->user_id = $user->user_id;
  44. $loginLog->login_ip = MYIP;
  45. $loginLog->login_time = TIMESTAMP;
  46. $loginLog->request_from = REQUEST_FROM;
  47. $loginLog->create_time = TIMESTAMP;
  48. $loginLog->update_time = TIMESTAMP;
  49. $loginLog->save();
  50. //更新用户关联数据
  51. \app\modules\ucenter\models\User::refreshData($user->user_id);
  52. return true;
  53. }
  54. //获取用户信息
  55. public static function getUserInfo($user_id=NULL,$key=NULL)
  56. {
  57. if(empty($user_id))
  58. {
  59. $identityInfo = Cookie::getInitCls()->getCookie(Yii::$app->params['userCookieName']);
  60. if(empty($identityInfo))
  61. {
  62. return null;
  63. }
  64. else
  65. {
  66. $identityInfo = string2array(sys_auth($identityInfo,'DECODE'));
  67. $user = User::findOne($identityInfo['user_id']);
  68. }
  69. }
  70. else
  71. {
  72. $identityInfo = [];
  73. $user = User::findOne($user_id);
  74. if(empty($user))
  75. {
  76. return null;
  77. }
  78. }
  79. $identityInfo['user_id'] = intval($user->user_id);
  80. $identityInfo['user_name'] = $user->user_name;
  81. $identityInfo['group_id'] = intval($user->group_id);
  82. $identityInfo['user_group_level'] = intval($user->user_group_level);
  83. $identityInfo['email']=$user->email;
  84. $identityInfo['mobile']=$user->mobile;
  85. $identityInfo['nick_name']=$user->nick_name;
  86. $identityInfo['real_name']=$user->real_name;
  87. $identityInfo['sex'] = $user->sex;
  88. $identityInfo['birthday'] = $user->birthday;
  89. $identityInfo['signature'] = $user->signature;
  90. $identityInfo['qq'] = $user->qq;
  91. $identityInfo['weixin'] = $user->weixin;
  92. $identityInfo['region_id']=$user->region_id;
  93. $identityInfo['address']=$user->address;
  94. $identityInfo['money'] = $user->money;
  95. $identityInfo['coin'] = $user->coin;
  96. $identityInfo['point'] = $user->point;
  97. $identityInfo['allpoint'] = $user->allpoint;
  98. $identityInfo['amount_money_income'] = $user->amount_money_income;
  99. $identityInfo['coin_income'] = $user->coin_income;
  100. $identityInfo['amount_coin_income'] = $user->amount_coin_income;
  101. $identityInfo['content_model_id'] = intval($user->content_model_id);
  102. $identityInfo['avatar'] = $user->avatar;
  103. $identityInfo['weixin_img'] = $user->weixin_img;
  104. $identityInfo['vip_info'] = $user?$user->vipInfo():null;
  105. $identityInfo['vip_settings'] = $user?$user->vipSettings():null;
  106. $identityInfo['org_vip_settings'] = $user?$user->vipSettings('org_vip_settings'):null;
  107. $identityInfo['vip_name'] = $user?$user->vipName():null;
  108. $identityInfo['org_vip_name'] = $user?$user->vipName('org_vip_settings'):null;
  109. $identityInfo['open_home'] = $user->open_home;
  110. $identityInfo['open_upload'] = $user->open_upload;
  111. $identityInfo['cart_num'] = 0;
  112. $identityInfo['referer_id'] = $user->referer_id;
  113. $identityInfo['referer_ids'] = $user->referer_ids;
  114. $identityInfo['is_lock'] = $user->is_lock;
  115. $identityInfo['is_delete'] = $user->is_delete;
  116. if($user)
  117. {
  118. //附表信息
  119. $tablename = userDetailTable($user->content_model_id);
  120. $userDetail = Yii::$app->db->createCommand("select * from $tablename where user_id=".$user->user_id)->queryOne();
  121. $identityInfo = array_merge($identityInfo,$userDetail);
  122. //用户组
  123. $identityInfo['group_name'] = $user->group?$user->group->group_name:'';
  124. $identityInfo['group_level_name'] = $user->grouplevel?$user->grouplevel->name:'';
  125. //获取用户等级基础权限
  126. $identityInfo['group_rights'] = string2array($user->grouplevel->settings);
  127. //获取未读消息数量
  128. $readIds = [];
  129. $readedMsgList = MessageRead::find()->where("user_id=".$user->user_id)->all();
  130. if(is_array($readedMsgList))foreach($readedMsgList as $readMsg)
  131. {
  132. $readIds[] = $readMsg->m_id;
  133. }
  134. $sysCount = Message::find()->where("message_type=1")->count();
  135. $sysCount = $sysCount-count($readIds);
  136. $commonCount = Message::find()->where("to_user=".$user->user_id." and have_read=0 and to_delete=0")->count();
  137. $identityInfo['msgcount'] = ($sysCount+$commonCount)>0?($sysCount+$commonCount):0;
  138. //获取关联机构信息
  139. $store = Yii::$app->db->createCommand("select * from {{%shopping_store}} where user_id=".$user->user_id)->queryOne();
  140. if($store)
  141. {
  142. $identityInfo['store'] = $store;
  143. $identityInfo['store_id'] = $store['store_id'];
  144. }
  145. //认证信息
  146. $certList = getSysconfigValue('user_cert_type');
  147. if(is_array($certList))foreach($certList as $k=>$name)
  148. {
  149. $identityInfo['cert_'.$k.'_name'] = $identityInfo['cert_'.$k]==1?$name:'';
  150. }
  151. }
  152. return !is_null($key)?$identityInfo[$key]:$identityInfo;
  153. }
  154. //判断用户是否登录,并验证信息是否合法
  155. public static function hasLogined()
  156. {
  157. $userInfo = self::getUserInfo();
  158. if(!empty($userInfo)&&sys_auth($userInfo['cookieHash'],'DECODE') == $userInfo['user_name'])
  159. {
  160. return true;
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. }
  167. //注销用户登录信息
  168. public static function logout()
  169. {
  170. Cookie::getInitCls()->deleteCookie(Yii::$app->params['userCookieName']);
  171. return true;
  172. }
  173. public static function test()
  174. {
  175. print_r(Yii::$app->controller->module->pointconfig);
  176. exit;
  177. }
  178. //创建新用户
  179. public static function createUser($userOpenAuth,$contentModelId)
  180. {
  181. //系统默认值
  182. $baseconfigResult = \app\modules\admin\models\Config::find()->where("name='baseconfig'")->one();
  183. $baseconfig = string2array($baseconfigResult->value);
  184. $pointconfigResult = \app\modules\admin\models\Config::find()->where("name='point'")->one();
  185. $pointconfig = string2array($pointconfigResult->value);
  186. if(empty($baseconfig['username_pre']))exit('请设置用户名前缀');
  187. $default_user_group = getDefaultUserGroup($contentModelId);
  188. if(empty($default_user_group))exit('请设置默认用户组');
  189. $default_user_group_level = getDefaultUserGroupLevel($contentModelId);
  190. if(empty($default_user_group_level))exit('请设置默认用户等级');
  191. //写用户
  192. $userInfo = string2array(base64_decode($userOpenAuth->user_info));
  193. $max_user = User::find()->orderBy(['user_id'=>SORT_DESC])->one();
  194. $user = new User();
  195. $user->request_from = (strpos($userOpenAuth->scene_id,'pc_')!==false||strpos($userOpenAuth->scene_id,'mplogin_')!==false)?1:REQUEST_FROM;
  196. $user->user_id = $max_user->user_id+1;
  197. $user->user_name = $baseconfig['username_pre'].str_pad($max_user->user_id+1,10,'0',STR_PAD_LEFT);
  198. $user->nick_name = $userInfo['nick_name']?$userInfo['nick_name']:Yii::$app->params['defaultNickName'];
  199. $user->avatar = $userInfo['avatar']?$userInfo['avatar']:'';
  200. $user->mobile = $userInfo['mobile']?$userInfo['mobile']:'';
  201. $user->sex = intval($userInfo['sex']);
  202. //用于分销的参数
  203. $share_no = \app\common\helpers\Cookie::getCookie(Yii::$app->params['shareArgName']);
  204. if(!empty($share_no))
  205. {
  206. $shareResult = getReferIdsByShareNo($share_no);
  207. $user->referer_id = $shareResult['referer_id'];
  208. $user->referer_ids = $shareResult['referer_ids'];
  209. }
  210. else
  211. {
  212. $user->referer_id = 0;
  213. }
  214. $user->register_time = TIMESTAMP;
  215. $user->user_group_level = $default_user_group_level;//默认用户等级
  216. $user->group_id = $default_user_group;//默认用户组
  217. $user->content_model_id = $contentModelId;
  218. if($user->save())
  219. {
  220. //邀请注册积分奖励
  221. if($pointconfig['invite_register_prize']>0&&$user->referer_id>0)
  222. {
  223. Wallet::pointChange($user->referer_id,$pointconfig['invite_register_prize'],'邀请注册',1,'point');
  224. }
  225. $tableName = userDetailTable($contentModelId);
  226. $sql = "insert into $tableName set user_id=".$user->user_id."";
  227. Yii::$app->db->createCommand($sql)->execute();
  228. //百度竞价跟踪
  229. $bd_vid = \app\common\helpers\Cookie::getCookie('bd_vid');
  230. if(!empty($bd_vid))
  231. {
  232. $higherconfigResult = \app\modules\admin\models\Config::find()->where("name='higherconfig'")->one();
  233. $higherconfig = string2array($higherconfigResult->value);
  234. $token = $higherconfig['reg_cpc_token'];
  235. if(!empty($token))
  236. {
  237. $cv = array(
  238. 'logidUrl' => APP_URL.'?bd_vid='.$bd_vid, // 您的落地页url
  239. 'newType' => 49 // 转化类型请按实际情况填写
  240. );
  241. $conversionTypes = array($cv);
  242. $baiducpc = new \app\common\components\BaiduCpc();
  243. $baiducpc->sendConvertData($token, $conversionTypes);
  244. }
  245. }
  246. return $user;
  247. }
  248. else
  249. {
  250. return false;
  251. }
  252. }
  253. }