Cron.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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')->getBuy($where);
  68. print_r($order);die;
  69. if($order) {
  70. $user = array();
  71. foreach ($order as $k => $v) {
  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. # 重新计算排行榜
  82. public function pai3_api()
  83. {
  84. $id = Dever::input('product_id', 28);
  85. # 购买小刊
  86. $where['product_id'] = $id;
  87. $where['status'] = 2;
  88. $order_id = Dever::input('order_id');
  89. if ($order_id) {
  90. $where['order_id'] = $order_id;
  91. }
  92. $uid = Dever::input('uid');
  93. if ($uid) {
  94. $where['uid'] = $uid;
  95. }
  96. $where['type'] = 2;
  97. //$where['score_status'] = 1;
  98. $order = Dever::db('journal/order')->getBuy($where);
  99. $user = array();
  100. if ($order) {
  101. foreach ($order as $k1 => $v1) {
  102. $this->paiOrder($v1, $user);
  103. //Dever::db('journal/order')->update(array('where_id' => $v1['id'], 'score_status' => 2));
  104. }
  105. }
  106. $test = Dever::input('test');
  107. if ($test == 1) {
  108. print_r($user);die;
  109. }
  110. # 重建积分排行
  111. foreach ($user as $k => $v) {
  112. print_r($v);
  113. $info = Dever::db('act/score')->one(array('uid' => $v['uid'], 'type' => 4, 'data_id' => $id));
  114. if (!$info) {
  115. $insert['uid'] = $v['uid'];
  116. $insert['type'] = 4;
  117. $insert['data_id'] = $id;
  118. $insert['score'] = $v['score'];
  119. Dever::db('act/score')->insert($insert);
  120. } else {
  121. $update['where_id'] = $info['id'];
  122. $update['uid'] = $v['uid'];
  123. $update['type'] = 4;
  124. $update['data_id'] = $id;
  125. $update['score'] = $v['score'] + $info['score'];
  126. Dever::db('act/score')->update($update);
  127. }
  128. }
  129. return 'ok';
  130. }
  131. private function paiOrder($order, &$user)
  132. {
  133. if (!isset($user[$order['uid']])) {
  134. $user[$order['uid']] = array();
  135. $user[$order['uid']]['uid'] = $order['uid'];
  136. $user[$order['uid']]['score'] = 0;
  137. }
  138. if ($order['type'] <= 2) {
  139. # 购买小刊、兑换小刊增加积分
  140. if ($order['type'] == 1 && $order['buy_id'] > 0) {
  141. $buy = Dever::db('journal/buy_num')->one($order['buy_id']);
  142. if ($buy) {
  143. $num = $buy['num'];
  144. $user[$order['uid']]['score'] += $num*20;
  145. }
  146. } elseif ($order['type'] == 2 && $order['code']) {
  147. $code = Dever::db('code/info')->one(array('code' => $order['code'], 'state' => 1, 'type' => 3));
  148. if ($code) {
  149. if ($code['order_id']) {
  150. $info = Dever::db('journal/order')->one(array('order_id' => $code['order_id'], 'status' => 2, 'type' => 3));
  151. if ($info) {
  152. $num = $code['product_num'];
  153. $user[$order['uid']]['score'] += $num*20;
  154. }
  155. } elseif ($code['product_num'] > 0) {
  156. $num = $code['product_num'];
  157. $user[$order['uid']]['score'] += $num*20;
  158. }
  159. }
  160. }
  161. }
  162. }
  163. # 收回多余的兑换码
  164. public function deleteCode_api()
  165. {
  166. $order_id = Dever::input('order_id');
  167. if ($order_id) {
  168. $where['order_id'] = $order_id;
  169. }
  170. $where['product_id'] = 28;
  171. //$where['refund_status'] = 1;
  172. $where['type'] = 3;
  173. $data = Dever::db('journal/order')->state($where);
  174. $test = Dever::input('test');
  175. if ($test == 1) {
  176. print_r($data);
  177. }
  178. if ($data) {
  179. foreach ($data as $k => $v) {
  180. $this->deleteCodes($v);
  181. }
  182. }
  183. return 'ok';
  184. }
  185. private function deleteCodes($data)
  186. {
  187. # 获取该订单所能拥有的兑换码数
  188. $code_num = 1;
  189. $uid = $data['uid'];
  190. if ($data['buy_id'] > 0) {
  191. $buy = Dever::db('journal/buy_num')->one($data['buy_id']);
  192. $code_num = $buy['code'];
  193. }
  194. # 这个订单下所有码
  195. $code_where['order_id'] = $data['order_id'];
  196. $code_where['type'] = 4;
  197. $code = Dever::db('code/info')->getData($code_where);
  198. # code_num = 1
  199. # code = 21
  200. $test = Dever::input('test');
  201. if ($test == 1) {
  202. echo $code_num;
  203. print_r($code);die;
  204. }
  205. if ($code) {
  206. $i = 1;
  207. foreach ($code as $k => $v) {
  208. if ($i > $code_num) {
  209. Dever::db('code/info')->update(array('where_id' => $v['id'], 'state' => 2));
  210. }
  211. $i++;
  212. }
  213. }
  214. }
  215. public function refund_api()
  216. {
  217. $order_id = Dever::input('order_id');
  218. if ($order_id) {
  219. $where['order_id'] = $order_id;
  220. }
  221. $where['product_id'] = 28;
  222. $where['status'] = 5;
  223. $where['refund_status'] = 1;
  224. $data = Dever::db('journal/order')->state($where);
  225. $test = Dever::input('test');
  226. if ($test == 1) {
  227. print_r($data);die;
  228. }
  229. if ($data) {
  230. foreach ($data as $k => $v) {
  231. $this->refundScore($v);
  232. }
  233. }
  234. }
  235. private function refundScore($data)
  236. {
  237. $score = 0;
  238. if ($data['cash'] == 6) {
  239. $score = 20;
  240. }
  241. if ($data['cash'] == 60) {
  242. $score = 20*10;
  243. }
  244. if ($data['cash'] == 588) {
  245. $score = 20*100;
  246. }
  247. if ($data['cash'] == 1728) {
  248. $score = 20*300;
  249. }
  250. if ($data['cash'] == 3348) {
  251. $score = 20*600;
  252. }
  253. if ($score > 0) {
  254. if ($data['type'] == 1) {
  255. $where['uid'] = $data['uid'];
  256. $user = Dever::db('score/user')->one($where);
  257. if ($user) {
  258. $user_score = $user['score'] - $score;
  259. if ($user_score < 0) {
  260. $user_score = 0;
  261. }
  262. Dever::db('score/user')->update(array('where_id' => $user['id'], 'score' => $user_score));
  263. }
  264. $where['type'] = 4;
  265. $where['data_id'] = 28;
  266. $user = Dever::db('act/score')->one($where);
  267. if ($user) {
  268. $user_score = $user['score'] - $score;
  269. if ($user_score < 0) {
  270. $user_score = 0;
  271. }
  272. Dever::db('act/score')->update(array('where_id' => $user['id'], 'score' => $user_score));
  273. }
  274. } elseif ($data['type'] == 3) {
  275. $code_where['order_id'] = $data['order_id'];
  276. //$code_where['type'] = 1;
  277. $code_where['create_uid'] = $data['uid'];
  278. $code = Dever::db('code/info')->state($code_where);
  279. if ($code) {
  280. print_r($code);
  281. foreach ($code as $k => $v) {
  282. if ($v['type'] == 1) {
  283. Dever::db('code/info')->update(array('where_id' => $v['id'], 'type' => 4));
  284. } elseif ($v['type'] == 3) {
  285. $this->refundScoreCode($v);
  286. }
  287. }
  288. }
  289. }
  290. Dever::db('journal/order')->update(array('where_id' => $data['id'], 'refund_status' => 2));
  291. }
  292. print_r($data);
  293. print_r($score);
  294. echo "\r\n";
  295. return $score;
  296. }
  297. private function refundScoreCode($data)
  298. {
  299. if (isset($data['score']) && $data['score'] > 0) {
  300. $score = $data['score'];
  301. } elseif (isset($data['product_num']) && $data['product_num'] > 0) {
  302. $score = $data['product_num'] * 20;
  303. }
  304. if ($score > 0) {
  305. $where['uid'] = $data['uid'];
  306. $user = Dever::db('score/user')->one($where);
  307. if ($user) {
  308. $user_score = $user['score'] - $score;
  309. if ($user_score < 0) {
  310. $user_score = 0;
  311. }
  312. Dever::db('score/user')->update(array('where_id' => $user['id'], 'score' => $user_score));
  313. }
  314. $where['type'] = 4;
  315. $where['data_id'] = 28;
  316. $user = Dever::db('act/score')->one($where);
  317. if ($user) {
  318. $user_score = $user['score'] - $score;
  319. if ($user_score < 0) {
  320. $user_score = 0;
  321. }
  322. Dever::db('act/score')->update(array('where_id' => $user['id'], 'score' => $user_score));
  323. }
  324. }
  325. }
  326. public function pays_api()
  327. {
  328. $where['product_id'] = 28;
  329. $where['type'] = 3;
  330. $where['status'] = 1;
  331. $where['cron_status'] = 1;
  332. $data = Dever::db('journal/order')->state($where);
  333. if ($data) {
  334. foreach ($data as $k => $v) {
  335. $this->getpays($v['order_id']);
  336. Dever::db('journal/order')->update(array('where_id' => $v['id'], 'cron_status' => 2));
  337. }
  338. }
  339. }
  340. private function getpays($order_id)
  341. {
  342. $url = 'http://mapi.jstyle.cn/pay/pay/?api.search&order_id='.$order_id.'&account_id=1';
  343. Dever::curl($url);
  344. }
  345. public function unpay_api()
  346. {}
  347. public function activeEnd_api()
  348. {}
  349. #用户未支付电子刊情况下,未支付订单超过2小时后,发送一次唤回支付提醒
  350. public function unpay()
  351. {
  352. $time = 7200;
  353. $where['cdate'] = time() + $time;
  354. $where['note'] = 1;
  355. $where['type'] = '1,3';
  356. $data = Dever::db('journal/order')->getDataByTime($where);
  357. if ($data) {
  358. foreach ($data as $k => $v) {
  359. $user = Dever::db('passport/user')->one($v['uid']);
  360. if (!$user) {
  361. continue;
  362. }
  363. Dever::db('journal/order')->update(array('where_id' => $v['id'], 'note' => 2));
  364. $journal = Dever::db('journal/info')->one($v['product_id']);
  365. if (Dever::project('message')) {
  366. 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']));
  367. }
  368. # 发送短消息
  369. if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms')) {
  370. $send = array();
  371. $send['name'] = $journal['name'];
  372. //Dever::load('sms/api.send', 'note_live', $user['mobile'], $send);
  373. }
  374. # 发模板消息
  375. $wechat = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'type' => 1, 'system_id' => $v['cate_id']));
  376. if ($wechat && Dever::project('wechat_applet')) {
  377. $send['key'] = 'unbuy_journal';
  378. $send['project_id'] = $v['cate_id'];
  379. $send['touser'] = $wechat['openid'];
  380. $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($v['uid']) . ',' . '4,' . $v['product_id'];
  381. $send['data'] = array
  382. (
  383. 'keyword1' => array('value' => $journal['name']),
  384. 'keyword2' => array('value' => $v['cash']),
  385. 'keyword3' => array('value' => '您订阅的'.$journal['name'].'尚未支付!'),
  386. );
  387. $send['data'] = json_encode($send['data']);
  388. $send['form_id'] = Dever::load('act/lib/form')->get($v['uid'], 2, $v['cate_id']);
  389. if ($send['form_id']) {
  390. Dever::load('wechat_applet/msg.send', $send);
  391. }
  392. }
  393. }
  394. }
  395. return 'ok';
  396. }
  397. #电子杂志活动结束前5个时,向己有电子刊阅读权限的用户发送活动结束提醒
  398. public function activeEnd()
  399. {
  400. $time = 5*3600;
  401. $where['ends'] = time();
  402. $where['end'] = $where['ends'] + $time;
  403. $data = Dever::db('journal/active')->getDataByTime($where);
  404. //echo Dever::sql();die;
  405. $test = Dever::input('test');
  406. if ($test == 1) {
  407. print_r($data);die;
  408. }
  409. if ($data) {
  410. foreach ($data as $ks => $vs) {
  411. $journal = Dever::db('journal/info')->one($vs['info_id']);
  412. $subscribe = Dever::db('act/subscribe')->state(array('note' => 1, 'type' => 4, 'data_id' => $vs['info_id']));
  413. if ($subscribe) {
  414. foreach ($subscribe as $k => $v) {
  415. $user = Dever::db('passport/user')->one($v['uid']);
  416. if ($user) {
  417. Dever::db('act/subscribe')->update(array('where_id' => $v['id'], 'note' => 2));
  418. if (Dever::project('message')) {
  419. 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']));
  420. }
  421. # 发送短消息
  422. if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms')) {
  423. $send = array();
  424. $send['name'] = $journal['name'];
  425. //Dever::load('sms/api.send', 'note_live', $user['mobile'], $send);
  426. }
  427. # 发模板消息
  428. $wechat = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'type' => 1, 'system_id' => $journal['cate_id']));
  429. if ($wechat && Dever::project('wechat_applet')) {
  430. $send['key'] = 'service_end_note';
  431. $send['project_id'] = $journal['cate_id'];
  432. $send['touser'] = $wechat['openid'];
  433. $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($v['uid']) . ',' . '4,' . $journal['id'];
  434. $send['data'] = array
  435. (
  436. 'keyword1' => array('value' => $journal['name']),
  437. 'keyword2' => array('value' => date('Y-m-d H:i', $vs['end'])),
  438. 'keyword3' => array('value' => '您参与的'.$journal['name'].'积分排行榜活动还有5小时就要结束了,想要冲榜赢奖品的要抓紧机会呀~'),
  439. );
  440. $send['data'] = json_encode($send['data']);
  441. $send['form_id'] = Dever::load('act/lib/form')->get($v['uid'], 1, $journal['cate_id']);
  442. if (!$send['form_id']) {
  443. $send['form_id'] = Dever::load('act/lib/form')->get($v['uid'], 2, $journal['cate_id']);
  444. }
  445. if ($send['form_id']) {
  446. Dever::load('wechat_applet/msg.send', $send);
  447. }
  448. }
  449. }
  450. }
  451. }
  452. }
  453. }
  454. return 'ok';
  455. }
  456. }