<?php
namespace app\appapi\controller\v1;

use app\appapi\ApiController;
use think\facade\Db;

use app\common\lib\Aes;

class User extends ApiController
{
    public function getUserInfo(){
        $user=Db::name('user')
		->where('user_id',$this->uid)
		->find();
        
        $aes = new Aes([]);
        if(strlen(trim($user['uniqid']))>0){
            $params = urlencode($aes->encrypt('verify_code='.$user['uniqid']));
        }else{
            $params = urlencode($aes->encrypt('user_id='.$user['user_id']));
        }

        $user['level_name'] = Db::name('user_level')->where('agent_id',$user['agent_id'])->where('level_id',$user['level'])->value('name');

        $user['nfcurl'] = getUrl().'/card/?params='.$params;

        $user['nc_func'] = explode(',',$user['nc_func']);

        $user['agent_prefix'] = Db::name('agent')->where('id',$user['agent_id'])->value('prefix');

        return $this->Success($user);
    }    

    public function setUserLevel(){
        $result=Db::name('user')
		->where('user_id',$this->uid)
		->update(['level'=>input('level')]);

		
        return $this->Success($result);
    }        

    public function setUserTpl(){
        try{
            $result=Db::name('user')
            ->where('user_id',$this->uid)
            ->update(['nc_template'=>input('tpl')]);    
        }catch(\Excenption $e){
            return $this->Error('更新失敗');
        }
        		
        return $this->Success($result);
    }        

    public function uploadAvatar(){

		$files = request()->file('file');
		$savename =  \think\facade\Filesystem::disk('public')->putFile( input('user_id'), $files);

		$avatar = getUrl().'/storage/'.$savename;

        
        // Db::name('user')
        // ->where('user_id',input('user_id'))
        // ->update(['avatar'=>$avatar]);

        return $this->Success($avatar);
    }

    public function updateSendCount(){
        $user_id = input('userid');
        
        Db::name('user')
        ->where('user_id',input('userid'))
        ->exp('send_count', 'send_count+1')
        ->update();
        // ->inc('send_count',1);

        return $this->Success('更新成功');
    }

}
