var tmpl=`
    <el-button round v-if="!isLogin"  :type="type" ><i class="fa fa-hand-pointer-o"></i><a :href="opath" style=" text-decoration:none;color:#666"><slot>OAUTH授权登录</slot></a></el-button>
`
module.exports=
`{
   template:'${tmpl}',
   props:['type',"appkey","subfix"],
   data:function(){
     return {
       isLogin:false,
       opath:"",
     };
   },
   mounted:function(){
      this.$nextTick(()=>{
        this.checkLogin();
      });
   },
   created:function(){

   },
   methods:{
       getHashParameter(key) {
        var hrefurl=location.href;
        if(window.location.href.indexOf("from=singlemessage#")!=-1){
          hrefurl=hrefurl.replace("from=singlemessage#","").replace("mobile?","mobile#");
        }
        if(window.location.href.indexOf("from=singlemessage&isappinstalled=0#")!=-1){
          hrefurl=hrefurl.replace("from=singlemessage&isappinstalled=0#","").replace("mobile?","mobile#");
        }
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
        var index=hrefurl.indexOf("?");
        if(index!=-1){
           var searchStr=hrefurl.substr(index);
           var r = searchStr.substr(1).match(reg);
           if (r != null) {
             var x = unescape(r[2]);
             return decodeURI(x);
           } else {
             return null;
           }
         }
      },
     checkLogin(){
         var self=this;
         this.$root.getReq("/web/userCtl/checkLogin",{}).then(function(d){
           /*检查之前先缓存请求字符串*/
           var f=self.getHashParameter("from");
           var c=self.getHashParameter("code");
           var q={from:f,code:c};
           if(q && q.from){
             localStorage.setItem("query",JSON.stringify(q));
           }
           if(d && d.status==0){
             self.$store.state.currentUser=d.data;
             self.isLogin=true;

             var qtmp=localStorage.getItem("query");
             if(qtmp!=""){
                qtmp=JSON.parse(qtmp);
             }else{
                qtmp=null;
             }
             self.$emit("logined",qtmp);
             localStorage.removeItem("query");
           }else{
             self.$store.state.currentUser=null;
             self.isLogin=false;
           }
         }).then(()=>{
            if(!self.isLogin){
              self.fetchOAuthPath();
            }
         });
     },
     fetchOAuthPath(){
         var self =this;
         this.$root.postReq("/web/wxCtl/fetchOAuthPath",{apk:this.appkey?this.appkey:"",subfix:this.subfix?this.subfix:""}).then(function(d){
           if(d && d.status==0){
             self.opath=d.data;
             console.log("11111111111111111111111111111111111111111111");
             console.log(self.opath);
           }
         });
     },
   },
   vname:"gsb-wxoauth"
}`
