Blame view

yohobuy/m.yohobuy.com/application/modules/Passport/controllers/Reg.php 9.46 KB
hf authored
1
<?php
hf authored
2
xuqi authored
3
use Action\AbstractAction;
hf authored
4 5
use LibModels\Wap\Passport\RegData;
use Plugin\Helpers;
hf authored
6
hf authored
7 8 9
/**
 * 注册的控制器
 */
xuqi authored
10 11
class RegController extends AbstractAction
{
hf authored
12
hf authored
13 14 15
    /**
     * 注册页
     */
xuqi authored
16 17
    public function indexAction()
    {
18 19 20
        // 设置注册有效时间30分钟, 防机器刷
        $this->setSession('_REG_EXPIRE', time() + 1800);
hf authored
21
        $this->setTitle('注册');
hf authored
22 23

        $data = array();
24
        $data['regIndex'] = true; // 模板中使用JS的标识
25
        $data['backUrl'] = 'javascript:history.go(-1)'; // 返回的URL链接
26 27 28 29
        $data['headerText'] = '注册'; // 头部信息
        $data['isPassportPage'] = true; // 模板中模块标识
        $data['areaCode'] = '+86'; // 默认的区号
        $data['countrys'] = RegData::getAreasData(); // 地区信息列表
hf authored
30
31 32 33 34
        $refer = $this->get('refer');
        if (!empty($refer)) {
            $this->setCookie('refer', $refer);
        }
35 36

        // 生成HTML(reg.html)
37
        //$this->_view->html('reg');
38
        // 渲染模板
xuqi authored
39 40 41
        $this->_view->display('index', $data);
    }
hf authored
42 43 44
    /**
     * 验证码
     * 
hf authored
45 46
     * @param string areaCode 地区编号
     * @param string phoneNum 手机号
hf authored
47 48
     * @param string token 访问TOKEN凭证
     */
xuqi authored
49 50
    public function codeAction()
    {
hf authored
51
        $token = $this->get('token');
hf authored
52 53
        $mobile = $this->get('phoneNum');
        $area = $this->get('areaCode', '86');
hf authored
54
hf authored
55 56 57 58
        // 判断是否允许访问, 不允许则跳转到错误页面
        if (!is_string($token) || !is_numeric($mobile) || !Helpers::verifyToken($mobile, $token)) {
            $this->error();
        }
hf authored
59
hf authored
60
        $this->setTitle('注册-验证码');
hf authored
61
62 63
        $data = array(
            'regCode' => true, // 模板中使用JS的标识
64
            'backUrl' => SITE_MAIN . '/?go=1', // 返回的URL链接
65 66 67 68 69
            'headerText' => '注册', // 头部信息
            'isPassportPage' => true, // 模板中模块标识
            'areaCode' => '+' . $area, // 地区编号
            'phoneNum' => $mobile, // 手机号
            'token' => $token, // 访问令牌
hf authored
70 71
        );
xuqi authored
72 73 74
        $this->_view->display('code', $data);
    }
hf authored
75 76 77
    /**
     * 填写密码页面
     * 
hf authored
78 79
     * @param string areaCode 地区编号
     * @param string phoneNum 手机号
hf authored
80 81
     * @param string token 访问TOKEN凭证
     */
xuqi authored
82 83
    public function passwordAction()
    {
hf authored
84
        $token = $this->get('token');
hf authored
85 86
        $mobile = $this->get('phoneNum');
        $area = $this->get('areaCode', '86');
hf authored
87
hf authored
88
        // 判断是否允许访问, 不允许则跳转到错误页面
89
        if (!is_string($token) || !is_numeric($mobile) || !is_numeric($area) || !Helpers::verifyToken($mobile, $token)) {
hf authored
90 91
            $this->error();
        }
hf authored
92
hf authored
93
        $this->setTitle('注册-设置密码');
hf authored
94
95 96 97 98 99 100 101 102 103
        $data = array(
            'regPwd' => true, // 模板中使用JS的标识
            'backUrl' => '/', // 返回的URL链接
            'headerText' => '注册', // 头部信息
            'isPassportPage' => true, // 模板中模块标识
            'areaCode' => $area, // 地区编号
            'phoneNum' => $mobile, // 手机号
            'token' => $token, // 访问令牌
        );
xuqi authored
104 105 106

        $this->_view->display('password', $data);
    }
hf authored
107
hf authored
108 109 110
    /**
     * 验证注册的手机号
     * 
hf authored
111 112
     * @param string areaCode 地区编号,注意不需要+号
     * @param string phoneNum 手机号
hf authored
113 114 115 116
     * @return json
     */
    public function verifymobileAction()
    {
hf authored
117
        $data = array('code' => 400, 'message' => '手机号已存在', 'data' => '');
hf authored
118
hf authored
119 120 121 122 123
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
hf authored
124
hf authored
125 126
            $mobile = $this->post('phoneNum');
            $area = $this->post('areaCode', '86');
hf authored
127
            /* 判断参数是否合法 */
hf authored
128
            if (!is_numeric($mobile) || !is_numeric($area)) {
hf authored
129 130
                break;
            }
hf authored
131
132 133 134 135 136 137
            /* 设置注册有效时间30分钟, 防机器刷 */
            $expire = $this->getSession('_REG_EXPIRE');
            if (empty($expire) || $expire < time()) {
                break;
            }
hf authored
138
            /* 向手机发送注册验证码 */
139
            $data = RegData::sendCodeToMobile($area, $mobile);
hf authored
140 141 142
            if (!isset($data['code'])) {
                break;
            }
hf authored
143 144

            /* 返回跳转到验证页面的链接 */
hf authored
145
            if ($data['code'] == 200) {
hf authored
146
                $token = Helpers::makeToken($mobile);
147
                $data['data'] = Helpers::url('/passport/reg/code', array('token' => $token, 'phoneNum' => $mobile, 'areaCode' => $area));
hf authored
148 149 150
            }
        } while (false);
hf authored
151
        $this->echoJson($data);
hf authored
152
    }
hf authored
153
hf authored
154 155 156
    /**
     * 验证注册的识别码
     * 
hf authored
157 158
     * @param string areaCode 地区编号,注意不需要+号
     * @param string phoneNum 手机号
hf authored
159 160 161 162 163 164
     * @param string token 访问TOKEN凭证
     * @param int code 验证码, 手机上收到的
     * @return json
     */
    public function verifycodeAction()
    {
xuqi authored
165
        $data = array('code' => 400, 'message' => '验证码错误', 'data' => '');
hf authored
166
hf authored
167 168 169 170 171
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
hf authored
172
hf authored
173 174
            $mobile = $this->post('phoneNum');
            $area = $this->post('areaCode');
hf authored
175 176
            $code = $this->post('code');
            /* 判断参数是否合法 */
hf authored
177
            if (!is_numeric($mobile) || !is_numeric($area) || !isset($code)) {
hf authored
178 179
                break;
            }
hf authored
180
181 182 183 184 185 186
            /* 设置注册有效时间30分钟, 防机器刷 */
            $expire = $this->getSession('_REG_EXPIRE');
            if (empty($expire) || $expire < time()) {
                break;
            }
hf authored
187 188 189 190 191
            /* 验证注册的标识码是否有效 */
            $data = RegData::validMobileCode($area, $mobile, $code);
            if (!isset($data['code'])) {
                break;
            }
hf authored
192 193

            /* 返回跳转到设置密码的链接 */
hf authored
194
            if ($data['code'] == 200) {
hf authored
195
                $token = Helpers::makeToken($mobile);
196
                $data['data'] = Helpers::url('/passport/reg/password', array('token' => $token, 'phoneNum' => $mobile, 'areaCode' => $area));
xuqi authored
197 198
            } else if ($data['code'] == 404) {
                $data['message'] = '验证码错误'; //统一验证提示
hf authored
199 200 201
            }
        } while (false);
hf authored
202 203
        $this->echoJson($data);
    }
hf authored
204
hf authored
205 206 207
    /**
     * 发送验证码
     * 
hf authored
208 209
     * @param string areaCode 地区编号,注意不需要+号
     * @param string phoneNum 手机号
hf authored
210 211 212 213
     * @return json
     */
    public function sendcodeAction()
    {
214
        $data = array('code' => 400, 'message' => '发送验证码失败', 'data' => '');
hf authored
215
hf authored
216 217 218 219 220
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
hf authored
221
hf authored
222 223
            $mobile = $this->post('phoneNum');
            $area = $this->post('areaCode', '86');
hf authored
224 225 226 227
            /* 判断参数是否合法 */
            if (!is_numeric($mobile) || !is_numeric($area)) {
                break;
            }
hf authored
228
229 230 231 232 233 234
            /* 设置注册有效时间30分钟, 防机器刷 */
            $expire = $this->getSession('_REG_EXPIRE');
            if (empty($expire) || $expire < time()) {
                break;
            }
hf authored
235 236 237 238 239 240 241 242
            /* 向手机发送注册验证码 */
            $data = RegData::sendCodeToMobile($area, $mobile);
            if (!isset($data['code'])) {
                break;
            }
        } while (false);

        $this->echoJson($data);
hf authored
243
    }
hf authored
244
hf authored
245 246 247
    /**
     * 设置密码
     * 
hf authored
248 249
     * @param string areaCode 地区编号,注意不需要+号
     * @param string phoneNum 手机号
hf authored
250 251 252 253 254 255
     * @param string token 访问TOKEN凭证
     * @param string password 用户设置的密码
     * @return json
     */
    public function setpasswordAction()
    {
hf authored
256
        $data = array('code' => 400, 'message' => '密码格式不正确', 'data' => '');
hf authored
257
hf authored
258 259 260 261 262
        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }
hf authored
263
hf authored
264
            $token = $this->post('token');
hf authored
265 266
            $mobile = $this->post('phoneNum');
            $area = $this->post('areaCode');
hf authored
267 268 269 270 271
            $password = $this->post('password');
            /* 判断参数是否合法 */
            if (!is_string($token) || !is_numeric($mobile) || !is_numeric($area) || !isset($password)) {
                break;
            }
hf authored
272
hf authored
273 274 275 276
            /* 判断是否允许访问 */
            if (!Helpers::verifyToken($mobile, $token)) {
                break;
            }
277 278 279 280 281

            /* 判断密码是否符合规则 */
            if (!Helpers::verifyPassword($password)) {
                break;
            }
hf authored
282
hf authored
283 284
            /* 验证注册的标识码是否有效 */
            $data = RegData::regMobile($area, $mobile, $password);
285
            if (!isset($data['code']) || $data['code'] != 200) {
hf authored
286 287
                break;
            }
hf authored
288
hf authored
289
            /* 返回跳转到来源页面 */
290 291 292 293 294
            $refer = $this->getCookie('refer');
            if (empty($refer)) {
                $refer = SITE_MAIN . '/?go=1';
            } else {
                $refer = rawurldecode($refer);
hf authored
295
            }
296 297
            $data['data']['session'] = Helpers::syncUserSession($data['data']['uid']);
            $data['data']['href'] = $refer;
298
299 300 301
            $token = Helpers::makeToken($data['data']['uid']);
            $this->setCookie('_TOKEN', $token);
            $this->setSession('_TOKEN', $token);
302
            $this->setSession('_LOGIN_UID', $data['data']['uid']);
hf authored
303 304
        } while (false);
hf authored
305
        $this->echoJson($data);
hf authored
306
    }
hf authored
307 308

}