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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/**
 * Created by PhpStorm.
 * User: yaobin
 * Date: 16/8/18
 * Time: 13:25
 */
 
/**
 * 接口控制器
 *
 * @package        app
 * @subpackage    core
 * @category    controller
 * @author        yaobin<645894453@qq.com>
 *
 */
class Api extends CI_Controller {
 
    public function __construct() {
        parent::__construct();
    }
 
    public function get_pro_byid($id){
        $project = $this->sysconfig_model->chenck_pro($id);
        echo $project;
    }
 
//    public function authorize() {
//        $open_id = $this->session->userdata('openid');
//        if(empty($open_id)) {
//            if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
//                $code = $_GET['code'];
//                if(empty($code)){
//                    $url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
//                    redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APP_ID."&redirect_uri=".urlencode($url)."&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
//                } else {
//                    $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.APP_ID.'&secret='.APP_SECRET.'&code='.$code.'&grant_type=authorization_code';
//                    $result = file_get_contents($url);
//                    $jsonInfo = json_decode($result, true);
//                    $open_id = $jsonInfo['openid'];
//                    if(!empty($open_id)) {
//                        $this->session->set_userdata('openid', $open_id);
//                    }
//                }
//            }
//        }
//        $uri = "http://www.funmall.com.cn/b_house/index/";
//        if(!empty($open_id)) {
//            //file_get_contents('http://www.funmall.com.cn/api/update_weixin_user/' . $open_id);
//            $uri .= $open_id . '/';
//            $funmallDB = $this->load->database("funmall", True);
//            $funmallDB->from('wx_user');
//            $funmallDB->where('open_id', $open_id);
//            $funmallDB->order_by('updated DESC');
//            $wxUser = $funmallDB->get()->row_array();
//            if(!empty($wxUser)) {
//                $uri .= $wxUser['broker_id'] . '/';
//            }
//        }
//        redirect($uri);
//    }
 
    public function view_art($broker_id) {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            $code = $_GET['code'];
            if(empty($code)){
                $url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
                redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APP_ID."&redirect_uri=".urlencode($url)."&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
            } else {
                $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.APP_ID.'&secret='.APP_SECRET.'&code='.$code.'&grant_type=authorization_code';
                $result = file_get_contents($url);
                $jsonInfo = json_decode($result, true);
                $open_id = $jsonInfo['openid'];
 
                $this->funmall_model->bindBroker($open_id, $broker_id);
                file_get_contents('http://www.funmall.com.cn/api/update_weixin_user/' . $open_id);
 
                $uri = "http://www.funmall.com.cn/api/view_art/" . $open_id . "/" . $broker_id;
                redirect($uri);
            }
        }
    }
 
    public function index() {
        $echoStr = $_GET["echostr"];
        if(isset($echoStr)) {
            if($this->checkSignature()){
                echo $echoStr;
                exit;
            }
        } else {
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 
            if (!empty($postStr)){
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $RX_TYPE = trim($postObj->MsgType);
                $result = "";
                switch ($RX_TYPE) {
                    case "text":
                        $result = $this->receiveText($postObj);
                        break;
                    case "event":
                        $result = $this->receiveEvent($postObj);
                        break;
                    case "image":
                        //$result = $this->receiveImage($postObj);
                        break;
                    default:
                        $result = "Unknow msg type: ".$RX_TYPE;
                        break;
                }
                echo $result;
                exit;
            } else {
                echo "";
                exit;
            }
        }
    }
 
 
    private function checkSignature() {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = 'ada823k21812jasd123dfg6fsdf';
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        if($tmpStr == $signature){
            return true;
        } else {
            return false;
        }
    }
 
    private function receiveEvent($object) {
        switch ($object->Event) {
            case "subscribe":
                $FromUserName = str_replace("", "", $object->FromUserName);
                if (!empty($object->EventKey)){
//                    $this->sysconfig_model->test(str_replace("qrscene_", "", $object->EventKey));
                    $parent_id = str_replace("qrscene_", "", $object->EventKey);
                    $this->sysconfig_model->bindUesr($FromUserName, $parent_id);
                }else{
                    $this->sysconfig_model->bindUesr($FromUserName);
                }
                break;
            case "unsubscribe":
                break;
            case "SCAN":
                break;
            case "CLICK":
                break;
            case "VIEW":
                break;
            case "LOCATION":
                break;
        }
//        return $this->transmitText($object, $content);
    }
 
    private function transmitText($object, $content) {
        $textTpl = "
            <xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                <FuncFlag>0</FuncFlag>
            </xml>
        ";
        return sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
    }
 
    private function transmitNews($object, $arr_item) {
        if(!is_array($arr_item))
            return;
 
        $itemTpl = "
            <item>
                <Title><![CDATA[%s]]></Title>
                <Description><![CDATA[%s]]></Description>
                <PicUrl><![CDATA[%s]]></PicUrl>
                <Url><![CDATA[%s]]></Url>
            </item>
        ";
        $item_str = "";
        foreach ($arr_item as $item)
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
 
        $newsTpl = "
        <xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
        <FromUserName><![CDATA[%s]]></FromUserName>
        <CreateTime>%s</CreateTime>
        <MsgType><![CDATA[news]]></MsgType>
        <Content><![CDATA[]]></Content>
        <ArticleCount>%s</ArticleCount>
        <Articles>$item_str</Articles>
        </xml>
        ";
        return sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
    }
 
 
    public function post($url, $post_data, $timeout = 300){
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/json;encoding=utf-8',
                'content' => urldecode(json_encode($post_data)),
                'timeout' => $timeout
            )
        );
        $context = stream_context_create($options);
        return file_get_contents($url, false, $context);
    }
 
 
    public function get_access_token() {
        $this->load->config('wxpay_config');
        $appid = $this->config->item('appid');
        $secret = $this->config->item('appsecret');
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $response = file_get_contents($url);
        return json_decode($response)->access_token;
    }
 
    public function get_or_create_ticket($id = '', $action_name = 'QR_SCENE') {
        $access_token = $this->get_access_token();
        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . $access_token;
        @$post_data->expire_seconds = 2592000;
        @$post_data->action_name = $action_name;
        @$post_data->action_info->scene->scene_id = $id;
        $ticket_data = json_decode($this->post($url, $post_data));
        $ticket = $ticket_data->ticket;
        $img_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
        $data['img'] = $img_url;
        $this->load->view('scan.php',$data);
//        return $ticket;
    }
 
}