在Discuz! X3.0中,积分的增减和记录是通过一系列函数和方法来实现的。你提供的代码片段展示了这一过程的详细实现。以下是对这些代码的详细解释和如何使用它们的指导。
### 1. `updatemembercount()` 方法
`updatemembercount()` 是一个入口方法,用于更新用户的积分。它接受多个参数,包括用户ID、积分操作规则、是否检查用户组升级、操作类型、关联ID、积分规则文本、自定义标题和自定义备注。
- 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. `_updatemembercount()` 方法
`_updatemembercount()` 是 `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 = $val;
- if($writelog) {
- $log = $val;
- }
- } else {
- $data = $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. `credit_log()` 方法
`credit_log()` 方法用于记录积分的增减操作。它会将操作记录插入到 `common_credit_log` 和 `common_credit_log_field` 表中。
- 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 = $v;
- }
- if(is_array($uids)) {
- foreach($uids as $k => $uid) {
- $log = $uid;
- $log = is_array($relatedid) ? $relatedid : $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. `updatemembercount()` 类方法
`updatemembercount()` 类方法位于 `source/class/class_credit.php` 中,负责执行最终的积分变更操作。它会更新用户的积分,并根据需要检查用户组升级。
- function updatemembercount($creditarr, $uids = 0, $checkgroup = true, $ruletxt = '') {
- global $_G;
- if(!$uids) $uids = intval($_G);
- $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 && $_G && $uids == array($_G);
- if($creditnotice) {
- if(!isset($_G)) {
- $_G = !empty($_COOKIE) ? explode('D', $_COOKIE) : array_fill(0, 9, 0);
- for($i = 1; $i $value) {
- if(!empty($key) && $value && in_array($key, $allowkey)) {
- $sql = $value;
- if($creditnotice && substr($key, 0, 10) == 'extcredits') {
- $i = substr($key, 10);
- $_G += $value;
- }
- }
- }
- if($creditnotice) {
- dsetcookie('creditnotice', implode('D', $_G).'D'.$_G);
- dsetcookie('creditbase', '0D'.implode('D', $_G));
- if(!empty($_G)) {
- dsetcookie('creditrule', strip_tags(implode("\t", $_G)));
- }
- }
- if($sql) {
- C::t('common_member_count')->increase($uids, $sql);
- }
- if($checkgroup && count($uids) == 1) $this->checkusergroup($uids);
- $this->extrasql = array();
- }
- }
复制代码
### 使用示例
假设你想给用户ID为1的用户增加第二个积分(`extcredits2`)10分,并记录这一操作,你可以这样调用:
- $uids = 1;
- $dataarr = array('extcredits2' => 10);
- $operation = 'add_credits'; // 操作类型
- $relatedid = 123; // 关联ID,例如帖子ID
- $ruletxt = '奖励积分'; // 积分规则文本
- $customtitle = '积分奖励'; // 自定义标题
- $custommemo = '用户发布了优质内容,奖励10积分'; // 自定义备注
- updatemembercount($uids, $dataarr, true, $operation, $relatedid, $ruletxt, $customtitle, $custommemo);
复制代码
### 总结
通过 `updatemembercount()` 方法及其相关函数,你可以轻松地在Discuz! X3.0中实现积分的增减和记录。这些方法提供了灵活的接口,允许你根据需要自定义积分的操作和记录方式。如果你有更多关于Discuz!开发的问题,欢迎继续提问!
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。 |