<?php
namespace app\app\logic;

use app\app\model\smscoderecordmodel;
use app\app\model\webusermodel;

/**
 * 登记记录表
 *
 * @author wj
 * @date 2022-07-22
 */
class webuserlogic extends baselogic
{
    /**
     * 设置请求数据规则
     * 20220107
     * wj
     */
    protected function setrules()
    {
        $list = [
            'login' => [
                ['name' => 'telno', 'title' => '手机号', 'require' => true, 'type' => 'numeric'],
                ['name' => 'code', 'title' => '验证码', 'require' => true, 'type' => 'numeric'],
            ],
            'sendsms' => [
                ['name' => 'telno', 'title' => '手机号', 'require' => true, 'type' => 'numeric'],
            ],
        ];
        return $list;
    }

    //后台或数据库直接添加数据
    public function login($arr)
    {
        $result = $this->checkparam(__FUNCTION__, $arr);
        if (1 != $result['status']) {
            return $result;
        }
        $data = $result['data'];
        $telno = $data['telno'];
        $code = $data['code'];
        //验证用户
        $m_w = new webusermodel();
        $winfo = $m_w->getinfobytelno($telno);
        if (empty($winfo)) {
            return backarr(0, "无用户数据");
        }
        $wid = $winfo['id'];
        //验证code
        $m_s = new smscoderecordmodel();
        $result = $m_s->checkcode($telno, $code);
        if (!$result['status']) {
            return $result;
        }
        $row = $m_w->settoken($wid, $telno);
        if (empty($row)) {
            return backarr(0, "用户token修改失败");
        }
        $winfo = $m_w->getinfobyid($wid, ['token']);
        return backarr(1, "用户登录成功", $winfo);
    }
    /**
     * 校验token
     *
     * @param  [type] $token
     * @return void
     * @author wj
     * @date 2022-07-25
     */
    public function checktoken($token)
    {
        $m_w = new webusermodel();
        $info = $m_w->getinfobytoken($token);
        return $info ? true : false;
    }
    /**
     * 发送短信
     *
     * @return void
     * @author wj
     * @date 2022-07-25
     */
    public function sendsms($arr)
    {
        $result = $this->checkparam(__FUNCTION__, $arr);
        if (1 != $result['status']) {
            return $result;
        }
        $data = $result['data'];
        $telno = $data['telno'];
        if (!isMoblid($telno)) {
            return backarr(0, "手机号格式错误");
        }
        //查未使用的验证码
        $m_s = new smscoderecordmodel();
        $where = ['telno' => $telno];
        $list = $m_s->getList($where, 'smscode', 1, 0)->toArray();
        if (empty($list)) {
            //发送短信
            //$code = randomStr();
            $code = "8888";
            $smsData = [
                'telno' => $telno,
                'smscode' => $code,
            ];
            $row = $m_s->insertData($smsData);
            if (empty($row)) {
                return backarr(0, "验证码添加失败");
            }
        } else {
            $code = $list[0]['smscode'];
        }
        //发送验证码
        $sendresult = true;
        if (!$sendresult) {
            return backarr(0, "验证码发送失败");
        }
        return backarr(1, "验证码发送成功");
    }

}
