<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2022/03/16 1500
 * Time: 15:00
 */

namespace app\api\controller\getui;

use think\Db;
use think\Request;

/**
 * 推送(unipush)
 * @package app\api\controller\core
 */
class Push extends Base
{

    /**
     * 2. 推送API_根据cid进行单推
     */
    public function pushToSingleByCid()
    {
        $param = $this->request->param();

        $cid = $param['cid'];
        $title = $param['title'];
        $content = $param['content'];
        $scene_type = $param['scene_type']?$param['scene_type']:"default";

        if(empty($cid)){
            $this->error("接收通知者id不能为空");
        }

        $push = new \GTPushRequest();
        $push->setRequestId($this->micro_time);
        $message = new \GTPushMessage();
        $notify = new \GTNotification();
        $notify->setTitle($title);
        $notify->setBody($content);
        //点击通知后续动作,目前支持以下后续动作:
        //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
        $notify->setClickType("payload");
        $array = ["scene_type"=>$scene_type];
        $notify->setPayload(json_encode($array,JSON_UNESCAPED_UNICODE));
        $message->setNotification($notify);
        $push->setPushMessage($message);
        $push->setCid($cid);
        //处理返回结果
        $result = $this->api->pushApi()->pushToSingleByCid($push);

        $status ="";
        if (!empty($result)) {
            if ($result['code'] == 0) {
                $data = $result['data'];
                if(!empty($data)){
                    foreach ($data as $k=>$v){
                        if(!empty($v[$cid])){
                            $status = $v[$cid];
                            break;
                        }
                    }
                }
                $this->success("请求成功", $status);
            } else {
                $this->error("请求异常[" . $result['code'] . $result['msg'] . "]");
            }
        } else {
            $this->error("unipush服务器请求异常");
        }
    }


}