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.

67 lines
2.2 KiB

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