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