阿里云市场有个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('未解锁状态');
}
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.