예제 #1
0
 public function wx_auto_login()
 {
     $code = trim($_GET['code']);
     $state = trim($_GET['state']);
     if ($code && $state) {
         //通过code获取用户信息
         $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . Wx_C('wx_appid') . '&secret=' . Wx_C('wx_secret') . '&code=' . $code . '&grant_type=authorization_code';
         $info = json_decode(get_url($url));
         $web_token = $info->access_token;
         $refresh_token = $info->refresh_token;
         $openid = $info->openid;
         $unionid = $info->unionid;
         //检查此用户是否已经注册过
         $member_data = M('Member')->where('openid=\'' . $openid . '\'')->find();
         if (is_array($member_data) && !empty($member_data)) {
             //更新用户微信网页授权access_token
             M('Member')->where('member_id=' . $member_data['member_id'])->save(array('web_token' => $web_token, 'refresh_token' => $refresh_token));
             //授权
             session('member_id', $member_data['member_id']);
         } else {
             //未关注
             if ($state == 'STATEuserinfo') {
                 $get_userinfo_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $web_token . '&openid=' . $openid . '&lang=zh_CN';
                 $user = json_decode(get_url($get_userinfo_url));
             } else {
                 //已关注
                 $access_token = get_wx_AccessToken(1);
                 $get_user_url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
                 $user = json_decode(get_url($get_user_url));
             }
             if ($user->nickname) {
                 $data = array();
                 $data['nickname'] = replaceEmoji($user->nickname);
                 //转义emoji
                 $data['openid'] = $user->openid;
                 $data['gender'] = $user->sex;
                 $data['country'] = $user->country;
                 $data['province'] = $user->province;
                 $data['city'] = $user->city;
                 $data['usercity'] = $user->city;
                 $data['avatar'] = $user->headimgurl;
                 $data['unionid'] = $user->unionid;
                 $data['web_token'] = $web_token;
                 $data['refresh_token'] = $refresh_token;
                 $data['register_time'] = NOW_TIME;
                 $return = M('Member')->add($data);
                 if ($return) {
                     session('member_id', $return);
                 }
             }
         }
     } else {
         $c_url = U('', $_GET, '', true);
         //当前地址  ERROR:该地址没有生成当前地址的参数项   导致授权之后跳转页面没有传参 已解决:2015-6-27 17:35:58
         $scope = 'snsapi_userinfo';
         $re_url = urlencode($c_url);
         $sq_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . Wx_C('wx_appid') . '&redirect_uri=' . $re_url . '&response_type=code&scope=' . $scope . '&state=STATEuserinfo#wechat_redirect';
         redirect($sq_url);
         //get_url($sq_url);
     }
 }
예제 #2
0
 public function post_menu()
 {
     //构造自定义菜单数据
     $WxMenu = M('WxMenu');
     $mu_map = array();
     $mu_map['p_id'] = array('eq', 0);
     $mu_map['show'] = array('eq', 1);
     $list = $WxMenu->where($mu_map)->order('sort asc')->select();
     $post_arr = array();
     $post_sub_arr = array();
     if (is_array($list) && !empty($list)) {
         foreach ($list as $key => $v) {
             if ($v['name']) {
                 $post_arr['button'][$key]['name'] = $v['name'];
                 $sub_mu = array();
                 $sub_mu['p_id'] = array('eq', $v['id']);
                 $sub_mu['is_show'] = array('eq', 1);
                 $sub_list = $WxMenu->where($sub_mu)->order('sort asc')->field('type,name,key_url')->select();
                 $sb = array();
                 if (is_array($sub_list) && !empty($sub_list)) {
                     foreach ($sub_list as $k => $sb) {
                         if ($sb['name']) {
                             $post_sub_arr[$k]['type'] = $sb['type'];
                             $post_sub_arr[$k]['name'] = $sb['name'];
                             if ($sb['type'] == 'view') {
                                 $post_sub_arr[$k]['url'] = $sb['key_url'];
                             } else {
                                 $post_sub_arr[$k]['key'] = $sb['key_url'];
                             }
                         }
                     }
                 } else {
                     $post_arr['button'][$key]['type'] = $v['type'];
                     if ($v['type'] == 'view') {
                         $post_arr['button'][$key]['url'] = $v['key_url'];
                     } else {
                         $post_arr['button'][$key]['key'] = $v['key_url'];
                     }
                 }
                 if (!empty($post_sub_arr)) {
                     $post_arr['button'][$key]['sub_button'] = $post_sub_arr;
                 }
                 unset($post_sub_arr);
             }
         }
     }
     $post_data = json_encode($post_arr, JSON_UNESCAPED_UNICODE);
     //防中文转义
     $post_data = str_replace("\\/", "/", $post_data);
     $wx_ACCESS_TOKEN = get_wx_AccessToken('1');
     $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $wx_ACCESS_TOKEN;
     $return = post_url($url, $post_data);
     if ($return) {
         $this->success('操作成功', U('Weixin/wx_menu'));
         exit;
     }
 }