Sms.php 1.3 KB

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