<?php
/**
 * MessageController.php UTF-8
 * 客服消息
 *
 * @date    : 2018/9/14 16:37
 *
 * @license 这不是一个自由软件,未经授权不许任何使用和传播。
 * @author  : wuyonghong <wyh@huosdk.com>
 * @version : HuoMp 1.0
 */

namespace mini\sdk\controller;

use huomp\model\weixin\MpConfModel;
use huoMpMsg\controller\MiniMessage;
use huoMpMsg\controller\OaMessage;
use mini\common\controller\V2ApiBaseController;
use think\Log;

class MessageController extends V2ApiBaseController {
    function _initialize() {
        parent::_initialize();
        Log::write($this->request->getContent(), 'wechat', true);
        $this->response_type = 'json';
    }

    /**
     * 接收小程序客服消息
     * 【域名】/mp/msg/:conf_id
     * https://developers.weixin.qq.com/miniprogram/dev/api/custommsg/receive.html#文本消息
     *
     * @param string $mp_id 微信ID
     */
    public function receive($mp_id = '') {
        if (!empty($_GET["echostr"])) {
            $this->checkToken($mp_id);
        }
        $_param_json = $this->request->getContent();
        $_param = @json_decode($_param_json, true);
        if (empty($_param) || empty($mp_id)) {
            $this->error('参数错误');
        }
        $_rs = (new MiniMessage())->receive($mp_id, $_param);
        if (true == $_rs) {
            die('success');
        }
        die('');
    }

    /**
     * 第一次小程序校验token
     *
     * @param string $mp_id 公众号ID
     */
    public function checkToken($mp_id = '') {
        $_token = '';
        $_data = (new MpConfModel())->getDataByMpId($mp_id);
        if (!empty($_data['ext_info']['Token'])) {
            $_token = $_data['ext_info']['Token'];
        }
        $_signature = $_GET["signature"];
        $_timestamp = $_GET["timestamp"];
        $_nonce = $_GET["nonce"];
        $_echo_str = $_GET["echostr"];
        $_tmp_arr = array($_token, $_timestamp, $_nonce);
        sort($_tmp_arr, SORT_STRING);
        $_tmp_str = implode($_tmp_arr);
        $_tmp_str = sha1($_tmp_str);
        if ($_tmp_str == $_signature) {
            die($_echo_str);
        } else {
            die('fail');
        }
    }

    /**
     * 第一次校验微信公众号token
     * 接收小程序客服消息
     * 【域名】/oa/msg/:conf_id
     * https://developers.weixin.qq.com/miniprogram/dev/api/custommsg/receive.html#文本消息
     *
     * @param string $mp_id 公众号ID
     */
    public function checkOaToken($mp_id = '') {
        $_token = '';
        $_data = (new MpConfModel())->getDataByMpId($mp_id);
        if (!empty($_data['ext_info']['Token'])) {
            $_token = $_data['ext_info']['Token'];
        }
        $_signature = $_GET["signature"];
        $_timestamp = $_GET["timestamp"];
        $_nonce = $_GET["nonce"];
        $_echo_str = $_GET["echostr"];
        $_tmp_arr = array($_token, $_timestamp, $_nonce);
        sort($_tmp_arr, SORT_STRING);
        $_tmp_str = implode($_tmp_arr);
        $_tmp_str = sha1($_tmp_str);
        if ($_tmp_str == $_signature) {
            die($_echo_str);
        } else {
            die('fail');
        }
    }

    /**
     * 接收公众号客服消息
     * 【域名】/oa/msg/:conf_id
     * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140453
     *
     * @param string $mp_id 微信ID
     *
     * @return bool|string
     */
    public function receiveOaMsg($mp_id = '') {
        if (!empty($_GET["echostr"])) {
            $this->checkOaToken($mp_id);
        }
        $_param_xml = $this->request->getContent();
        $_param = \huolib\tool\Xml::xmlToArr($_param_xml);
        if (empty($_param) || empty($mp_id)) {
            $this->error('参数错误');
        }
        Log::write($_param, 'wechat', true);
        $_msg_type = $_param['MsgType'];
        switch ($_msg_type) {
            case 'event':
                (new OaMessage())->receiveEventMsg($mp_id, $_param);
                break;
            default:
                (new OaMessage())->receive($mp_id, $_param);
        }
    }
}