<?php

/* ========================================================================
 * $Id: register.php 3659 2017-03-22 09:31:18Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
define('CUR_URL',onez('store')->href('/store/store.php',0));
$action=onez()->gp('action');
if($action=='install'){
  onez()->location(onez('store')->href('/store/store.php?'.http_build_query($_GET)));
}
$goto=(int)onez()->gp('goto');
$market=(int)onez()->gp('market');
$G['title']='请注册您的站点';

#初始化表单
$form=onez('admin')->widget('form')
  ->set('title',$G['title'])
  ->set('values',$G['this']->option())
;

#创建表单项
$form->add(array('type'=>'hidden','key'=>'action','value'=>'save'));


$form->add(array('label'=>'友情提示','type'=>'html','html'=>'<code>首次使用，请在佳蓝云引擎注册您的站点</code>'));
$form->add(array('label'=>'云引擎APP ID','type'=>'text','key'=>'onez_appid','hint'=>'','notempty'=>'云引擎APP ID不能为空'));
$form->add(array('label'=>'云引擎APP KEY','type'=>'text','key'=>'onez_appkey','hint'=>'','notempty'=>'云引擎APP KEY不能为空'));
$form->add(array('label'=>'请使用微信扫描以下二维码获取','type'=>'html','html'=>''));


#处理提交
if($onez=$form->submit()){
  $post=array();
  $post['appid']=$appid=onez()->gp('onez_appid');
  $appkey=onez()->gp('onez_appkey');
  //$post['appkey']=onez()->gp('appkey');
  $post['sitetoken']=$sitetoken=$G['this']->token;
  $time=time();
  $post['time']=$time;
  $post['md5']=md5("$appid&$sitetoken&$time".md5($appkey));
  $data=onez()->post('http://onez.vip.onezphp.com/index.php?action=register',http_build_query($post));
  $json=json_decode($data,1);
  if(!$json['sitekey']){
    exit($data);
  }
  $G['this']->option_set(array(
    'onez_appid'=>$appid,
    'onez_appkey'=>$appkey,
    'onez_sitetoken'=>$sitetoken,
    'onez_sitekey'=>$json['sitekey'],
  ));
  $G['this']->set('onez_appid',$appid);
  $G['this']->set('onez_appkey',$appkey);
  $G['this']->set('onez_sitetoken',$sitetoken);
  $G['this']->set('onez_sitekey',$json['sitekey']);
  if($goto){
    onez()->ok('注册成功',$goto);
  }else{
    onez()->ok('注册成功',onez('store')->shopurl($market));
  }
}
onez('admin')->header();
?>
<style>
.addok{
  color:green;
}
</style>
<section class="content-header">
  <h1>
    <?php echo $G['title']?><small></small>
  </h1>
  <ol class="breadcrumb">
    <li>
      <a href="<?php echo onez()->href('/')?>">
        <i class="fa fa-dashboard">
        </i>
        管理首页
      </a>
    </li>
    <li class="active">
      <?php echo $G['title'];?>
    </li>
  </ol>
</section>
<section class="content">
  <form id="form-common" method="post">
    <div class="box box-info">
      <div class="box-header with-border">
        <h3 class="box-title">
          <?php echo $G['title']?>
        </h3>
        <div class="box-tools pull-right">
        </div>
      </div>
      <div class="box-body">
        <?php echo $form->code();?>
        <?php onez('qrcode.doit')->show('http://onez.vip.onezphp.com/index.php?action=appid&t='.uniqid(),'_get_appid')?>
      </div>
      <div class="box-footer clearfix tipbox">
        <button type="submit" class="btn btn-primary">
          确定
        </button>
      </div>
    </div>
  </form>
</section>
<script type="text/javascript">
function _cleartip(){
  $('.mytip').hide('slow');
  $('.addok').removeClass('addok');
}
function _get_appid(o){
  $('#input-onez_appid').val(o.appid).addClass('addok');
  $('#input-onez_appkey').val(o.appkey).addClass('addok');
  $('.mytip').remove();
  $('<span class="mytip" style="color:#ff0000">获取成功，已自动为您填写！</span>').show('fast').appendTo('.tipbox');
  setTimeout('_cleartip()',3000);
}
</script>
<?php
echo $form->js();
onez('admin')->footer();
?>