<?php

namespace crmeb\services;

use think\facade\Db;

/**
 * 短信服务
 * Class SMSService
 * @package crmeb\services
 */
class SMSService
{

    // 短信账号
    private static $SMSAccount;

    // 短信token
    private static $SMSToken;

    public static $status;

    // 短信请求地址
    private static $SMSUrl = 'https://sms.crmeb.net/api/';

    //短信支付回调地址
    private static $payNotify;
    //验证码
    const VERIFICATION_CODE = 518076;
    //支付成功
    const PAY_SUCCESS_CODE = 520268;
    //发货提醒
    const DELIVER_GOODS_CODE = 520269;
    //确认收货提醒
    const TAKE_DELIVERY_CODE = 520271;
    //管理员下单提醒
    const ADMIN_PLACE_ORDER_CODE = 520272;
    //管理员退货提醒
    const ADMIN_RETURN_GOODS_CODE = 520274;
    //管理员支付成功提醒
    const ADMIN_PAY_SUCCESS_CODE = 520273;
    //管理员确认收货
    const ADMIN_TAKE_DELIVERY_CODE = 520422;

    /**
     * get: 发送短信验证码
     * path: sendSmsCode
     * method: sendSmsCode
     * param: phone - {string} 手机号
     */
    public  static function send($phone, array  $param ,$template="VERIFICATION_CODE")
    {
        $code = $param['code'];
        $target = "http://cf.51welink.com/submitdata/Service.asmx/g_Submit";
        $company = sys_config('site_name');
        switch ($template)
        {
            case 'PAY_SUCCESS_CODE':
                break;
            case 'DELIVER_GOODS_CODE':
                break;
            case  'TAKE_DELIVERY_CODE':
                break;
            case 'ADMIN_PLACE_ORDER_CODE':
                break;
            case 'ADMIN_RETURN_GOODS_CODE':
                break;
            case 'ADMIN_PAY_SUCCESS_CODE':
                break;
            case 'ADMIN_TAKE_DELIVERY_CODE':
                break;
            default:
                $content=sprintf('您的注册验证码为：%d 。验证码有效期为%d分钟，请尽快填写！【%s】',$code,5,$company);

        }

        $post_data = "sname=dlycwl01&spwd=ycwl123456&scorpid=&sprdid=1012818&sdst=".$phone."&smsg=".rawurlencode($content);
        $gets = self::post($post_data, $target);
        if($gets){
            /*
            $data['phone'] = $phone;
            $data['code'] = $code;
            $data['addtime'] = time();
            $re = Db::name('sms')->insertGetId($data);*/

                return  ['status' => 200, 'msg' => '短信发送成功'];

        }else{
            return ['status' => 400, 'msg' => '发送失败'];
        }
    }
    //第三方短信平台
    private  static  function post($data, $target){
        $url_info = parse_url($target);
        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader .= "Host:" . $url_info['host'] . "\r\n";
        $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader .= "Content-Length:" . strlen($data) . "\r\n";
        $httpheader .= "Connection:close\r\n\r\n";
        //$httpheader .= "Connection:Keep-Alive\r\n\r\n";
        $httpheader .= $data;

        $fd = fsockopen($url_info['host'], 80);
        fwrite($fd, $httpheader);
        $gets = "";
        while(!feof($fd)) {
            $gets .= fread($fd, 128);
        }
        fclose($fd);
        if($gets != ''){
            $start = strpos($gets, '<?xml');
            if($start > 0) {
                $gets = substr($gets, $start);
            }
        }
        return $gets;
    }
}