小程序授权

  public function getopenid()
{
$APPID = 'wx1a8b582067ebb8a5';//自己配置
$AppSecret = '08fb395003441b482583b218d306280b';//自己配置
$file =file_get_contents("php://input");

$code=json_decode($file,true);

if (!$code) {
$code =$code['code'];
}

if ($code['code']) {
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=$APPID&secret=$AppSecret&js_code=" . $code['code'] . "&grant_type=authorization_code";
$rst = $this->http_get($url, TRUE);
$lists = json_decode($rst, TRUE);
//$is_dl 是否绑定手机号(1未绑定手机号 2绑定手机号)
//$is_pwd 是否绑定提现密码(1未绑定提现密码 2绑定提现密码)
if ($lists['openid']) {
$yhxx = M('user')->where(array('openid' => $lists['openid']))->find();
if ($yhxx['id']) {
$lists1['login_time'] = time();
$lists1['openid'] = $lists['openid'];
// vendor("PHP.wxBizDataCrypt");
// $encryptedData = I('encryptedData');
//
// $iv = I('iv');
// $pc= new WXBizDataCrypt($APPID, $lists['sign']);
// $errCode=$pc->decryptData($encryptedData,$iv,$code);
$aa1 = M('user')->where(array('openid' => $lists['openid'], 'is_xs' => 1))->save($lists1);
$aa = M('user')->where(array('id' => $yhxx['id']))->find();
} else {
$lists1['login_time'] = time();
$lists1['last_time'] = time();
$lists1['addtime'] = time();
$lists1['openid'] = $lists['openid'];

//$lists1['session_key']=$lists['session_key'];

//设置推荐人
/*if($_POST['t_id']){
$lists1['t_id']=$_POST['t_id'];
}*/
if ($yhaa = M('user')->add($lists1)) {
$id= M()->getLastInsID();
$aa = M('user')->where(array('id' => $id))->find();
}

}
if ($aa) {
$lists['phone'] = $aa['phone'];
$data = array("result" => '1', "message" => "登录成功!",'openid' => $lists['openid'],"id"=>$aa['id'],"is_dl"=>1); //组装JSON
echo json_encode($data); //返回
die;
} else {
$data = array("result" => '2', "message" => "系统繁忙,请重试!"); //组装JSON
echo json_encode($data); //返回
die;
}
} else {
$data = array("result" => '11', "message" => "系统繁忙,请重试!"); //组装JSON
echo json_encode($data); //返回
die;
}
} else {
$data = array("result" => '3', "message" => "参数有误!", "code" => $code); //组装JSON
echo json_encode($data); //返回
die;
}
}

public function http_get($url, $ssl = FALSE)
{
$curl = curl_init();//启动一个curl会话
curl_setopt($curl, CURLOPT_URL, $url);//要访问的地址
if ($ssl) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);//对认证证书检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);//从证书中检查ssl加密算法是否存在
curl_setopt($curl, CURLOPT_SSLVERSION, 4);
}
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);//模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);//自动设置referer
curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0);//显示返回的header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回
$tmpinfo = curl_exec($curl);//执行操作
if (curl_errno($curl)) {
var_dump(curl_error($curl));
return FALSE;
}
curl_close($curl);//关闭会话
return $tmpinfo;
}
原文地址:https://www.cnblogs.com/maohongli/p/13680758.html