Cron.php 18 KB

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