public function addsell() { $isCard = $_POST['iscard']; unset($_POST['iscard']); //判断数据是否齐全 if (empty($_POST)) { $this->error('没有提交任何东西'); exit; } if ($isCard < 0) { $this->error('请选择支付方式'); exit; } if ($_POST['sell_expense'] < 1) { $this->error('消费金额必须大于0元'); exit; } $token = session("token"); // 检查卡号 $cardnum = $this->_post('card_num'); $card = M('Member_card_create')->where(array('token' => $token, 'number' => $cardnum, 'status' => 1))->find(); if (!$card) { $this->error('卡号不存在'); } if (!$card['wecha_id']) { $this->error('该卡号未被领用,无法消费'); } //获取商家设置 tp_member_card_exchange $getset = M('Member_card_exchange')->where(array('token' => $token))->find(); $where = array('token' => $token, 'wecha_id' => $card['wecha_id'], 'status' => 1); $userinfo = M('Userinfo')->where($where)->find(); if (!$getset || !$userinfo) { Log::record("商家:" . implode($getset, '|') . "会员:" . implode($userinfo, '|') . 'sql:' . M('userinfo')->getlastSql()); $this->error('没有找到相关的会员信息!'); } $amount = $_POST['sell_expense']; $scorebeadd = intval($amount) * $getset['reward']; $da['total_score'] = $userinfo['total_score'] + $scorebeadd; $da['expend_score'] = $userinfo['expend_score'] + $scorebeadd; if ($isCard) { if ($amount > $userinfo['total_money']) { $this->error("会员卡余额不足!"); exit; } else { $da['total_money'] = $userinfo['total_money'] - $amount; $da['spend_money'] = $userinfo['spend_money'] + $amount; } } $model = new Model(); //事务处理开始 $model->startTrans(); //在总额上操作 $isSuccess = $model->autoCheckToken($_POST); //验证表单令牌,防止重复提交 if ($isSuccess) { unset($_POST[C('TOKEN_NAME')]); //除去post数组中的令牌数据,防止table方法提交失败 $isSuccess = $model->table('tp_userinfo')->where($where)->save($da); } $data['token'] = $token; $data['wecha_id'] = $card['wecha_id']; //若上一步成功则写入积分记录表 if ($isSuccess) { $scoredata = array('sign_time' => time(), 'is_sign' => false, 'score_type' => 2, 'expense' => $scorebeadd, 'delete' => 0); $scoredata = array_merge($data, $scoredata, $_POST); unset($scoredata['card_num']); $isSuccess = $model->table('tp_member_card_sign')->add($scoredata); } //若上一步成功且是会员卡余额消费则写入充值记录表 if ($isSuccess && $isCard) { $moneydata = array('card_num' => $cardnum, 'amount' => $amount, 'comment' => $_POST['remark'], 'type' => 1, 'logon_ip' => getenv('REMOTE_ADDR'), 'logon_user_id' => session('uid'), 'oprator' => session('uname')); $moneydata = array_merge($data, $moneydata); $isSuccess = $model->table('tp_member_charge')->add($moneydata); } if ($isSuccess) { $model->commit(); //成功则事务结束 $this->success('操作成功'); } else { $model->rollback(); //失败则rollback $this->error('服务器繁忙,请稍候再试'); } }