sms.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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($params, $msg, $merchant)
  35. {
  36. $sjId = $merchant['id'];
  37. $asset = MerchantAssetService::getBySjId($sjId, 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($params, $msg, $merchant);
  46. }
  47. //自由发短,无ip和数量限制 ssh 2019.12.24
  48. public static function freeSend($params, $msg, $merchant = null)
  49. {
  50. $ptStyle = dict::getDict('ptStyle', 'hd');
  51. if (isset(Yii::$app->params['ptStyle'])) {
  52. $ptStyle = Yii::$app->params['ptStyle'];
  53. }
  54. $open = WxOpenService::getById($ptStyle);
  55. $name = isset($open['name']) ? $open['name'] : '花卉宝';
  56. $sign = isset($merchant) == true ? "【{$merchant['name']}】" : "【{$name}】";
  57. $msg = $sign . $msg;
  58. $sms = new ChuanglanSmsApi();
  59. $result = $sms->sendVariableSMS($msg, $params);
  60. if (!is_null(json_decode($result))) {
  61. $output = json_decode($result, true);
  62. if (isset($output['code']) && $output['code'] == '0') {
  63. Yii::info("短信发送成功,参数:{$params} 返回:" . $result);
  64. } else {
  65. Yii::info("短信发送失败,参数:{$params} 返回:" . $result);
  66. }
  67. } else {
  68. Yii::info("短信发送失败,参数:{$params} 返回:" . $result);
  69. }
  70. }
  71. //发短信,有ip和用户数量限制 ssh 2020.2.27
  72. public static function send($params, $msg, $merchant = null)
  73. {
  74. $ptStyle = Yii::$app->params['ptStyle'];
  75. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => $ptStyle]);
  76. if (empty($open)) {
  77. util::fail('没有找到平台');
  78. }
  79. $name = isset($open['name']) ? $open['name'] : '花卉宝';
  80. $sign = isset($merchant) == true ? "【{$merchant['name']}】" : "【{$name}】";
  81. $msg = $sign . $msg;
  82. //验证是否有权限发短信
  83. $uniqueId = isset($merchant['id']) ? $merchant['id'] : 1;
  84. $ip = httpUtil::ip();
  85. $statusKey = 'SMS_STATUS_' . $uniqueId . '_' . $ip;
  86. $hasSend = Yii::$app->redis->executeCommand('GET', [$statusKey]);
  87. if (!empty($hasSend)) {
  88. util::fail('请60秒后再操作');
  89. }
  90. $todayKey = 'SMS_NUM_' . date("Ymd") . '_' . $uniqueId . '_' . $ip;
  91. $num = Yii::$app->redis->executeCommand('GET', [$todayKey]);
  92. $num = is_numeric($num) ? $num : 0;
  93. if ($num > self::LIMIT) {
  94. util::fail('您今天申请的短信条数达到上限');
  95. }
  96. Yii::$app->redis->executeCommand('SETEX', [$statusKey, 60, 'hasSend']);
  97. Yii::$app->redis->executeCommand('SETEX', [$todayKey, 86400, ++$num]);
  98. $sms = new ChuanglanSmsApi();
  99. $sms->sendVariableSMS($msg, $params);
  100. }
  101. }