sms.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace common\components;
  3. use biz\sj\services\MerchantAssetService;
  4. use bizHd\wx\services\WxOpenService;
  5. use Yii;
  6. /**
  7. * 短信接口
  8. *
  9. *
  10. * 平台系统发短信
  11. * 考虑短信总数是否足够
  12. * 平台客户发短信需要限制IP和数量(user_id)
  13. *
  14. * 商家发短信
  15. * 考虑总条数
  16. * 考虑商家短信是否充值
  17. *
  18. * 商家系统发短信息不需要限制IP和数量
  19. *
  20. * 商家的客户发短信需要限制IP和数量
  21. * 如果平台余额充足,分不限制和根据ip数量限制二种
  22. * 如果商家余额充足,分不限制和根据ip数量限制二种
  23. * 如果不足就记录一下,不要发送
  24. */
  25. class sms
  26. {
  27. //短信服务帐号
  28. const ACCOUNT = 'N2890342';
  29. //短信服务密码
  30. const PASSWORD = 'abc178c7';
  31. //每个用户每天可以申请的短信数量
  32. const LIMIT = 15;
  33. //商家发短信 ssh 2019.12.24
  34. public static function merchantSend($mobile, $msg, $merchant)
  35. {
  36. $merchantId = $merchant['id'];
  37. $asset = MerchantAssetService::getByMerchantId($merchantId, true);
  38. $remainSmsNum = $asset->remainSmsNum;
  39. if ($remainSmsNum <= 0) {
  40. Yii::info($merchant['name'] . '短信余额不足');
  41. return false;
  42. }
  43. $asset->remainSmsNum = --$remainSmsNum;
  44. $asset->save();
  45. self::freeSend($mobile, $msg, $merchant);
  46. }
  47. //自由发短,无ip和数量限制 ssh 2019.12.24
  48. public static function freeSend($mobile, $msg, $merchant = null)
  49. {
  50. $open = WxOpenService::getById(1);
  51. $name = isset($open['name']) ? $open['name'] : '花卉宝';
  52. $sign = isset($merchant) == true ? "【{$merchant['name']}】" : "【{$name}】";
  53. $msg .= $sign;
  54. $sms = new ChuanglanSmsApi();
  55. $result = $sms->sendVariableSMS($msg, $mobile);
  56. if (!is_null(json_decode($result))) {
  57. $output = json_decode($result, true);
  58. if (isset($output['code']) && $output['code'] == '0') {
  59. Yii::info("短信发送成功,手机号:{$mobile} 返回:" . $result);
  60. } else {
  61. Yii::info("短信发送失败,手机号:{$mobile} 返回:" . $result);
  62. }
  63. } else {
  64. Yii::info("短信发送失败,手机号:{$mobile} 返回:" . $result);
  65. }
  66. }
  67. //发短信,有ip和用户数量限制 ssh 2020.2.27
  68. public static function send($mobile, $msg, $merchant, $isOpen = 1)
  69. {
  70. $open = [];
  71. if ($isOpen == 1) {
  72. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => \biz\wx\classes\WxOpenClass::OPEN_STYLE_HD]);
  73. }
  74. if ($isOpen == 2) {
  75. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => \biz\wx\classes\WxOpenClass::OPEN_STYLE_GHS]);
  76. }
  77. if ($isOpen == 3) {
  78. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => \biz\wx\classes\WxOpenClass::OPEN_STYLE_MALL]);
  79. }
  80. if (empty($open)) {
  81. util::fail('没有找到平台');
  82. }
  83. $name = isset($open['name']) ? $open['name'] : '花卉宝';
  84. $sms = new chuanglanSMS(self::ACCOUNT, self::PASSWORD);
  85. $sign = isset($merchant) == true ? "【{$merchant['name']}】" : "【{$name}】";
  86. $msg .= $sign;
  87. //验证是否有权限发短信
  88. $uniqueId = isset($merchant['id']) ? $merchant['id'] : 1;
  89. $ip = httpUtil::ip();
  90. $statusKey = 'SMS_STATUS_' . $uniqueId . '_' . $ip;
  91. $hasSend = Yii::$app->redis->executeCommand('GET', [$statusKey]);
  92. if (!empty($hasSend)) {
  93. util::fail('请60秒后再操作');
  94. }
  95. $todayKey = 'SMS_NUM_' . date("Ymd") . '_' . $uniqueId . '_' . $ip;
  96. $num = Yii::$app->redis->executeCommand('GET', [$todayKey]);
  97. $num = is_numeric($num) ? $num : 0;
  98. if ($num > self::LIMIT) {
  99. util::fail('您今天申请的短信条数达到上限');
  100. }
  101. Yii::$app->redis->executeCommand('SETEX', [$statusKey, 60, 'hasSend']);
  102. Yii::$app->redis->executeCommand('SETEX', [$todayKey, 86400, ++$num]);
  103. $sms->send($mobile, $msg);
  104. }
  105. }