Cron.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. namespace Main\Lib;
  3. use Dever;
  4. class Cron
  5. {
  6. public function buy_code_api()
  7. {
  8. # 根据订单的buy_id来确定生成的本数
  9. $id = Dever::input('product_id', 26);
  10. $where['product_id'] = $id;
  11. $where['status'] = 2;
  12. //$where['score_status'] = 1;
  13. $order_id = Dever::input('order_id');
  14. if ($order_id) {
  15. $where['order_id'] = $order_id;
  16. }
  17. $uid = Dever::input('uid');
  18. if ($uid) {
  19. $where['uid'] = $uid;
  20. }
  21. $order = Dever::db('act/order')->state($where);
  22. $test = Dever::input('test');
  23. if ($test == 1) {
  24. print_r($order);die;
  25. }
  26. if ($order) {
  27. $db = Dever::load('code/lib/core');
  28. foreach ($order as $k => $v) {
  29. if ($v['type'] == 1 || $v['type'] == 3) {
  30. $v['code'] = '';
  31. $v['type'] = 3;
  32. $db->createCodeByOrder($v);
  33. }
  34. }
  35. }
  36. return 'ok';
  37. }
  38. public function buy_num_api()
  39. {
  40. # 根据订单的buy_id来确定生成的本数
  41. $id = Dever::input('product_id', 26);
  42. $where['product_id'] = $id;
  43. $where['status'] = 2;
  44. //$where['score_status'] = 1;
  45. $order_id = Dever::input('order_id');
  46. if ($order_id) {
  47. $where['order_id'] = $order_id;
  48. }
  49. $uid = Dever::input('uid');
  50. if ($uid) {
  51. $where['uid'] = $uid;
  52. }
  53. $order = Dever::db('act/order')->state($where);
  54. $test = Dever::input('test');
  55. if ($test == 1) {
  56. print_r($order);die;
  57. }
  58. if ($order) {
  59. $db = Dever::load('act/lib/num');
  60. $user = array();
  61. foreach ($order as $k => $v) {
  62. if ($v['type'] == 1 || $v['type'] == 3) {
  63. if ($v['buy_id'] > 0) {
  64. $num = Dever::db('journal/buy_num')->one($v['buy_id']);
  65. if ($num) {
  66. if (isset($user[$v['uid']]) && $user[$v['uid']]) {
  67. $user[$v['uid']]['num'] += $num['num'];
  68. } else {
  69. $user[$v['uid']] = array();
  70. $user[$v['uid']]['uid'] = $v['uid'];
  71. $user[$v['uid']]['id'] = $v['product_id'];
  72. $user[$v['uid']]['num'] = $num['num'];
  73. }
  74. }
  75. }
  76. }
  77. }
  78. foreach ($user as $k => $v) {
  79. $db->submit($v['id'], $v['uid'], $v['num'], false);
  80. }
  81. }
  82. return 'ok';
  83. }
  84. # 增加积分
  85. public function scores2_api()
  86. {
  87. //$where['start'] = Dever::maketime('2019-04-24 23:12:40');
  88. $where['start'] = Dever::maketime('2019-04-25 00:34:55');
  89. $where['end'] = Dever::maketime('2019-04-25 14:47:16');
  90. $where['score_type'] = 1;
  91. $where['action_id'] = Dever::input('action_id');
  92. $data = Dever::db('score/action_log')->getDataByDate($where);
  93. $test = Dever::input('test');
  94. if ($test == 1) {
  95. print_r($data);die;
  96. }
  97. if ($data) {
  98. foreach ($data as $k => $v) {
  99. Dever::load('score/lib/core.oper?log_id='.$v['id']);
  100. }
  101. }
  102. return $data;
  103. # 购买小刊
  104. /*
  105. $where['product_id'] = Dever::input('journal_id', 28);
  106. $where['type'] = 1;
  107. $where['status'] = 2;
  108. $data = Dever::db('journal/order')->state($where);
  109. $score = 20;
  110. if ($data) {
  111. foreach ($data as $k => $v) {
  112. # 计算该订单能加的积分数
  113. if ($v['buy_id'] && $v['buy_id'] > 0) {
  114. $buy = Dever::db('journal/buy_num')->one($data['buy_id']);
  115. if ($buy && $buy['num'] > 0) {
  116. $update_score = $score*$buy['num'];
  117. }
  118. }
  119. }
  120. }
  121. # 兑换小刊
  122. $where['product_id'] = Dever::input('journal_id', 28);
  123. $where['type'] = 2;
  124. $where['status'] = 2;
  125. $data = Dever::db('journal/order')->state($where);
  126. */
  127. }
  128. # 去重
  129. public function quchong_api()
  130. {
  131. # 购买小刊
  132. $id = Dever::input('product_id', 28);
  133. $where['product_id'] = $id;
  134. $where['status'] = 2;
  135. $order_id = Dever::input('order_id');
  136. if ($order_id) {
  137. $where['order_id'] = $order_id;
  138. }
  139. $uid = Dever::input('uid');
  140. if ($uid) {
  141. $where['uid'] = $uid;
  142. }
  143. $where['type'] = 2;
  144. //$where['score_status'] = 1;
  145. $order = Dever::db('journal/order')->state($where);
  146. if($order) {
  147. $user = array();
  148. foreach ($order as $k => $v) {
  149. if ($v['type'] == 2) {
  150. $key = $v['uid'] . '_' . $v['code'];
  151. if (!isset($user[$key])) {
  152. $user[$key] = $v;
  153. } else {
  154. Dever::db('journal/order')->update(array('where_id' => $v['id'], 'state' => 2));
  155. }
  156. }
  157. }
  158. }
  159. return 'ok';
  160. }
  161. # 重新计算排行榜
  162. public function pai4_api()
  163. {
  164. $id = Dever::input('product_id', 28);
  165. # 购买小刊
  166. $where['product_id'] = $id;
  167. $where['status'] = 2;
  168. $order_id = Dever::input('order_id');
  169. if ($order_id) {
  170. $where['order_id'] = $order_id;
  171. }
  172. $uid = Dever::input('uid');
  173. if ($uid) {
  174. $where['uid'] = $uid;
  175. }
  176. $where['type'] = array(1,3);
  177. //$where['score_status'] = 1;
  178. $order = Dever::db('journal/order')->getBuy($where);
  179. if ($order) {
  180. $user = array();
  181. foreach ($order as $k => $v) {
  182. $this->paiOrder1($v, $user);
  183. }
  184. }
  185. $test = Dever::input('test');
  186. if ($test == 1) {
  187. print_r($user);die;
  188. }
  189. # 重建积分排行
  190. foreach ($user as $k => $v) {
  191. print_r($v);
  192. $info = Dever::db('act/score')->one(array('uid' => $v['uid'], 'type' => 4, 'data_id' => $id));
  193. if (!$info) {
  194. $insert['uid'] = $v['uid'];
  195. $insert['type'] = 4;
  196. $insert['data_id'] = $id;
  197. $insert['score'] = $v['score'];
  198. Dever::db('act/score')->insert($insert);
  199. } else {
  200. $update['where_id'] = $info['id'];
  201. $update['uid'] = $v['uid'];
  202. $update['type'] = 4;
  203. $update['data_id'] = $id;
  204. $update['score'] = $v['score'] + $info['score'];
  205. Dever::db('act/score')->update($update);
  206. }
  207. }
  208. return 'ok';
  209. }
  210. public function paiOrder1($order, &$user)
  211. {
  212. if (!isset($user[$order['uid']])) {
  213. $user[$order['uid']] = array();
  214. $user[$order['uid']]['uid'] = $order['uid'];
  215. $user[$order['uid']]['score'] = 0;
  216. }
  217. $score = 0;
  218. if ($order['cash'] == 6) {
  219. $score = 20;
  220. }
  221. if ($order['cash'] == 60) {
  222. $score = 20*10;
  223. }
  224. if ($order['cash'] == 588) {
  225. $score = 20*100;
  226. }
  227. if ($order['cash'] == 1728) {
  228. $score = 20*300;
  229. }
  230. if ($order['cash'] == 3348) {
  231. $score = 20*600;
  232. }
  233. $user[$order['uid']]['score'] += $score;
  234. }
  235. # 重新计算排行榜
  236. public function pai3_api()
  237. {
  238. $id = Dever::input('product_id', 28);
  239. # 购买小刊
  240. $where['product_id'] = $id;
  241. $where['status'] = 2;
  242. $order_id = Dever::input('order_id');
  243. if ($order_id) {
  244. $where['order_id'] = $order_id;
  245. }
  246. $uid = Dever::input('uid');
  247. if ($uid) {
  248. $where['uid'] = $uid;
  249. }
  250. $where['type'] = 2;
  251. //$where['score_status'] = 1;
  252. $order = Dever::db('journal/order')->getBuy($where);
  253. $user = array();
  254. if ($order) {
  255. foreach ($order as $k1 => $v1) {
  256. $this->paiOrder($v1, $user);
  257. //Dever::db('journal/order')->update(array('where_id' => $v1['id'], 'score_status' => 2));
  258. }
  259. }
  260. $test = Dever::input('test');
  261. if ($test == 1) {
  262. print_r($user);die;
  263. }
  264. # 重建积分排行
  265. foreach ($user as $k => $v) {
  266. print_r($v);
  267. $info = Dever::db('act/score')->one(array('uid' => $v['uid'], 'type' => 4, 'data_id' => $id));
  268. if (!$info) {
  269. $insert['uid'] = $v['uid'];
  270. $insert['type'] = 4;
  271. $insert['data_id'] = $id;
  272. $insert['score'] = $v['score'];
  273. Dever::db('act/score')->insert($insert);
  274. } else {
  275. $update['where_id'] = $info['id'];
  276. $update['uid'] = $v['uid'];
  277. $update['type'] = 4;
  278. $update['data_id'] = $id;
  279. $update['score'] = $v['score'] + $info['score'];
  280. Dever::db('act/score')->update($update);
  281. }
  282. }
  283. return 'ok';
  284. }
  285. private function paiOrder($order, &$user)
  286. {
  287. if (!isset($user[$order['uid']])) {
  288. $user[$order['uid']] = array();
  289. $user[$order['uid']]['uid'] = $order['uid'];
  290. $user[$order['uid']]['score'] = 0;
  291. }
  292. if ($order['type'] <= 2) {
  293. # 购买小刊、兑换小刊增加积分
  294. if ($order['type'] == 1 && $order['buy_id'] > 0) {
  295. $buy = Dever::db('journal/buy_num')->one($order['buy_id']);
  296. if ($buy) {
  297. $num = $buy['num'];
  298. $user[$order['uid']]['score'] += $num*20;
  299. }
  300. } elseif ($order['type'] == 2 && $order['code']) {
  301. $code = Dever::db('code/info')->one(array('code' => $order['code'], 'state' => 1, 'type' => 3));
  302. if ($code) {
  303. if ($code['order_id']) {
  304. $info = Dever::db('journal/order')->one(array('order_id' => $code['order_id'], 'status' => 2, 'type' => 3));
  305. if ($info) {
  306. $num = $code['product_num'];
  307. $user[$order['uid']]['score'] += $num*20;
  308. }
  309. } elseif ($code['product_num'] > 0) {
  310. $num = $code['product_num'];
  311. $user[$order['uid']]['score'] += $num*20;
  312. }
  313. }
  314. }
  315. }
  316. }
  317. # 收回多余的兑换码
  318. public function deleteCode_api()
  319. {
  320. $order_id = Dever::input('order_id');
  321. if ($order_id) {
  322. $where['order_id'] = $order_id;
  323. }
  324. $where['product_id'] = 28;
  325. //$where['refund_status'] = 1;
  326. $where['type'] = 3;
  327. $data = Dever::db('journal/order')->state($where);
  328. $test = Dever::input('test');
  329. if ($test == 1) {
  330. print_r($data);
  331. }
  332. if ($data) {
  333. foreach ($data as $k => $v) {
  334. $this->deleteCodes($v);
  335. }
  336. }
  337. return 'ok';
  338. }
  339. private function deleteCodes($data)
  340. {
  341. # 获取该订单所能拥有的兑换码数
  342. $code_num = 1;
  343. $uid = $data['uid'];
  344. if ($data['buy_id'] > 0) {
  345. $buy = Dever::db('journal/buy_num')->one($data['buy_id']);
  346. $code_num = $buy['code'];
  347. }
  348. # 这个订单下所有码
  349. $code_where['order_id'] = $data['order_id'];
  350. $code_where['type'] = 4;
  351. $code = Dever::db('code/info')->getData($code_where);
  352. # code_num = 1
  353. # code = 21
  354. $test = Dever::input('test');
  355. if ($test == 1) {
  356. echo $code_num;
  357. print_r($code);die;
  358. }
  359. if ($code) {
  360. $i = 1;
  361. foreach ($code as $k => $v) {
  362. if ($i > $code_num) {
  363. Dever::db('code/info')->update(array('where_id' => $v['id'], 'state' => 2));
  364. }
  365. $i++;
  366. }
  367. }
  368. }
  369. public function refund_api()
  370. {
  371. $order_id = Dever::input('order_id');
  372. if ($order_id) {
  373. $where['order_id'] = $order_id;
  374. }
  375. $where['product_id'] = 28;
  376. $where['status'] = 5;
  377. $where['refund_status'] = 1;
  378. $data = Dever::db('journal/order')->state($where);
  379. $test = Dever::input('test');
  380. if ($test == 1) {
  381. print_r($data);die;
  382. }
  383. if ($data) {
  384. foreach ($data as $k => $v) {
  385. $this->refundScore($v);
  386. }
  387. }
  388. }
  389. private function refundScore($data)
  390. {
  391. $score = 0;
  392. if ($data['cash'] == 6) {
  393. $score = 20;
  394. }
  395. if ($data['cash'] == 60) {
  396. $score = 20*10;
  397. }
  398. if ($data['cash'] == 588) {
  399. $score = 20*100;
  400. }
  401. if ($data['cash'] == 1728) {
  402. $score = 20*300;
  403. }
  404. if ($data['cash'] == 3348) {
  405. $score = 20*600;
  406. }
  407. if ($score > 0) {
  408. if ($data['type'] == 1) {
  409. $where['uid'] = $data['uid'];
  410. $user = Dever::db('score/user')->one($where);
  411. if ($user) {
  412. $user_score = $user['score'] - $score;
  413. if ($user_score < 0) {
  414. $user_score = 0;
  415. }
  416. Dever::db('score/user')->update(array('where_id' => $user['id'], 'score' => $user_score));
  417. }
  418. $where['type'] = 4;
  419. $where['data_id'] = 28;
  420. $user = Dever::db('act/score')->one($where);
  421. if ($user) {
  422. $user_score = $user['score'] - $score;
  423. if ($user_score < 0) {
  424. $user_score = 0;
  425. }
  426. Dever::db('act/score')->update(array('where_id' => $user['id'], 'score' => $user_score));
  427. }
  428. } elseif ($data['type'] == 3) {
  429. $code_where['order_id'] = $data['order_id'];
  430. //$code_where['type'] = 1;
  431. $code_where['create_uid'] = $data['uid'];
  432. $code = Dever::db('code/info')->state($code_where);
  433. if ($code) {
  434. print_r($code);
  435. foreach ($code as $k => $v) {
  436. if ($v['type'] == 1) {
  437. Dever::db('code/info')->update(array('where_id' => $v['id'], 'type' => 4));
  438. } elseif ($v['type'] == 3) {
  439. $this->refundScoreCode($v);
  440. }
  441. }
  442. }
  443. }
  444. Dever::db('journal/order')->update(array('where_id' => $data['id'], 'refund_status' => 2));
  445. }
  446. print_r($data);
  447. print_r($score);
  448. echo "\r\n";
  449. return $score;
  450. }
  451. private function refundScoreCode($data)
  452. {
  453. if (isset($data['score']) && $data['score'] > 0) {
  454. $score = $data['score'];
  455. } elseif (isset($data['product_num']) && $data['product_num'] > 0) {
  456. $score = $data['product_num'] * 20;
  457. }
  458. if ($score > 0) {
  459. $where['uid'] = $data['uid'];
  460. $user = Dever::db('score/user')->one($where);
  461. if ($user) {
  462. $user_score = $user['score'] - $score;
  463. if ($user_score < 0) {
  464. $user_score = 0;
  465. }
  466. Dever::db('score/user')->update(array('where_id' => $user['id'], 'score' => $user_score));
  467. }
  468. $where['type'] = 4;
  469. $where['data_id'] = 28;
  470. $user = Dever::db('act/score')->one($where);
  471. if ($user) {
  472. $user_score = $user['score'] - $score;
  473. if ($user_score < 0) {
  474. $user_score = 0;
  475. }
  476. Dever::db('act/score')->update(array('where_id' => $user['id'], 'score' => $user_score));
  477. }
  478. }
  479. }
  480. public function pays_api()
  481. {
  482. $where['product_id'] = 28;
  483. $where['type'] = 3;
  484. $where['status'] = 1;
  485. $where['cron_status'] = 1;
  486. $data = Dever::db('journal/order')->state($where);
  487. if ($data) {
  488. foreach ($data as $k => $v) {
  489. $this->getpays($v['order_id']);
  490. Dever::db('journal/order')->update(array('where_id' => $v['id'], 'cron_status' => 2));
  491. }
  492. }
  493. }
  494. private function getpays($order_id)
  495. {
  496. $url = 'http://mapi.jstyle.cn/pay/pay/?api.search&order_id='.$order_id.'&account_id=1';
  497. Dever::curl($url);
  498. }
  499. public function unpay_api()
  500. {}
  501. public function activeEnd_api()
  502. {}
  503. #用户未支付电子刊情况下,未支付订单超过2小时后,发送一次唤回支付提醒
  504. public function unpay()
  505. {
  506. $time = 7200;
  507. $where['cdate'] = time() + $time;
  508. $where['note'] = 1;
  509. $where['type'] = '1,3';
  510. $data = Dever::db('journal/order')->getDataByTime($where);
  511. if ($data) {
  512. foreach ($data as $k => $v) {
  513. $user = Dever::db('passport/user')->one($v['uid']);
  514. if (!$user) {
  515. continue;
  516. }
  517. Dever::db('journal/order')->update(array('where_id' => $v['id'], 'note' => 2));
  518. $journal = Dever::db('journal/info')->one($v['product_id']);
  519. if (Dever::project('message')) {
  520. Dever::load('message/lib/data')->push(-1, $v['uid'], '支付提醒', '您订阅的'.$journal['name'].'尚未支付!', 11, $v['cate_id'], 1, Dever::load('act/lib/note')->push(4, $journal['id'], $journal['name']));
  521. }
  522. # 发送短消息
  523. if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms')) {
  524. $send = array();
  525. $send['name'] = $journal['name'];
  526. //Dever::load('sms/api.send', 'note_live', $user['mobile'], $send);
  527. }
  528. # 发模板消息
  529. $wechat = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'type' => 1, 'system_id' => $v['cate_id']));
  530. if ($wechat && Dever::project('wechat_applet')) {
  531. $send['key'] = 'unbuy_journal';
  532. $send['project_id'] = $v['cate_id'];
  533. $send['touser'] = $wechat['openid'];
  534. $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($v['uid']) . ',' . '4,' . $v['product_id'];
  535. $send['data'] = array
  536. (
  537. 'keyword1' => array('value' => $journal['name']),
  538. 'keyword2' => array('value' => $v['cash']),
  539. 'keyword3' => array('value' => '您订阅的'.$journal['name'].'尚未支付!'),
  540. );
  541. $send['data'] = json_encode($send['data']);
  542. $send['form_id'] = Dever::load('act/lib/form')->get($v['uid'], 2, $v['cate_id']);
  543. if ($send['form_id']) {
  544. Dever::load('wechat_applet/msg.send', $send);
  545. }
  546. }
  547. }
  548. }
  549. return 'ok';
  550. }
  551. #电子杂志活动结束前5个时,向己有电子刊阅读权限的用户发送活动结束提醒
  552. public function activeEnd()
  553. {
  554. $time = 5*3600;
  555. $where['ends'] = time();
  556. $where['end'] = $where['ends'] + $time;
  557. $data = Dever::db('journal/active')->getDataByTime($where);
  558. //echo Dever::sql();die;
  559. $test = Dever::input('test');
  560. if ($test == 1) {
  561. print_r($data);die;
  562. }
  563. if ($data) {
  564. foreach ($data as $ks => $vs) {
  565. $journal = Dever::db('journal/info')->one($vs['info_id']);
  566. $subscribe = Dever::db('act/subscribe')->state(array('note' => 1, 'type' => 4, 'data_id' => $vs['info_id']));
  567. if ($subscribe) {
  568. foreach ($subscribe as $k => $v) {
  569. $user = Dever::db('passport/user')->one($v['uid']);
  570. if ($user) {
  571. Dever::db('act/subscribe')->update(array('where_id' => $v['id'], 'note' => 2));
  572. if (Dever::project('message')) {
  573. Dever::load('message/lib/data')->push(-1, $v['uid'], '活动结束提醒', '您参与的'.$journal['name'].'积分排行榜活动还有5小时就要结束了,想要冲榜赢奖品的要抓紧机会呀〜', 11, $journal['cate_id'], 1, Dever::load('act/lib/note')->push(4, $journal['id'], $journal['name']));
  574. }
  575. # 发送短消息
  576. if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms')) {
  577. $send = array();
  578. $send['name'] = $journal['name'];
  579. //Dever::load('sms/api.send', 'note_live', $user['mobile'], $send);
  580. }
  581. # 发模板消息
  582. $wechat = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'type' => 1, 'system_id' => $journal['cate_id']));
  583. if ($wechat && Dever::project('wechat_applet')) {
  584. $send['key'] = 'service_end_note';
  585. $send['project_id'] = $journal['cate_id'];
  586. $send['touser'] = $wechat['openid'];
  587. $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($v['uid']) . ',' . '4,' . $journal['id'];
  588. $send['data'] = array
  589. (
  590. 'keyword1' => array('value' => $journal['name']),
  591. 'keyword2' => array('value' => date('Y-m-d H:i', $vs['end'])),
  592. 'keyword3' => array('value' => '您参与的'.$journal['name'].'积分排行榜活动还有5小时就要结束了,想要冲榜赢奖品的要抓紧机会呀~'),
  593. );
  594. $send['data'] = json_encode($send['data']);
  595. $send['form_id'] = Dever::load('act/lib/form')->get($v['uid'], 1, $journal['cate_id']);
  596. if (!$send['form_id']) {
  597. $send['form_id'] = Dever::load('act/lib/form')->get($v['uid'], 2, $journal['cate_id']);
  598. }
  599. if ($send['form_id']) {
  600. Dever::load('wechat_applet/msg.send', $send);
  601. }
  602. }
  603. }
  604. }
  605. }
  606. }
  607. }
  608. return 'ok';
  609. }
  610. }