Sms.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Com;
  3. use EasySwoole\EasySwoole\Config;
  4. use Mrgoon\AliSms\AliSms;
  5. use App\Models\SmsSet;
  6. use App\Models\OperatorsUser;
  7. class Sms
  8. {
  9. private static $instance;
  10. protected $uniacid = 17; //默认固定
  11. protected $config = Array(); //阿里云接口调用设置
  12. protected $operators_id; //运营商ID
  13. protected $aliSms; //阿里短信接口对象
  14. protected $operators_name; //小程序名称,没有则默认为"前台优选"
  15. function __construct($operators_id)
  16. {
  17. //获取运营商短信设置
  18. $this->operators_id = $operators_id;
  19. $smsSet = $this->sms_set();
  20. // 配置信息
  21. $this->config['access_key'] = $smsSet['dayu_key'];
  22. $this->config['access_secret'] = $smsSet['dayu_secret'];
  23. //小程序名称
  24. $operators_res = OperatorsUser::create()->where('id',$operators_id)->get()->toArray();
  25. $this->operators_name = $operators_res['key']?$operators_res['operators_name']:'前台优选';
  26. $sign_arr = [
  27. '津辉优选',
  28. '小富源选',
  29. '国基乐购',
  30. '喜相悦',
  31. '方盒优选',
  32. ];
  33. $this->config['sign_name'] = in_array($this->operators_name, $sign_arr) ? $this->operators_name : '前台优选';
  34. $this->aliSms = new AliSms();
  35. }
  36. /**
  37. * 发送短信
  38. * @param $mobile 手机号码
  39. * @param $plate_code 短信模板CODE
  40. * @param $param 数据参数
  41. */
  42. public function send($mobile,$plate_code,$param){
  43. $response = $this->aliSms->sendSms($mobile, $plate_code, $param, $this->config);
  44. $response = json_decode(json_encode($response),true);
  45. $result['request'] = [
  46. 'mobile' => $mobile,
  47. 'plate_code' => $plate_code,
  48. 'param' => $param,
  49. 'operators_name' => $this->operators_name,
  50. ];
  51. $result['response'] = $response;
  52. return $result;
  53. }
  54. protected function sms_set()
  55. {
  56. //如果存在运营商sms设置则使用该设置
  57. if($this->operators_id){
  58. $operators_set = SmsSet::create()->get([
  59. 'uniacid' => $this->uniacid,
  60. 'operators_id' => $this->operators_id,
  61. 'dayu' => 1,
  62. ]);
  63. if($operators_set){
  64. return $operators_set;
  65. }
  66. }
  67. return $operators_set = SmsSet::create()->where('uniacid',$this->uniacid)->
  68. where('dayu',1)->where(' (operators_id is null or operators_id=0) ')->get()->toArray();
  69. }
  70. }