Cron.php 16 KB

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