util.php 29 KB

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