Cron.php 15 KB

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