123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\library\Sms as Smslib;
- use app\common\model\User;
- use think\Db;
- use think\Exception;
- use think\exception\PDOException;
- use think\helper\hash\Md5;
- use think\Hook;
- use think\Queue;
- /**
- * 手机短信接口
- */
- class Assms extends Api
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- public function addSmsLog(){
- $post=$this->request->post();
- $sign =$post['sign'];
- if($sign!=Md5('longyi_as')){
- $this->error('sign error');
- }
- $this->addSmsQuote($post);
- $this->success(__('success'));
- }
- public function cancelSmsLog(){
- $post=$this->request->post();
- $sign =$post['sign'];
- if($sign!=Md5('longyi_as')){
- $this->error('sign error');
- }
- $quote_id= $post['quote_id'];
- $mobile =$post['mobile'];
- $where['quote_id']=$quote_id;
- $where['ymobile']=$mobile;
- $where['status']=1;
- Db::name('sms_log')->where($where)->delete();
- $this->success(__('success'));
- }
- public function addSmsQuote($quote)
- {
- $quote_id= $quote['quote_id'];
- $name = $quote['name'];
- $mobile =$quote['mobile'];
- $email =$quote['email'];
- $countryCode = $quote['countryCode'];
- $countryMobile =911;
- if(!$mobile){
- return ;
- }
- if($countryCode){
- $countryMobile = getCountryMobile($countryCode);
- }
- if($mobile){
- $datas =checkMobile($countryMobile,$mobile,$countryCode);
- $mobiles=$datas['mobile'];
- //删除手机号、购物车ID相同待发送短信信息
- $where['mobile']=$mobiles;
- $where['status']=1;
- Db::name('sms_log')->where($where)->delete();
- unset($where);
- $where['status']=1;
- $template= Db::name('sms_template')->where($where)->select();
- $data=array();
- //循环添加发送短信
- foreach ($template as $k=> $v){
- $template_id = $v['template_id'];
- $data[$k]['email'] =$email;
- $data[$k]['ymobile'] =$mobile;
- $data[$k]['quote_id']=$quote_id;
- $data[$k]['template_id']=$template_id;
- $data[$k]['status']=$datas['status'];
- $data[$k]['mobile']=$datas['mobile'];
- $data[$k]['addtime']=time();
- $data[$k]['uptime']=time();
- $data[$k]['sendtime']=time()+$v['sendtime'];
- $data[$k]['remarks']=$countryMobile;
- $data[$k]['template_body']= $this->getMssage($v['template_body'],$name);
- $data[$k]['template_name']= $v['name'];
- }
- try {
- Db::name('sms_log')->insertAll($data);
- }catch (\PDOException $e){
- $e->getMessage();
- } catch (\Exception $e) {
- var_dump($e->getMessage());
- }
- }
- }
- public function getMssage($template_body,$name){
- $search = '{#NAME}';
- $message=str_replace($search,$name,$template_body);
- return $message;
- }
- public function addLogQueue(){
- $model = new \app\admin\model\sms\Log;
- $where['sendtime']=array('lt',time());
- $where['status']=1;
- $one_push = Db::name('sms_log')->where($where)->order('log_id')->limit(10)->select();
- if (empty($one_push)) {
- echo '没有查到待发送短信';
- exit;
- }
- // 1.当前任务将由哪个类来负责处理。
- // 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
- $jobHandlerClassName ='app\job\Sendsms';
- // 2.当前任务归属的队列名称,如果为新队列,会自动创建
- $jobQueueName = "createSmsJob";
- $new=array();
- foreach ($one_push as $k=>$v){
- $new[$k]['log_id']=$v['log_id'];
- $new[$k]['status']=2;
- $jobData['log_id']=$v['log_id'];
- $jobData['mobile']=$v['mobile'];
- $jobData['template_body']=$v['template_body'];
- $isPushed = Queue::push($jobHandlerClassName , $jobData , $jobQueueName);
- // database 驱动时,返回值为 1|false ; redis 驱动时,返回值为 随机字符串|false
- if ($isPushed !== false) {
- echo $v['mobile']." 队列加入成功";
- } else {
- file_get_contents('test.txt',$v['mobile'],FILE_APPEND);
- echo "队列加入失败";
- }
- }
- $model->saveAll($new);
- }
- }
|