<?php

namespace app\common\service;


use app\common\model\Config;
use app\common\model\MessageTemplate;
use chuanglan\Chuanglan;
use think\facade\Log;

class SmsService
{

    public function send($mobile, $code, $content_param = [])
    {
        if (env('APP_DEBUG')) {
            return ['code' => 0];
        }
        $message = MessageTemplate::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 = new Chuanglan();
        $sms->send($mobile, ['message' => $msg]);

        return ['code' => 0];
    }

    public function examineSend($code, $content_param = [])
    {
        $mobile = Config::getConfigValue('examine_mobile');
        return $this->send($mobile, $code, $content_param);
    }
}