Cron.php 16 KB

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