sender.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/12/3
  6. * Time: 下午3:38
  7. */
  8. namespace async;
  9. use member_info;
  10. use Sms;
  11. use push_helper;
  12. use schema_helper;
  13. use push_sender;
  14. use Log;
  15. use Exception;
  16. class sender
  17. {
  18. private $mSmsFilters;
  19. private $mSmsParams;
  20. private $mSmsType;
  21. private $mSmsObject;
  22. public function __construct($sms_filters,$sends_params)
  23. {
  24. $this->mSmsFilters = $sms_filters;
  25. $this->mSmsFilters[] = new notapp_filter();
  26. $this->mSmsParams = $sends_params['sms_params'];
  27. $this->mSmsType = $sends_params['type'];
  28. $this->mSmsObject = new Sms();
  29. }
  30. public function run($minfos,$oper_result)
  31. {
  32. $params = params_table::instance()->find_params($this->mSmsType);
  33. foreach ($minfos as $minfo)
  34. {
  35. try
  36. {
  37. if($this->filtrate($minfo)) {
  38. Log::record("send_sms",Log::DEBUG);
  39. $this->send_sms($minfo);
  40. }
  41. else {
  42. Log::record("send_push",Log::DEBUG);
  43. $this->send_push($minfo,$params,$oper_result);
  44. }
  45. usleep(100000);
  46. }
  47. catch (Exception $ex)
  48. {
  49. Log::record(__METHOD__ . " {$ex->getTraceAsString()}",Log::ERR);
  50. }
  51. }
  52. }
  53. private function send_sms(member_info $minfo)
  54. {
  55. $mobile = $minfo->mobile();
  56. $status = $this->mSmsObject->send_oper($mobile,$this->mSmsType,$this->mSmsParams);
  57. return ($status == 0);
  58. }
  59. private function schema_url($oper_result)
  60. {
  61. if(empty($oper_result)) return '';
  62. if(array_key_exists('type_sn',$oper_result)) {
  63. $type_sn = $oper_result['type_sn'];
  64. return schema_helper::bonus_detail($type_sn);
  65. }
  66. else {
  67. return '';
  68. }
  69. }
  70. private function send_push($minfo,$params,$oper_result)
  71. {
  72. $title = $params['push_title'];
  73. $text = $params['push_text'];
  74. $url = $this->schema_url($oper_result);
  75. $user = $minfo->member_id();
  76. $push_param = push_helper::pop_message($user,$title,$text,$url);
  77. $push = new push_sender();
  78. $status = $push->send($push_param);
  79. }
  80. private function filtrate(member_info $minfo)
  81. {
  82. foreach ($this->mSmsFilters as $filter)
  83. {
  84. if($filter->filtrate($minfo) == false) {
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. public static function create($sms_filters,$sms_params)
  91. {
  92. return new sender($sms_filters,$sms_params);
  93. }
  94. }