<?php
defined('IN_IA') || exit('Access Denied');
load()->func('communication');
function sms_order_notice($oid)
{

}
function sms_order_paystaus($oid){
  $sms_order = pdo_get("evzg_express_sms_order",array('id'=>$oid));
  if(empty($sms_order)){
      return false;
  }
  $shop = pdo_get("evzg_express_shop",array('id'=>$sms_order['sid']));
  pdo_update("evzg_express_shop",array('chitnum'=>intval($shop['chitnum'])+$sms_order['num']),array('id'=>$sms_order['sid']));
  return true;
}
/**
 * 根据订单发送大鱼短信
 * @var $id 订单id 录入快件表
 */
function sms_send_dayu($id){
    global $_W;
    mload()->classs('signaturehelper');
    mload()->model('shop');
    if(empty($id)){
        return  false;
    }
   //判断当前使用模板类型
   $settings = $_W['current_module']['config'];
   $log = array();
   $express = pdo_get("evzg_express_expres",array('id'=>$id));
   $sid = $express['sid'];
   $shop_set = shop_get_set();
   $shop = shop_get_info($sid);
   if(empty($settings['dayutpl'])){
       $TemplateCode=$settings['dayu']['dayu_temp2'];//对应的模板
       $TemplateParam=array(
           'shop' => $shop['name'],
           'ordersn' => $express['orderno']
       );// 模板的变量
   }elseif ($settings['dayutpl']==1){
       $TemplateCode=$settings['dayu']['dayu_temp3'];//对应的模板
       $TemplateParam=array(
           'shopname' => $express['expressshop'],
           'address'=>$shop['city'].$shop['district'].$shop['address'],
           'tel'=>$shop['mobile'],
           'code' => $express['orderno'],
       );// 模板的变量
   }
   $mobile = $express['mobile'];
   $member = pdo_get("evzg_express_member",array('mobile'=>$mobile));

   if($shop_set['smsprice']>0&&!empty($express['sid'])){
       $smsnum = shop_get_smsnum($sid);

       if(empty($smsnum)){
           if(!empty($log)){
               $log['oid'] = $express['id'];
               $log['sid'] = $express['sid'];
               if(empty($member['openid'])){
                   $log['openid'] = $member['mobile'];
               }
               if(empty($log['operator'])){
                   $log['operator'] = '系统';
               }
               $log['content'] = "阿里大鱼：商户余额不足";
               $log['addtime']=time();
               $log['msg'] = "阿里大鱼：商户余额不足";
               $log['type'] = 1;
               msgLog($log);
           }
           return false;
       }
   }
   $params = array ();

   // *** 需用户填写部分 ***

   // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
   $accessKeyId = $settings['dayu']['dayu_appkey'];
   $accessKeySecret = $settings['dayu']['dayu_secretkey'];

   // fixme 必填: 短信接收号码
   $params["PhoneNumbers"] = $mobile;

   // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
   $params["SignName"] = $settings['dayu']['dayu_sign'];

   // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
   $params["TemplateCode"] = $TemplateCode;

   // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
   $params['TemplateParam'] = $TemplateParam;

   // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
   if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
       $params["TemplateParam"] = json_encode($params["TemplateParam"]);
   }
   // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
   $helper = new SignatureHelper();

   // 此处可能会抛出异常，注意catch
   $content = $helper->request(
       $accessKeyId,
       $accessKeySecret,
       "dysmsapi.aliyuncs.com",
       array_merge($params, array(
           "RegionId" => "cn-hangzhou",
           "Action" => "SendSms",
           "Version" => "2017-05-25",
       ))
   );
   $contents = json_decode(json_encode($content),true);
   if(!empty($log)){
       $log['oid'] = $express['id'];
       $log['sid'] = $express['sid'];
       if(empty($log['openid'])){
           $log['openid'] = $mobile;
       }
       if(empty($log['operator'])){
           $log['operator'] = '系统';
       }
       if(empty($log['content'])){
           $log['content'] = "阿里大鱼：".$params["TemplateParam"];
       }

       $log['addtime']=time();
       $log['msg'] = json_encode($content);
       $log['type'] = 1;
       $log['mobile'] = $mobile;
       $logid = msgLog($log);

   }
   if($contents['Message']=='OK'){
       $log['status'] =1;
       if($shop_set['smsprice']>0&&!empty($log['sid'])){
           sms_send_success($logid,$settings);
       }
   }
}
function sms_send_success($logid,$settings=''){
    $log = pdo_get('evzg_express_log',array('id'=>$logid));
    if(empty($log)){
        return false;
    }
    $mobile = $log['mobile'];
    if(!empty($log['msg'])){
        $msg = json_decode($log['msg'],true);
        $bizId = $msg['BizId'];
    }else{
        $bizId = '';
    }
    if(!empty($settings)&&!empty($mobile)){
       $result = sms_check_code($settings,$mobile,$bizId);
       $code = true;
       if(empty($result['SmsSendDetailDTOs']['SmsSendDetailDTO'])){
           $status =-1;
           $code = false;
       }else{
           if($result['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus']!=1){
               $status =$result['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'];
               $code =false;
           }else{
               $status ==1;
           }
       }
       pdo_update("evzg_express_log",array('status'=>$status),array('id'=>$logid));
    }
    $sid = $log['sid'];
    $shop = pdo_get("evzg_express_shop",array('id'=>$sid));
    if(empty($shop)){
        return false;
    }
    if(!$code){
        return false;
    }
    pdo_update("evzg_express_shop",array('chitnum'=>intval($shop['chitnum'])-1),array('id'=>$sid));
    return true;
}
//检查阿里大于发送状态
function sms_check_sendcode($logid,$settings){
    $log = pdo_get('evzg_express_log',array('id'=>$logid));
    if(empty($log)){
        return false;
    }
    if($log['status']!=-1){
        return false;
    }
    $mobile = $log['mobile'];
    if(!empty($log['msg'])){
        $msg = json_decode($log['msg'],true);
        $bizId = $msg['BizId'];
    }else{
        $bizId = '';
    }
    if(!empty($settings)&&!empty($mobile)){
        $result = sms_check_code($settings,$mobile,$bizId);
        $code = true;
        if(empty($result['SmsSendDetailDTOs']['SmsSendDetailDTO'])){
            $status =-1;
            $code = false;
        }else{
            if($result['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus']!=3){
                $status =$result['SmsSendDetailDTOs']['SmsSendDetailDTO'][0]['SendStatus'];
                $code =false;
            }else{
                $status =1;
            }
        }
        pdo_update("evzg_express_log",array('status'=>$status),array('id'=>$logid));
    }
    //var_dump($result);die();
    $sid = $log['sid'];
    $shop = pdo_get("evzg_express_shop",array('id'=>$sid));
    if(empty($shop)){
        return false;
    }
    if(!$code){
        return false;
    }
    pdo_update("evzg_express_shop",array('chitnum'=>intval($shop['chitnum'])-1),array('id'=>$sid));
    return true;
}
function sms_check_code($settings,$mobile,$bizId=''){
    mload()->classs('signaturehelper');
    $params = array ();

    // *** 需用户填写部分 ***
    // fixme 必填：是否启用https
    $security = false;

    // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
    $accessKeyId = $settings['dayu']['dayu_appkey'];
    $accessKeySecret = $settings['dayu']['dayu_secretkey'];

    // fixme 必填: 短信接收号码
    $params["PhoneNumber"] = $mobile;

    // fixme 必填: 短信发送日期，格式Ymd，支持近30天记录查询
    $params["SendDate"] = date("Ymd");

    // fixme 必填: 分页大小
    $params["PageSize"] = 1;

    // fixme 必填: 当前页码
    $params["CurrentPage"] = 1;

    // fixme 可选: 设置发送短信流水号
    $params["BizId"] = $bizId;

    // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***

    // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
    $helper = new SignatureHelper();

    // 此处可能会抛出异常，注意catch
    $content = $helper->request(
        $accessKeyId,
        $accessKeySecret,
        "dysmsapi.aliyuncs.com",
        array_merge($params, array(
            "RegionId" => "cn-hangzhou",
            "Action" => "QuerySendDetails",
            "Version" => "2017-05-25",
        )),
        $security
    );
    return json_decode(json_encode($content),true);
}
?>