Assms.php 4.6 KB

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