util.php 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. <?php
  2. namespace refill;
  3. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  4. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  5. require_once(BASE_HELPER_PATH . '/queue/monitor.php');
  6. require_once(BASE_HELPER_PATH . '/refill/policy/rlock.php');
  7. require_once(BASE_HELPER_PATH . '/refill/EventManager.php');
  8. use queue;
  9. use mtopcard;
  10. use Log;
  11. use Exception;
  12. use Cache;
  13. use QueueClient;
  14. use refill;
  15. use trans_wapper;
  16. class util
  17. {
  18. const ThirdRefillAmount = 100;
  19. static function make_mobile()
  20. {
  21. static $prefix = ["139", "138", "137", "136", "135", "134", "159", "158", "157", "150", "151", "152",
  22. "188", "187", "182", "183", "184", "178", "130", "131", "132", "156", "155", "186", "185",
  23. "176", "133", "153", "189", "180", "181", "177"];
  24. $pos = mt_rand(0, count($prefix) - 1);
  25. $no = "{$prefix[$pos]}" . mt_rand(10000000, 99999999);
  26. return $no;
  27. }
  28. public static function can_refill($card_no, $card_type)
  29. {
  30. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard) {
  31. $result = rcache('card_expired', '', "{$card_no}");
  32. if (empty($result)) {
  33. wcache("card_expired", [$card_no => time()], '');
  34. return [true, 0];
  35. } else {
  36. $latest = current($result);
  37. $cur = time();
  38. $success = ($cur - $latest) > 2;
  39. if ($success) {
  40. wcache("card_expired", [$card_no => time()], '');
  41. }
  42. return [$success, $latest + 2 - $cur];
  43. }
  44. } else {
  45. return [true, 0];
  46. }
  47. }
  48. public static function can_commit($card_no, $card_type)
  49. {
  50. if ($card_type == mtopcard\SinopecCard || $card_type == mtopcard\PetroChinaCard)
  51. {
  52. $result = rcache('card_expired', '', "{$card_no}");
  53. if (empty($result)) {
  54. wcache("card_expired", [$card_no => time()], '');
  55. return [true, 0];
  56. }
  57. else
  58. {
  59. $latest = current($result);
  60. $cur = time();
  61. $lowest = 30;
  62. if ($cur > $latest && ($cur - $latest) >= $lowest) {
  63. wcache("card_expired", [$card_no => time()], '');
  64. return [true, 0];
  65. } else {
  66. wcache("card_expired", [$card_no => $latest + $lowest], '');
  67. return [false, $latest + $lowest - $cur];
  68. }
  69. }
  70. }
  71. else {
  72. return [true, 0];
  73. }
  74. }
  75. static function write_card($card_no, $card_type,$bind_phone)
  76. {
  77. if(empty($bind_phone)) {
  78. return false;
  79. }
  80. if ($card_type !== mtopcard\SinopecCard && $card_type !== mtopcard\PetroChinaCard) {
  81. return false;
  82. }
  83. $mobile_types = [mtopcard\ChinaMobileCard,mtopcard\ChinaUnicomCard,mtopcard\ChinaTelecomCard];
  84. $ctype = mtopcard\simple_card_type($bind_phone);
  85. if (!in_array($ctype,$mobile_types)) {
  86. return false;
  87. }
  88. $mod_topcard = Model('topcard');
  89. $ret = $mod_topcard->get_card($card_no);
  90. if (empty($ret)) {
  91. $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
  92. } else {
  93. $mod_topcard->edit($card_no,$bind_phone);
  94. }
  95. dcache($card_no, 'cardrefill-');
  96. return true;
  97. }
  98. static function read_card($card_no, $card_type = 0)
  99. {
  100. if (empty($card_no)) return false;
  101. $data = rcache($card_no, 'cardrefill-');
  102. if (empty($data)) {
  103. $mod_topcard = Model('topcard');
  104. $ret = $mod_topcard->get_card($card_no);
  105. if (empty($ret)) {
  106. if ($card_type === 0) {
  107. $card_type = mtopcard\card_type($card_no,$regin_no);
  108. }
  109. $bind_phone = util::make_mobile();
  110. $mod_topcard->add($card_no, $card_type, time(), $bind_phone);
  111. $data['bind_phone'] = $bind_phone;
  112. $data['refill_time'] = time();
  113. $data['times'] = 0;
  114. $data['black_card'] = 0;
  115. wcache($card_no, $data, 'cardrefill-');
  116. } else {
  117. $val = $ret[0];
  118. $data['bind_phone'] = $val['bind_phone'];
  119. $data['black_card'] = $val['black_card'];
  120. $data['refill_time'] = time();
  121. $data['times'] = 0;
  122. }
  123. }
  124. //之前没加black_card处理,这个字段不存在.
  125. if (!array_key_exists('black_card', $data)) {
  126. $data['black_card'] = 0;
  127. }
  128. return $data;
  129. }
  130. static function inc_card($card_no, $card_info)
  131. {
  132. $card_info['times'] += 1;
  133. $card_info['refill_time'] = time();
  134. wcache($card_no, $card_info, 'cardrefill-');
  135. }
  136. public static function del_card($card_no)
  137. {
  138. dcache($card_no, 'cardrefill-');
  139. }
  140. public static function set_black($card_no)
  141. {
  142. if (empty($card_no)) return false;
  143. $card_info = util::read_card($card_no);
  144. if (!empty($card_info)) {
  145. $card_info['black_card'] = 1;
  146. $mod_topcard = Model('topcard');
  147. $mod_topcard->table('topcard')->where(['card_no' => $card_no])->update(['black_card' => 1]);
  148. wcache($card_no, $card_info, 'cardrefill-');
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. private static function black_order($order_sn, $msg)
  155. {
  156. static $errMsgs = ["只能给主卡且卡状态正常的加油卡充值", "加油卡卡号错误或不支持"];
  157. if (empty($msg)) return false;
  158. if (in_array($msg, $errMsgs)) {
  159. $refill = Model('refill_order');
  160. $order = $refill->getOrderInfo(['order_sn' => $order_sn]);
  161. if (empty($order)) return false;
  162. $card_no = $order['card_no'];
  163. return util::set_black($card_no);
  164. }
  165. }
  166. public static function black_from_log($file_name)
  167. {
  168. $fn = fopen($file_name, "r");
  169. if (empty($fn)) {
  170. Log::record("Open File {$file_name} error.", Log::ERR);
  171. return false;
  172. } else {
  173. Log::record("{$file_name} start woring", Log::DEBUG);
  174. }
  175. $errs = [];
  176. while (!feof($fn)) {
  177. $line = trim(fgets($fn));
  178. $ret = preg_match('/[\w\W]+"channelOrderNumber":"(?P<order_sn>[^"]+)"[\w\W]+"message":"(?P<message>[\x{4e00}-\x{9fa5}]+)"[\w\W]+"status":109/u', $line, $matches);
  179. if ($ret) {
  180. $order_sn = $matches['order_sn'];
  181. $message = $matches['message'];
  182. self::black_order($order_sn, $message);
  183. $errs[$message] = empty($errs[$message]) ? 1 : $errs[$message] + 1;
  184. }
  185. }
  186. foreach ($errs as $msg => $count) {
  187. Log::record("msg:{$msg} count:{$count}", Log::DEBUG);
  188. }
  189. fclose($fn);
  190. return true;
  191. }
  192. public static function async_add($params, $period = 10)
  193. {
  194. try {
  195. QueueClient::async_push("AysncAddDispatcher", ['method' => 'add', 'params' => $params], $period);
  196. return true;
  197. } catch (Exception $ex) {
  198. return false;
  199. }
  200. }
  201. public static function async_notify($chname,$data, $period)
  202. {
  203. try {
  204. QueueClient::async_push("AysncAddDispatcher", ['method' => 'notify', 'params' => ['channel' => $chname, 'params' => $data]], $period);
  205. return true;
  206. } catch (Exception $ex) {
  207. return false;
  208. }
  209. }
  210. public static function push_add($params)
  211. {
  212. try
  213. {
  214. $ret = self::push_queue('add', $params);
  215. return $ret !== false;
  216. }
  217. catch (Exception $ex) {
  218. return false;
  219. }
  220. }
  221. public static function push_add_zero($params)
  222. {
  223. try
  224. {
  225. $ret = self::push_queue('add_zero', $params);
  226. return $ret !== false;
  227. }
  228. catch (Exception $ex) {
  229. return false;
  230. }
  231. }
  232. public static function push_addthird($params)
  233. {
  234. try
  235. {
  236. $ret = self::push_queue('addthird', $params);
  237. return $ret !== false;
  238. }
  239. catch (Exception $ex) {
  240. return false;
  241. }
  242. }
  243. public static function push_notify($chname, $params)
  244. {
  245. try
  246. {
  247. $ret = self::push_queue('notify', ['channel' => $chname, 'params' => $params]);
  248. return $ret !== false;
  249. }
  250. catch (Exception $ex) {
  251. return false;
  252. }
  253. }
  254. public static function push_notify_merchant($order_id, $manual)
  255. {
  256. try
  257. {
  258. $ret = self::push_queue('notify_mechant', ['order_id' => $order_id, 'manual' => $manual]);
  259. return $ret !== false;
  260. }
  261. catch (Exception $ex) {
  262. return false;
  263. }
  264. }
  265. public static function push_query($order_id)
  266. {
  267. try
  268. {
  269. $ret = self::push_queue('query', ['order_id' => $order_id]);
  270. return $ret !== false;
  271. }
  272. catch (Exception $ex) {
  273. return false;
  274. }
  275. }
  276. public static function push_auto_query($order_id,$query_times)
  277. {
  278. try
  279. {
  280. $ret = self::push_queue('query_auto', ['order_id' => $order_id,'query_times' => $query_times]);
  281. return $ret !== false;
  282. }
  283. catch (Exception $ex) {
  284. return false;
  285. }
  286. }
  287. public static function push_query_net($order_id)
  288. {
  289. try {
  290. $ret = self::push_queue('query_net', ['order_id' => $order_id]);
  291. return $ret !== false;
  292. }
  293. catch (Exception $ex) {
  294. return false;
  295. }
  296. }
  297. public static function manual_success($order_id)
  298. {
  299. try
  300. {
  301. $ret = self::push_queue('manual_success', ['order_id' => $order_id]);
  302. return $ret !== false;
  303. }
  304. catch (Exception $ex) {
  305. return false;
  306. }
  307. }
  308. public static function manual_cancel($order_id)
  309. {
  310. try
  311. {
  312. $ret = self::push_queue('manual_cancel', ['order_id' => $order_id]);
  313. return $ret !== false;
  314. }
  315. catch (Exception $ex) {
  316. return false;
  317. }
  318. }
  319. public static function push_queue($method, $value)
  320. {
  321. if (defined('USE_COROUTINE') && USE_COROUTINE && defined('COROUTINE_HOOK_TCP') && COROUTINE_HOOK_TCP) {
  322. $queue_name = 'QUEUE_DISPATCHER_CO';
  323. $ins = Cache::getInstance('cacheredis');
  324. return $ins->lpush($queue_name, serialize([$method => $value]));
  325. }
  326. else {
  327. return queue\DispatcherClient::instance()->push($method,$value);
  328. }
  329. }
  330. public static function dispatcher_queue_length()
  331. {
  332. $ins = Cache::getInstance('cacheredis');
  333. return $ins->lLen('QUEUE_DISPATCHER_CO');
  334. }
  335. public static function monitor_queue_length()
  336. {
  337. $ins = Cache::getInstance('cacheredis');
  338. return $ins->lLen('REFILL_MONITOR_QUEUE');
  339. }
  340. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  341. public static function monitor_submit($mchid, $spec, $card_type, $mch_amount, $time)
  342. {
  343. $time = intval($time);
  344. queue\MonitorClient::instance()->onSubmit($mchid,$time,$spec,$card_type,$mch_amount);
  345. }
  346. public static function monitor_callback($mchid,$spec,$card_type,$mch_amount,$channel_amount,$succ,$time)
  347. {
  348. queue\MonitorClient::instance()->onCallback($mchid,$time,$spec,$card_type,floatval($mch_amount),floatval($channel_amount),$succ);
  349. }
  350. public static function monitor_netchk($chname,$succ)
  351. {
  352. queue\MonitorClient::instance()->onNetCheck($chname, time(),$succ);
  353. }
  354. public static function monitor_commit($chname, $spec, $card_type, $channel_amount,$commit_time)
  355. {
  356. queue\MonitorClient::instance()->onCommit($chname, $commit_time, $spec, $card_type, $channel_amount);
  357. }
  358. public static function monitor_notify($chname, $spec, $card_type, $channel_amount, $period, $succ,$commit_time,$mch_amount)
  359. {
  360. if ($succ) {
  361. $mch_amount = floatval($mch_amount);
  362. } else {
  363. $mch_amount = 0;
  364. }
  365. queue\MonitorClient::instance()->onNotify($chname, $commit_time, $spec, $card_type, floatval($channel_amount), $period, $succ,$mch_amount);
  366. }
  367. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  368. public static function onEventBeforeSubmit(order $order) : bool
  369. {
  370. $card_type = $order->card_type();
  371. Log::record("onEvent before submit {$order->unique_id()} card_type=$card_type",Log::DEBUG);
  372. return EventManager::instance()->onBeforeSubmit($order);
  373. }
  374. public static function onEventSubmit(order $order)
  375. {
  376. Log::record("onEvent submit {$order->unique_id()}",Log::DEBUG);
  377. EventManager::instance()->onSubmit($order);
  378. }
  379. public static function onEventBeforeCommit(order $order, $ch_name): bool
  380. {
  381. Log::record("onEvent before commit uid={$order->unique_id()} channel=$ch_name", Log::DEBUG);
  382. return EventManager::instance()->onBeforeCommit($order,$ch_name);
  383. }
  384. public static function onEventCommit(order $order, $ch_name)
  385. {
  386. Log::record("onEvent commit uid={$order->unique_id()} channel=$ch_name", Log::DEBUG);
  387. EventManager::instance()->onCommit($order,$ch_name);
  388. }
  389. public static function onEventNeterror(order $order, $ch_name)
  390. {
  391. Log::record("onEvent neterror uid={$order->unique_id()} channel=$ch_name", Log::DEBUG);
  392. EventManager::instance()->onNeterror($order,$ch_name);
  393. }
  394. public static function onEventNotify($refill_info, $order_info, $success)
  395. {
  396. $uid = "{$refill_info['mchid']}-{$refill_info['mch_order']}";
  397. $ch_name = $refill_info['channel_name'];
  398. Log::record("onEvent notify uid=$uid channel=$ch_name success=$success", Log::DEBUG);
  399. EventManager::instance()->onNotify($refill_info, $order_info, $success);
  400. }
  401. public static function onEventComplete($refill_info, $order_info, $success)
  402. {
  403. $uid = "{$refill_info['mchid']}-{$refill_info['mch_order']}";
  404. $ch_name = $refill_info['channel_name'];
  405. Log::record("onEvent complete uid=$uid channel=$ch_name success=$success", Log::DEBUG);
  406. EventManager::instance()->onComplete($refill_info, $order_info, $success);
  407. }
  408. public static function onEventCallback($refill_info, $mch_info, &$ctls)
  409. {
  410. EventManager::instance()->onEventCallback($refill_info, $mch_info, $ctls);
  411. }
  412. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  413. public static function set_order_channels($mchid,$mchorder,$datas)
  414. {
  415. $ins = Cache::getInstance('cacheredis');
  416. $name = 'order_channels';
  417. $key = "{$mchid}-{$mchorder}";
  418. $ins->hset($name, '', [$key => serialize($datas)]);
  419. }
  420. public static function get_order_channels($mchid, $mchorder)
  421. {
  422. //old-name oil_exclude_channels
  423. $ins = Cache::getInstance('cacheredis');
  424. $name = 'order_channels';
  425. $key = "{$mchid}-{$mchorder}";
  426. $chnames = $ins->hget($name, '', $key);
  427. $chnames = unserialize($chnames);
  428. if(is_array($chnames)) {
  429. return $chnames;
  430. } else {
  431. return [];
  432. }
  433. }
  434. public static function del_order_channels($mchid, $mchorder)
  435. {
  436. $ins = Cache::getInstance('cacheredis');
  437. $name = 'order_channels';
  438. $key = "{$mchid}-{$mchorder}";
  439. $ins->hdel($name, '', $key);
  440. }
  441. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  442. public static function set_cancel_order($mchid,$mch_order)
  443. {
  444. $ins = Cache::getInstance('cacheredis');
  445. $name = 'order_cancel_hash';
  446. $key = "{$mchid}-{$mch_order}";
  447. $ins->hset($name, '', [$key=> 1]);
  448. }
  449. public static function query_cancel_order($mchid,$mch_order)
  450. {
  451. $ins = Cache::getInstance('cacheredis');
  452. $name = 'order_cancel_hash';
  453. $key = "{$mchid}-{$mch_order}";
  454. $value = $ins->hget($name,'',$key);
  455. return $value;
  456. }
  457. public static function del_cancel_order($mchid,$mch_order)
  458. {
  459. $ins = Cache::getInstance('cacheredis');
  460. $name = 'order_cancel_hash';
  461. $key = "{$mchid}-{$mch_order}";
  462. $ins->hdel($name, $key);
  463. }
  464. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  465. public static function set_next_order($mchid,$mch_order)
  466. {
  467. $ins = Cache::getInstance('cacheredis');
  468. $name = 'order_next_hash';
  469. $key = "{$mchid}-{$mch_order}";
  470. $ins->hset($name, '', [$key=> 1]);
  471. }
  472. public static function query_next_order($mchid,$mch_order)
  473. {
  474. $ins = Cache::getInstance('cacheredis');
  475. $name = 'order_next_hash';
  476. $key = "{$mchid}-{$mch_order}";
  477. $value = $ins->hget($name,'',$key);
  478. return $value;
  479. }
  480. public static function del_next_order($mchid,$mch_order)
  481. {
  482. $ins = Cache::getInstance('cacheredis');
  483. $name = 'order_success_hash';
  484. $key = "{$mchid}-{$mch_order}";
  485. $ins->hdel($name, $key);
  486. }
  487. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  488. public static function merchant_debt_stoped($mchid)
  489. {
  490. if($mchid > 0)
  491. {
  492. $ret = rcache('merchant-debt-judge', 'refill-',"{$mchid}");
  493. if(empty($ret)) {
  494. return false;
  495. }
  496. $stoped = intval($ret[$mchid]);
  497. return ($stoped === 1);
  498. }
  499. else {
  500. return false;
  501. }
  502. }
  503. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  504. public static function loop_order_inc($card_no, $spec)
  505. {
  506. if(defined('COMPANY_NAME') && in_array(COMPANY_NAME,['ZY_COMPANY']))
  507. {
  508. $ins = Cache::getInstance('cacheredis');
  509. $name = 'loop_order_check_query';
  510. $key = "{$card_no}-{$spec}";
  511. $count = $ins->hget($name, '', $key);
  512. $count = intval($count);
  513. if($count < 0) {
  514. $ins->hset($name, '', [$key => 0]);
  515. return false;
  516. }
  517. elseif($count === 0) {
  518. $ins->hIncrBy($name, $key, 1);
  519. return false;
  520. }
  521. else {
  522. $ins->hIncrBy($name, $key, 1);
  523. return true;
  524. }
  525. }
  526. else {
  527. return false;
  528. }
  529. }
  530. public static function loop_order_dec($card_no, $spec)
  531. {
  532. if (defined('COMPANY_NAME') && in_array(COMPANY_NAME, ['ZY_COMPANY'])) {
  533. $ins = Cache::getInstance('cacheredis');
  534. $name = 'loop_order_check_query';
  535. $spec = intval($spec);
  536. $key = "{$card_no}-{$spec}";
  537. $ins->hIncrBy($name, $key, -1);
  538. }
  539. }
  540. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  541. public static function push_queue_order($mchid,$mch_order,$order_state)
  542. {
  543. if(empty($mch_order)) return;
  544. $ins = Cache::getInstance('cacheredis');
  545. $name = 'merchant_order_query';
  546. $key = "{$mchid}-{$mch_order}";
  547. $ins->hset($name, '', [$key => $order_state]);
  548. }
  549. public static function del_queue_order($mchid,$mch_order)
  550. {
  551. if(empty($mch_order)) return;
  552. $ins = Cache::getInstance('cacheredis');
  553. $name = 'merchant_order_query';
  554. $key = "{$mchid}-{$mch_order}";
  555. $ret = $ins->hdel($name, '', $key);
  556. }
  557. public static function pop_queue_order($mchid,$mch_order,$order_time = 0)
  558. {
  559. util::del_order_channels($mchid,$mch_order);
  560. Model('refill_order')->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_HANDLED]);
  561. $ins = Cache::getInstance('cacheredis');
  562. $name = 'merchant_order_query';
  563. $key = "{$mchid}-{$mch_order}";
  564. $ret = $ins->hdel($name, '', $key);
  565. }
  566. public static function query_queue_order($mchid,$mch_order)
  567. {
  568. $ins = Cache::getInstance('cacheredis');
  569. $name = 'merchant_order_query';
  570. $key = "{$mchid}-{$mch_order}";
  571. $value = $ins->hget($name,'',$key);
  572. return $value;
  573. }
  574. public static function need_check($net_errno)
  575. {
  576. if(empty($net_errno)) return false;
  577. [$type,$code] = explode('-',$net_errno);
  578. $code = intval($code);
  579. if($type == "CURL") {
  580. static $errors = [CURLE_GOT_NOTHING,CURLE_RECV_ERROR];
  581. return in_array($code,$errors);
  582. } elseif($type == "HTTP") {
  583. static $excludes = [404];
  584. return !in_array($code,$excludes);
  585. } else {
  586. return false;
  587. }
  588. }
  589. public static function order_errflag($net_errno) {
  590. return ($net_errno === 'ORDER_CREATE_FAIL');
  591. }
  592. public static function onOrderSuccess($refill_info,$order_info)
  593. {
  594. $data = store_member::instance()->get_member($order_info['store_id']);
  595. if(empty($data)) {
  596. Log::record("cannot find member when store_id={$order_info['store_id']}",Log::ERR);
  597. return false;
  598. }
  599. $data['order_sn'] = $refill_info['order_sn'];
  600. $data['amount'] = $refill_info['channel_amount'];
  601. $model_pd = Model('predeposit');
  602. $model_pd->changePd('order_pay',$data,true);
  603. return true;
  604. }
  605. public static function getProvider($name,$type = 'RefillPhone')
  606. {
  607. $file = BASE_HELPER_RAPI_PATH . "/$name/{$type}.php";
  608. if(!file_exists($file)){
  609. Log::record("provider api file={$file} not exist.",Log::DEBUG);
  610. return false;
  611. } else {
  612. require_once($file);
  613. Log::record("file={$file} load success.",Log::DEBUG);
  614. }
  615. $class_name = "refill\\{$name}\\{$type}";
  616. if (class_exists($class_name, false)) {
  617. $caller = new $class_name([]);
  618. return $caller;
  619. } else {
  620. $error = "Base Error: class {$class_name} isn't exists!";
  621. Log::record($error, Log::ERR);
  622. return false;
  623. }
  624. }
  625. public static function xmlToArray($xml)
  626. {
  627. $object = simplexml_load_string($xml);
  628. $val = json_decode(json_encode($object), true);
  629. $msg = json_encode($val);
  630. Log::record("xmlToArray result={$msg}", Log::DEBUG);
  631. return $val;
  632. }
  633. //for tester
  634. public static function send_normal($order_sn)
  635. {
  636. $status = mt_rand(1,100);
  637. if($status > 97) {
  638. $status = 1;
  639. } else {
  640. $status = 0;
  641. }
  642. $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
  643. go(function () use ($url, $status,$order_sn)
  644. {
  645. sleep(3);
  646. while (true)
  647. {
  648. $net_errno = 0;
  649. $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
  650. if($resp == 'SUCCESS') {
  651. break;
  652. }
  653. }
  654. Log::record("resp = {$resp}",Log::DEBUG);
  655. });
  656. }
  657. public static function send_quick($order_sn)
  658. {
  659. $status = mt_rand(1,10);
  660. if($status > 3) {
  661. $status = 1;
  662. } else {
  663. $status = 0;
  664. }
  665. $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
  666. go(function () use ($url, $status,$order_sn)
  667. {
  668. sleep(3);
  669. while (true)
  670. {
  671. $net_errno = 0;
  672. $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
  673. if($resp == 'SUCCESS') {
  674. break;
  675. }
  676. }
  677. Log::record("resp = {$resp}",Log::DEBUG);
  678. });
  679. }
  680. public static function retry_canceled_order($order_id, $skip)
  681. {
  682. $mod_order = Model('vr_order');
  683. $mod_refill = Model('refill_order');
  684. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  685. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  686. if(empty($refill_info) || empty($order_info)) {
  687. return [false,'无此订单或者订单已经重试中了...'];
  688. }
  689. $tran = new trans_wapper($mod_order,'notify change order state trans');
  690. try
  691. {
  692. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
  693. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
  694. $order_state = intval($order_info['order_state']);
  695. if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
  696. $tran->commit();
  697. return [false,'订单已经在重试'];
  698. }
  699. $mod_refill->edit($order_id, ['is_retrying' => 1]);
  700. $tran->commit();
  701. $order = refill\order::from_db($refill_info,$order_info);
  702. $params = $order->queue_params();
  703. $params['order_time'] = time();
  704. if ($skip) {
  705. $mchid = $refill_info['mchid'];
  706. $mch_order = $refill_info['mch_order'];
  707. refill\util::set_next_order($mchid, $mch_order);
  708. }
  709. if(util::push_add($params)) {
  710. return [true,''];
  711. } else {
  712. return [false,'加入队列出错'];
  713. }
  714. }
  715. catch (Exception $ex) {
  716. $tran->rollback();
  717. Log::record($ex->getMessage(),Log::ERR);
  718. return [false,"{$ex->getMessage()}"];
  719. }
  720. }
  721. public static function transfer_success_order($order_id, $manual_recharge_amount)
  722. {
  723. $mod_order = Model('vr_order');
  724. $mod_refill = Model('refill_order');
  725. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  726. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  727. if(empty($refill_info) || empty($order_info)) {
  728. return [false,'无此订单'];
  729. }
  730. $tran = new trans_wapper($mod_order,'notify change order state trans');
  731. try
  732. {
  733. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
  734. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
  735. $order_state = intval($order_info['order_state']);
  736. if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
  737. $tran->commit();
  738. return [false,'订单已经在重试'];
  739. }
  740. $mod_refill->edit($order_id, ['is_retrying' => 1]);
  741. $order = refill\order::from_db($refill_info,$order_info);
  742. $mchid = $order->mchid();
  743. [$success,$success_order_id,$errmsg] = refill\RefillFactory::instance()->success_order($order);
  744. if(!$success) {
  745. $tran->rollback();
  746. return [false, $errmsg];
  747. }
  748. $mod_refill->edit($success_order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  749. $mod_refill->edit($order_id, ['is_retrying' => 0]);
  750. if($manual_recharge_amount > 0) {
  751. $mod_refill->edit($success_order_id, ['channel_amount' => $manual_recharge_amount]);
  752. }
  753. $tran->commit();
  754. return [true,''];
  755. }
  756. catch (Exception $ex) {
  757. $tran->rollback();
  758. Log::record($ex->getMessage(),Log::ERR);
  759. return [false,"{$ex->getMessage()}"];
  760. }
  761. }
  762. public static function vr_order_part()
  763. {
  764. $miner = function ($time) {
  765. return strtotime(date('Y-m-d',$time)) - 86400 * 15;
  766. };
  767. $maxer = function ($time) {
  768. return $time + 3600;
  769. };
  770. $now = time();
  771. return [['egt', $miner($now)], ['elt', $maxer($now)], 'and'];
  772. }
  773. public static function refill_order_part($order_time = 0)
  774. {
  775. $miner = function ($time) {
  776. return strtotime(date('Y-m-d',$time)) - 86400 * 15;
  777. };
  778. $maxer = function ($time) {
  779. return $time + 3600;
  780. };
  781. if($order_time == 0)
  782. {
  783. $now = time();
  784. return [['egt', $miner($now)], ['elt', $maxer($now)], 'and'];
  785. } else {
  786. return $order_time;
  787. }
  788. }
  789. private static function part_calc($time, $sub_period, $add_period)
  790. {
  791. //查询30天的订单
  792. $miner = function ($time) use($sub_period) {
  793. return strtotime(date('Y-m-d', $time)) - $sub_period;
  794. };
  795. $maxer = function ($time) use($add_period) {
  796. return $time + $add_period;
  797. };
  798. $namer = function ($time) {
  799. return 'p'.date('Ym', $time);
  800. };
  801. if (defined('DB_PARTIONED') && DB_PARTIONED)
  802. {
  803. if(is_string($time)) {
  804. $time = intval($time);
  805. }
  806. if($time == 0)
  807. {
  808. $now = time();
  809. $a = $namer($miner($now));
  810. $b = $namer($maxer($now));
  811. if($a != $b) {
  812. return [$a,$b];
  813. } else {
  814. return $a;
  815. }
  816. }
  817. else {
  818. return $namer($time);
  819. }
  820. }
  821. else {
  822. return '';
  823. }
  824. }
  825. public static function part_query($order_time = 0)
  826. {
  827. return self::part_calc($order_time, 86400 * 30, 3600);
  828. }
  829. public static function part_notify()
  830. {
  831. return self::part_calc(0, 86400 * 7, 3600);
  832. }
  833. public static function part_refill($order_time)
  834. {
  835. return self::part_calc($order_time, 86400 * 2, 3600);
  836. }
  837. public static function part_vr_order($add_time)
  838. {
  839. return self::part_calc($add_time, 86400 * 2, 3600);
  840. }
  841. public static function part_vr_order_time($order_time)
  842. {
  843. $namer = function ($time) {
  844. return 'p'.date('Ym', $time);
  845. };
  846. if (defined('DB_PARTIONED') && DB_PARTIONED)
  847. {
  848. if(is_string($order_time)) {
  849. $order_time = intval($order_time);
  850. }
  851. if ($order_time == 0) {
  852. return '';
  853. }
  854. else
  855. {
  856. $a = $namer($order_time);
  857. $b = $namer(time() + 3600); //当前时间加上一个小时误差
  858. if ($a == $b) {
  859. return $a;
  860. } else {
  861. return [$a, $b];
  862. }
  863. }
  864. }
  865. else {
  866. return '';
  867. }
  868. }
  869. public static function part_vr_create()
  870. {
  871. return self::part_calc(0, 3600, 3600);
  872. }
  873. public static function calc_part($time = 0)
  874. {
  875. return self::part_calc($time, 86400 * 15, 3600);
  876. }
  877. public static function write_ch_submit_times($chname, $fail_times, $enough_times)
  878. {
  879. $name = 'channel_submit_times_limit';
  880. $data = ["$chname" => json_encode([$fail_times, $enough_times])];
  881. wcache($name,$data,'refill-');
  882. }
  883. public static function read_ch_submit_times($chname)
  884. {
  885. $name = 'channel_submit_times_limit';
  886. $ret = rcache($name,'refill-',$chname);
  887. if(empty($ret)) {
  888. return [-1, -1];
  889. }
  890. $times = $ret[$chname];
  891. [$fail_times, $enough_times] = json_decode($times,true);
  892. return [$fail_times, $enough_times];
  893. }
  894. public static function read_ches_submit_times()
  895. {
  896. $name = 'channel_submit_times_limit';
  897. $datas = rcache($name,'refill-');
  898. if(empty($datas)) {
  899. return [-1, -1];
  900. }
  901. $ret = [];
  902. foreach ($datas as $ch_name => $times) {
  903. $ret[$ch_name] = json_decode($times, true);
  904. }
  905. return $ret;
  906. }
  907. }