mChannels)) { return true; } $val = $this->read($card_no); if($val === false) { return true; } //目标是最佳解决撞单问题,唯一有效办法是睡眠. $osc = $val['sc']; //old succ channel $ost = $val['st']; //old succ time //如果有充值中的相同卡号的单子,则睡眠. if(!empty($val['cc'])) { return false; } //成功的通道是撞单通道,并且成功时间还没过期,则睡眠 $now = time(); if (in_array($osc, $this->mChannels) and ($ost + $this->mSuccInterval >= $now)) { return false; } return true; } private function submit($card_no, $oid) { if(empty($this->mChannels)) { return; } $org_val = [ 'sc' => '', //succ ch_name 'st' => 0, //succ time 'cc' => [] //正在提交的通道 ]; $val = $this->read($card_no); if($val === false) { $val = $org_val; } $val['cc'][$oid] = ''; $this->write($card_no,$val); } private function can_commit($card_no, $oid, $ch_name) { if(empty($this->mChannels)) { return true; } //非撞单通道都可以提 if(!in_array($ch_name,$this->mChannels)) { return true; } $val = $this->read($card_no); if($val === false) { return true; } $ost = $val['st']; //old succ time $now = time(); if($ost + $this->mSuccInterval >= $now) { return false; } $commits = count($val['cc']); if($commits > 1) { return false; } return true; } private function commit($card_no, $oid, $ch_name) { if(empty($this->mChannels)) { return; } $val = $this->read($card_no); if($val === false) { return; } $val['cc'][$oid] = $ch_name; $this->write($card_no,$val); } private function notify($card_no, $oid, $ch_name,$succ) { if(empty($this->mChannels)) { return; } $val = $this->read($card_no); if($val === false) { return; } if(!$succ) { $val['cc'][$oid] = ''; $this->write($card_no,$val); } else { $val['cc'][$oid] = $ch_name; $this->write($card_no,$val); } } private function complete($card_no, $oid, $ch_name,$succ) { if(empty($this->mChannels)) { return; } $val = $this->read($card_no); if($val === false) { return; } unset($val['cc'][$oid]); if($succ) { $now = time(); if(empty($val['sc'])) { $val['sc'] = $ch_name; $val['st'] = $now; } else { $osc = $val['sc']; //old succ channel $ost = $val['st']; //old succ time if(in_array($osc,$this->mChannels)) { //过去的成功通道,在撞单通道中。 //如果新的通道也在撞单通道中,或者老的成功时间过了撞单周期 //更新成功通道信息 if (in_array($ch_name, $this->mChannels) or ($ost + $this->mSuccInterval < $now)) { $val['sc'] = $ch_name; $val['st'] = $now; } } else { $val['sc'] = $ch_name; $val['st'] = $now; } } } $this->write($card_no,$val); } private function write($card_no, $val) { $data = [$card_no => json_encode($val)]; wcache(card_crash::cache_name, $data, 'refill-'); } private function read($card_no) { $ret = rcache(card_crash::cache_name, 'refill-', $card_no); if (empty($ret)) { return false; } $values = $ret[$card_no]; return json_decode($values, true); } private function delete($card_no) { dcache(card_crash::cache_name, 'refill-', $card_no); } }