<?php

namespace app\common\service;

use app\common\model\SettingModel;
use app\common\model\SmsTemplateModel;
use think\facade\Log;

class SmsService
{
    public function send($mobile, $code, $content_param = [])
    {
        $message = SmsTemplateModel::where('code', $code)->find();
        if (empty($message)) {
            Log::error('短信模板不存在：' . $code);
            return ['code' => 1, 'msg' => '模板不存在'];
        }

        $msg = $message['content'];
        if (!empty($content_param)) {
            foreach ($content_param as $k => $v) {
                $msg = str_replace('{$' . ($k + 1) . '}', $v, $msg);
            }
        }

        $sms_obj = SettingModel::getConfigValue('sms_type');
        $sms = new $sms_obj();
        return $sms->send($mobile, ['message' => $msg]);
    }
}