前端
手机号一键登录
后台
query($q);
dump($res);
}
/**
* post: 用户&&商家注册或登陆
* path: register
* method: register
* param: phone - {string} 手机号
* param: type - {int} 1为用户 2为商家
* param: friend_id - {int} 用户id 分享是传入
*/
public function registerOrLogin()
{
$type = input('type');
$wxCode = input('code');
$encryptedData = input('encryptedData');
$iv = input('iv');
$appid = 'wxcd35e514f4b305ba';
$appsecret = '5a5aeafd46d487933f765f3c9f95bb9d';
$oauth2Url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appsecret."&js_code=".$wxCode."&grant_type=authorization_code";
$result = file_get_contents($oauth2Url);
$obj = json_decode($result);
$session_key = $obj->session_key;
$openid = $obj->openid;
$unionid = $obj->unionid;
$pc = new wxBizDataCrypt($appid, $session_key);
$errCode = $pc->decryptData($encryptedData, $iv, $data );
if ($errCode == 0) {
$numDatas = json_decode($data);
$phone = $numDatas->phoneNumber;
$userinfo = $this->is_register($phone);
if (! $userinfo){
// 在db插入新用户
$data = [
'name' => 'U' . $phone.$type,
'nick' => 'U' . substr($phone, -4),
'headimg' => '/uploads/default/head.png',
'phone' => $phone,
'addtime' => date("Y-m-d H:i:s"),
'type' => $type,
'openid' => $openid
];
$res = db($this->user)->insertGetId($data);
if ($res){
$returnData = [
'userId' => $userinfo['id'],
'nick' => $userinfo['nick'],
'headimg' => 'http://app.51oil.net.cn'.$userinfo['headimg'],
];
$this->result($returnData, 1, '');
}
}else{
$returnData = [
'userId' => $userinfo['id'],
'nick' => $userinfo['nick'],
'headimg' => 'url'.$userinfo['headimg'],
];
$this->result($returnData, 1, '');
}
} else {
print($errCode . "\n");
}
}
/**
* post: 手机号查询用户信息
* path: is_register
* method: is_register
* param: phone - {string} 手机号
*/
public function is_register($phone)
{
if (!$phone || !is_phone($phone)) {
$this->result('', 0, '请输入正确格式的手机号码');
}
$find = db($this->user)->where('phone', $phone)->find();
return $find;
}
//https请求(支持GET和POST)
public function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
//https
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}