bin.shen
2016-12-05 a4c9331bbfe3e8765ccdc1c54cc6931bac49cc82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
if (! defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * 扩展业务控制器   
 */
 
//define('AMQP_DEBUG', true);
require_once __ROOT__ . '/vendor/autoload.php';
 
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
 
class MY_Controller extends CI_Controller
{
    /**
     * MY_Controller constructor.
     */
    public function __construct ()
    {
        parent::__construct();
        date_default_timezone_set('PRC');
        $this->cismarty->assign('base_url',base_url());//url路径
//$this->session->sess_destroy();die;
        $this->load->config('wxpay_config');
        $appid = $this->config->item('appid');
        $secret = $this->config->item('appsecret');
 
 
        if(!$openid = $this->session->userdata('openid')){
            if(empty($_GET['code'])){
                $url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
                $url = urlencode($url);
                redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$url}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
            }else{
                $j_access_token = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$_GET['code']}&grant_type=authorization_code");
                $a_access_token = json_decode($j_access_token,true);
                $openid = $a_access_token["openid"];
                $this->send_open_id($openid);
 
                //获取用户基本信息
                $rs = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}");
                $rs = json_decode($rs,true);
                $access_token = $rs['access_token'];
                $rs = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$openid}&lang=zh_CN");
                $rs = json_decode($rs,true);
 
                $this->sysconfig_model->check_openid($openid);
                if($rs['subscribe'] != 1){
                    $userID = $_GET['userID'] ? $_GET['userID'] : '';
                    $r_url = 'api/get_or_create_ticket/'.$userID;
                    redirect(site_url($r_url));
                    exit();
                }
                $this->session->set_userdata('openid', $openid);
                $this->session->set_userdata('headimgurl', $rs['headimgurl']);
                $this->session->set_userdata('nickname', $rs['nickname']);
            }
        } else {
            $this->send_open_id($openid);
        }
    }
 
    private function send_open_id($openid) {
        if(!$openid) return;
 
        $connection = new AMQPStreamConnection('121.40.92.176', 5672, 'guest', 'guest');
        $channel = $connection->channel();
        $channel->exchange_declare('open_id', 'fanout', false, false, false);
        $msg = new AMQPMessage($openid);
        $channel->basic_publish($msg, 'open_id');
        $channel->close();
        $connection->close();
    }
 
//
//    public function check_openid($openid){
//        $user_info = $this->db2->select()->from('user_info')->where('openid',$openid)->get()->row_array();
//
//        $rs = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}");
//        $rs = json_decode($rs,true);
//        $access_token = $rs['access_token'];
//        $rs = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$openid}&lang=zh_CN");
//        $rs = json_decode($rs,true);
//
//        if($rs['subscribe'] < 1){
//            return -1;
//        }
//        $this->session->set_userdata('headimgurl', $rs['headimgurl']);
//        if($user_info){
//            $this->session->set_userdata('username',$user_info['name']);
//        }else{
//
//            $data = array(
//                'name'=>$rs['nickname'],
//                'sex'=>$rs['sex'],
//                'openid'=>$openid
//            );
//            $this->db2->insert('user_info',$data);
//
//            $this->session->set_userdata('username',$rs['nickname']);
//        }
//        return 1;
//    }
 
 
 
    //重载smarty方法assign
    public function assign($key,$val) {
        $this->cismarty->assign($key,$val);
    }  
 
    
    //重载smarty方法display
    public function display($html) {
        $this->cismarty->display($html);  
    }
 
    
    /**
     * 树状结构菜单
     **/
    public function subtree($arr,$id=0,$lev=1)
    {
        static $subs = array();
        foreach($arr as $v){
            if((int)$v['parent_id']==$id){
                $v['lev'] = $lev;
                $subs[]=$v;
                $this->subtree($arr,$v['id'],$lev+1);
            }
        }
        return $subs;
    }
    
    /**
     * 获取页码列表
     * 例如<上一页>...56789<下一页>
     * @param int $total 总页数
     * @param int $current 当前页
     * @param int $page_list_size 显示页码个数
     * @return array 显示页码的数组
     **/
    public function get_page_list($total,$current,$page_list_size = '7')
    {
        $page= array();
        if($total<$page_list_size){
            for($i=1;$i<=$total;$i++){
                $page[]=$i;
            }
        }else{
            if($current <= ceil($page_list_size/2)){
            //当前页小于居中页码,则正常打印
                for($i=1;$i<=$page_list_size;$i++){
                    $page[]=$i;
                }
                
            }else if($current > ($total - ceil($page_list_size/2))){
            //最后几页正常打印
                for($i=0;$i<$page_list_size;$i++){
                    $page[]=$total-$i;
                }
                $page = array_reverse($page);
            }else{
                for($i=$current-floor($page_list_size/2);$i<=$current+floor($page_list_size/2);$i++){
                    $page[]=$i;
                }
            }
        }
        //return array_reverse($page);
        return $page;
    }
 
    public function buildWxData(){
        $signPackage = $this->wxjssdk->wxgetSignPackage();
        //变量
        $this->cismarty->assign('wxappId',$signPackage["appId"]);
        $this->cismarty->assign('wxtimestamp',$signPackage["timestamp"]);
        $this->cismarty->assign('wxnonceStr',$signPackage["nonceStr"]);
        $this->cismarty->assign('wxsignature',$signPackage["signature"]);
    }
 
    private function wxcreateNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
}
 
 
/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */