<?php

namespace App\Http\Controllers\Miniprogram;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\Controller;

class Action extends Controller
{
    /**
     * 创建动作意图
     * @param  Request  $request
     * @return array
     * @throws \ApiException
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'action' => 'required',
            'param' => 'required'
        ]);
        $uid = Auth::auth();
        Redis::hset("fpdx_mini_action:{$uid}", $request->input('action'), $request->input('param'));
        return response([
            'code' => 200,
            'message' => "success"
        ]);
    }

    /**
     * 获取动作意图
     * @param  string  $action
     * @return mixed
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function get(string $action)
    {
        $uid = Auth::auth();
        $param = Redis::hget("fpdx_mini_action:{$uid}", $action);
        if (empty($param)) {
            return response([
                'code' => 201,
                'message' => '无意图'
            ]);
        } else {
            return response([
                'code' => 200,
                'message' => 'success',
                'data' => [
                    'action' => $param
                ]
            ]);
        }
    }
}
