Assms.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Sms as Smslib;
  5. use app\common\model\User;
  6. use think\Db;
  7. use think\Exception;
  8. use think\exception\PDOException;
  9. use think\helper\hash\Md5;
  10. use think\Hook;
  11. use think\Queue;
  12. /**
  13. * 手机短信接口
  14. */
  15. class Assms extends Api
  16. {
  17. protected $noNeedLogin = '*';
  18. protected $noNeedRight = '*';
  19. public function addSmsLog(){
  20. $post=$this->request->post();
  21. $sign =$post['sign'];
  22. if($sign!=Md5('longyi_as')){
  23. $this->error('sign error');
  24. }
  25. $this->addSmsQuote($post);
  26. $this->success(__('success'));
  27. }
  28. public function addSmsQuote($quote)
  29. {
  30. $quote_id= $quote['quote_id'];
  31. $name = $quote['name'];
  32. $mobile =$quote['mobile'];
  33. $email =$quote['email'];
  34. $countryCode = $quote['countryCode'];
  35. $countryMobile =911;
  36. if(!$mobile){
  37. return ;
  38. }
  39. if($countryCode){
  40. $countryMobile = getCountryMobile($countryCode);
  41. }
  42. if($mobile){
  43. $datas =checkMobile($countryMobile,$mobile,$countryCode);
  44. $mobiles=$datas['mobile'];
  45. //删除手机号、购物车ID相同待发送短信信息
  46. $where['mobile']=$mobiles;
  47. $where['status']=1;
  48. Db::name('sms_log')->where($where)->delete();
  49. unset($where);
  50. $where['status']=1;
  51. $template= Db::name('sms_template')->where($where)->select();
  52. $data=array();
  53. //循环添加发送短信
  54. foreach ($template as $k=> $v){
  55. $template_id = $v['template_id'];
  56. $data[$k]['email'] =$email;
  57. $data[$k]['ymobile'] =$mobile;
  58. $data[$k]['quote_id']=$quote_id;
  59. $data[$k]['template_id']=$template_id;
  60. $data[$k]['status']=$datas['status'];
  61. $data[$k]['mobile']=$datas['mobile'];
  62. $data[$k]['addtime']=time();
  63. $data[$k]['uptime']=time();
  64. $data[$k]['sendtime']=time()+$v['sendtime'];
  65. $data[$k]['remarks']=$countryMobile;
  66. $data[$k]['template_body']= $this->getMssage($v['template_body'],$name);
  67. $data[$k]['template_name']= $v['name'];
  68. }
  69. try {
  70. Db::name('sms_log')->insertAll($data);
  71. }catch (\PDOException $e){
  72. $e->getMessage();
  73. } catch (\Exception $e) {
  74. var_dump($e->getMessage());
  75. }
  76. }
  77. }
  78. public function getMssage($template_body,$name){
  79. $search = '{#NAME}';
  80. $message=str_replace($search,$name,$template_body);
  81. return $message;
  82. }
  83. public function addLogQueue(){
  84. $model = new \app\admin\model\sms\Log;
  85. $where['sendtime']=array('lt',time());
  86. $where['status']=1;
  87. $one_push = Db::name('sms_log')->where($where)->order('log_id')->limit(10)->select();
  88. if (empty($one_push)) {
  89. echo '没有查到待发送短信';
  90. exit;
  91. }
  92. // 1.当前任务将由哪个类来负责处理。
  93. // 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
  94. $jobHandlerClassName ='app\job\Sendsms';
  95. // 2.当前任务归属的队列名称,如果为新队列,会自动创建
  96. $jobQueueName = "createSmsJob";
  97. $new=array();
  98. foreach ($one_push as $k=>$v){
  99. $new[$k]['log_id']=$v['log_id'];
  100. $new[$k]['status']=2;
  101. $jobData['log_id']=$v['log_id'];
  102. $jobData['mobile']=$v['mobile'];
  103. $jobData['template_body']=$v['template_body'];
  104. $isPushed = Queue::push($jobHandlerClassName , $jobData , $jobQueueName);
  105. // database 驱动时,返回值为 1|false ; redis 驱动时,返回值为 随机字符串|false
  106. if ($isPushed !== false) {
  107. echo $v['mobile']." 队列加入成功";
  108. } else {
  109. file_get_contents('test.txt',$v['mobile'],FILE_APPEND);
  110. echo "队列加入失败";
  111. }
  112. }
  113. $model->saveAll($new);
  114. }
  115. }