phpcms2008积分操作函数的使用方法

如果安装了pay模块,也就是财务模块.直接使用以下代码:
 
$pay = load('pay_api.class.php', 'pay', 'api');
$pay->update_exchange('phpcms', 'point', -$readpoint, $data['title'].$contentid, '', $contentid.$C['repeatchargedays']);


第一句:加载pay类文件;

第二句:调用 pay类里的update_exchange函数来操作积分.

函数原型如下:

 
 /**
  *
  * @params string  $module moudel name
  * @params string  $type ( point or amount(money) )
  * @params float   $number (point or money) amount (如果减少 point(or money) 用'-'(减号)连接)
  * @params mixed   $note note
  * @params mixed   $userid 为0表示为当前用户 int&array 使用array的时候建议对用户进行增加 point or amount
  *  @params string  $authid  文章权限
  * @return boolean
  */

 function update_exchange($module, $type, $number, $note = '', $userid =0, $authid = '')
 {
  global $MODULE, $_userid, $_username, $_point, $_amount;
        $inputid = $_userid;
        $inputer = $_username;
  if(!isset($MODULE[$module])) showmessage('模块不存在!');
  $field = $type == 'point' ? 'point' : 'amount';
  if(!is_numeric($number)) showmessage('金额不对!');
        $number = floatval($number);
  $time = date('Y-m-d H:i:s', TIME);
  $ip = IP;
        $authid = trim($authid);
        if(!empty($authid))
        {
            $authid = md5(AUTH_KEY.$authid);
        }
        if(!is_array($userid))
        {
            $userid = intval($userid);
            if($userid < 1)
            {
                $userid = $_userid;
                $username = $_username;
                if ('amount' == $type)
                {
                    $blance = $_amount + $number;
                    if($blance < 0)  {$this->error = 0; return false;} //showmessage('金额不足');
     if($blance > 9999.99) {$this->error = 7;return false;} //showmessage('金额超过最大额');
                }
                else
                {
                    $blance = $_point + $number;
                    if($blance < 0)  {$this->error = 1; return false;} //showmessage('点数不足');
                }
                $this->member->set($userid, array($field=>$blance));
                $value = "('{$module}' ,'{$type}','{$number}','{$blance}' ,'{$userid}','{$username}','{$inputid}','{$inputer}', '{$time}', '{$ip}', '{$note}', '{$authid}')";
            }
            else
            {
                $userinfo = $this->member->get($userid, array($field,'username'));
                $username = $userinfo['username'];
                if ('amount' == $type)
                {
                    $blance = $userinfo['amount'] + $number;
                    if($blance < 0)  {$this->error = 0; return false;} //showmessage('金额不足');
     if($blance > 9999.99) {$this->error = 7;return false;} //showmessage('金额超过最大额');
                }
                else
                {
                    $blance = $userinfo['point'] + $number;
                    if($blance < 0) {$this->error = 1;return false;} //showmessage('点数不足');
                }
                $this->member->set($userid, array($field=>$blance));
                $value = "('{$module}' ,'{$type}','{$number}','{$blance}' ,'{$userid}','{$username}','{$inputid}','{$inputer}', '{$time}', '{$ip}', '{$note}', '{$authid}')";
            }
            $this->setStat($type,$number);
        }
        else
        {
            $value = '';
            foreach($userid AS $k => $v)
            {
                if($v && !empty($v))
                {
                    $userinfo = $this->member->get($v, array($field,'username'));
                    $username = $userinfo['username'];
                    if ('amount' == $type)
                    {
                        $blance = $userinfo['amount'] + $number;
                        if($blance < 0)  {$this->error = 0; return false;} //showmessage('金额不足');
                    }
                    else
                    {
                        $blance = $userinfo['point'] + $number;
                        if($blance < 0) {$this->error = 1;return false;} //showmessage('点数不足');
                    }
                    $this->member->set($userid, array($field=>$blance));
                    $value .=  "('{$module}' ,'{$type}','{$number}','{$blance}' ,'{$v}','{$username}','{$inputid}','{$inputer}', '{$time}', '{$ip}', '{$note}', '{$authid}'),";
                    $this->setStat($type,$number);
                }
            }
            $value = substr($value, 0, -1);
        }
  $sql = "INSERT INTO `$this->exchange_table` (`module` ,`type`,`number`,`blance` ,`userid`,`username`,`inputid`,`inputer`, `time`, `ip`, `note`, `authid`) VALUES ".$value;
  if ($this->db->query($sql)) return true;
 }




函数 update_exchange($module, $type, $number, $note = '', $userid =0, $authid = '') 有6个参数;

  * @params string  $module moudel name
  * @params string  $type ( point or amount(money) )
  * @params float   $number (point or money) amount (如果减少 point(or money) 用'-'(减号)连接)
  * @params mixed   $note note
  * @params mixed   $userid 为0表示为当前用户 int&array 使用array的时候建议对用户进行增加 point or amount
  *  @params string  $authid  文章权限


具体的就不多解释了.希望大家仔细看看这段解释.

用这个类和函数,可以实现你对积分的操作,比如登陆增加积分,评论增加积分等.

以登陆增加积分为例,在登陆处理文件 member/login.php 里,实际上它调用了member.class.php类文件的login函数;

那在member/include/member.class.php 的login函数里 $this->_userid = $r['userid']; 下面加如下代码:

$pay = load('pay_api.class.php', 'pay', 'api');
$pay->update_exchange('member', 'point', 5, '登陆增加积分5分', $this->_userid, '');


即表示登陆增加5分.当然如果你还需要更加复杂的处理,比如一天登陆n次只增加一次积分等等,需要另外开发了.

本文内容同步发布在phpcms官方论坛:
http://bbs.phpcms.cn/thread-150913-1-1.html

你可能感兴趣的:(thread,sql,PHP,bbs,Exchange)