Cron.php 13 KB

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