| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace addons\SmartForm\console\controllers;
- use addons\SmartForm\common\enums\SmartFormEnum;
- use addons\SmartForm\common\listeners\OrderCreateListener;
- use addons\SmartForm\common\logic\SmsMessage;
- use addons\SmartForm\common\models\AddonsSmartFormTemplate;
- use addons\SmartForm\common\models\AddonsSmartFormTemplateColumn;
- use addons\SmartForm\common\models\AddonsSmartFormTemplateUserFormInfo;
- use addons\SmartForm\common\models\AddonsStartFormSmsNotice;
- use addons\SmartForm\common\service\SmsNoticeService;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\sms\Sms;
- use yii\console\Controller;
- use Overtrue\EasySms\PhoneNumber;
- class SmsNoticeController extends Controller
- {
- /**
- * 智能表单发送短信 - 每分钟一次
- * @return void
- * @throws \Exception
- */
- public function actionSend()
- {
- // 取消已经过期的
- // AddonsStartFormSmsNotice::updateAll(['send_status' => 20],
- // ['and', ['send_status' => StatusEnum::DISABLED], ['<=', 'send_at', time()]]);
- $ids = AddonsStartFormSmsNotice::find()->where(['<=', 'send_at', time()])
- ->andWhere(['send_status' => StatusEnum::ENABLED]) // 付款后的才能进行发送
- // ->andWhere(['>', 'order_id', StatusEnum::DISABLED])
- ->select('id')->column();
- if (!empty($ids)) {
- AddonsStartFormSmsNotice::updateAll(['send_status' => 10], ['in', 'id', $ids]);
- // 执行
- foreach ($ids as $id) {
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['id' => $id],
- self::class, 'async_handle');
- }
- }
- }
- public function actionTest()
- {
- // $this->async_handle(['id' => 48]);
- // SmsNoticeService::setStatus(1505, [927], 20, 0);
- (new OrderCreateListener())->async_handle(['order_ids' => [8966]]);
- }
- public static function async_handle($params)
- {
- if (empty($params['id'])) return;
- $notice = AddonsStartFormSmsNotice::getOne([
- ['id' => $params['id']],
- ['send_status' => 10] // 智能是发送中的数据
- ]);
- if (empty($notice) || empty($notice->content) || empty($notice->template_id)) return;
- $user = $notice->user;
- if (empty($user)) return;
- try {
- // 执行短信发送
- $area_code = '86';// 初始化区号
- $gateways = 'domestic';// 默认国内短信
- // 检测是登录并且区号不为空则使用国际短信
- if (!empty($user->area_code)) {
- $gateways = 'international';
- $area_code = $user->area_code;
- }
- $sms = new Sms($notice->mall_id, $gateways);
- // 手机号码,发送时间,发送内容,变量,
- // 需要组装数据
- // 组装数据
- // 表单数据
- // 填写数据
- // 用户数据
- $collect_vars = [];
- $uArr = $user->toArray();
- foreach ($uArr as $key => $value) {
- if (in_array($key, ['created_at', 'updated_at', 'birthday', 'junior_at', 'last_login_at', 'upgrade_level_at',
- 'inviter_at']) && !empty($value)) {
- $value = date("Y-m-d H:i:s");
- }
- $collect_vars["user.{$key}"] = $value;
- }
- $column = AddonsSmartFormTemplateColumn::getOne([
- ['id' => $notice->column_id]
- ], true);
- foreach ($column as $key => $value) {
- if (in_array($key, ['created_at', 'updated_at']) && !empty($value)) {
- $value = date("Y-m-d H:i:s");
- }
- $collect_vars["info.{$key}"] = $value;
- }
- $collect_vars["info.created_at"] = date('Y-m-d H:i:s', $notice->created_at); // 提交时间
- $template = AddonsSmartFormTemplate::getOne([
- ['id' => $column['smart_form_template_id']]
- ], true);
- foreach ($template as $key => $value) {
- if (in_array($key, ['created_at', 'updated_at']) && !empty($value)) {
- $value = date("Y-m-d H:i:s");
- }
- $collect_vars["template.{$key}"] = $value;
- }
- // 填写内容
- $user_write_data = AddonsSmartFormTemplateUserFormInfo::getOne([
- ['smart_form_components_id' => $column['id']],
- ['user_id' => $uArr['id']]
- ], true);
- foreach ($user_write_data as $key => $value) {
- if (in_array($key, ['created_at', 'updated_at']) && !empty($value)) {
- $value = date("Y-m-d H:i:s");
- }
- $collect_vars["write.{$key}"] = $value;
- }
- $var_keys = array_column($notice->send_var, 'name', 'key');
- $message_data = [];
- $content = $notice->content;
- foreach (SmartFormEnum::SMS_KEYS as $key => $value) {
- if (!empty($var_keys[$key])) {
- $message_data[substr($var_keys[$key], 2, -1)] = $collect_vars[$key] ?? '';
- $content = str_replace($var_keys[$key], $collect_vars[$key] ?? '', $content);
- }
- }
- $sms_message_attr = [
- 'content' => $content, // 需要自己格式化内容?
- 'template' => $notice->template_id,
- 'data' => $message_data
- ];
- $message = new SmsMessage($sms_message_attr);
- $mobile = new PhoneNumber($user->mobile, $area_code);
- $res = $sms->easySms->send($mobile, $message);
- $notice->send_status = 100; // 发送成功
- } catch (\Exception $e) {
- $notice->send_status = 50; // 发送失败
- $res['err'] = [
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => $e->getFile(),
- 'trace' => $e->getTrace(),
- ];
- }
- if (!empty($res)) $notice->send_res = $res;
- if (!$notice->save()) {
- \Yii::error($notice->getErrorMessage(), __METHOD__);
- }
- }
- }
|