阿里云市场有个phpwind9.x 积分兑换商城插件,看其源码,事务用 lockProcess 搞,感觉哪不对,对 php 和 wind 不熟啊,感觉会有性能问题吧,如果搞个正儿八经的积分商城的话。
/**
* 兑换奖品
*/
public function doAwardAction() {
$info = array();
$info['uid'] = $this->loginUser->uid;
$info['username'] = $this->loginUser->username;
$info['prize_id'] = $this->getInput('prize_id', 'post');
!$info['prize_id'] && $this->showError('奖品不存在!');
$info['num'] = $this->getInput('num', 'post');
$info['name'] = $this->getInput('name', 'post');
$info['address'] = $this->getInput('address', 'post');
if(!$info['address']){
$this->showError('详细地址不能为空');
}
$info['zip'] = $this->getInput('zip', 'post');
$info['tel'] = $this->getInput('tel', 'post');
if (!is_numeric($info['num']) || $info['num'] <= 0) {
$this->showError('请输入正确的兑换数量!');
}
//读取应用配置
$config = Wekit::C('weibojifen');
//获取流通货币的名字,单位,以及该用户的值
Wind::import('SRV:credit.bo.PwCreditBo');
$creditBo = PwCreditBo::getInstance();
$credit_num = $config['prizeCredit'];
$credit_name = $creditBo->cType[$credit_num];
$credit_unit = $creditBo->cUnit[$credit_num];
$credit_value = $this->loginUser->info['credit'.$credit_num];
//得到奖品信息
$prize_info = $this->_getPrizeDs()->getPrizeInfo($info['prize_id']);
$needpay = $prize_info['price'] * $info['num'];
$needpay = intval($needpay);
if (empty($prize_info)){
$this->showError('非法 id');
}
if ($prize_info['num'] < 1){
$this->showError('该奖品暂时缺货!');
}
if ($prize_info['num'] < $info['num']) {
$this->showError("该奖品只剩下存货".$prize_info['num']."件!");
}
if ($credit_value < $needpay) {
$this->showError('您没有足够的积分参与该奖品的兑换!');
}
//生成订单
$info['price'] = $prize_info['price'];
$info['state'] = 0;
$info['create_time'] = Pw::getTime();
$info['logistics'] = '';
$info['logistics_id'] = '';
$info['send_time'] = 0;
$_flag = 'add_order';
$_time = 300;
$servce = Wekit::load('process.srv.PwProcessService');
if ($servce->lockProcess($_flag, $_time)){
//积分操作
$creditBo->set($this->loginUser->uid, $config['prizeCredit'], -$needpay);
$creditBo->addLog('积分兑换', array($credit_num => -$needpay), new PwUserBo($this->loginUser->uid));
$creditBo->writeLog();
//增加订单
$this->_getOrderDs()->addOrderInfo($info);
//减少奖品数目
$this->_getPrizeDs()->updatePrizeNum($info['prize_id'], array('num'=>num-$info['num'],'sellnum'=>sellnum+$info['num']));
$servce->unlockProcess($_flag);
$this->showMessage("积分兑换奖品成功!");
}else{
$this->showError('未解锁状态');
}
}