xiao9469 发表于 2021-12-29 17:16:37

discuz 获取用户资料getuserprofile函数解析

此函数,提供一个参数,内容为用户的字段名,即可返回出来他值的内容
如:我要获取用户
威望::getuserprofile('extcredits1')
金钱: getuserprofile('extcredits2')
贡献:getuserprofile('extcredits3')
用户uid:getuserprofile(uid')
听从:getuserprofile('follower ')
收听:getuserprofile('following ')
主题: getuserprofile('threads')

函数定义:function_core.php
函数参数:
field:资料段名称
返回值:存在返回资料内容反正返回null
function getuserprofile($field) {      global $_G;      if(isset($_G['member'][$field])) {                return $_G['member'][$field];      }      static $tablefields = array(                'count'                => array('extcredits1','extcredits2','extcredits3','extcredits4','extcredits5','extcredits6','extcredits7','extcredits8','friends','posts','threads','digestposts','doings','blogs','albums','sharings','attachsize','views','oltime','todayattachs','todayattachsize', 'follower', 'following', 'newfollower', 'blacklist'),                'status'      => array('regip','lastip','lastvisit','lastactivity','lastpost','lastsendmail','invisible','buyercredit','sellercredit','favtimes','sharetimes','profileprogress'),                'field_forum'      => array('publishfeed','customshow','customstatus','medals','sightml','groupterms','authstr','groups','attentiongroup'),                'field_home'      => array('videophoto','spacename','spacedescription','domain','addsize','addfriend','menunum','theme','spacecss','blockposition','recentnote','spacenote','privacy','feedfriend','acceptemail','magicgift','stickblogs'),                'profile'      => array('realname','gender','birthyear','birthmonth','birthday','constellation','zodiac','telephone','mobile','idcardtype','idcard','address','zipcode','nationality','birthprovince','birthcity','resideprovince','residecity','residedist','residecommunity','residesuite','graduateschool','company','education','occupation','position','revenue','affectivestatus','lookingfor','bloodtype','height','weight','alipay','icq','qq','yahoo','msn','taobao','site','bio','interest','field1','field2','field3','field4','field5','field6','field7','field8'),                'verify'      => array('verify1', 'verify2', 'verify3', 'verify4', 'verify5', 'verify6', 'verify7'),      );      $profiletable = '';      foreach($tablefields as $table => $fields) {                if(in_array($field, $fields)) {                        $profiletable = $table;                        break;                }      }      if($profiletable) {                if(is_array($_G['member']) && $_G['member']['uid']) {                        space_merge($_G['member'], $profiletable);                } else {                        foreach($tablefields[$profiletable] as $k) {                              $_G['member'][$k] = '';                        }                }                return $_G['member'][$field];      }      return null;}

婷姐 发表于 2021-12-29 17:17:29

这个是看起来很专业的,这个是不错的,

Discuz智能体 发表于 2025-3-21 05:32:25

`getuserprofile` 是 Discuz! 中用于获取用户资料的一个函数。它通过传入一个字段名来返回该字段对应的用户资料内容。这个函数在 `function_core.php` 文件中定义,主要用于从全局变量 `$_G` 中获取用户的相关信息。

### 函数解析

#### 函数定义
function getuserprofile($field) {
    global $_G;
    if(isset($_G)) {
      return $_G;
    }
    static $tablefields = array(
      'count' => array('extcredits1','extcredits2','extcredits3','extcredits4','extcredits5','extcredits6','extcredits7','extcredits8','friends','posts','threads','digestposts','doings','blogs','albums','sharings','attachsize','views','oltime','todayattachs','todayattachsize', 'follower', 'following', 'newfollower', 'blacklist'),
      'status' => array('regip','lastip','lastvisit','lastactivity','lastpost','lastsendmail','invisible','buyercredit','sellercredit','favtimes','sharetimes','profileprogress'),
      'field_forum' => array('publishfeed','customshow','customstatus','medals','sightml','groupterms','authstr','groups','attentiongroup'),
      'field_home' => array('videophoto','spacename','spacedescription','domain','addsize','addfriend','menunum','theme','spacecss','blockposition','recentnote','spacenote','privacy','feedfriend','acceptemail','magicgift','stickblogs'),
      'profile' => array('realname','gender','birthyear','birthmonth','birthday','constellation','zodiac','telephone','mobile','idcardtype','idcard','address','zipcode','nationality','birthprovince','birthcity','resideprovince','residecity','residedist','residecommunity','residesuite','graduateschool','company','education','occupation','position','revenue','affectivestatus','lookingfor','bloodtype','height','weight','alipay','icq','qq','yahoo','msn','taobao','site','bio','interest','field1','field2','field3','field4','field5','field6','field7','field8'),
      'verify' => array('verify1', 'verify2', 'verify3', 'verify4', 'verify5', 'verify6', 'verify7'),
    );
    $profiletable = '';
    foreach($tablefields as $table => $fields) {
      if(in_array($field, $fields)) {
            $profiletable = $table;
            break;
      }
    }
    if($profiletable) {
      if(is_array($_G) && $_G) {
            space_merge($_G, $profiletable);
      } else {
            foreach($tablefields as $k) {
                $_G = '';
            }
      }
      return $_G;
    }
    return null;
}

#### 参数说明
- `$field`:需要获取的用户资料字段名。

#### 返回值
- 如果字段存在,则返回对应的用户资料内容。
- 如果字段不存在,则返回 `null`。

#### 函数逻辑
1. **全局变量 `$_G`**:`$_G` 是 Discuz! 中的一个全局变量,存储了当前用户的各种信息。
2. **静态变量 `$tablefields`**:这个数组定义了不同类别的用户资料字段,分别对应不同的数据库表或数据来源。
   - `count`:用户的一些统计信息,如积分、帖子数、好友数等。
   - `status`:用户的状态信息,如注册IP、最后登录时间等。
   - `field_forum`:论坛相关的用户字段。
   - `field_home`:用户空间相关的字段。
   - `profile`:用户的个人资料字段,如姓名、性别、生日等。
   - `verify`:用户的认证信息。
3. **查找字段对应的表**:通过遍历 `$tablefields`,找到传入的 `$field` 对应的表名 `$profiletable`。
4. **合并数据**:如果找到了对应的表名,则调用 `space_merge` 函数将对应的用户数据合并到 `$_G` 中。
5. **返回结果**:如果字段存在,则返回 `$_G` 中对应的值;否则返回 `null`。

### 使用示例
// 获取用户的威望
$extcredits1 = getuserprofile('extcredits1');

// 获取用户的金钱
$extcredits2 = getuserprofile('extcredits2');

// 获取用户的贡献
$extcredits3 = getuserprofile('extcredits3');

// 获取用户的UID
$uid = getuserprofile('uid');

// 获取用户的粉丝数
$follower = getuserprofile('follower');

// 获取用户的关注数
$following = getuserprofile('following');

// 获取用户发布的主题数
$threads = getuserprofile('threads');

### 注意事项
1. **字段名**:确保传入的字段名在 `$tablefields` 中有定义,否则会返回 `null`。
2. **全局变量 `$_G`**:`$_G` 必须在函数调用前已经初始化,否则函数会直接返回 `$_G` 而不进行任何处理。
3. **数据合并**:`space_merge` 函数用于将指定表的数据合并到 `$_G` 中,确保数据是最新的。

### 总结
`getuserprofile` 是一个非常有用的函数,可以方便地获取用户的各类信息。通过传入不同的字段名,可以获取到用户的积分、状态、个人资料等信息。在使用时,确保传入的字段名正确,并且 `$_G` 已经初始化。

如果你有更多关于 Discuz! 开发的问题,欢迎随时提问!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
页: [1]
查看完整版本: discuz 获取用户资料getuserprofile函数解析