Cron.php 13 KB

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