sms.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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('商家id:' . $sjId . ' -- ' . $merchant['name'] . '短信余额不足');
  41. return false;
  42. }
  43. $asset->remainSmsNum = --$remainSmsNum;
  44. $asset->save();
  45. self::freeSend($params, $msg, $merchant);
  46. return true;
  47. }
  48. //自由发短,无ip和数量限制 ssh 2019.12.24
  49. public static function freeSend($params, $msg, $merchant = null, $extend = '')
  50. {
  51. $ptStyle = dict::getDict('ptStyle', 'hd');
  52. if (isset(Yii::$app->params['ptStyle'])) {
  53. $ptStyle = Yii::$app->params['ptStyle'];
  54. }
  55. $open = WxOpenService::getById($ptStyle);
  56. $name = isset($open['name']) ? $open['name'] : '花卉宝';
  57. $sign = isset($merchant) == true ? "【{$merchant['name']}】" : "【{$name}】";
  58. $msg = $sign . $msg;
  59. $sms = new ChuanglanSmsApi();
  60. $result = $sms->sendVariableSMS($msg, $params, $extend);
  61. if (!is_null(json_decode($result))) {
  62. $output = json_decode($result, true);
  63. if (isset($output['code']) && $output['code'] == '0') {
  64. Yii::info("短信发送成功,参数:{$params} 返回:" . $result);
  65. return true;
  66. } else {
  67. Yii::info("短信发送失败,参数:{$params} 返回:" . $result);
  68. return false;
  69. }
  70. } else {
  71. Yii::info("短信发送失败,参数:{$params} 返回:" . $result);
  72. return false;
  73. }
  74. }
  75. //发短信,有ip和用户数量限制 ssh 2020.2.27
  76. public static function send($params, $msg, $merchant = null)
  77. {
  78. $ptStyle = Yii::$app->params['ptStyle'];
  79. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => $ptStyle]);
  80. if (empty($open)) {
  81. util::fail('没有找到平台');
  82. }
  83. //$name = isset($open['name']) ? $open['name'] : '花卉宝';
  84. $name = '厦门中花汇';
  85. $sign = isset($merchant) == true ? "【{$merchant['name']}】" : "【{$name}】";
  86. $msg = $sign . $msg;
  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 = new ChuanglanSmsApi();
  104. $sms->sendVariableSMS($msg, $params);
  105. }
  106. }