util.php 33 KB

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