You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.3 KiB

  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use App\Commons\Log;
  7. use App\Model\v3\CsInfo;
  8. use App\Model\v3\Market;
  9. use App\Service\v3\Interfaces\SmsServiceInterface;
  10. use Hyperf\Di\Annotation\Inject;
  11. class SmsAliService implements SmsServiceInterface
  12. {
  13. const TEMPLATE_COMMUNITY_FINANCIAL = 'SMS_200690862';
  14. /**
  15. * @Inject
  16. * @var Log
  17. */
  18. protected $log;
  19. public function send($phone, $template, $templateParams, $signName='懒族生活')
  20. {
  21. $alisms = config('alisms');
  22. AlibabaCloud::accessKeyClient($alisms['app_key'], $alisms['app_secret'])
  23. ->regionId($alisms['regionid'])
  24. ->asDefaultClient();
  25. try {
  26. $result = AlibabaCloud::rpc()
  27. ->product($alisms['product'])
  28. // ->scheme('https') // https | http
  29. ->version('2017-05-25')
  30. ->action('SendSms')
  31. ->method('POST')
  32. ->host($alisms['host'])
  33. ->options([
  34. 'query' => [
  35. 'RegionId' => $alisms['regionid'],
  36. 'PhoneNumbers' => $phone,
  37. 'SignName' => $signName,
  38. 'TemplateCode' => $template,
  39. 'TemplateParam' => $templateParams,
  40. ],
  41. ])
  42. ->request();
  43. return $result->toArray();
  44. } catch (ClientException $e) {
  45. $this->log->event('alisms', ['alisms_error_ClientException' => $e->getErrorMessage()]);
  46. return false;
  47. } catch (ServerException $e) {
  48. $this->log->event('alisms', ['alisms_error_ServerException' => $e->getErrorMessage()]);
  49. return false;
  50. }
  51. }
  52. public function sendForCommunityFinancial($userId, $money)
  53. {
  54. $csInfo = CsInfo::query()->where(['admin_user_id' => $userId])->first();
  55. $market = Market::query()->where(['id' => $csInfo->market_id])->first();
  56. $params = ['user_name' => $csInfo->name, 'market_name' => $market->name, 'money' => $money];
  57. return $this->send($csInfo->phone, self::TEMPLATE_COMMUNITY_FINANCIAL, json_encode($params));
  58. }
  59. }