Cron.php 22 KB

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