public function setUserCredit($uid, $action, $type = 1, $des = array())
{
if (!$uid) {
$this->info = false;
return $this;
}
if (is_array($action)) {
$creditSet = $action;
if ($action['name']) {
// 获取配置规则
$credit_ruls = $this->getCreditRules();
foreach ($credit_ruls as $v) {
if ($v['name'] == $action['name']) {
$creditSet = array_merge($v, $action);
}
}
}
} else {
// 获取配置规则
$credit_ruls = $this->getCreditRules();
foreach ($credit_ruls as $v) {
if ($v['name'] == $action) {
$creditSet = $v;
}
}
}
if (!$creditSet) {
$this->info = '积分规则不存在';
return $this;
}
//dump($creditSet);exit;
//根据 周期范围 和 周期内最多奖励次数 判断是否变更积分
if ($creditSet['cycle_times']) {
$now = time();
switch ($creditSet['cycle']) {
case 'year':
$beginTime = mktime(0, 0, 0, 1, 1, date('Y', $now));
break;
case 'month':
$beginTime = mktime(0, 0, 0, date('m', $now), '1', date('Y', $now));
default:
$beginTime = strtotime(date('Y-m-d 00:00:00', $now));
break;
}
$c['uid'] = $uid;
$c['ctime'] = array('between', array($beginTime, $now));
$times = D('credit_record')->where($c)->count();
if ($times >= $creditSet['cycle_times']) {
$this->info = '积分变更已达上限';
return $this;
}
}
$creditUserDao = M('credit_user');
$creditUser = $creditUserDao->where("uid={$uid}")->find();
// 用户积分
// 积分计算
if ($type == 'reset') {
foreach ($this->creditType as $v) {
$creditUser[$v['name']] = $creditSet[$v['name']];
}
} else {
$change = array();
$detail = array();
$type = intval($type);
foreach ($this->creditType as $v) {
$creditUser[$v['name']] = $creditUser[$v['name']] + $type * $creditSet[$v['name']];
//记录
if ($creditSet[$v['name']] != 0) {
if ($creditSet[$v['name']] * $type > 0) {
$c = '+' . $creditSet[$v['name']];
} else {
$c = $creditSet[$v['name']];
}
$change[] = $v['alias'] . '<font color="red">' . $c . '</font>';
$detail[$v['name']] = "{$c}";
}
}
}
$creditUser['uid'] || ($creditUser['uid'] = $uid);
$creditUser['type'] = $creditSet['type'] ? intval($creditSet) : 1;
if ($creditUserDao->where('uid=' . $creditUser['uid'])->count()) {
$map['id'] = $creditUser['id'];
$map['uid'] = $creditUser['uid'];
unset($creditUser['id']);
unset($creditUser['uid']);
$res = $creditUserDao->where($map)->save($creditUser);
} else {
$res = $creditUserDao->add($creditUser);
}
//记录
$record['cid'] = $creditSet['id'];
$record['uid'] = $uid;
$record['action'] = $creditSet['alias'];
$record['change'] = implode(',', $change);
$record['ctime'] = time();
$replace = array_keys($des);
foreach ($replace as &$v) {
$v = '{' . $v . '}';
}
$record['des'] = str_replace($replace, $des, $creditSet['des']);
$record['detail'] = $detail ? json_encode($detail) : '{}';
D('credit_record')->add($record);
// 用户进行积分操作后,登录用户的缓存将修改
$this->cleanCache($uid);
// $userLoginInfo = S('S_userInfo_'.$uid);
// if(!empty($userLoginInfo)) {
// $userLoginInfo['credit']['score']['credit'] = $creditUser['score'];
// $userLoginInfo['credit']['experience']['credit'] = $creditUser['experience'];
// S('S_userInfo_'.$uid, $userLoginInfo);
// }
if ($res) {
$this->info = $creditSet['info'];
return $this;
} else {
$this->info = false;
return $this;
}
}