<?php
/**
 * lionfish 狮子鱼社区团购系统
 *
 * ==========================================================================
 * @link      http://www.liofis.com/
 * @copyright Copyright (c) 2015 liofis.com. 
 * @license   http://www.liofis.com/license.html License
 * ==========================================================================
 *
 * @author    fish
 *
 */
namespace Home\Controller;


class MpnotifyController extends CommonController {

    protected function _initialize(){

        $this->checkSignature();
    }
    /**
     * @author yj
     * @desc 接收微信小程序通知入口
     */
	public function index()
    {
        $data = file_get_contents('php://input');

        $ins_data = [];
        $ins_data['text'] = ( $data );
        $ins_data['is_success'] = 1;
        $ins_data['type'] = '2';
        $ins_data['log'] = date('Y-m-d H:i:s');

        M('log')->add($ins_data);

        echo 'success';
        die();
    }


    private function checkSignature()
    {
        if( isset($_GET['echostr']) )
        {
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];

            $token = D('Home/Front')->get_config_by_name('tradecomponts_token');
            $tmpArr = array($token, $timestamp, $nonce);
            sort($tmpArr, SORT_STRING);
            $tmpStr = implode( $tmpArr );
            $tmpStr = sha1( $tmpStr );

            if ($tmpStr == $signature ) {
                echo $_GET['echostr'];
                die();
            } else {
                return false;
            }
        }

    }
	
}