<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class MobileAction extends MemberAction
{
    public function run()
    {
        if (isset($_POST['oldmobile']))
        {
            $iorange = $this->iorange();
            $oldMobile = S::gp('oldmobile','P');
            $mobile1 = S::gp('mobile1','P');
            $mobile2 = S::gp('mobile2','P');
            
            $nowMobile = $this->db()->getValue('SELECT mobile FROM '.Members::table().' WHERE uid='.$this->uid());
            if (empty($nowMobile))
            {
                throw new iSystemException('未知错误，请联系管理员');
            }
            
            if ($oldMobile != $nowMobile)
            {
                throw new iSystemException('当前使用的手机号不正确，请重新输入');
            }
            if ($mobile1 != $mobile2)
            {
                throw new iSystemException('两次输入的新手机号不一致，请返回重新输入');
            }
            if (empty($mobile1) || $mobile1 < 1300000000 || $mobile1 > 19000000000)
            {
                throw new iSystemException('请输入正确的新手机号');
            }
            $check = $this->db()->getValue('SELECT COUNT(*) FROM '.Members::table().' WHERE mobile='.S::sqlEscape($mobile1));
            if ($check)
            {
                throw new iSystemException('您输入的新手机号已经被注册');
            }
            
            try
            {
                Members::single()->hasMoney($this->uid(), 1000, $this->userName());
            }
            catch (NotEnoughMoneyException $exce)
            {
                throw new iSystemException($exce->getMessage());
            }
            
            $this->db()->update(Members::table())->value(array('groupid'=>8,'verify'=>0,'mobile'=>$mobile1))->where('uid='.$this->uid())->q();
            iCredits::single($this->uid(), $this->userName())->consume(1000);
            $message[] = '手机号修改成功';
            $message[] = memberLink('返回书房').'.'.ilink('重新验证',url('verify'));
            $this->showMessage($message);
        }
        $this->getController()->render('mobile');
    }
}