<?php
/**
 * AJAX数据获取
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 

namespace Common\Model;
use Think\Model;
class AjaxModel extends Model
{
    protected $tableName = 'goods';

    //发送验证短信
    public function sendsms($postdata)
    {
        $regular= new \Common\Vendor\Regular();
        $backdata=array('status'=>0,'msg'=>'');
        $mobile = $postdata['mobile'];
        $unique = $postdata['unique'];//是否需要检测被占用 0:不检测 
		$send_type = $postdata['type']?$postdata['type']:1;//短信类型 1：注册验证 2：找回密码 3：提现验证 4:店铺开卡
        if (empty($mobile)) {
            $backdata['msg'] = '手机号不能为空';return $backdata;
        }
        if(!empty($mobile) && !$regular->isPhone($mobile)){
            $backdata['msg'] = '手机号格式不正确';return $backdata;
        }
        if ($unique == 1) {//要检测是否被抢占（用于注册，绑定时使用）
            $mobilecount = M('Member')->where('mobile=' . $mobile)->count();
            if ($mobilecount > 0) {
                $backdata['msg'] = '手机号已存在';return $backdata;
            }
        }elseif ($unique == 2) {//要检测会员是否存在（取回密码）
            $mobilecount = M('Member')->where('mobile=' . $mobile)->count();
            if ($mobilecount <= 0) {
                $backdata['msg'] = '手机号不存在';return $backdata;
            }
        }elseif ($unique == 3) {//要检测店铺是否存在（取回密码）
            $shop_id = M('Member')->where('mobile=' . $mobile)->getField('shop_id');
            if ($shop_id == 0) {
                $backdata['msg'] = '手机号不存在';return $backdata;
            }
        }elseif ($unique == 4) {//店铺提现
            $status = M('Shops')->where('contacts_phone=' . $mobile)->getField('status');
            if (!$status) {
                $backdata['msg'] = '手机号不存在';return $backdata;
            }
        }elseif ($unique == 5) {//店铺找回密码
            $status = M('Suppliers')->where('phone=' . $mobile)->getField('status');
            if (!$status) {
                $backdata['msg'] = '手机号不存在';return $backdata;
            }
        }elseif ($unique == 6) {//店铺提现
            $status = M('Suppliers')->where('phone=' . $mobile)->getField('status');
            if (!$status) {
                $backdata['msg'] = '手机号不存在';return $backdata;
            }
        }
        if (C('SMS_REG_VERIFY') && empty($this->member_info) && !isset($postdata['unverify']) && $send_type !=3 && $send_type !=4) {
            $regverify = I('post.regverify');
            if (empty($regverify)) {
                $backdata['msg'] = '验证码不能为空';return $backdata;
            }
            if (!check_verify($regverify, 'Member')) {//验证验证码
                $backdata['msg'] = '验证码错误';return $backdata;
            }
        }
        $data['to'] = $mobile;
        $data['verify_code'] = $data['code'] = randStr(4, '0123456789');
		if($send_type==1){//注册短信
			$result = sendbytheme($data, 'SMS_USER_VERIFY');
		}elseif($send_type==2){//找回密码短信
			$result = sendbytheme($data, 'GET_PSW_VERIFY');
		}elseif($send_type==3){//提现短信
			$result = sendbytheme($data, 'GET_MONEY_VERIFY');
		}elseif($send_type==4){
			$result = sendbytheme($data, 'SMS_SHOPSCARD');
		}
        if ($result == '发送成功') {
            $backdata['status'] = 1;
        }
        $backdata['msg'] = $result;
        return $backdata;
    }

    public function sendsms_email($postdata)
    {
        $regular= new \Common\Vendor\Regular();
        $backdata['status'] = 0;
        $email = $postdata['email'];
        if (empty($email)) {
            $backdata['msg'] = '邮箱不能为空';
            return $backdata;
        }
        if(!empty($email) && !$regular->isEmail($email)){
            $backdata['msg'] = '邮箱格式不正确';
            return $backdata;
        }
        $data['to'] = $email;
        $data['verify_code'] = $data['code'] = randStr(4, '0123456789');
        $result = sendbytheme($data, 'EMAIL_USER_EMAIL');
        if ($result == '发送成功') {
            $backdata['status'] = 1;
        } else {
            $backdata['status'] = 0;
        }
        $backdata['msg'] = $result;
        return $backdata;
    }

    public function msg_sender()
    {//自动发送列表消息
        $map['is_send'] = $map['is_success'] = 0;
        $mod = M('SmsLogs');
        $lists = $mod->where($map)->field('id,to,from,send_type,content,uid,title')->limit(5)->order('id')->select();
        foreach ($lists as $v) {
            $where = array();
            $where['id'] = $v['id'];
            if ($v['send_type'] == 1) {
                $sendresult = dosendemail($v['to'], $v['from'], $v['title'], $v['content']);
                if ($sendresult) {
                    $data['is_success'] = '1';
                    $data['result'] = '发送成功';
                } else {
                    $data['is_success'] = '0';
                    $data['result'] = $mail->ErrorInfo;
                }
            } else {
                $sendresult = dosendsms($v['to'], $v['title']);
                $data['is_success'] = $sendresult['status'];
                $data['result'] = $sendresult['msg'];
            }
            $data['is_send'] = 1;
            $data['sendtime'] = NOW_TIME;
            $mod->where($where)->save($data);
        }
    }

    //清空历史
    public function clearhistory()
    {
        $return['status']=0;
        session('historyids',array());
        $goodsids=session('historyids');
        if(empty($goodsids)) $return['status']=1;
        return $return;
    }
}
?>