phpcms实现积分兑换余额(壹)
[ 2016/05/01, Phpcms , 5106阅, 0评 ]

1.jpg

2.jpg

官方程序默认的是余额兑换积分功能,那么该怎么实现积分兑换余额呢?很简单,把原来的功能逆转一下就可以啦,废话不多说,直接上代码:

首先,给该功能命名为:change_duirmb

在模板文件夹下的member\left.html中加上相应的代码

<li{if ROUTE_A=="change_duirmb"} class="on"{/if}><a href="index.php?m=member&c=index&a=change_duirmb"><img src="{IMG_PATH}member/ico_duihuan.png" width="16" height="16" /> 积分兑余额</a></li>

在member文件夹下新建change_duirmb.html文件并放入以下代码

{template 'member', 'header'}
<div id="memberArea">
	{template 'member', 'left'}
	<div class="col-auto">
		<div class="col-1 ">
			<h5 class="title">积分兑余额</h5>
			<div class="content">
			<form method="post" action="" id="myform" name="myform">
			<table width="100%" cellspacing="0" class="table_form">
				<tr>
					<th width="100">{L('account_remain')}:</th> 
					<td>	
						<font style="color:#F00; font-size:18px;font-family:Georgia,Arial; font-weight:700">{$memberinfo[amount]}</font>{L('unit_yuan')},
						<font style="color:#F00; font-size:12px;font-family:Georgia,Arial; font-weight:700">{$memberinfo[point]}</font>{L('unit_point')}
					</td>
				</tr>
				<tr>
				<script language="JavaScript">
				<!--
				$(document).ready(function() {
					$("#point").html($("#money").val()/{if !empty($member_setting['rmb_point_rate'])}{$member_setting['rmb_point_rate']}{else}10{/if});
					$("#money").keyup(function() {
						$(this).val($(this).val().replace(/[^\d]/g,''));
						$("#point").html($("#money").val()/{if !empty($member_setting['rmb_point_rate'])}{$member_setting['rmb_point_rate']}{else}10{/if});
						if({$memberinfo[point]} < $(this).val()) {
							$("#alert").html('{L('not_sufficient_funds')}');
						} else {
							$("#alert").html('');
						}
					});
				});
				//-->
				</script>
					<th width="100">支出:</th> 
					<td>	
						<input type="text" class="input-text" name="money" id="money" size="4">点
						可兑换余额:<font id="point" style="color:#F00; font-size:12px;font-family:Georgia,Arial; font-weight:700">0</font>元
						<font id="alert" style="color:#F00; font-size:12px;font-family:Georgia,Arial; font-weight:700"></font>
					</td>
				</tr>
				<tr>
					<th width="100"></th> 
					<td><input type="submit" class="button" name="buy" value="兑换"></td>
				</tr>
			</table>
			</form>
			</div>
			<span class="o1"></span><span class="o2"></span><span class="o3"></span><span class="o4"></span>
		</div>
		<div class="bk10"></div>
	</div>
</div>
<div class="clear"></div>
{template 'member', 'footer'}

然后在\phpcms\modules\member\index.php中“积分兑换”函数之后约900行加入以下代码

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * 积分兑换rmb
	 */
	public function change_duirmb() {
		$memberinfo = $this->memberinfo;
		//加载用户模块配置
		$member_setting = getcache('member_setting');
		$this->_init_phpsso();
		$setting = $this->client->ps_getcreditlist();
		$outcredit = unserialize($setting);
		$setting = $this->client->ps_getapplist();
		$applist = unserialize($setting);
		if(isset($_POST['buy'])) {
			if(!is_numeric($_POST['money']) || $_POST['money'] < 0) {
				showmessage(L('jifen_cuowu'), HTTP_REFERER);//提示 积分错误
			} else {
				$money = intval($_POST['money']);
			}
			if($memberinfo['point'] < $money) {
				showmessage(L('jifen_buzu'), HTTP_REFERER);//提示积分不足
			}
			//此处比率读取用户配置
			$zengjia = $money/$member_setting['rmb_point_rate'];//计算增加的金额
			$this->db->update(array('amount'=>"+=$zengjia"), array('userid'=>$memberinfo['userid']));//余额增加,不记录
			//加入消费记录,同时扣除金钱
			pc_base::load_app_class('spend','pay',0);
			spend::dui_rmb($money, L('dui_rmb'), $memberinfo['userid'], $memberinfo['username']);//写入积分消费记录
			showmessage(L('operation_success'), HTTP_REFERER);
		} else {
			$credit_list = pc_base::load_config('credit');
			include template('member', 'change_duirmb');
		}
	}

因为需要把该操作写入到“积分消费”记录中,所以还需要在\phpcms\modules\pay\classes\spend.class.php中加入以下代码:

//////兑换余额 start//////////////////////////////////////////////////////////////////////////////////////////////
	public static function dui_rmb($value, $msg, $userid = '', $username = '', $op_userid = '', $op_username = '', $logo = '') {
		return self::dui_rmb_la(array('username'=>$username, 'userid'=>$userid, 'type'=>2, 'value'=>$value, 'op_userid'=>$op_userid, 'op_username'=>$op_username, 'msg'=>$msg,'logo'=>$logo));
	}
	/**
	 * 添加兑换记录,添加到消费记录,
	 * @param array $data 添加消费记录参数
	 */
	private static function dui_rmb_la($data) {
		$data['userid'] = isset($data['userid']) && intval($data['userid']) ? intval($data['userid']) : 0;
		$data['username'] = isset($data['username']) ? trim($data['username']) : '';
		$data['op_userid'] = isset($data['op_userid']) && intval($data['op_userid']) ? intval($data['op_userid']) : 0;
		$data['op_username'] = isset($data['op_username']) ? trim($data['op_username']) : '';
		$data['type'] = isset($data['type']) && intval($data['type']) ? intval($data['type']) : 0;
		//将value值作为float型
		$data['value'] = isset($data['value']) && floatval($data['value']) ? floatval($data['value']) : 0;
		$data['msg'] = isset($data['msg']) ? trim($data['msg']) : '';
		$data['logo'] = isset($data['logo']) ? trim($data['logo']) : '';
		$data['creat_at'] = SYS_TIME;
		//检察消费类型
		if (!in_array($data['type'], array(1,2))) {
			return false;
		}
		//检察消费描述
		if (empty($data['msg'])) {
			self::$msg = 1;
			return false;
		}
		//检察消费金额
		if (empty($data['value'])) {
			self::$msg = 2;
			return false;
		}
		//检察userid和username并偿试再次的获取
		if (empty($data['userid']) || empty($data['username'])) {
			if (defined('IN_ADMIN')) {
				self::$msg = 3;
				return false;
			} elseif (!$data['userid'] = param::get_cookie('_userid') || !$data['username'] = param::get_cookie('_username')) {
				self::$msg = 3;
				return false;
			} else {
				self::$msg = 3;
				return false;
			}
		}
		//检察op_userid和op_username并偿试再次的获取
		if (defined('IN_ADMIN') && (empty($data['op_userid']) || empty($data['op_username']))) {
			$data['op_username'] = param::get_cookie('admin_username');
			$data['op_userid'] = param::get_cookie('userid');
		}
		//数据库连接
		if (empty(self::$db)) {
			self::connect();
		}
		$member_db = pc_base::load_model('member_model');
		//判断用户的金钱或积分是否足够。
		if (!self::_check_user($data['userid'], $data['type'], $data['value'], $member_db)) {
			self::$msg = 6;
			return false;
		} 
		$sql = array();
		if ($data['type'] == 1) {//金钱方式消费
			$sql = array('amount'=>"+=".$data['value']);
		} elseif ($data['type'] == 2) { //积分方式消费
			$sql = array('point'=>'-='.$data['value']);
		} else {
			self::$msg = 7;
			return false;
		}
		//进入数据库操作
		if ($member_db->update($sql, array('userid'=>$data['userid'], 'username'=>$data['username'])) && self::$db->insert($data)) {
			self::$msg = 0;
			return true;
		} else {
			self::$msg = 8;
			return false;
		}
	}
//////兑换余额 end//////////////////////////////////////////////////////////////////////////////////////////////

然后,因为数据库中对应的字段用的是int型来储存,所以如果是小数的话就会变成整数,代码中以改为以float型数据来处理,所以还需要进入数据库把数据表v9_pay_spend中的value字段改为图示类型:

QQ截图20160501174301.jpg

此处更改之后,后台的消费记录及前台会员中心中的数值也会变成带有2位小数

3.jpg

4.jpg

到此,积分兑换余额功能就可以实现啦,赶快试试吧

有朋自远方来...评论一下呗O(∩_∩)O