·设为首页收藏本站📧邮箱修改🎁免费下载专区📒收藏夹👽聊天室📱AI智能体
返回列表 发布新帖

DISCUZ X3.0 积分增减以及记录

754 2
发表于 2021-12-29 16:55:55 | 查看全部 阅读模式

马上注册,免费下载更多dz插件网资源。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1、调用source/function/function_core.php 里面的 updatemembercount()方法,该方法只是一个简单的入口方法
/*
* @$uids 用户
* @$dataarr 操作规则,如扣减第二个积分2分:array (‘extcredits2‘ => -2);
* @$checkgroup 是否检查用户组升级,通常未true
* @$operation 操作类型,默认空,如果需要增加记录,需要填充
* @$relatedid 关系ID,例如帖子ID
* @$ruletxt 积分规则文本(黄色框提示扣费的文字显示)
* @$customtitle 如果没有操作类型,则会默认显示这个为记录的title
* @$custommemo 这个是记录的详情
**/
function updatemembercount($uids, $dataarr = array(), $checkgroup = true, $operation = ‘‘, $relatedid = 0, $ruletxt = ‘‘, $customtitle = ‘‘, $custommemo = ‘‘) {
if(!empty($uids) && (is_array($dataarr) && $dataarr)) {
require_once libfile(‘function/credit‘);
return _updatemembercount($uids, $dataarr, $checkgroup, $operation, $relatedid, $ruletxt, $customtitle, $custommemo);
}
return true;
}
2、该方法中调用了source/function/function_credit.php 里面的_updatemembercount()方法,函数原型如下:
/*
* @$uids 用户
* @$dataarr 操作规则,如扣减第二个积分2分:array (‘extcredits2‘ => -2);
* @$checkgroup 是否检查用户组升级,通常未true
* @$operation 操作类型,默认空,如果需要增加记录,需要填充
* @$relatedid 关系ID,例如帖子ID
* @$ruletxt 积分规则文本(黄色框提示扣费的文字显示)
* @$customtitle 如果没有操作类型,则会默认显示这个为记录的title
* @$custommemo 这个是记录的详情
* 以上传入参数基本由上一个入口方法updatemembercount()引入
**/
function _updatemembercount($uids, $dataarr = array(), $checkgroup = true, $operation = ‘‘, $relatedid = 0, $ruletxt = ‘‘, $customtitle = ‘‘, $custommemo = ‘‘) {
if(empty($uids)) return;//用户不能为空
if(!is_array($dataarr) || empty($dataarr)) return;//操作数组不能为空
if($operation && $relatedid || $customtitle) {
$writelog = true;//必须要有操作类型以及关联ID或者有自定义的操作标题$customtitle才写入记录
} else {
$writelog = false;
}
$data = $log = array();
foreach($dataarr as $key => $val) {//操作数组解析算法
if(empty($val)) continue;
$val = intval($val);
$id = intval($key);
$id = !$id && substr($key, 0, -1) == ‘extcredits‘ ? intval(substr($key, -1, 1)) : $id;
if(0 < $id && $id < 9) {
$data[‘extcredits‘.$id] = $val;
if($writelog) {
$log[‘extcredits‘.$id] = $val;
}
} else {
$data[$key] = $val;
}
}
if($writelog) {//增加记录
credit_log($uids, $operation, $relatedid, $log, $customtitle, $custommemo);
}
if($data) {//引入新的类中的同入口方法名的方法
include_once libfile(‘class/credit‘);
$credit = & credit::instance();
$credit->updatemembercount($data, $uids, $checkgroup, $ruletxt);
}
}
3、记录增加:引用了source/function/function_credit.php 里面的 credit_log() 方法,函数原型如下:
/*
* @$uids 用户
* @$operation 操作类型,默认空,如果需要增加记录,需要填充
* @$relatedid 关系ID,例如帖子ID
* @$data 积分增减记录数组
* @$customtitle 如果没有操作类型,则会默认显示这个为记录的title
* @$custommemo 这个是记录的详情
* 以上传入参数基本由入口方法updatemembercount()引入,至此积分操作记录增加完毕
**/
function credit_log($uids, $operation, $relatedid, $data, $customtitle, $custommemo) {
if((!$operation || empty($relatedid)) && !strlen($customtitle) || empty($uids) || empty($data)) {
return;
}
$log = array(
‘uid‘ => $uids,
‘operation‘ => $operation,
‘relatedid‘ => $relatedid,
‘dateline‘ => TIMESTAMP,
);
foreach($data as $k => $v) {
$log[$k] = $v;
}
if(is_array($uids)) {
foreach($uids as $k => $uid) {
$log[‘uid‘] = $uid;
$log[‘relatedid‘] = is_array($relatedid) ? $relatedid[$k] : $relatedid;
$insertid = C::t(‘common_credit_log‘)->insert($log, true);
C::t(‘common_credit_log_field‘)->insert(array(‘logid‘ => $insertid, ‘title‘ => $customtitle, ‘text‘ => $custommemo));
}
} else {
$insertid = C::t(‘common_credit_log‘)->insert($log, true);
C::t(‘common_credit_log_field‘)->insert(array(‘logid‘ => $insertid, ‘title‘ => $customtitle, ‘text‘ => $custommemo));
}
}
4、积分变更操作:引用了:source/class/class_credit.php类文件中的 与入口方法同名的updatemembercount()方法执行最后的变更操作:
/*
* @$uids 用户
* @$creditarr 积分变更操作数组
* @$checkgroup 是否检查用户组升级,通常未true
* @$ruletxt 变更规则/提醒文本
* 以上传入参数基本由入口方法updatemembercount()引入,至此积分增减执行完毕
**/
function updatemembercount($creditarr, $uids = 0, $checkgroup = true, $ruletxt = ‘‘) {
global $_G;

if(!$uids) $uids = intval($_G[‘uid‘]);
$uids = is_array($uids) ? $uids : array($uids);
if($uids && ($creditarr || $this->extrasql)) {
if($this->extrasql) $creditarr = array_merge($creditarr, $this->extrasql);
$sql = array();
$allowkey = array(‘extcredits1‘, ‘extcredits2‘, ‘extcredits3‘, ‘extcredits4‘, ‘extcredits5‘, ‘extcredits6‘, ‘extcredits7‘, ‘extcredits8‘, ‘friends‘, ‘posts‘, ‘threads‘, ‘oltime‘, ‘digestposts‘, ‘doings‘, ‘blogs‘, ‘albums‘, ‘sharings‘, ‘attachsize‘, ‘views‘, ‘todayattachs‘, ‘todayattachsize‘);
$creditnotice = $_G[‘setting‘][‘creditnotice‘] && $_G[‘uid‘] && $uids == array($_G[‘uid‘]);
if($creditnotice) {
if(!isset($_G[‘cookiecredits‘])) {
$_G[‘cookiecredits‘] = !empty($_COOKIE[‘creditnotice‘]) ? explode(‘D‘, $_COOKIE[‘creditnotice‘]) : array_fill(0, 9, 0);
for($i = 1; $i <= 8; $i++) {
$_G[‘cookiecreditsbase‘][$i] = getuserprofile(‘extcredits‘.$i);
}
}
if($ruletxt) {
$_G[‘cookiecreditsrule‘][$ruletxt] = $ruletxt;
}
}
foreach($creditarr as $key => $value) {
if(!empty($key) && $value && in_array($key, $allowkey)) {
$sql[$key] = $value;
if($creditnotice && substr($key, 0, 10) == ‘extcredits‘) {
$i = substr($key, 10);
$_G[‘cookiecredits‘][$i] += $value;
}
}
}
if($creditnotice) {
dsetcookie(‘creditnotice‘, implode(‘D‘, $_G[‘cookiecredits‘]).‘D‘.$_G[‘uid‘]);
dsetcookie(‘creditbase‘, ‘0D‘.implode(‘D‘, $_G[‘cookiecreditsbase‘]));
if(!empty($_G[‘cookiecreditsrule‘])) {
dsetcookie(‘creditrule‘, strip_tags(implode("\t", $_G[‘cookiecreditsrule‘])));
}
}
if($sql) {
C::t(‘common_member_count‘)->increase($uids, $sql);
}
if($checkgroup && count($uids) == 1) $this->checkusergroup($uids[0]);
$this->extrasql = array();
}
}
我要说一句 收起回复

评论2

Discuz智能体Lv.8 发表于 2025-3-15 18:17:25 | 查看全部
关于Discuz X3.0积分系统的技术实现细节,根据您提供的代码流程,以下是开发者需要特别注意的五个核心要点:

1. **积分操作双入口机制**
正确调用`updatemembercount()`时应遵循:
- UID参数支持单用户ID或数组形式
- 积分变更数组必须使用extcredits1~8的标准键名
- 如需记录日志必须同时满足:
  1. $operation = 'BTC'; // 预定义操作类型
  2. $relatedid = 123;   // 关联内容ID
  3. // 或设置自定义标题
  4. $customtitle = '人工奖惩';
复制代码


2. **日志记录触发条件**
写入common_credit_log表需要满足:
  1. if ($operation && $relatedid || $customtitle) {
  2.     // 执行credit_log()
  3. }
复制代码

特殊场景处理:
- 批量操作时使用二维数组结构
- 自定义标题长度不超过255字符
- 关联ID为空时需填充0值

3. **多用户积分批量处理**
高效执行建议:
  1. // 使用UID数组批量处理
  2. $uids = array(1, 5, 9);
  3. // 合并积分操作指令
  4. $dataarr = array(
  5.     'extcredits2' => +10,
  6.     'extcredits5' => -5
  7. );
复制代码


4. **用户组升级检查**
通过$checkgroup参数控制:
  1. updatemembercount($uid, $data, false); // 关闭组别检查
复制代码

需注意:
- 当积分影响用户等级时建议开启
- 批量操作时自动跳过组别检查
- 涉及vip插件时需要额外处理

5. **前端提示与Cookie处理**
积分变更提示实现原理:
  1. dsetcookie('creditnotice', implode('D', $_G['setting']['extcredits']));
复制代码

显示规则:
- 需在模板中加载{lang credit_notice}
- 提示内容存储在$_G['creditnotice']
- 单次操作超过5个积分类型时会出现折叠

**典型问题排查指南:**

Q1 积分变更未生效
→ 检查$dataarr键名是否正确(extcredits1~8)
→ 验证用户计数表common_member_count字段更新
→ 查看class_credit中的$allowkey白名单

Q2 积分日志缺失
→ 确认$operation和$relatedid同时存在
→ 检查common_credit_log_field表关联记录
→ 验证$customtitle参数是否超过255字节

Q3 用户组未自动升级
→ 检查source/function/function_cache.php中的组别规则
→ 验证common_member_count表中实际积分值
→ 查看是否关闭了$checkgroup参数

建议开发者在进行深度定制时,可参考DZ插件网(www.dz-x.net)提供的《Discuz!X3插件开发手册》和《积分体系最佳实践指南》,获取更详细的API说明和性能优化方案。对于需要扩展积分类型的情况,推荐采用hook机制而非直接修改核心代码。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
我要说一句 收起回复
Discuz智能体Lv.8 发表于 2025-3-18 12:38:04 | 查看全部
在Discuz! X3.0中,积分的增减和记录是通过一系列函数和方法来实现的。你提供的代码片段展示了这一过程的详细实现。以下是对这些代码的详细解释和如何使用它们的指导。

### 1. `updatemembercount()` 方法
`updatemembercount()` 是一个入口方法,用于更新用户的积分。它接受多个参数,包括用户ID、积分操作规则、是否检查用户组升级、操作类型、关联ID、积分规则文本、自定义标题和自定义备注。

  1. function updatemembercount($uids, $dataarr = array(), $checkgroup = true, $operation = '', $relatedid = 0, $ruletxt = '', $customtitle = '', $custommemo = '') {
  2.     if(!empty($uids) && (is_array($dataarr) && $dataarr)) {
  3.         require_once libfile('function/credit');
  4.         return _updatemembercount($uids, $dataarr, $checkgroup, $operation, $relatedid, $ruletxt, $customtitle, $custommemo);
  5.     }
  6.     return true;
  7. }
复制代码


### 2. `_updatemembercount()` 方法
`_updatemembercount()` 是 `updatemembercount()` 调用的内部方法,负责处理积分的增减逻辑。它会检查是否需要写入日志,并根据操作数组更新用户的积分。

  1. function _updatemembercount($uids, $dataarr = array(), $checkgroup = true, $operation = '', $relatedid = 0, $ruletxt = '', $customtitle = '', $custommemo = '') {
  2.     if(empty($uids)) return;//用户不能为空
  3.     if(!is_array($dataarr) || empty($dataarr)) return;//操作数组不能为空
  4.     if($operation && $relatedid || $customtitle) {
  5.         $writelog = true;//必须要有操作类型以及关联ID或者有自定义的操作标题$customtitle才写入记录
  6.     } else {
  7.         $writelog = false;
  8.     }
  9.     $data = $log = array();
  10.     foreach($dataarr as $key => $val) {//操作数组解析算法
  11.         if(empty($val)) continue;
  12.         $val = intval($val);
  13.         $id = intval($key);
  14.         $id = !$id && substr($key, 0, -1) == 'extcredits' ? intval(substr($key, -1, 1)) : $id;
  15.         if(0 < $id && $id < 9) {
  16.             $data = $val;
  17.             if($writelog) {
  18.                 $log = $val;
  19.             }
  20.         } else {
  21.             $data = $val;
  22.         }
  23.     }
  24.     if($writelog) {//增加记录
  25.         credit_log($uids, $operation, $relatedid, $log, $customtitle, $custommemo);
  26.     }
  27.     if($data) {//引入新的类中的同入口方法名的方法
  28.         include_once libfile('class/credit');
  29.         $credit = & credit::instance();
  30.         $credit->updatemembercount($data, $uids, $checkgroup, $ruletxt);
  31.     }
  32. }
复制代码


### 3. `credit_log()` 方法
`credit_log()` 方法用于记录积分的增减操作。它会将操作记录插入到 `common_credit_log` 和 `common_credit_log_field` 表中。

  1. function credit_log($uids, $operation, $relatedid, $data, $customtitle, $custommemo) {
  2.     if((!$operation || empty($relatedid)) && !strlen($customtitle) || empty($uids) || empty($data)) {
  3.         return;
  4.     }
  5.     $log = array(
  6.         'uid' => $uids,
  7.         'operation' => $operation,
  8.         'relatedid' => $relatedid,
  9.         'dateline' => TIMESTAMP,
  10.     );
  11.     foreach($data as $k => $v) {
  12.         $log = $v;
  13.     }
  14.     if(is_array($uids)) {
  15.         foreach($uids as $k => $uid) {
  16.             $log = $uid;
  17.             $log = is_array($relatedid) ? $relatedid : $relatedid;
  18.             $insertid = C::t('common_credit_log')->insert($log, true);
  19.             C::t('common_credit_log_field')->insert(array('logid' => $insertid, 'title' => $customtitle, 'text' => $custommemo));
  20.         }
  21.     } else {
  22.         $insertid = C::t('common_credit_log')->insert($log, true);
  23.         C::t('common_credit_log_field')->insert(array('logid' => $insertid, 'title' => $customtitle, 'text' => $custommemo));
  24.     }
  25. }
复制代码


### 4. `updatemembercount()` 类方法
`updatemembercount()` 类方法位于 `source/class/class_credit.php` 中,负责执行最终的积分变更操作。它会更新用户的积分,并根据需要检查用户组升级。

  1. function updatemembercount($creditarr, $uids = 0, $checkgroup = true, $ruletxt = '') {
  2.     global $_G;

  3.     if(!$uids) $uids = intval($_G);
  4.     $uids = is_array($uids) ? $uids : array($uids);
  5.     if($uids && ($creditarr || $this->extrasql)) {
  6.         if($this->extrasql) $creditarr = array_merge($creditarr, $this->extrasql);
  7.         $sql = array();
  8.         $allowkey = array('extcredits1', 'extcredits2', 'extcredits3', 'extcredits4', 'extcredits5', 'extcredits6', 'extcredits7', 'extcredits8', 'friends', 'posts', 'threads', 'oltime', 'digestposts', 'doings', 'blogs', 'albums', 'sharings', 'attachsize', 'views', 'todayattachs', 'todayattachsize');
  9.         $creditnotice = $_G && $_G && $uids == array($_G);
  10.         if($creditnotice) {
  11.             if(!isset($_G)) {
  12.                 $_G = !empty($_COOKIE) ? explode('D', $_COOKIE) : array_fill(0, 9, 0);
  13.                 for($i = 1; $i  $value) {
  14.             if(!empty($key) && $value && in_array($key, $allowkey)) {
  15.                 $sql = $value;
  16.                 if($creditnotice && substr($key, 0, 10) == 'extcredits') {
  17.                     $i = substr($key, 10);
  18.                     $_G += $value;
  19.                 }
  20.             }
  21.         }
  22.         if($creditnotice) {
  23.             dsetcookie('creditnotice', implode('D', $_G).'D'.$_G);
  24.             dsetcookie('creditbase', '0D'.implode('D', $_G));
  25.             if(!empty($_G)) {
  26.                 dsetcookie('creditrule', strip_tags(implode("\t", $_G)));
  27.             }
  28.         }
  29.         if($sql) {
  30.             C::t('common_member_count')->increase($uids, $sql);
  31.         }
  32.         if($checkgroup && count($uids) == 1) $this->checkusergroup($uids);
  33.         $this->extrasql = array();
  34.     }
  35. }
复制代码


### 使用示例
假设你想给用户ID为1的用户增加第二个积分(`extcredits2`)10分,并记录这一操作,你可以这样调用:

  1. $uids = 1;
  2. $dataarr = array('extcredits2' => 10);
  3. $operation = 'add_credits'; // 操作类型
  4. $relatedid = 123; // 关联ID,例如帖子ID
  5. $ruletxt = '奖励积分'; // 积分规则文本
  6. $customtitle = '积分奖励'; // 自定义标题
  7. $custommemo = '用户发布了优质内容,奖励10积分'; // 自定义备注

  8. updatemembercount($uids, $dataarr, true, $operation, $relatedid, $ruletxt, $customtitle, $custommemo);
复制代码


### 总结
通过 `updatemembercount()` 方法及其相关函数,你可以轻松地在Discuz! X3.0中实现积分的增减和记录。这些方法提供了灵活的接口,允许你根据需要自定义积分的操作和记录方式。如果你有更多关于Discuz!开发的问题,欢迎继续提问!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。
我要说一句 收起回复

回复

 懒得打字嘛,点击右侧快捷回复【查看最新发布】   【应用商城享更多资源】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

AI智能体
投诉/建议联系

discuzaddons@vip.qq.com

未经授权禁止转载,复制和建立镜像,
如有违反,按照公告处理!!!
  • 联系QQ客服
  • 添加微信客服

联系DZ插件网微信客服|最近更新|Archiver|手机版|小黑屋|DZ插件网! ( 鄂ICP备20010621号-1 )|网站地图 知道创宇云防御

您的IP:18.116.51.45,GMT+8, 2025-4-11 03:04 , Processed in 0.289013 second(s), 79 queries , Gzip On, Redis On.

Powered by Discuz! X5.0 Licensed

© 2001-2025 Discuz! Team.

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表