<?php

Doo::loadController('base', true);
Doo::loadLibrary('formConnect');

# 会员

class users extends base{
    
    public $tid = 34;
    public $name = 'users';
    
    public function index(){               
        $this->renderc($this->name.'/index', $this->data);
    }
    
    public function create(){
        $this->renderc($this->name.'/create', $this->data);
    }
    
    public function createAJAX(){
        if ($this->http_ajax_post('create'.$this->name)){
            $formConnect = new formConnect();
            $formConnect->base = $this;
            echo $formConnect->post($_POST);
            return;
        }
        echo '<越狱>非法请求';
    }
    
    public function edit(){
        $this->data['id'] = intval($this->request->get('id', 'int'));
        $this->renderc($this->name.'/edit', $this->data);
    }
    
    public function editAJAX(){
        if ($this->http_ajax_post('edit'.$this->name)){
            $formConnect = new formConnect();
            $formConnect->base = $this;
            echo $formConnect->post($_POST);
            return;
        }
        echo '<越狱>非法请求';
    }
    
    public function delete(){
        if ($this->http_ajax_post('delete'.$this->name)){
            $formConnect = new formConnect();
            $formConnect->base = $this;
            $id = intval($this->request->getPost('id', 'int'));
            echo $formConnect->delete($id, $this->tid);
            return;            
        }
        echo '<越狱>非法请求';
    }
    
    public function getWxUser(){
        Doo::loadLibrary('Wechat');
        $id = intval($this->request->getPost('id', 'int'));
        $wxappId = service::get_option('wxappId');
        $wxappSecret = service::get_option('wxappSecret');
        $wxtoken = service::get_option('wxtoken');
        $wxAESKey = service::get_option('wxAESKey');
        $wx = new Wechat(array(
            'token' => $wxtoken,
            'encodingaeskey' => $wxAESKey,
            'appid' => $wxappId,
            'appsecret' => $wxappSecret
        ));
        Doo::loadModel('ShopUsers');
        $SU = new ShopUsers();
        $SU->id = $id;
        $db = $SU->getOne();
        if ($db->subscribe){
            $user = $wx->getUserInfo($db->openid);
            $SU->dateline = time();
            $SU->province = $user['province'];
            $SU->city = $user['city'];
            $SU->sex = $user['sex'];
            $SU->headimgurl = $user['headimgurl'];
            if (empty($db->nickname)){
                $SU->nickname = $user['nickname'];
            }
            $SU->update();
            print_r($user);
        }else{
            echo '对方未关注公众号';
        }
    }
    
}