Cron.php 16 KB

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