Cron.php 17 KB

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