<?php
/**
 * Created by PhpStorm.
 * User: 中闽 < 1464674022@qq.com >
 * Date: 2019/12/5
 * Time: 17:44
 */

namespace app\api\controller;


use app\api\controller\base\Base;
use app\common\model\User;

class Common extends Base
{
    //注册
    public function register()
    {
        $post = $this->request->param();
        $validate = new \think\Validate([
            ['openid', 'max:50'],
            ['unionid', 'max:50'],
            ['nickname|昵称', 'require|max:50'],
            ['head_pic|头像', 'require|max:255'],
            ['sex|性别', 'in:1,2'],
            ['country|国家', 'max:50'],
            ['province|省份', 'max:50'],
            ['city|城市', 'max:50'],
        ]);
        if (!$validate->check($post)) {
            $this->json_error('提交失败：' . $validate->getError());
        }
        $user = new User();
        $data = [
            'openid' => $post['openid']??'',
            'unionid' => $post['unionid']??'',
            'passport' => $post['openid']??'',
            'nickname' => $post['nickname'],
            'user_type' => User::TYPE_WECHAT,
            'user_cate' => User::CATE_USER,
            'head_pic' => $post['head_pic'],
            'status' => User::STATUS_PASS,
            'ip' => $this->request->ip(),
            'sex' => $post['sex']??0,
            'country' => $post['country']??'',
            'province' => $post['province']??'',
            'city' => $post['city']??'',
        ];
        if (false == $user->allowField(true)->save($data)) {
            $this->json_error('添加失败');
        } else {
            $this->json_success('添加成功', 'index');
        }
    }

}
