Sms.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. $this->config['sign_name'] = $this->operators_name;
  27. $this->aliSms = new AliSms();
  28. }
  29. /**
  30. * 发送短信
  31. * @param $mobile 手机号码
  32. * @param $plate_code 短信模板CODE
  33. * @param $param 数据参数
  34. */
  35. public function send($mobile,$plate_code,$param){
  36. $response = $this->aliSms->sendSms($mobile, $plate_code, $param, $this->config);
  37. $response = json_decode(json_encode($response),true);
  38. $result['request'] = [
  39. 'mobile' => $mobile,
  40. 'plate_code' => $plate_code,
  41. 'param' => $param,
  42. 'operators_name' => $this->operators_name,
  43. ];
  44. $result['response'] = $response;
  45. return $result;
  46. }
  47. protected function sms_set()
  48. {
  49. //如果存在运营商sms设置则使用该设置
  50. if($this->operators_id){
  51. $operators_set = SmsSet::create()->get([
  52. 'uniacid' => $this->uniacid,
  53. 'operators_id' => $this->operators_id,
  54. 'dayu' => 1,
  55. ]);
  56. if($operators_set){
  57. return $operators_set;
  58. }
  59. }
  60. return $operators_set = SmsSet::create()->where('uniacid',$this->uniacid)->
  61. where('dayu',1)->where(' (operators_id is null or operators_id=0) ')->get()->toArray();
  62. }
  63. }