<?php
/**
 * Created by PhpStorm.
 * User: xiezhixian
 * Date: 2018/5/4
 * Time: 上午11:05
 */

namespace app\api\controller\v1\app;

use app\api\model\APPUser;
use app\api\model\Sms;
use app\api\controller\Api;
use think\Controller;
use app\api\logic\app\SendLogic;
use app\common\tools\Guodulink;
use app\api\logic\app\UserLogic;

/**
 * 所有资源类接都必须继承基类控制器
 * 基类控制器提供了基础的验证，包含app_token,请求时间，请求是否合法的一系列的验证
 * 在所有子类中可以调用$this->clientInfo对象访问请求客户端信息，返回为一个数组
 * 在具体资源方法中，不需要再依赖注入，直接调用$this->request返回为请具体信息的一个对象
 * date:2017-07-25
 */
class Send extends Base
{
    /**
     * 允许访问的方式列表，资源数组如果没有对应的方式列表，请不要把该方法写上，如user这个资源，客户端没有delete操作
     */
    public $restMethodList = 'get|post|put|';

  /*  public function index()
    {
        $request = $this->selectParam(['mobile','sms_type']);
        $this->check($request,'Base.sendSms');
        $info = SendLogic::SendSms($request['mobile'],$request['sms_type']);
        if ($info) {
            $this->returnmsg(403, $data = [], $header = [], $type = "", $info, $message = "信息发送出现了一点小问题哟~");
        }
        //发送成功
        $this->render(200);
    }*/

    /**
     * @author: Airon
     * @time:   2018年5月
     * description 发送验证码
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $request = $this->selectParam(['mobile', 'sms_type' => 'login']);
        $this->check($request, 'Base.sendSms');
        if ($request['sms_type'] != 'login' and $request['sms_type'] != 'bind') {
            $bool = UserLogic::checkUser($request['mobile']);
            if(empty($bool)){
                $this->returnmsg(403, [], [],"", [], "该手机号未注册");
            }
        }

        $info = SendLogic::SendSms($request['mobile'], $request['sms_type']);
        if ($info === false) {
            $this->returnmsg(403, [], [],"", [], "信息发送出现了一点小问题哟～");
        }
        //发送成功
        $this->render(200);
    }

    /**
     * @author: Airon
     * @time:   2018年5月
     * description 验证验证码
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function validateCode()
    {
        $request = $this->selectParam(['mobile', 'sms_type' => 'pass','code']);
        $request['sendPartnerSms'] = $request['sms_type'];
        $this->check($request, 'Base.sendSms');
        if ($request['sms_type'] != 'login' and $request['sms_type'] != 'bind') {
            $bool = UserLogic::checkUser($request['mobile']);
            if(empty($bool)){
                $this->returnmsg(403, [], [],"", [], "该手机号未注册");
            }
        }

        $info = SendLogic::validateCode($request);
        if ($info === false) {
            $this->returnmsg(403, [], [], "", [], "验证码错误");
        }
        $this->render("验证成功",200);
    }
}
