<?php
/** App登录控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

namespace Uapp\Controller;
class LoginController extends BaseController {
	 /**
     * 登录
     */
    public function dologin(){
    	$param=I('param.');
        $param['unverify'] = 1;
        $param['is_app'] = 1;
        $verify=I('mobilecode');
        $param['verify']=$param['mobilecode'];unset($param['mobilecode']);
        $mobile=I('mobile');
        $return['status']=3;
        if(empty($mobile) || empty($verify)) {
            $return['msg']='请输入手机号或验证码！';
            $this->ajaxReturn($return);
        }
        $result=D('Login')->appcheck_login($param);
        //转化为app返回数据统一格式
        if($result['status']!=1){
            $return['msg'] = $result['info'];
            $this->ajaxReturn($return);
        }else{
            $userinfo['id'] = $result['data']['id'];
            $userinfo['username'] = $result['data']['mobile'];
            if(!empty($result['data']['nickname'])){
                $userinfo['username'] = $result['data']['nickname'];
            }elseif(!empty($result['data']['username'])){
                $userinfo['username'] = $result['data']['username'];
            }
            $userinfo['is_littleshop']=$result['data']['is_littleshop'];
            $userinfo['lshop_id']=0;
            // if($result['data']['is_littleshop']==1){
            $userinfo['lshop_id']=intval(D('Wxsmall/Login')->get_lshop_id($result['data']['id']));
            // }
            $userinfo['token'] = $result['data']['password'];
            $return['msg'] = $result['info'];
            $return['data'] = $userinfo;
            $return['status'] = 1;
            $this->set_push_param($userinfo);
        }
        $this->ajaxReturn($return);
    }
    //新加微信登录
    public function wxlogin(){
        $postdata = I("param.");
        $return = D("Login")->qqlogin('wx',$postdata,'1');
        if($return['status'] == 1){
            $return['is_littleshop']=$return['is_littleshop'];
            $return['lshop_id']=intval(D('Wxsmall/Login')->get_lshop_id($return['id']));
            $userinfo['id'] = $return['id'];
            $this->set_push_param($userinfo);
        }
        $this->ajaxReturn($return);

    }
    /**
     * 为会员追加设备参数（推送）
     */
    public function set_push_param($data){
        $registrationId=I('registrationId');//设备ID
        $systemType=I('systemType');

        if(!$registrationId||!$data)return true;
        if($systemType=='android'){
            $save['push_android_registration'] = $registrationId;
        }else{
            $save['push_ios_registration'] = $registrationId;
        }
        //极光推送
        // $save['registration_id'] = $registrationId;
        
        $is_ajpush_m = M('Member')->where($save)->count();//防止设备id重复追加
        if ($is_ajpush_m) {
            $save_d['registration_id'] = '';
            foreach ($is_ajpush_m as $k => $v) {
                M('Member')->where('id=' . $v['id'])->save($save_d);
            }
        }
        M('Member')->where('id=' . $data['id'])->save($save);
    }
    /**
     * 更新或新增登录信息
     */
    public function setshopid(){
        $param = I('param.');
        $result = D('Wxsmall/Login')->setshopid($param);
        $this->ajaxReturn($result);
    }
}