Everyday.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace addons\crontab\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Log;
  7. /**
  8. * 定时任务接口
  9. *
  10. * 以Crontab方式每分钟定时执行,且只可以Cli方式运行
  11. * @internal
  12. */
  13. class Everyday extends Controller
  14. {
  15. /**
  16. * 初始化方法,最前且始终执行
  17. */
  18. public function _initialize()
  19. {
  20. // 只可以以cli方式执行
  21. // if (!$this->request->isCli()) {
  22. // $this->error('Autotask script only work at client!');
  23. // }
  24. parent::_initialize();
  25. // 清除错误
  26. error_reporting(0);
  27. // 设置永不超时
  28. set_time_limit(0);
  29. }
  30. /**
  31. * 执行定时任务
  32. */
  33. public function index()
  34. {
  35. $this->coupon_status();
  36. }
  37. public function coupon_status(){
  38. // 结算
  39. Db::startTrans();
  40. try {
  41. $user_coupon = Db::name('user_coupon')->where('coupon_status',0)->where('expiration','<',time())->select();
  42. foreach ($user_coupon as $k=>$v){
  43. Db::name('user_coupon')->where('id',$v['id'])->setField('coupon_status',2);
  44. }
  45. Db::commit();
  46. } catch (\Exception $e) {
  47. Db::rollback();
  48. trace('[优惠券]:,' . date('Y-m-d H:i:s') . $e->getMessage(), 'debug');
  49. }
  50. }
  51. }