Cron.php 17 KB

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