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.

106 lines
3.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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\Constants\v3\ErrorCode;
  7. use App\Exception\ErrorCodeException;
  8. use App\Model\v3\CsInfo;
  9. use App\Model\v3\Market;
  10. use Exception;
  11. use App\Commons\Log;
  12. use App\Constants\v3\SmsTemplateCode;
  13. use Hyperf\Di\Annotation\Inject;
  14. use App\Service\v3\Interfaces\SmsSendServiceInterface;
  15. class SmsAliSendService implements SmsSendServiceInterface
  16. {
  17. /**
  18. * @Inject
  19. * @var Log
  20. */
  21. protected $log;
  22. public function do($tel, $template, $templateParams, $signName = '')
  23. {
  24. try {
  25. $aliSms = config('alisms');
  26. AlibabaCloud::accessKeyClient($aliSms['app_key'], $aliSms['app_secret'])
  27. ->regionId($aliSms['regionid'])
  28. ->asDefaultClient();
  29. $result = AlibabaCloud::rpc()
  30. ->product($aliSms['product'])
  31. ->version('2017-05-25')
  32. ->action('SendSms')
  33. ->method('POST')
  34. ->host($aliSms['host'])
  35. ->options([
  36. 'query' => [
  37. 'RegionId' => $aliSms['regionid'],
  38. 'PhoneNumbers' => $tel,
  39. 'SignName' => $signName ?: $aliSms['sign_name'],
  40. 'TemplateCode' => $template,
  41. 'TemplateParam' => $templateParams,
  42. ],
  43. ])
  44. ->request();
  45. return $result->toArray();
  46. } catch (ClientException $e) {
  47. $this->log->event('alisms', ['alisms_error_ClientException' => $e->getErrorMessage(), 'tel' => $tel, 'template' => json_encode($templateParams)]);
  48. throw new ErrorCodeException(ErrorCode::SMS_SEND_FAILURE);
  49. } catch (ServerException $e) {
  50. $this->log->event('alisms', ['alisms_error_ServerException' => $e->getErrorMessage(), 'tel' => $tel, 'template' => json_encode($templateParams)]);
  51. throw new ErrorCodeException(ErrorCode::SMS_SEND_FAILURE);
  52. } catch (Exception $e) {
  53. $this->log->event('alisms', ['alisms_error_Exception' => $e->getErrorMessage(), 'tel' => $tel, 'template' => json_encode($templateParams)]);
  54. throw new ErrorCodeException(ErrorCode::SMS_SEND_FAILURE);
  55. }
  56. }
  57. public function check()
  58. {
  59. // TODO: Implement check() method.
  60. }
  61. public function undo()
  62. {
  63. // TODO: Implement undo() method.
  64. }
  65. public function doVerifyCode($tel, $code)
  66. {
  67. $params = ['code' => $code];
  68. return $this->do($tel, SmsTemplateCode::ALI_VERIFY_CODE, json_encode($params));
  69. }
  70. public function checkVerifyCode($tel, $code)
  71. {
  72. // TODO: Implement checkVerifyCode() method.
  73. }
  74. public function undoVerifyCode($tel, $code)
  75. {
  76. // TODO: Implement removeVerifyCode() method.
  77. }
  78. public function doCommunityFinancial($userId, $money)
  79. {
  80. $csInfo = CsInfo::query()->where(['admin_user_id' => $userId])->first();
  81. $market = Market::query()->where(['id' => $csInfo->market_id])->first();
  82. $params = ['user_name' => $csInfo->name, 'market_name' => $market->name, 'money' => $money];
  83. return $this->do($csInfo->phone, SmsTemplateCode::ALI_COMMUNITY_FINANCIAL, json_encode($params));
  84. }
  85. public function doWithdrawFail($storeName, $money, $errorCode, $errorCodeDes)
  86. {
  87. // 微信接口提示商户名称:${store_name},提现金额:${money}元,失败原因:${note}。
  88. $params = ['store_name' => $storeName, 'money' => floatval(bcdiv($money, '100', 2)), 'note' => '(公司账户)'.$errorCodeDes];
  89. return $this->do('18077798210', SmsTemplateCode::ALI_PAY_FOR_WITHDRAW_FAIL, json_encode($params));
  90. }
  91. }