Cron.php 16 KB

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