<?php

/**
 * 生成订单号
 * @return string
 */
function createOrderNo()
{
    return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}
/**
 * [send_post 发送post请求]
 * Author：上官君墨
 * Email：<mengjunjun@dqiucun.net>
 * @param  [str] $url       [请求地址]
 * @param  [arr] $post_data [post键值对数据]
 */
function send_post($url, $post_data){
  $postdata = http_build_query($post_data);
  $options = array(
    'http' => array(
      'method' => 'POST',
      'header' => 'Content-type:application/x-www-form-urlencoded',
      'content' => $postdata,
      'timeout' => 15 * 60 // 超时时间，单位:s
    )
  );
  $context = stream_context_create($options);
  $result = file_get_contents($url, false, $context);
  return $result;
}

$url = 'https://open.nuonuo.com/accessToken';
$appKey="SD69037503";//诺诺ISV商应用appKey
$appSecret="SD5B153E50A344D7";//诺诺ISV商应用appKey

$data = $_REQUEST;
if (isset($data['code']) && $data['code'] !='' && isset($data['taxnum']) && $data['taxnum'] !='') {
	// 第二步 回调得到code
	//第三步，获取access_token
	$post_data = [
		'code'=>$data['code'],
		'client_id'=>$appKey,
		'client_secret'=>$appSecret,
		'grant_type'=>'authorization_code',
		'redirect_uri'=>'http://ltd.dqiucun.net/index.php',
		'taxNum'=>$data['taxnum'],
	];
	$res = send_post($url, $post_data);
// 	{
//         "access_token": "35ee26aac1e0daa4e72e501kiiuf8lni",
//         "refresh_token": "f4505dfc4736c7102e12384d0066b135",
//         "oauthUser": "{\"userName\":\"91520102MAAJMBK502\",\"registerType\":\"1\"}",
//         "expires_in": 86400,
//         "userId": "b5d541196abb45b6afde0992de0376c1"
//     }
	print_r($res);die;
}else{
	
    // https://open.nuonuo.com/authorization.html?appKey=SD69037503&response_type=code&redirect_uri=http://ltd.dqiucun.net/index.php&state=dqiucun
	// 第一步 获取code
	$code_url = 'https://open.nuonuo.com/authorize?appKey='.$appKey.'&response_type=code&redirect_uri=http://ltd.dqiucun.net/index.php&state=dqiucun';

	Header("Location: ".$code_url);
}



