Cron.php 21 KB

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