Cron.php 14 KB

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