|
@@ -0,0 +1,120 @@
|
|
|
|
+<?php
|
|
|
|
+namespace Home\Controller;
|
|
|
|
+use Think\Controller;
|
|
|
|
+class WeixinController extends Controller {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取微信access_token
|
|
|
|
+ */
|
|
|
|
+ public function get_access_token(){
|
|
|
|
+ $access_token = M('config')->field('value,time')->where(array('name'=>'access'))->find();
|
|
|
|
+ $invalid_time = $access_token['time']+7000;
|
|
|
|
+ if(!empty($access_token['value']) && $invalid_time > NEW_TIME){
|
|
|
|
+ return $access_token['value'];
|
|
|
|
+ }else{
|
|
|
|
+ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('appid').'&secret='.C('appsecret');
|
|
|
|
+ $data = curl_request($url);
|
|
|
|
+ M('config')->where(array('name'=>'access'))->save(array('value'=>$data['access_token'],'time'=>NEW_TIME));
|
|
|
|
+ return $data['access_token'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function get_user_info(){
|
|
|
|
+ $access_token = $this->get_access_token();
|
|
|
|
+ if(!session('?openid')){
|
|
|
|
+ $open_id = $this->get_user_access_token();
|
|
|
|
+ }else{
|
|
|
|
+ $open_id = session('openid');
|
|
|
|
+ }
|
|
|
|
+ $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$open_id.'&lang=zh_CN';
|
|
|
|
+ $data = curl_request($url);
|
|
|
|
+ return $data;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取微信code
|
|
|
|
+ */
|
|
|
|
+ public function get_code($url){
|
|
|
|
+ $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.C('appid').'&redirect_uri='.urlencode(C('URL').$url).'&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect';
|
|
|
|
+ echo "<script>location.href='{$url}'</script>";die;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 通过code 获取用户 access_token
|
|
|
|
+ */
|
|
|
|
+ public function get_user_access_token(){
|
|
|
|
+ if (empty(I('get.code'))){
|
|
|
|
+ $this->get_code(CONTROLLER_NAME.'/'.ACTION_NAME);
|
|
|
|
+ }else{
|
|
|
|
+ if(!empty(I('get.code'))){
|
|
|
|
+ $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.C('appid').'&secret='.C('appsecret').'&code='.I('get.code').'&grant_type=authorization_code';
|
|
|
|
+ $data = curl_request($url);
|
|
|
|
+ $openid = $data['openid'];
|
|
|
|
+ $uid = M('User')->where(array('openid'=>$openid))->getField('uid');
|
|
|
|
+ if(empty($uid)){
|
|
|
|
+ $data['openid'] = $openid;
|
|
|
|
+ $uid = M('User')->add($data);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(!empty($uid)){
|
|
|
|
+ session('uid',$uid);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(!empty($openid)){
|
|
|
|
+ session('openid',$openid);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ return $openid?$openid:session('openid');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 发放微信红包
|
|
|
|
+ */
|
|
|
|
+ public function set_user_envelopes($amd){
|
|
|
|
+
|
|
|
|
+ if(!session('?openid') || !session('?uid')){
|
|
|
|
+ $openid = $this->get_user_access_token();
|
|
|
|
+ }else{
|
|
|
|
+ $openid = session('openid');
|
|
|
|
+ }
|
|
|
|
+ $string = get_random_string(32);
|
|
|
|
+ $envelopes = array(
|
|
|
|
+ 'nonce_str' => $string,
|
|
|
|
+ 'mch_billno' => substr(session('openid'),0,4).date('YmdHsi'),
|
|
|
|
+ 'mch_id'=>C('key'),
|
|
|
|
+ 'wxappid'=>C('appid'),
|
|
|
|
+ 'send_name'=>C('name'),
|
|
|
|
+ 're_openid'=>$openid,
|
|
|
|
+ 'total_amount'=>$amd,
|
|
|
|
+ 'total_num'=>1,
|
|
|
|
+ 'wishing'=>C('wishing'),
|
|
|
|
+ 'client_ip'=>'39.107.251.71',
|
|
|
|
+ 'act_name'=>C('act_name'),
|
|
|
|
+ 'remark'=>C('remark'),
|
|
|
|
+ 'scene_id'=>'PRODUCT_2',
|
|
|
|
+ );
|
|
|
|
+ $envelopes['sign'] = A('Home/Weixinpay')->getSign($envelopes);
|
|
|
|
+ $envelopes = arrayToXml($envelopes);
|
|
|
|
+ $data = xmlToArray(curlPost('https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack',$envelopes));
|
|
|
|
+ if($data['err_code'] == 'SUCCESS'){
|
|
|
|
+ $wx_data = array(
|
|
|
|
+ 'ststus'=>'1',
|
|
|
|
+ 'send_listid'=>$data['send_listid'],
|
|
|
|
+ 'openid'=>$data['re_openid'],
|
|
|
|
+ 'info'=>$data['err_code'].$data['err_code_des'],
|
|
|
|
+ );
|
|
|
|
+ }else{
|
|
|
|
+ $wx_data = array(
|
|
|
|
+ 'ststus'=>'0',
|
|
|
|
+ 'info'=>$data['err_code'].$data['err_code_des'],
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ return $wx_data;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|