Sms.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php namespace Msg\Lib\Method;
  2. use Dever;
  3. class Sms
  4. {
  5. private $config;
  6. # 初始化
  7. public function init($template_id, $config_id, $account, $project = 'api')
  8. {
  9. if (empty($account['mobile'])) {
  10. Dever::error('手机号不存在');
  11. }
  12. $msg = 'msg';
  13. if ($project != 'api') {
  14. $msg = $project;
  15. }
  16. $this->config = Dever::db('account_sms', $msg)->find(['account_id' => $config_id, 'template_id' => $template_id]);
  17. if (!$this->config) {
  18. Dever::error('短信配置不存在');
  19. }
  20. $this->config['project'] = $project;
  21. $this->config['account'] = $account['mobile'];
  22. return $this->config['account'];
  23. }
  24. # 发送短信
  25. public function send($api, $content, $param)
  26. {
  27. if (!$this->config) {
  28. Dever::error('短信配置不存在');
  29. }
  30. $send['mobile'] = $this->config['account'];
  31. $send['TemplateCode'] = $this->config['code'];
  32. $send['TemplateParam'] = $param;
  33. $send['log'] = true;
  34. $result = Dever::load('account', 'api')->run($api, 'send_sms', $send, 1, 'run', $this->config['project']);
  35. return Dever::json_encode($result);
  36. }
  37. }