<?php

namespace App\Http\Controllers\Syj;

use App\Models\Syj\UserModel;
use App\Services\QQMiniApp\Login;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{

    /**
     * 授权注册登陆
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \App\Services\QQMiniApp\QQException
     */
    public function auth(Request $request)
    {
        $this->validate($request, [
            'js_code' => 'required',
        ]);
        $js_code = $request->get('js_code');

        $qqMiniApp = new Login();
        $user_info = $qqMiniApp->code2Session($js_code);
        $user = UserModel::updateOrCreate(
            ['openid' => $user_info->openid],
            ['unionid' => isset($user_info->unionid) ? $user_info->unionid : null, 'login_at' => time()]
        );
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $user
        ]);
    }


    /**
     * 用户信息
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function user(Request $request)
    {
        $user = UserModel::where('openid', $request->header('openid'))->firstOrFail();
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $user
        ]);
    }


    /**
     * 修改用户信息
     * @param $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function updateUser(Request $request)
    {
        $this->validate($request, [
            'nickname' => 'max:16',
            'avatar' => 'url',
            'sex' => 'integer',
            'province' => '',
            'city' => '',
        ]);

        $user = UserModel::where('openid', $request->header('openid'))->firstOrFail();

        // 补充信息并上传头像至oss
        if (!$user->nickname) {
            $user->nickname = $request->nickname;
        }
        if (!$user->avatar) {
            try {
                $avatar = str_replace('/100', '/0', $request->avatar);
                $avatar = $this->makeAvatar($avatar);
            } catch (\Exception $e) {
                $avatar = $request->avatar;
            }
            $user->avatar = $avatar;
        }
        if (!$user->sex) {
            $user->sex = $request->sex;
        }
        if (!$user->province) {
            $user->province = $request->province;
        }
        if (!$user->city) {
            $user->city = $request->city;
        }

        $user->save();
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $user
        ]);
    }

    private function makeAvatar($url)
    {
        $http = "http://www.koudaidaxue.com/Tools/Upload/wxHeader2Avatar";
        $response = \Curl::to($http)->withData([
            'url' => $url
        ])->asJsonResponse()->post();
        return $response->data->url;
    }
}
