SmsNoticeController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace addons\SmartForm\console\controllers;
  3. use addons\SmartForm\common\enums\SmartFormEnum;
  4. use addons\SmartForm\common\listeners\OrderCreateListener;
  5. use addons\SmartForm\common\logic\SmsMessage;
  6. use addons\SmartForm\common\models\AddonsSmartFormTemplate;
  7. use addons\SmartForm\common\models\AddonsSmartFormTemplateColumn;
  8. use addons\SmartForm\common\models\AddonsSmartFormTemplateUserFormInfo;
  9. use addons\SmartForm\common\models\AddonsStartFormSmsNotice;
  10. use addons\SmartForm\common\service\SmsNoticeService;
  11. use common\enums\RabbitMqEnum;
  12. use common\enums\StatusEnum;
  13. use common\helpers\sms\Sms;
  14. use yii\console\Controller;
  15. use Overtrue\EasySms\PhoneNumber;
  16. class SmsNoticeController extends Controller
  17. {
  18. /**
  19. * 智能表单发送短信 - 每分钟一次
  20. * @return void
  21. * @throws \Exception
  22. */
  23. public function actionSend()
  24. {
  25. // 取消已经过期的
  26. // AddonsStartFormSmsNotice::updateAll(['send_status' => 20],
  27. // ['and', ['send_status' => StatusEnum::DISABLED], ['<=', 'send_at', time()]]);
  28. $ids = AddonsStartFormSmsNotice::find()->where(['<=', 'send_at', time()])
  29. ->andWhere(['send_status' => StatusEnum::ENABLED]) // 付款后的才能进行发送
  30. // ->andWhere(['>', 'order_id', StatusEnum::DISABLED])
  31. ->select('id')->column();
  32. if (!empty($ids)) {
  33. AddonsStartFormSmsNotice::updateAll(['send_status' => 10], ['in', 'id', $ids]);
  34. // 执行
  35. foreach ($ids as $id) {
  36. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['id' => $id],
  37. self::class, 'async_handle');
  38. }
  39. }
  40. }
  41. public function actionTest()
  42. {
  43. // $this->async_handle(['id' => 48]);
  44. // SmsNoticeService::setStatus(1505, [927], 20, 0);
  45. (new OrderCreateListener())->async_handle(['order_ids' => [8966]]);
  46. }
  47. public static function async_handle($params)
  48. {
  49. if (empty($params['id'])) return;
  50. $notice = AddonsStartFormSmsNotice::getOne([
  51. ['id' => $params['id']],
  52. ['send_status' => 10] // 智能是发送中的数据
  53. ]);
  54. if (empty($notice) || empty($notice->content) || empty($notice->template_id)) return;
  55. $user = $notice->user;
  56. if (empty($user)) return;
  57. try {
  58. // 执行短信发送
  59. $area_code = '86';// 初始化区号
  60. $gateways = 'domestic';// 默认国内短信
  61. // 检测是登录并且区号不为空则使用国际短信
  62. if (!empty($user->area_code)) {
  63. $gateways = 'international';
  64. $area_code = $user->area_code;
  65. }
  66. $sms = new Sms($notice->mall_id, $gateways);
  67. // 手机号码,发送时间,发送内容,变量,
  68. // 需要组装数据
  69. // 组装数据
  70. // 表单数据
  71. // 填写数据
  72. // 用户数据
  73. $collect_vars = [];
  74. $uArr = $user->toArray();
  75. foreach ($uArr as $key => $value) {
  76. if (in_array($key, ['created_at', 'updated_at', 'birthday', 'junior_at', 'last_login_at', 'upgrade_level_at',
  77. 'inviter_at']) && !empty($value)) {
  78. $value = date("Y-m-d H:i:s");
  79. }
  80. $collect_vars["user.{$key}"] = $value;
  81. }
  82. $column = AddonsSmartFormTemplateColumn::getOne([
  83. ['id' => $notice->column_id]
  84. ], true);
  85. foreach ($column as $key => $value) {
  86. if (in_array($key, ['created_at', 'updated_at']) && !empty($value)) {
  87. $value = date("Y-m-d H:i:s");
  88. }
  89. $collect_vars["info.{$key}"] = $value;
  90. }
  91. $collect_vars["info.created_at"] = date('Y-m-d H:i:s', $notice->created_at); // 提交时间
  92. $template = AddonsSmartFormTemplate::getOne([
  93. ['id' => $column['smart_form_template_id']]
  94. ], true);
  95. foreach ($template as $key => $value) {
  96. if (in_array($key, ['created_at', 'updated_at']) && !empty($value)) {
  97. $value = date("Y-m-d H:i:s");
  98. }
  99. $collect_vars["template.{$key}"] = $value;
  100. }
  101. // 填写内容
  102. $user_write_data = AddonsSmartFormTemplateUserFormInfo::getOne([
  103. ['smart_form_components_id' => $column['id']],
  104. ['user_id' => $uArr['id']]
  105. ], true);
  106. foreach ($user_write_data as $key => $value) {
  107. if (in_array($key, ['created_at', 'updated_at']) && !empty($value)) {
  108. $value = date("Y-m-d H:i:s");
  109. }
  110. $collect_vars["write.{$key}"] = $value;
  111. }
  112. $var_keys = array_column($notice->send_var, 'name', 'key');
  113. $message_data = [];
  114. $content = $notice->content;
  115. foreach (SmartFormEnum::SMS_KEYS as $key => $value) {
  116. if (!empty($var_keys[$key])) {
  117. $message_data[substr($var_keys[$key], 2, -1)] = $collect_vars[$key] ?? '';
  118. $content = str_replace($var_keys[$key], $collect_vars[$key] ?? '', $content);
  119. }
  120. }
  121. $sms_message_attr = [
  122. 'content' => $content, // 需要自己格式化内容?
  123. 'template' => $notice->template_id,
  124. 'data' => $message_data
  125. ];
  126. $message = new SmsMessage($sms_message_attr);
  127. $mobile = new PhoneNumber($user->mobile, $area_code);
  128. $res = $sms->easySms->send($mobile, $message);
  129. $notice->send_status = 100; // 发送成功
  130. } catch (\Exception $e) {
  131. $notice->send_status = 50; // 发送失败
  132. $res['err'] = [
  133. 'msg' => $e->getMessage(),
  134. 'line' => $e->getLine(),
  135. 'file' => $e->getFile(),
  136. 'trace' => $e->getTrace(),
  137. ];
  138. }
  139. if (!empty($res)) $notice->send_res = $res;
  140. if (!$notice->save()) {
  141. \Yii::error($notice->getErrorMessage(), __METHOD__);
  142. }
  143. }
  144. }