Cron.php 16 KB

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