<?php
namespace app\common\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;


class Common extends BaseController
{

    //获取省市区
    public  function getArea() {
        $where['ParentId'] = Request::param('id',100000);
        $data = Db::name('area')->where($where)->field("id,Name,LevelType")->select()->toArray();
        return $this->returnJson($data,'success');
    }

    //通过id获取地区名称
    public  function getName($id) {
        $where['area_id'] = $id;
        return Db::name('area')->where($where)->value('area_name');
    }

    //查看所有的银行列表
    public function bankAccountList() {

        $data = Db::name("bank_account")->select()->toArray();

        return $this->returnJson($data,'success');

    }



    //阿里云图片上传
    public function uploadAlioss()
    {
        // 配置文件
        $alioss = config('alioss.');
        $keyId = $alioss['keyId'];
        $keySecret = $alioss['keySecret'];
        $endpoint = $alioss['endpoint'];
        $bucket = $alioss['bucket'];
        $errors = [
            UPLOAD_ERR_INI_SIZE => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值',
            UPLOAD_ERR_FORM_SIZE => '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值',
            UPLOAD_ERR_PARTIAL => '文件只有部分被上传',
            UPLOAD_ERR_NO_FILE => '没有文件被上传',
            UPLOAD_ERR_NO_TMP_DIR => '找不到临时文件夹',
            UPLOAD_ERR_CANT_WRITE => '文件写入失败'
        ];
        if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
            return $this->returnJson([],$errors[$_FILES['image']['error']],400);
        }
        $file = request()->file('image');
        if (!$file) {
            return $this->returnJson([],"请选择上传图片",400);
        }
        $info = $file->getInfo();
        $size = $info['size'];//字节
        if ($size > 8 * 1024 * 1024) {
            return $this->returnJson([],"图片大小请不要超过8M",400);
        }
        // 文件名称生成
        $name = $info['name'];
        $type = substr($name, strrpos($name, '.') + 1);//取后缀
        $file_name = sha1(date('YmdHis', time()) . uniqid()) . '.' . $type;
        // 文件路径生成
        $file_path = 'hotel/hotel/' . $this->admin_id . '/' . date('Ym') . '/' . date('d');
        $url = $file_path . '/' . $file_name;
        try {
            $ossClient = new OssClient($keyId, $keySecret, $endpoint);
            $result = $ossClient->uploadFile($bucket, $url, $info['tmp_name']);
            if (isset($result['info']['http_code']) AND $result['info']['http_code'] == 200) {
                $url_arr = config('alioss.pic_url') . '/' . $url;
            } else {
                return $this->returnJson([],"图片上传错误",400);
            }
        } catch (OssException $e) {
            $msg = $e->getMessage();
            return $this->returnJson([],$msg,400);
        }
        return $this->returnJson($url_arr);

    }






}