profile->getProWithUid($this->auth->getUid());
//	$falg = Doo::acl()->isAllowed($uGroups['groups'], $resource, $action);
//	if (!$falg)
//	    return Doo::acl()->defaultFailedRoute;
        if ($this->auth->getUid()) {
            $uGroups = $this->profile->getProWithUid($this->auth->getUid());
            $falg = Doo::acl()->isAllowed($uGroups['groups'], $resource, $action);
            if (!$falg)
                return Doo::acl()->defaultFailedRoute;
        } else {
            return Doo::acl()->defaultFailedRoute;
        }
    }
    public function __construct()
    {
        $this->data['numofchecking'] = null;
        $this->auth = new Auth();
        $this->attfile = new attFile();
        $this->profile = new Profile();
        $this->project = new Project();
        $this->contractact = new Contractact();
        $this->actmeasure = new actMeasure();
        $this->numofperact = new NumofperAct();
        $this->user = new User();
        $this->ph = new PasswordHash(8, FALSE);
        $this->sms = new Sms(Doo::conf()->SMS_URL, Doo::conf()->SMS_ACCOUNT, DOO::conf()->SMS_PASSWORD, DOO::conf()->SMS_EXTNO);
        $this->aconfig = new AConfig();
        $this->measureauditact = new MeasureauditAct();
        $this->sign = new signn();
        $this->change = new Changes();
        $this->__hashids = new Hashids\Hashids('jlzfuserid', 8);
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
        $this->data['currChannle'] = '';
        $this->data['user'] = $this->profile->getProWithUid($this->auth->getUid());
        $mpidArray = $this->measureauditact->getAuditProject2($this->auth->getUid());
        if (isset($mpidArray[0]['pid'])) {
            foreach ($mpidArray as $key => $value) {
                // 审批操作按照时间排序多标段
                $this->data['numofchecking'] += $this->measureauditact->getCountMyChecking($value['pid'], $this->auth->getUid());
            }
        }
        if (!isset($this->data['numofchecking']) || $this->data['numofchecking'] == 0) {
            $this->data['numofchecking'] = null;
        }
        $this->data['signSwitch'] = $this->aconfig->getOne(array('select' => 'signswitch', 'asArray' => TRUE))['signswitch'];
        //获取需要您签署的数目
        if($this->data['signSwitch'] > 0)
            $this->data['needSignNum'] = $this->sign->getNeedSignNumbyUid($this->auth->getUid()) != 0 ? $this->sign->getNeedSignNumbyUid($this->auth->getUid()) : '';
        //获取需要你审批的变更令数目
        $this->data['needChangeNum'] = $this->change->getNeedChangeNumbyUid($this->auth->getUid()) != 0 ? $this->change->getNeedChangeNumbyUid($this->auth->getUid()) : '';
        //获取桌面版本
        $this->data['version'] = '';
        $this->getsoftware();
    }
    function getsoftware(){
        $StrJson = ($this->aconfig->getOne(array('select' => 'upgradeinfo', 'asArray' => TRUE))['upgradeinfo']);
        $upgradeinfo = json_decode($StrJson, true);
        if ($upgradeinfo) {
            $this->data['version'] = $upgradeinfo['version'];
            $this->data['download'] = $upgradeinfo['download'];
        }
    }
    /**
     *
     * @return type
     */
    public function index()
    {
        $this->data['uprofile'] = $this->profile->getProWithUid($this->auth->getUid());
        $this->data['uprofile']['email'] = $this->auth->getUemail();
        if (isset($_POST['name']) && isset($_POST['company']) && isset($_POST['jobs']) && isset($_POST['phone'])) {
            $this->profile->upProfile($this->auth->getUid(), $_POST);
            return DOO::conf()->APP_URL . 'user/profile';
        }
        $this->render('edit-profile', $this->data, TRUE);
    }
    public function avatar()
    {
        // TODO:加入目录可否写入判断
        if (count($_FILES) == 3) {
            $result = array();
            $result['success'] = false;
            $successNum = 0;
            $avatarNumber = 1;
            $i = 0;
            $msg = '';
            $dir = 'global/avatar';
            while (list($key, $val) = each($_FILES)) {
                if ($_FILES[$key]['error'] > 0) {
                    $msg .= $_FILES[$key]['error'];
                } else {
                    $fileName = date("YmdHis") . '_' . floor(microtime() * 1000) . '_' . self::createRandomCode(8);
                    if ($key == '__source') {
                        $initParams = $_POST["__initParams"];
                        $virtualPath = "$dir/php_source_$fileName.jpg";
                        $result['sourceUrl'] = '/' . $virtualPath . $initParams;
                        move_uploaded_file($_FILES[$key]["tmp_name"], Doo::conf()->SITE_PATH . $virtualPath);
                        $successNum++;
                    } else if (strpos($key, '__avatar') === 0) {
                        $virtualPath = "$dir/php_avatar" . $avatarNumber . "_$fileName.jpg";
                        $result['avatarUrls'][$i] = $virtualPath;
                        if ($i == 2) {
                            $this->profile->setAvatar($this->auth->getUid(), $result['avatarUrls'][$i]);
                        }
                        move_uploaded_file($_FILES[$key]["tmp_name"], $virtualPath);
                        $successNum++;
                        $i++;
                    }
                }
            }
            $result['msg'] = $msg;
            if ($successNum > 0) {
                $result['success'] = true;
            }
            print json_encode($result);
            die;
        }
        $this->render('edit-profile-avtra', $this->data, TRUE);
    }
    /*     * ************************************************************
     *  生成指定长度的随机码。
     *  @param int $length 随机码的长度。
     *  @access public
     * ************************************************************ */
    function createRandomCode($length)
    {
        $randomCode = "";
        $randomChars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        for ($i = 0; $i < $length; $i++) {
            $randomCode .= $randomChars{mt_rand(0, 35)};
        }
        return $randomCode;
    }
    public function proSection()
    {
        /**
         * 计量期数
         * 完成进度
         *
         */
        $this->getFav();
        $contractArray = $this->contractact->getRowByPid($this->params['pid']); // 获取合同段
        $htmlstr = NULL;
        foreach ($contractArray as $k => $v) {
            $retval = $this->actmeasure->getAllbyStid($v['stid']); // 获取标段
            $bdhtmlstr = NULL;
            $biaoduantotalmoney = 0;
            $currTotal = 0;
            $progress = $otherprogress = '0%';
            $currTotal = $this->numofperact->getCountTotalSTID($v['stid']);
            //
            foreach ($retval as $key => $value) {
                $snTotalProgress = $currTotalProgress = $lessTotalProgress = '0%';
                $numCount = $this->numofperact->getCountTotalnum($value['pmid']);
                $totalplus = $this->numofperact->getCountTotalplus($value['pmid']);
                $snTotal = $this->numofperact->getStopNow($value['pmid']); //截止目前
                $currcomTotal = $this->numofperact->getCurrTotal($value['pmid']); //截止目前
                if ($numCount > 0) {
                    $nTotalProgress = number_format($snTotal['sntotal'], 2, '.', ',');
                    $ncurrTotalProgress = number_format($currcomTotal['currtotal'], 2, '.', ',');
                    $nlessTotalProgress = number_format($totalplus['totalplus'] - $currcomTotal['currtotal'] - $snTotal['sntotal'], 2, '.', ',');
                    $snTotalProgress = round(($snTotal['sntotal'] / $totalplus['totalplus']) * 100) . '%';
                    $currTotalProgress = round(($currcomTotal['currtotal'] / $totalplus['totalplus']) * 100) . '%';
                    $lessTotalProgress = round((($totalplus['totalplus'] - $currcomTotal['currtotal'] - $snTotal['sntotal']) / $totalplus['totalplus']) * 100) . '%';
                }
                $biaoduantotalmoney += $value['contracttotal'];
                $totalmoney = number_format($value['contracttotal'], 2, '.', ',');
                $bdhtmlstr .= '
				
				    | 标段名 | 计量期数 | 总价 | 截止本期累计完成/本期完成/未完成 | 
|---|
				
				
					| ' . $value['pmname'] . ' | 共 ' . $numCount . ' 期(本期 审批中) | ¥' . $totalmoney . ' | 
						' . $snTotalProgress . ' ' . $currTotalProgress . ' ' . $lessTotalProgress . ' | 
';
            }
            if ($biaoduantotalmoney > 0 && ($currTotal['totalplus'] > 0)) {
                $nprogress = number_format($currTotal['totalplus'], 2, '.', ',');
                $notherprogress = number_format($biaoduantotalmoney - $currTotal['totalplus'], 2, '.', ',');
                $progress = round(($currTotal['totalplus'] / $biaoduantotalmoney) * 100) . '%';
                $otherprogress = round((($biaoduantotalmoney - $currTotal['totalplus']) / $biaoduantotalmoney) * 100) . '%';
                $biaoduantotalmoney = number_format($biaoduantotalmoney, 2, '.', ',');
            }
            $htmlstr .= '
			
			    
				
				    
					| ' . $v['stname'] . '(' . $v['stkey'] . ') | 总价: | ¥' . $biaoduantotalmoney . ' | 完成进度: | 
						' . $progress . ' ' . $otherprogress . ' | 
				
			    
			    
			 
		     ';
        }
        $this->data['htmlstr'] = $htmlstr;
        if (isset($_POST['secname']) && $_POST['secname']) {
            $this->contractact->insertContract($this->params['pid'], $this->auth->getUid(), $_POST['secname']);
            return Doo::conf()->APP_URL . 'project/' . $this->params['pid'] . '/section';
        }
        $this->data['pid'] = $this->params['pid'];
        $this->render('w-project-section', $this->data, TRUE);
    }
    /**
     *
     * @return type
     */
    public function sms()
    {
        if (isset($_POST['mobile']) && isset($_POST['verifycode'])) {
            $vmArray = $this->auth->getVerifyMobile();
            if (isset($vmArray) && $vmArray) {
                if (md5($_POST['mobile'] . $_POST['verifycode']) == md5($vmArray['mobile'] . $vmArray['code'])) {
                    $this->profile->updateMobile($this->auth->getUid(), $vmArray['mobile']);
                    die(json_encode(array('status'=> true)));
                }
            }
        }
        $this->data['uprofile'] = $this->profile->getProWithUid($this->auth->getUid());
        $this->data['smsNoticeSwitch'] = $this->aconfig->getOne(array('select' => 'smsswitch', 'asArray' => TRUE))['smsswitch'];
        $this->render('edit-profile-sms', $this->data, TRUE);
    }
    /**
     *
     * @return type
     */
    public function smsEdit()
    {
        if (isset($_POST['mobile']) && isset($_POST['verifycode'])) {
            $vmArray = $this->auth->getVerifyMobile();
            if (isset($vmArray) && $vmArray) {
                if (md5($_POST['mobile'] . $_POST['verifycode']) == md5($vmArray['mobile'] . $vmArray['code'])) {
                    $this->profile->updateMobile($this->auth->getUid(), $vmArray['mobile']);
                    return Doo::conf()->APP_URL . 'user/profile/sms/edit';
                }
            }
        }
        $this->data['uprofile'] = $this->profile->getProWithUid($this->auth->getUid());
        $this->render('edit-profile-sms-edit', $this->data, TRUE);
    }
    /**
     *
     * @return type
     */
    public function checkMobile()
    {
//        $this->data['uprofile']['email'] = $this->auth->getUemail();
//        if (isset($_POST['name']) && isset($_POST['company']) && isset($_POST['jobs']) && isset($_POST['phone']) && isset($_POST['mobile'])) {
//            $this->profile->upProfile($this->auth->getUid(), $_POST);
//            return DOO::conf()->APP_URL . 'user/profile';
//        }
        die(json_encode(array('mobile' => (int)$this->profile->checkMobile($_POST['mobile']))));
    }
    /**
     *
     * @return type
     */
    public function mobileVerify()
    {
//        $this->data['uprofile']['email'] = $this->auth->getUemail();
//        if (isset($_POST['name']) && isset($_POST['company']) && isset($_POST['jobs']) && isset($_POST['phone']) && isset($_POST['mobile'])) {
//            $this->profile->upProfile($this->auth->getUid(), $_POST);
//            return DOO::conf()->APP_URL . 'user/profile';
//        }
        die(json_encode(array('mobile' => (int)$this->profile->checkMobile($_POST['mobile']))));
    }
    /**
     *
     * @return type
     */
    public function smsSend()
    {
        $randNum = rand(1000, 9999);
        $this->auth->setVerifyMobile(array('mobile' => $_POST['mobile'], 'code' => $randNum));
        $res = $this->sms->sendSms($_POST['mobile'], Doo::conf()->SMS_TIPS['AUDIT_NOTICE'] . $randNum . Doo::conf()->SMS_TIPS['END_MSG']);
        die(json_encode(array('verify' => $res)));
    }
    Function fNumber($number)
    {
        if ($number == '')
            Return "-";
        $nlen = strlen($number);
        while ($nlen > 3) {
            $fNumber = "," . substr($number, $nlen - 3, 3);
            $number = substr($number, 0, -3);
            $nlen = strlen($number);
        }
        if ($nlen <= 3) {
            $fNumber = $number . $fNumber;
        }
        Return $fNumber;
    }
    private function getFav()
    {
        $proArray = $this->project->getAll();
        $this->data['othrPro'] = [];
        foreach ($proArray as $key => $value) {
            if ($value['pid'] == $this->params['pid']) {
                $this->data['currProName'] = $value['pname'];
                $this->data['currProID'] = $value['pid'];
            } else {
                $this->data['othrPro'][] = $proArray[$key];
            }
        }
    }
    public function proSectionMeasure()
    {
        //此处未做更改,JSON文件已经固定名称
        $jsonpath = pathinfo($this->attfile->getMaxRow()['filepath']);
        if (isset($jsonpath['dirname'])) {
            $extPathdir = Doo::conf()->SITE_PATH . $jsonpath['dirname'] . '/' . $jsonpath['filename'];
            if ($handle = opendir($extPathdir)) {
                while (false !== ($file = readdir($handle))) {
                    $filename = NULL;
                    $filename = pathinfo($file);
                    if ($filename['extension'] == 'json')
                        $proArray = json_decode(file_get_contents($extPathdir . '/' . $file), TRUE, JSON_UNESCAPED_UNICODE);
                }
                closedir($handle);
            }
            $this->data['proArray'] = $proArray['Bills'];
        } else {
            $this->data['proArray'] = [];
        }
        $this->data['mpid'] = $this->params['mpid'];
        $this->data['pid'] = $this->params['pid'];
        $this->render('w-project-section-measure', $this->data, TRUE);
    }
    public function substr_replace_cn($string, $repalce = '*', $start = 0, $len = 0)
    {
        $count = mb_strlen($string, 'UTF-8'); //此处传入编码,建议使用utf-8。此处编码要与下面mb_substr()所使用的一致
        if (!$count) {
            return $string;
        }
        if ($len == 0) {
            $end = $count;  //传入0则替换到最后
        } else {
            $end = $start + $len;       //传入指定长度则为开始长度+指定长度
        }
        $i = 0;
        $returnString = '';
        while ($i < $count) { //循环该字符串
            $tmpString = mb_substr($string, $i, 1, 'UTF-8'); // 与mb_strlen编码一致
            if ($start <= $i && $i < $end) {
                $returnString .= $repalce;
            } else {
                $returnString .= $tmpString;
            }
            $i++;
        }
        return $returnString;
    }
    function unicode_encode($name)
    {//to Unicode
        $name = iconv('UTF-8', 'UCS-2', $name);
        $len = strlen($name);
        $str = '';
        for ($i = 0; $i < $len - 1; $i = $i + 2) {
            $c = $name[$i];
            $c2 = $name[$i + 1];
            if (ord($c) > 0) {// 两个字节的字
                $str .= '\\' . base_convert(ord($c), 10, 16) . base_convert(ord($c2), 10, 16);
            } else {
                $str .= $c2;
            }
        }
        $str = strtoupper($str);
        return $str;
    }
    function unicode_decode($name)
    {//Unicode to
        $pattern = '/([\w]+)|(\\\u([\w]{4}))/i';
        preg_match_all($pattern, $name, $matches);
        if (!empty($matches)) {
            $name = '';
            for ($j = 0; $j < count($matches[0]); $j++) {
                $str = $matches[0][$j];
                if (strpos($str, '\\u') === 0) {
                    $code = base_convert(substr($str, 2, 2), 16, 10);
                    $code2 = base_convert(substr($str, 4), 16, 10);
                    $c = chr($code) . chr($code2);
                    $c = iconv('UCS-2', 'UTF-8', $c);
                    $name .= $c;
                } else {
                    $name .= $str;
                }
            }
        }
        return $name;
    }
    public function proDetail()
    {
        // 面包屑导航项目
        $this->data['currproArray'] = $this->data['allproArray'] = NULL;
        $allproArray = $this->project->getAll();
        foreach ($allproArray as $kk => $vv) {
            if ($vv['pid'] == $this->params['pid']) {
                $this->data['currproArray'] = $vv;
            } else {
                $this->data['allproArray'][] = $vv;
            }
        }
        // 面包屑导航合同
        $conArray = $this->contractact->getAll();
        $measureArray = $this->actmeasure->getRowByPmid($this->params['pmid']);
        if (isset($measureArray['stid'])) {
            foreach ($conArray as $kkk => $vvv) {
                if ($vvv['stid'] == $measureArray['stid']) {
                    $this->data['currconArray'] = $vvv;
                } else {
                    $this->data['allconArray'][] = $vvv;
                }
            }
        } else {
            $this->data['currconArray'] = $this->data['allconArray'] = NULL;
        }
        // 面包屑导航标段
        $this->data['curractmeasureArray'] = NULL;
        $this->data['allactmeasureArray'] = [];
        $actmeasureArray = $this->actmeasure->getAll();
        foreach ($actmeasureArray as $kkkk => $vvvv) {
            if ($vvvv['stid'] == $measureArray['stid']) {
                if (($vvvv['pmid'] == $this->params['pmid'])) {
                    $this->data['curractmeasureArray'] = $vvvv;
                } else {
                    $this->data['allactmeasureArray'][] = $vvvv;
                }
            }
        }
        // 图标
        $this->data['MeasureArray'] = $this->numofperact->getRowByPmid($this->params['pmid']);
        $measureArray = $this->actmeasure->getRowByPmid($this->params['pmid']);
        foreach ($this->data['MeasureArray'] as $key => $value) {
            if ($measureArray['contracttotal'] > ($value['currdone'] > 0)) {
                $this->data['MeasureArray'][$key]['lessTotal'] = round(($value['currdone'] / $measureArray['contracttotal']) * 100);
            } else {
                $this->data['MeasureArray'][$key]['lessTotal'] = 0;
            }
        }
        $this->data['pmid'] = $this->params['pmid'];
        $this->data['pid'] = $this->params['pid'];
        $this->render('w-project-section-detail', $this->data, TRUE);
    }
    public function welcome()
    {
//	if (!$this->auth->isLoggedIn())
//	    return Doo::conf()->APP_URL;
        if ($this->profile->getProWithUid($this->auth->getUid())['userid'])
            return Doo::conf()->APP_URL . 'project/list';
        if (isset($_POST['welform'])) {
            $profileUserArray = $_POST;
            $profileUserArray['userid'] = $this->auth->getUid();
            $this->profile->insertProfile($profileUserArray); // 无自增字段返回0
            return Doo::conf()->APP_URL . 'project/list';
        }
        $this->render('welcome', $this->data);
    }
    public function prolist()
    {
//	if (!$this->auth->isLoggedIn())
//	    return Doo::conf()->APP_URL;
//	$proArray = new stdClass();
//	$jsonpath = pathinfo($this->attfile->getMaxRow()['filepath']);
//	$extPathdir = Doo::conf()->SITE_PATH . $jsonpath['dirname'] . '/' . $jsonpath['filename'];
//	if ($handle = opendir($extPathdir)) {
//	    while (false !== ($file = readdir($handle))) {
//		$filename = pathinfo($file);
//		if ($filename['extension'] == 'json')
//		    $proArray = json_decode(file_get_contents($extPathdir . '/' . $file));
//	    }
//	    closedir($handle);
//	}
//	$this->data['proArray'] = null;
        $this->render('s-project', $this->data);
    }
// ajax提取密码名称
    public function getAjaxSection()
    {
        if (!$this->isAjax())
            return;
        echo json_encode($_POST);
    }
    public function repasswd()
    {
        if (isset($_POST['oldpasswd']) && isset($_POST['newpasswd']) && isset($_POST['renewpasswd']) && ($_POST['newpasswd'] == $_POST['renewpasswd'])) {
            $userArray = $this->user->getRowUser($this->auth->getUid());
            if ($this->ph->CheckPassword($_POST['oldpasswd'], $userArray['upass'])) {
                if ($this->user->updatePassWd($this->auth->getUid(), $_POST['newpasswd']))
                    return Doo::conf()->APP_URL . 'signout';
            }
        }
        $this->render('edit-profile-pw', $this->data, TRUE);
    }
    public function appdownload(){
        $token = $this->auth->getWebToken($this->auth->getUid());
        if($token == ''){
            exit('token不存在或已过期,请重新登录 '.DOO::conf()->APP_URL);
        }
        //加密uid
        $uid = $this->__hashids->encode($this->auth->getUid());
        $content = Doo::conf()->APP_URL.'api/app/scan/validate?user='.$uid.'&token='.$token;
        $qrcodeurl = 'global/qrcode/';
        $filepath = $qrcodeurl.'qrcode_'.$uid.'.png';
        $this->codemake($content,$filepath);
        $this->data['qrcode'] = $filepath;
        $this->render('edit-app', $this->data, TRUE);
    }
    function codemake($content,$filepath){
        $value = $content;//二维码内容
        $errorCorrectionLevel = 'H';//容错级别
        $matrixPointSize = 3;//生成图片大小
//生成二维码图片
        QRcode::png($value, $filepath, $errorCorrectionLevel, $matrixPointSize,0);
//        $logo = 'jb51.png';//准备好的logo图片
//        $QR = 'qrcode.png';//已经生成的原始二维码图
//
//        if ($logo !== FALSE) {
//            $QR = imagecreatefromstring(file_get_contents($QR));
//            $logo = imagecreatefromstring(file_get_contents($logo));
//            $QR_width = imagesx($QR);//二维码图片宽度
//            $QR_height = imagesy($QR);//二维码图片高度
//            $logo_width = imagesx($logo);//logo图片宽度
//            $logo_height = imagesy($logo);//logo图片高度
//            $logo_qr_width = $QR_width / 5;
//            $scale = $logo_width/$logo_qr_width;
//            $logo_qr_height = $logo_height/$scale;
//            $from_width = ($QR_width - $logo_qr_width) / 2;
//            //重新组合图片并调整大小
//            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
//                $logo_qr_height, $logo_width, $logo_height);
//        }
//输出图片
//        Header("Content-type: image/png");
//        ImagePng($QR);
    }
}
?>