<?php
/**
 *------Create thems Model------
 *------SCWPHP  Version 1.0.0------
 *------Dev Model Jions------
 *------Create Time 2017-06-12 05:08:18------
 */
namespace App\Api\Model;

use \System\Model;
use Biz\Account\AccountManager;

class AccountDetail extends Model {

    protected $table = 'account_detailed';

    /**
     * 修改用户基本信息
     *
     * @access public
     * @return String
     */
    public function updateUserInfo()
    {
        // 获取用户信息
        $accountManagerClass = new AccountManager;
        $userInfo = $accountManagerClass -> getCurrentUser();
        if (empty($userInfo['identity'])) {
            Render([], '2001', lang('Common','Api') -> get('user does login'));
        }
        $updateWhere['account_identity'] = $userInfo['identity'];
        $updateData = [];
        // 提取修改数据
        if (!empty($_POST['email'])) {
            $updateData['email'] = $_POST['email'];
        }
        if (!empty($_POST['phone'])) {
            $updateData['phone'] = $_POST['phone'];
        }
        if (!empty($_POST['img_id'])) {
            $updateData['img_id'] = $_POST['img_id'];
            $updateData['img_url'] = '';
            $oldImg = lm('Account_detailed', 'commons') -> accountDetailed(['img_url'], $updateWhere);
            // 删除以前的文件
            unlink(ROOT_PATH . $oldImg -> img_url);
        }
        if (!empty($_POST['name'])) {
            $updateData['name'] = $_POST['name'];
        }
        if (empty($updateData)) {
            Render([], '4027', lang('Common','Api') -> get('please enter the modification'));
        }
        return lm('Account_detailed', 'commons') -> updateDetailed($updateWhere, $updateData);
    }

    /**
     * 上传用户基本信息头像
     *
     * @access public
     * @return String
     */
    public function updateHeader()
    {
        // 获取用户信息
        $accountManagerClass = new AccountManager;
        $userInfo = $accountManagerClass -> getCurrentUser();
        if (empty($userInfo['identity'])) {
            Render([], '2001', lang('Common','Api') -> get('user does login'));
        }
        // 规定上传格式
        $allowedType = ["png", "jpg", "gif"];
        $allowedExtensions = ["image/jpg","image/jpeg","image/png","image/pjpeg","image/gif","image/bmp","image/x-png"];
        // 最大为 100 Kb
        $maxSize = 102400;
        // 获取上传文件
        $img = $_FILES['img'];
        // 获取上传文件格式并转换为小写
        $imgType = strtolower(substr($img['name'],strrpos($img['name'],".") + 1));
        // 判断格式是否符合要求
        if (!in_array($imgType, $allowedType) || !in_array(strtolower($img['type']), $allowedExtensions)) {
            Render([], '50040', lang('Common','Api') -> get('Picture files are not supported'));
        }
        if ($img['size'] > $maxSize) {
            Render([], '50041', lang('Common','Api') -> get('Picture too large'));
        }
        // 修改文件名
        $imgName = strtotime('now') + rand(10000, 99999);
        $filePath = ROOT_PATH . '/Public/img/header/';
        $imgPath = '/Public/img/header/' . $imgName . ".png";
        if (move_uploaded_file($img['tmp_name'], $filePath . $imgName . ".png")) {
            $updateWhere['account_identity'] = $userInfo['identity'];
            $oldImg = lm('Account_detailed', 'commons') -> accountDetailed(['img_url'], $updateWhere);
            // 删除以前的文件
            unlink(ROOT_PATH . $oldImg -> img_url);
            lm('Account_detailed', 'commons') -> updateDetailed($updateWhere, ['img_url' => $imgPath]);
        } else {
            Render([], '50042', lang('Common','Api') -> get('Upload failure'));
        }
    }
}
