<?php
/** 店铺登录
 * @Copyright (C) 2018 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author dongziyi <2066207038@qq.com>
 */

namespace Pad\Model;
use Think\Model;
class ShopsRoleAuthModel extends Model{
    /**
     * 登录高级权限账号授权
     */
    public function auth_login($param){
        $return=array('status'=>3,'msg'=>'此账号没有权限');
        if(empty($param['username']) || empty($param['password']) || empty($param['authcode'])|| empty($param['login_name'])){
            $return['msg']="参数错误";
            return $return;
        }
        $map=array();
        $map['login_name']=$param['username'];
        $shopinfo=M("ShopsAccount")->where($map)->field(true)->find();
        if(empty($shopinfo)){
            $return['msg']="该账号不存在";
            return $return;
        }
        $postpassword = creatpwd($param['password'],'HANCHAO'.$shopinfo['salt']);
        if($postpassword!=$shopinfo['password']){
            $return['msg']="密码错误";
            return $return;
        }
        $role_id=idtoname($shopinfo['id'],"ShopsRoleAccount",'role_id','account_id',false);
        $map=array();
        $map['role_id']=$role_id;
        $auth_role=M("ShopsRoleAuth")->where($map)->cache(true)->getField('action',true);
        $is_role=false;
        if(in_array($param['authcode'],$auth_role)){
            $is_role=true;
        }elseif(strstr($param['authcode'],'_')){
            $auth_code=explode('_',$param['authcode']);
            $auth_code=$auth_code[0]."_allrule";
            if(in_array($auth_code,$auth_role)) {
                $is_role=true;
            }
        }
        if($is_role){
            $res = $this->save_auth_token($param, $auth_role);
            if($res['status']==1){
                $return['status']=1;
                $return['data']['app_authtoken'] = $res['app_authtoken'];
                $return['data']['authcode'] = $res['authcode'];
            }
        }
        return $return;
    }
    /**
     * 存储授权信息
     * @param $param 参数
     * @param $auth_role 授权码
     */
    private function save_auth_token($param,$auth_role){
        $return=array("status"=>3);
        if(empty($param)|| empty($auth_role)){
            $return['msg']="参数错误";
            return $return;
        }
            $data = array();
            $time = NOW_TIME+10*60;
            $key='S4DFA4FASR9SD4FS6ADSP';
            $data['app_token'] = SHA1($param['authcode'] . $key . $time);
            $data['app_token_time'] = $time;
            $map = array();
            $map['login_name'] = $param['login_name'];
            $res = M("ShopsAccount")->where($map)->save($data);
            if ($res) {
                $return['status'] = 1;
                $return['app_authtoken'] = $data['app_token'].".".$time;
                $return['authcode'] = $param['authcode'];
                $return['time'] = $time;
                return $return;
            } else {
                $return['msg'] = "保存失败";
                return $return;
            }
    }

    /**
     * 服务端权限验签
     * @param $param=array('sid'=>"门店子账号id（ShopsAccount的id）",'code'=>'权限标识','app_authKey'=>'客户端权限缓存')
     * @return array
     */
    public function check_auth_code($auth_code,$param=''){
        $return=array("status"=>4,'data'=>$param['auth_code']);
        //临时权限判断（以后去掉）
        if(empty($param['auth_code'])){$return['status']=1;return $return;}
        
        if(empty($param))$param=I('param.');
        if($param['is_root']==1){
            $return['status']=1;
            return $return;
        }
        if(empty($auth_code) || empty($param) || empty($param['auth_code']) || empty($param['auth_appkey']) || $auth_code!=$param['auth_code']){
            $return['msg']="参数错误";
            $return['status']=3;
            return $return;
        }
        if($param['is_root']!=1 && empty($param['sid'])){
            $return['msg']="参数错误";
            $return['status']=3;
        }
        $time=NOW_TIME;
        //应用权限判断
        if(strstr($param['auth_code'],'main')){
            $main_code=explode('-',$param['auth_code']);
            if($main_code){
                if(strstr($main_code[1],'_')){
                    $use_code=explode('_',$main_code[1]);
                    $map=array();
                    $map['app_mark']=$use_code[0];
                    $map['end_time']=array("gt",$time);
                    $map['status']=1;
                    $count=M("SiteApp")->where($map)->count();
                    if($count>0){
                        $return['status']=1;
                        return $return;
                    }
                }
            }
        }
    //门店权限判断
       $map=array();
        $map['a.account_id']=$param['sid'];
        $join=" LEFT JOIN __SHOPS_ACCOUNT__ sa ON sa.id=a.account_id";
        $role_id=M("ShopsRoleAccount")->alias('a')->where($map)->join($join)->getField('a.role_id');
        //$role_id=idtoname($param['sid'],"ShopsRoleAccount",'role_id','account_id',false);
        $map=array();
        $authallrule='';
        if(str_exists($param['auth_code'],'_')){
            $mainstrs=array();
            $mainstrs=explode('_',$param['auth_code']);
            if($mainstrs[0])$authallrule=$mainstrs[0].'_allrule';
        }
        if($authallrule) {
            $map['action|action'] = array($param['auth_code'], $authallrule, '_multi' => true);
        }else{
            $map['action|action'] = array($param['auth_code'], $param['auth_code'].'_allrule', '_multi' => true);
        }
        $map['role_id']=$role_id;
        $count=M("ShopsRoleAuth")->where($map)->count();
        if($count>0){
            $return['status']=1;
            return $return;
        }
        $app_key=explode('.',$param['auth_appkey']);
        $key='S4DFA4FASR9SD4FS6ADSP';
        $app_authKey = SHA1($param['auth_code'] . $key . $app_key[1]);
        if($app_authKey!= $app_key[0] || $time>$app_key[1]){
            $return['msg']="此账号没有权限";
            return $return;
        }else {
            if($param['is_root']==1){
                $admininfo=idtoname($param['root_id'],'Admin','password');
                if(empty($admininfo) || $admininfo!=$param['root_token']){
                    $return['msg']="此账号没有权限";
                    return $return;
                }
            }
            //启用 单个授权模式
           /*$map = array();
            $map['id'] = $param['sid'];
            $shopinfo = M("ShopsAccount")->where($map)->field(true)->find();
            if ($app_authKey != $shopinfo['app_token'] || $time > $shopinfo['app_token_time']) {
                $return['msg'] = "此账号没有权限";
                return $return;
            }*/
        }
        $return['status']=1;
        return $return;
    }
}
?>