public function handle1($url=null){ //链接数据库 $link = mysqli_connect(DB_HOST, DB_USER,DB_PASSWORD,DB_NAME ) or die('数据库连接失败1'); mysqli_set_charset($link,'utf8'); $_POST = utils::_filter_input($_POST); $saveData = $this->userPassport->pre_signup_process($_POST); if( $member_id = $this->userPassport->save_members($saveData,$msg) ){ $this->userObject->set_member_session($member_id); $this->bind_member($member_id); foreach(kernel::servicelist('b2c_save_post_om') as $object) { $object->set_arr($member_id, 'member'); $refer_url = $object->get_arr($member_id, 'member'); } /*注册完成后做某些操作! begin*/ foreach(kernel::servicelist('b2c_register_after') as $object) { $object->registerActive($member_id); } /*end*/ $data['member_id'] = $member_id; $data['uname'] = $saveData['pam_account']['login_account']; $data['passwd'] = $_POST['pam_account']['psw_confirm']; $data['email'] = $_POST['contact']['email']; $data['refer_url'] = $refer_url ? $refer_url : ''; $data['is_frontend'] = true; $obj_account=$this->app->model('member_account'); $obj_account->fireEvent('register',$data,$member_id); if(!strpos($_SESSION['pc_next_page'],'cart')){ $url = $this->gen_url(array('app'=>'b2c','ctl'=>'site_passport','act'=>'sign_tips')); }else{ $url = $_SESSION['pc_next_page']; } //会员注册成功,处理member_id 开始 $login_name = $_POST["pam_account"]["login_name"]; $sql = "select member_id from sdb_pam_members where password_account = '{$login_name}'"; // echo $sql; $res = mysqli_query($link, $sql); $row = mysqli_fetch_assoc($res); $member_id = $row['member_id']; //链接数据库,插入数据 $openid = $_COOKIE['openid']; $sql = "update sdb_trustlogin_trustinfo set member_id = ('{$member_id}') where openid = '{$openid}'"; $res = mysqli_query($link, $sql); if($res && mysqli_affected_rows($link) > 0){ //删除cookie setcookie("openid", $openid, time()-3600); $this->splash('success',$url,app::get('b2c')->_('注册成功'),$ajax_request); } //会员注册成功,处理member_id 结束 $this->splash('failed',$back_url,app::get('b2c')->_('注册失败'),$ajax_request); } }
临时页面
public function linshi(){ $code = $_GET['code']; $state = $_GET['state']; if($state === 'STATE'){ $this->loginWeixin($code); }elseif($state === 'wxBind'){ $this->bindWeixin($code); }else{ return redirect("http://www.sunmil.cn"); } }
loginWeixin
private function loginWeixin($code){ //链接数据库 $link = mysqli_connect('127.0.0.1', 'root','root', 'ec') or die('数据库连接失败'); mysqli_set_charset($link,'utf8'); $appid = $this->appid; $appsecert = $this->appsecert; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecert."&code=".$code."&grant_type=authorization_code"; //curl模拟get请求,获取结果 $res = $this->http_curl($url); //转化为数组 $result = json_decode($res,true); $openid = $result['openid']; //openid存在,直接登录,openid不存在,先注册再登录 $sql = "select openid from sdb_trustlogin_trustinfo"; $res = mysqli_query($link, $sql); $row = mysqli_fetch_all($res); //foreach 判断 foreach ($row As $v) { if(in_array($openid,$v,true)){ //缺少登录信息 $sql = "select member_id from sdb_trustlogin_trustinfo where openid = '{$openid}'"; //查出member_id $res = mysqli_query($link, $sql); $row = mysqli_fetch_assoc($res); $member_id = $row['member_id']; //查出会员信息 $sql = "select * from sdb_pam_members where member_id = '{$member_id}'"; $res = mysqli_query($link, $sql); $row = mysqli_fetch_assoc($res); $login_name = $row['login_account']; //存入cookie setcookie('UNAME',$login_name, time()+360000); echo "<script>window.location.href='http://www.sunmil.cn'</script>"; exit; } } $access_token = $result['access_token']; //获取用户基本信息 $getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; $userInfo = $this->http_curl($getInfoUrl); $trustinfo = json_decode($userInfo); //判断trustinfo表里面是否存在该条数据 $openid = $trustinfo->openid; $nickname = $trustinfo->nickname; $sex = $trustinfo->sex; $city = $trustinfo->city; $province = $trustinfo->province; $country = $trustinfo->country; $avatar = $trustinfo->headimgurl; $trust_source = 'trustlogin_plugin_weixin'; //连接数据库,插入数据 $sql = "insert into sdb_trustlogin_trustinfo (openid,realname,avatar,gender,province,city,trust_source) values ('{$openid}','{$nickname}','{$avatar}','{$sex}','{$province}','{$city}','{$trust_source}')"; $res = mysqli_query($link, $sql); if($res && mysqli_affected_rows($link) > 0){ //存入cookie setcookie('openid',$openid, time()+3600); header("Location:"); }else{ echo '失败了'; } }
curl模拟get请求