util.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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_add_zero($params, $period = 10)
  202. {
  203. try {
  204. QueueClient::async_push("AysncAddDispatcher", ['method' => 'add_zero', 'params' => $params], $period);
  205. return true;
  206. } catch (Exception $ex) {
  207. return false;
  208. }
  209. }
  210. public static function async_notify($chname,$data, $period)
  211. {
  212. try {
  213. QueueClient::async_push("AysncAddDispatcher", ['method' => 'notify', 'params' => ['channel' => $chname, 'params' => $data]], $period);
  214. return true;
  215. } catch (Exception $ex) {
  216. return false;
  217. }
  218. }
  219. public static function push_add($params)
  220. {
  221. try
  222. {
  223. $ret = self::push_queue('add', $params);
  224. return $ret !== false;
  225. }
  226. catch (Exception $ex) {
  227. return false;
  228. }
  229. }
  230. public static function push_add_zero($params)
  231. {
  232. try
  233. {
  234. $ret = self::push_queue('add_zero', $params);
  235. return $ret !== false;
  236. }
  237. catch (Exception $ex) {
  238. return false;
  239. }
  240. }
  241. public static function push_addthird($params)
  242. {
  243. try
  244. {
  245. $ret = self::push_queue('addthird', $params);
  246. return $ret !== false;
  247. }
  248. catch (Exception $ex) {
  249. return false;
  250. }
  251. }
  252. public static function push_notify($chname, $params)
  253. {
  254. try
  255. {
  256. $ret = self::push_queue('notify', ['channel' => $chname, 'params' => $params]);
  257. return $ret !== false;
  258. }
  259. catch (Exception $ex) {
  260. return false;
  261. }
  262. }
  263. public static function push_notify_merchant($order_id, $manual)
  264. {
  265. try
  266. {
  267. $ret = self::push_queue('notify_mechant', ['order_id' => $order_id, 'manual' => $manual]);
  268. return $ret !== false;
  269. }
  270. catch (Exception $ex) {
  271. return false;
  272. }
  273. }
  274. public static function push_notify_merchant_success($mchid, $mch_order)
  275. {
  276. try
  277. {
  278. $ret = self::push_queue('notify_merchant_success', ['mchid' => $mchid, 'mch_order' => $mch_order]);
  279. return $ret !== false;
  280. }
  281. catch (Exception $ex) {
  282. return false;
  283. }
  284. }
  285. public static function push_query($order_id)
  286. {
  287. try
  288. {
  289. $ret = self::push_queue('query', ['order_id' => $order_id]);
  290. return $ret !== false;
  291. }
  292. catch (Exception $ex) {
  293. return false;
  294. }
  295. }
  296. public static function push_auto_query($order_id,$query_times)
  297. {
  298. try
  299. {
  300. $ret = self::push_queue('query_auto', ['order_id' => $order_id,'query_times' => $query_times]);
  301. return $ret !== false;
  302. }
  303. catch (Exception $ex) {
  304. return false;
  305. }
  306. }
  307. public static function push_query_net($order_id)
  308. {
  309. try {
  310. $ret = self::push_queue('query_net', ['order_id' => $order_id]);
  311. return $ret !== false;
  312. }
  313. catch (Exception $ex) {
  314. return false;
  315. }
  316. }
  317. public static function push_query_timeout($mchid,$mch_order)
  318. {
  319. try {
  320. $ret = self::push_queue('query_timeout', ['mchid' => $mchid, 'mch_order' => $mch_order]);
  321. return $ret !== false;
  322. }
  323. catch (Exception $ex) {
  324. return false;
  325. }
  326. }
  327. public static function manual_success($order_id)
  328. {
  329. try
  330. {
  331. $ret = self::push_queue('manual_success', ['order_id' => $order_id]);
  332. return $ret !== false;
  333. }
  334. catch (Exception $ex) {
  335. return false;
  336. }
  337. }
  338. public static function manual_cancel($order_id)
  339. {
  340. try
  341. {
  342. $ret = self::push_queue('manual_cancel', ['order_id' => $order_id]);
  343. return $ret !== false;
  344. }
  345. catch (Exception $ex) {
  346. return false;
  347. }
  348. }
  349. public static function push_queue($method, $value)
  350. {
  351. if (defined('USE_COROUTINE') && USE_COROUTINE && defined('COROUTINE_HOOK_TCP') && COROUTINE_HOOK_TCP) {
  352. $queue_name = 'QUEUE_DISPATCHER_CO';
  353. $ins = Cache::getInstance('cacheredis');
  354. return $ins->lpush($queue_name, serialize([$method => $value]));
  355. }
  356. else {
  357. return queue\DispatcherClient::instance()->push($method,$value);
  358. }
  359. }
  360. public static function dispatcher_queue_length()
  361. {
  362. $ins = Cache::getInstance('cacheredis');
  363. return $ins->lLen('QUEUE_DISPATCHER_CO');
  364. }
  365. public static function monitor_queue_length()
  366. {
  367. $ins = Cache::getInstance('cacheredis');
  368. return $ins->lLen('REFILL_MONITOR_QUEUE');
  369. }
  370. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  371. public static function monitor_submit($mchid, $spec, $card_type, $mch_amount, $time)
  372. {
  373. $time = intval($time);
  374. queue\MonitorClient::instance()->onSubmit($mchid,$time,$spec,$card_type,$mch_amount);
  375. }
  376. public static function monitor_callback($mchid,$spec,$card_type,$mch_amount,$channel_amount,$succ,$time)
  377. {
  378. queue\MonitorClient::instance()->onCallback($mchid,$time,$spec,$card_type,floatval($mch_amount),floatval($channel_amount),$succ);
  379. }
  380. public static function monitor_netchk($chname,$succ)
  381. {
  382. queue\MonitorClient::instance()->onNetCheck($chname, time(),$succ);
  383. }
  384. public static function monitor_commit($chname, $spec, $card_type, $channel_amount,$commit_time)
  385. {
  386. queue\MonitorClient::instance()->onCommit($chname, $commit_time, $spec, $card_type, $channel_amount);
  387. }
  388. public static function monitor_notify($chname, $spec, $card_type, $channel_amount, $period, $succ,$commit_time,$mch_amount)
  389. {
  390. if ($succ) {
  391. $mch_amount = floatval($mch_amount);
  392. } else {
  393. $mch_amount = 0;
  394. }
  395. queue\MonitorClient::instance()->onNotify($chname, $commit_time, $spec, $card_type, floatval($channel_amount), $period, $succ,$mch_amount);
  396. }
  397. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  398. public static function onEventBeforeSubmit(order $order) : bool
  399. {
  400. $card_type = $order->card_type();
  401. Log::record("onEvent before submit {$order->unique_id()} card_type=$card_type",Log::DEBUG);
  402. return EventManager::instance()->onBeforeSubmit($order);
  403. }
  404. public static function onEventSubmit(order $order)
  405. {
  406. Log::record("onEvent submit {$order->unique_id()}",Log::DEBUG);
  407. EventManager::instance()->onSubmit($order);
  408. }
  409. public static function onEventBeforeCommit(order $order, $ch_name): bool
  410. {
  411. Log::record("onEvent before commit uid={$order->unique_id()} channel=$ch_name", Log::DEBUG);
  412. return EventManager::instance()->onBeforeCommit($order,$ch_name);
  413. }
  414. public static function onEventCommit(order $order, $ch_name)
  415. {
  416. Log::record("onEvent commit uid={$order->unique_id()} channel=$ch_name", Log::DEBUG);
  417. EventManager::instance()->onCommit($order,$ch_name);
  418. }
  419. public static function onEventNeterror(order $order, $ch_name)
  420. {
  421. Log::record("onEvent neterror uid={$order->unique_id()} channel=$ch_name", Log::DEBUG);
  422. EventManager::instance()->onNeterror($order,$ch_name);
  423. }
  424. public static function onEventNotify($refill_info, $order_info, $success)
  425. {
  426. $uid = "{$refill_info['mchid']}-{$refill_info['mch_order']}";
  427. $ch_name = $refill_info['channel_name'];
  428. Log::record("onEvent notify uid=$uid channel=$ch_name success=$success", Log::DEBUG);
  429. EventManager::instance()->onNotify($refill_info, $order_info, $success);
  430. }
  431. public static function onEventComplete($refill_info, $order_info, $success)
  432. {
  433. $uid = "{$refill_info['mchid']}-{$refill_info['mch_order']}";
  434. $ch_name = $refill_info['channel_name'];
  435. Log::record("onEvent complete uid=$uid channel=$ch_name success=$success", Log::DEBUG);
  436. EventManager::instance()->onComplete($refill_info, $order_info, $success);
  437. }
  438. public static function onEventCallback($refill_info, $mch_info, &$ctls)
  439. {
  440. EventManager::instance()->onEventCallback($refill_info, $mch_info, $ctls);
  441. }
  442. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  443. public static function set_order_channels($mchid,$mchorder,$datas)
  444. {
  445. $ins = Cache::getInstance('cacheredis');
  446. $name = 'order_channels';
  447. $key = "$mchid-$mchorder";
  448. $ins->hset($name, '', [$key => serialize($datas)]);
  449. }
  450. public static function get_order_channels($mchid, $mchorder)
  451. {
  452. //old-name oil_exclude_channels
  453. $ins = Cache::getInstance('cacheredis');
  454. $name = 'order_channels';
  455. $key = "$mchid-$mchorder";
  456. $chnames = $ins->hget($name, '', $key);
  457. $chnames = unserialize($chnames);
  458. if(is_array($chnames)) {
  459. return $chnames;
  460. } else {
  461. return [];
  462. }
  463. }
  464. public static function del_order_channels($mchid, $mchorder)
  465. {
  466. $ins = Cache::getInstance('cacheredis');
  467. $name = 'order_channels';
  468. $key = "$mchid-$mchorder";
  469. $ins->hdel($name, '', $key);
  470. }
  471. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  472. public static function set_cancel_order($mchid,$mch_order)
  473. {
  474. $ins = Cache::getInstance('cacheredis');
  475. $name = 'order_cancel_hash';
  476. $key = "$mchid-$mch_order";
  477. $ins->hset($name, '', [$key=> 1]);
  478. }
  479. public static function query_cancel_order($mchid,$mch_order)
  480. {
  481. $ins = Cache::getInstance('cacheredis');
  482. $name = 'order_cancel_hash';
  483. $key = "{$mchid}-{$mch_order}";
  484. $value = $ins->hget($name,'',$key);
  485. return $value;
  486. }
  487. public static function del_cancel_order($mchid,$mch_order)
  488. {
  489. $ins = Cache::getInstance('cacheredis');
  490. $name = 'order_cancel_hash';
  491. $key = "{$mchid}-{$mch_order}";
  492. $ins->hdel($name, $key);
  493. }
  494. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  495. public static function set_next_order($mchid,$mch_order)
  496. {
  497. $ins = Cache::getInstance('cacheredis');
  498. $name = 'order_next_hash';
  499. $key = "$mchid-$mch_order";
  500. $ins->hset($name, '', [$key=> 1]);
  501. }
  502. public static function query_next_order($mchid,$mch_order)
  503. {
  504. $ins = Cache::getInstance('cacheredis');
  505. $name = 'order_next_hash';
  506. $key = "{$mchid}-{$mch_order}";
  507. $value = $ins->hget($name,'',$key);
  508. return $value;
  509. }
  510. public static function del_next_order($mchid,$mch_order)
  511. {
  512. $ins = Cache::getInstance('cacheredis');
  513. $name = 'order_success_hash';
  514. $key = "{$mchid}-{$mch_order}";
  515. $ins->hdel($name, $key);
  516. }
  517. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  518. public static function merchant_debt_stoped($mchid)
  519. {
  520. if($mchid > 0)
  521. {
  522. $ret = rcache('merchant-debt-judge', 'refill-',"{$mchid}");
  523. if(empty($ret)) {
  524. return false;
  525. }
  526. $stoped = intval($ret[$mchid]);
  527. return ($stoped === 1);
  528. }
  529. else {
  530. return false;
  531. }
  532. }
  533. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  534. public static function loop_order_inc($card_no, $spec)
  535. {
  536. if(defined('COMPANY_NAME') && in_array(COMPANY_NAME,['ZY_COMPANY']))
  537. {
  538. $ins = Cache::getInstance('cacheredis');
  539. $name = 'loop_order_check_query';
  540. $key = "{$card_no}-{$spec}";
  541. $count = $ins->hget($name, '', $key);
  542. $count = intval($count);
  543. if($count < 0) {
  544. $ins->hset($name, '', [$key => 0]);
  545. return false;
  546. }
  547. elseif($count === 0) {
  548. $ins->hIncrBy($name, $key, 1);
  549. return false;
  550. }
  551. else {
  552. $ins->hIncrBy($name, $key, 1);
  553. return true;
  554. }
  555. }
  556. else {
  557. return false;
  558. }
  559. }
  560. public static function loop_order_dec($card_no, $spec)
  561. {
  562. if (defined('COMPANY_NAME') && in_array(COMPANY_NAME, ['ZY_COMPANY'])) {
  563. $ins = Cache::getInstance('cacheredis');
  564. $name = 'loop_order_check_query';
  565. $spec = intval($spec);
  566. $key = "{$card_no}-{$spec}";
  567. $ins->hIncrBy($name, $key, -1);
  568. }
  569. }
  570. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  571. public static function push_queue_order($mchid,$mch_order,$order_state)
  572. {
  573. if(empty($mch_order)) return;
  574. $ins = Cache::getInstance('cacheredis');
  575. $name = 'merchant_order_query';
  576. $key = "{$mchid}-{$mch_order}";
  577. $ins->hset($name, '', [$key => $order_state]);
  578. }
  579. public static function del_queue_order($mchid,$mch_order)
  580. {
  581. if(empty($mch_order)) return;
  582. $ins = Cache::getInstance('cacheredis');
  583. $name = 'merchant_order_query';
  584. $key = "{$mchid}-{$mch_order}";
  585. $ret = $ins->hdel($name, '', $key);
  586. }
  587. public static function pop_queue_order($mchid,$mch_order,$order_time = 0)
  588. {
  589. util::del_order_channels($mchid,$mch_order);
  590. Model('refill_order')->partition(util::part_refill($order_time))->edit_detail($mchid,$mch_order,['order_state' => ORDER_STATE_HANDLED]);
  591. $ins = Cache::getInstance('cacheredis');
  592. $name = 'merchant_order_query';
  593. $key = "{$mchid}-{$mch_order}";
  594. $ret = $ins->hdel($name, '', $key);
  595. }
  596. public static function query_queue_order($mchid,$mch_order)
  597. {
  598. $ins = Cache::getInstance('cacheredis');
  599. $name = 'merchant_order_query';
  600. $key = "{$mchid}-{$mch_order}";
  601. $value = $ins->hget($name,'',$key);
  602. return $value;
  603. }
  604. public static function need_check($net_errno)
  605. {
  606. if(empty($net_errno)) return false;
  607. [$type,$code] = explode('-',$net_errno);
  608. $code = intval($code);
  609. if($type == "CURL") {
  610. static $errors = [CURLE_GOT_NOTHING,CURLE_RECV_ERROR];
  611. return in_array($code,$errors);
  612. } elseif($type == "HTTP") {
  613. static $excludes = [404];
  614. return !in_array($code,$excludes);
  615. } else {
  616. return false;
  617. }
  618. }
  619. public static function order_errflag($net_errno) {
  620. return ($net_errno === 'ORDER_CREATE_FAIL');
  621. }
  622. public static function onOrderSuccess($refill_info,$order_info)
  623. {
  624. $data = store_member::instance()->get_member($order_info['store_id']);
  625. if(empty($data)) {
  626. Log::record("cannot find member when store_id={$order_info['store_id']}",Log::ERR);
  627. return false;
  628. }
  629. $data['order_sn'] = $refill_info['order_sn'];
  630. $data['amount'] = $refill_info['channel_amount'];
  631. $model_pd = Model('predeposit');
  632. $model_pd->changePd('order_pay',$data,true);
  633. return true;
  634. }
  635. public static function getProvider($name,$type = 'RefillPhone')
  636. {
  637. $file = BASE_HELPER_RAPI_PATH . "/$name/{$type}.php";
  638. if(!file_exists($file)){
  639. Log::record("provider api file={$file} not exist.",Log::DEBUG);
  640. return false;
  641. } else {
  642. require_once($file);
  643. Log::record("file={$file} load success.",Log::DEBUG);
  644. }
  645. $class_name = "refill\\{$name}\\{$type}";
  646. if (class_exists($class_name, false)) {
  647. $caller = new $class_name([]);
  648. return $caller;
  649. } else {
  650. $error = "Base Error: class {$class_name} isn't exists!";
  651. Log::record($error, Log::ERR);
  652. return false;
  653. }
  654. }
  655. public static function xmlToArray($xml)
  656. {
  657. $object = simplexml_load_string($xml);
  658. $val = json_decode(json_encode($object), true);
  659. $msg = json_encode($val);
  660. Log::record("xmlToArray result={$msg}", Log::DEBUG);
  661. return $val;
  662. }
  663. //for tester
  664. public static function send_normal($order_sn)
  665. {
  666. $status = mt_rand(1,100);
  667. if($status > 97) {
  668. $status = 1;
  669. } else {
  670. $status = 0;
  671. }
  672. $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
  673. go(function () use ($url, $status,$order_sn)
  674. {
  675. sleep(3);
  676. while (true)
  677. {
  678. $net_errno = 0;
  679. $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
  680. if($resp == 'SUCCESS') {
  681. break;
  682. }
  683. }
  684. Log::record("resp = {$resp}",Log::DEBUG);
  685. });
  686. }
  687. public static function send_quick($order_sn)
  688. {
  689. $status = mt_rand(1,10);
  690. if($status > 3) {
  691. $status = 1;
  692. } else {
  693. $status = 0;
  694. }
  695. $url = BASE_SITE_URL . "/mobile/callback/refill_baidu.php";
  696. go(function () use ($url, $status,$order_sn)
  697. {
  698. sleep(3);
  699. while (true)
  700. {
  701. $net_errno = 0;
  702. $resp = http_request($url,['status' => $status,'order_sn' => $order_sn],'GET', false, [], $net_errno);
  703. if($resp == 'SUCCESS') {
  704. break;
  705. }
  706. }
  707. Log::record("resp = {$resp}",Log::DEBUG);
  708. });
  709. }
  710. public static function retry_canceled_order($order_id, $skip)
  711. {
  712. $mod_order = Model('vr_order');
  713. $mod_refill = Model('refill_order');
  714. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  715. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  716. if(empty($refill_info) || empty($order_info)) {
  717. return [false,'无此订单或者订单已经重试中了...'];
  718. }
  719. $tran = new trans_wapper($mod_order,'notify change order state trans');
  720. try
  721. {
  722. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
  723. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
  724. $order_state = intval($order_info['order_state']);
  725. if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
  726. $tran->commit();
  727. return [false,'订单已经在重试'];
  728. }
  729. $mod_refill->edit($order_id, ['is_retrying' => 1]);
  730. $tran->commit();
  731. $order = refill\order::from_db($refill_info,$order_info);
  732. $params = $order->queue_params();
  733. $params['order_time'] = time();
  734. if ($skip) {
  735. $mchid = $refill_info['mchid'];
  736. $mch_order = $refill_info['mch_order'];
  737. refill\util::set_next_order($mchid, $mch_order);
  738. }
  739. if(util::push_add($params)) {
  740. return [true,''];
  741. } else {
  742. return [false,'加入队列出错'];
  743. }
  744. }
  745. catch (Exception $ex) {
  746. $tran->rollback();
  747. Log::record($ex->getMessage(),Log::ERR);
  748. return [false,"{$ex->getMessage()}"];
  749. }
  750. }
  751. public static function transfer_success_order($order_id, $manual_recharge_amount)
  752. {
  753. $mod_order = Model('vr_order');
  754. $mod_refill = Model('refill_order');
  755. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  756. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  757. if(empty($refill_info) || empty($order_info)) {
  758. return [false,'无此订单'];
  759. }
  760. $tran = new trans_wapper($mod_order,'notify change order state trans');
  761. try
  762. {
  763. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id], '*', true, true);
  764. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id, 'inner_status' => 0], '*', true, true);
  765. $order_state = intval($order_info['order_state']);
  766. if(empty($refill_info) || $refill_info['is_retrying'] == 1 || $order_state != ORDER_STATE_CANCEL) {
  767. $tran->commit();
  768. return [false,'订单已经在重试'];
  769. }
  770. $mod_refill->edit($order_id, ['is_retrying' => 1]);
  771. $order = refill\order::from_db($refill_info,$order_info);
  772. $mchid = $order->mchid();
  773. [$success,$success_order_id,$errmsg] = refill\RefillFactory::instance()->success_order($order);
  774. if(!$success) {
  775. $tran->rollback();
  776. return [false, $errmsg];
  777. }
  778. $mod_refill->edit($success_order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  779. $mod_refill->edit($order_id, ['is_retrying' => 0]);
  780. if($manual_recharge_amount > 0) {
  781. $mod_refill->edit($success_order_id, ['channel_amount' => $manual_recharge_amount]);
  782. }
  783. $tran->commit();
  784. return [true,''];
  785. }
  786. catch (Exception $ex) {
  787. $tran->rollback();
  788. Log::record($ex->getMessage(),Log::ERR);
  789. return [false,"{$ex->getMessage()}"];
  790. }
  791. }
  792. public static function vr_order_part()
  793. {
  794. $miner = function ($time) {
  795. return strtotime(date('Y-m-d',$time)) - 86400 * 15;
  796. };
  797. $maxer = function ($time) {
  798. return $time + 3600;
  799. };
  800. $now = time();
  801. return [['egt', $miner($now)], ['elt', $maxer($now)], 'and'];
  802. }
  803. public static function refill_order_part($order_time = 0)
  804. {
  805. $miner = function ($time) {
  806. return strtotime(date('Y-m-d',$time)) - 86400 * 15;
  807. };
  808. $maxer = function ($time) {
  809. return $time + 3600;
  810. };
  811. if($order_time == 0)
  812. {
  813. $now = time();
  814. return [['egt', $miner($now)], ['elt', $maxer($now)], 'and'];
  815. } else {
  816. return $order_time;
  817. }
  818. }
  819. private static function part_calc($time, $sub_period, $add_period)
  820. {
  821. //查询30天的订单
  822. $miner = function ($time) use($sub_period) {
  823. return strtotime(date('Y-m-d', $time)) - $sub_period;
  824. };
  825. $maxer = function ($time) use($add_period) {
  826. return $time + $add_period;
  827. };
  828. $namer = function ($time) {
  829. return 'p'.date('Ym', $time);
  830. };
  831. if (defined('DB_PARTIONED') && DB_PARTIONED)
  832. {
  833. if(is_string($time)) {
  834. $time = intval($time);
  835. }
  836. if($time == 0)
  837. {
  838. $now = time();
  839. $a = $namer($miner($now));
  840. $b = $namer($maxer($now));
  841. if($a != $b) {
  842. return [$a,$b];
  843. } else {
  844. return $a;
  845. }
  846. }
  847. else {
  848. return $namer($time);
  849. }
  850. }
  851. else {
  852. return '';
  853. }
  854. }
  855. public static function part_query($order_time = 0)
  856. {
  857. return self::part_calc($order_time, 86400 * 30, 3600);
  858. }
  859. public static function part_notify()
  860. {
  861. return self::part_calc(0, 86400 * 7, 3600);
  862. }
  863. public static function part_refill($order_time)
  864. {
  865. return self::part_calc($order_time, 86400 * 2, 3600);
  866. }
  867. public static function part_vr_order($add_time)
  868. {
  869. return self::part_calc($add_time, 86400 * 2, 3600);
  870. }
  871. public static function part_vr_order_time($order_time)
  872. {
  873. $namer = function ($time) {
  874. return 'p'.date('Ym', $time);
  875. };
  876. if (defined('DB_PARTIONED') && DB_PARTIONED)
  877. {
  878. if(is_string($order_time)) {
  879. $order_time = intval($order_time);
  880. }
  881. if ($order_time == 0) {
  882. return '';
  883. }
  884. else
  885. {
  886. $a = $namer($order_time);
  887. $b = $namer(time() + 3600); //当前时间加上一个小时误差
  888. if ($a == $b) {
  889. return $a;
  890. } else {
  891. return [$a, $b];
  892. }
  893. }
  894. }
  895. else {
  896. return '';
  897. }
  898. }
  899. public static function part_vr_create()
  900. {
  901. return self::part_calc(0, 3600, 3600);
  902. }
  903. public static function calc_part($time = 0)
  904. {
  905. return self::part_calc($time, 86400 * 15, 3600);
  906. }
  907. public static function write_ch_submit_times($chname, $fail_times, $enough_times)
  908. {
  909. $name = 'channel_submit_times_limit';
  910. $data = ["$chname" => json_encode([$fail_times, $enough_times])];
  911. wcache($name,$data,'refill-');
  912. }
  913. public static function read_ch_submit_times($chname)
  914. {
  915. $name = 'channel_submit_times_limit';
  916. $ret = rcache($name,'refill-',$chname);
  917. if(empty($ret)) {
  918. return [-1, -1];
  919. }
  920. $times = $ret[$chname];
  921. [$fail_times, $enough_times] = json_decode($times,true);
  922. return [$fail_times, $enough_times];
  923. }
  924. public static function read_ches_submit_times()
  925. {
  926. $name = 'channel_submit_times_limit';
  927. $datas = rcache($name,'refill-');
  928. if(empty($datas)) {
  929. return [-1, -1];
  930. }
  931. $ret = [];
  932. foreach ($datas as $ch_name => $times) {
  933. $ret[$ch_name] = json_decode($times, true);
  934. }
  935. return $ret;
  936. }
  937. public static function write_yifutong_order($mch_order,$params)
  938. {
  939. wcache('yifutong_order', [$mch_order => serialize($params)], 'yft-');
  940. }
  941. public static function read_yifutong_order($mch_order)
  942. {
  943. $data = rcache('yifutong_order', 'yft-', $mch_order);
  944. $val = empty($data[$mch_order]) ? [] : unserialize($data[$mch_order]);
  945. return $val;
  946. }
  947. }