Cron.php 13 KB

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