Cron.php 15 KB

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