<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\admin\controller\base\Permissions;
use think\Db;

class Smsconfig extends Permissions
{
    public function index()
    {
        $data = Db::name('smsconfig')->where('sms', 'sms')->find();
        $this->assign('data', $data);
        $grouptabs = (new \app\common\model\ConfigTab())->order('sort desc')->select();
        $this->assign('tabs', $grouptabs);
        return $this->fetch();
    }


    public function publish()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $validate = new \think\Validate([
                ['appkey', 'require', 'AppKey不能为空'],
                ['secretkey', 'require', 'SecretKey不能为空'],
                ['name', 'require', '短信签名不能为空'],
                ['code', 'require', '短信模板ID不能为空'],
            ]);
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }

            //$post['content'] = htmlentities($post['content']);
            if (false == Db::name('smsconfig')->where('sms', 'sms')->update($post)) {
                $this->error('提交失败');
            } else {
                $this->success('提交成功', 'admin/smsconfig/index');
            }
        } else {
            $this->error('非法请求');
        }
    }


    public function smsto()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $validate = new \think\Validate([
                ['phone', 'require|length:11,11|number', '手机号码不能为空|手机号码格式不正确|手机号码格式不正确'],
            ]);
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }

            $phone = (string)$post['phone'];

            $param = '{"name":"Tplay用户"}';

            $smsto = SendSms($param, $phone);

            if (!empty($smsto)) {
                $this->error('发送失败');
            } else {
                $this->success('短信发送成功');
            }
        } else {
            return $this->fetch();
        }
    }
}
