Timedtask.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use think\Db;
  7. /**
  8. * @title 定时任务
  9. * Class Timedtask
  10. * @controller Timedtask
  11. * @group base
  12. */
  13. class Timedtask
  14. {
  15. /**
  16. * @title 定时取消订单
  17. * @desc 定时取消订单
  18. * @author qc
  19. * @url /api/Timedtask/cancelOrder
  20. * @method GET
  21. */
  22. public function cancelOrder()
  23. {
  24. $sel_time = time() - 900;// 十五分钟内未支付的自动取消
  25. $order_list = Db::table('ticket_order')
  26. ->field('id,s_id,total_num')
  27. ->where(['pay_state'=>0,'cancel_state'=>0])
  28. ->where('create_tamp','<',$sel_time)
  29. ->select();
  30. foreach ($order_list as $order_info)
  31. {
  32. Db::startTrans();
  33. try {
  34. Db::table('ticket_order')
  35. ->where(['id'=>$order_info['id']])
  36. ->update(['cancel_state'=>1,'cancel_at'=>date('Y-m-d H:i:s'),'cancel_desc'=>'支付超时!自动取消']);
  37. Db::table('film_info')->where('id', $order_info['s_id'])
  38. ->setInc('surplus_num', $order_info['total_num']);//增加场次余票
  39. Db::commit();
  40. }catch (\Exception $e){
  41. Db::rollback();
  42. }
  43. }
  44. }
  45. /**
  46. * @title 电影开场前(30分钟)定时发送消息提醒
  47. * @desc 电影开场前(30分钟)定时发送消息提醒
  48. * @author qc
  49. * @url /api/Timedtask/sendMessage
  50. * @method GET
  51. */
  52. public function sendMessage()
  53. {
  54. $order_list = Db::table('ticket_order')
  55. ->alias('o')
  56. ->field('o.id,o.s_id,o.tel,l.name,f.show_time')
  57. ->join('film_info f','o.s_id = f.id','LEFT')
  58. ->join('film_list l','o.f_id = l.id','LEFT')
  59. ->where(['o.pay_state'=>1,'o.is_remind'=>1,'o.has_remind'=>0,])
  60. ->where('show_time','< time',date("Y-m-d H:i:s",time() + 1800))
  61. ->select();
  62. AlibabaCloud::accessKeyClient('LTAI5tKATi9QpdtaBpEHeCP4', 'F8B77fPPitlfRScTuPmQxfphLRpt6a')
  63. ->regionId('cn-hangzhou')->asDefaultClient();
  64. foreach ($order_list as $order_info)
  65. {
  66. if(!$order_info['tel'] || strlen($order_info['tel']) != 11) continue;
  67. $result = AlibabaCloud::rpc()->product('Dysmsapi')->version('2017-05-25')
  68. ->action('SendSms')->method('POST')->host('dysmsapi.aliyuncs.com')
  69. ->options([
  70. 'query' => [
  71. 'RegionId' => "cn-hangzhou",
  72. 'PhoneNumbers' => $order_info['tel'],
  73. 'SignName' => "火星帝界",
  74. 'TemplateCode' => "SMS_223197414",
  75. 'TemplateParam' => json_encode(['film_name'=>$order_info['name'],'show_time'=>$order_info['show_time']]),
  76. ],
  77. ])->request();
  78. $result = $result->toArray();
  79. if($result['Code'] == "OK") {
  80. Db::table('ticket_order')->where(['id'=>$order_info['id']])->update(['has_remind'=>1]);
  81. }
  82. }
  83. }
  84. }