|
@@ -8,13 +8,14 @@ use Passport\Lib\Base;
|
|
|
|
|
|
class Applet extends Base
|
|
|
{
|
|
|
- public function init()
|
|
|
+ public function init()
|
|
|
{
|
|
|
- $uid = $this->check();
|
|
|
+ $uid = $this->check();
|
|
|
$user = Dever::load('passport/user-one', $uid);
|
|
|
|
|
|
return $user;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 用户绑定 生成用户信息
|
|
|
*
|
|
@@ -22,67 +23,16 @@ class Applet extends Base
|
|
|
*/
|
|
|
public function bind()
|
|
|
{
|
|
|
- $token = Dever::project('token');
|
|
|
- $applet = array();
|
|
|
-
|
|
|
- $system = Dever::input('system', 1);
|
|
|
-
|
|
|
- $config = Dever::config('base', 'project')->applet;
|
|
|
-
|
|
|
- if ($token) {
|
|
|
- $applet = Dever::db('token/project')->one($system);
|
|
|
- }
|
|
|
-
|
|
|
- if (!$applet) {
|
|
|
- if (isset($config['project']) && $config['project']) {
|
|
|
- $applet = Dever::db($config['project'])->one($system);
|
|
|
- } else {
|
|
|
- $applet = $config;
|
|
|
- }
|
|
|
- }
|
|
|
+ $create = Dever::input('create', 1);
|
|
|
+ $data = $this->getLoginInfo();
|
|
|
|
|
|
- if (!$applet) {
|
|
|
- Dever::alert('错误的appid');
|
|
|
+ if ($create == 1) {
|
|
|
+ $data = $this->create($data);
|
|
|
}
|
|
|
|
|
|
- $appid = $applet['appid'];
|
|
|
- $secret = $applet['secret'];
|
|
|
- $url = $config['url'];
|
|
|
- $code = Dever::input('code');
|
|
|
-
|
|
|
- $url .= '?appid=' . $appid;
|
|
|
- $url .= '&secret=' . $secret;
|
|
|
- $url .= '&js_code=' . $code;
|
|
|
- $url .= '&grant_type=authorization_code';
|
|
|
-
|
|
|
- $data = Dever::curl($url);
|
|
|
- Dever::log($data, 'passport_applet');
|
|
|
- if (strstr($data, 'errcode')) {
|
|
|
- Dever::alert($data);
|
|
|
- }
|
|
|
-
|
|
|
- //YzJkOThpRFhwZ1lQTF9mZl9hLVZjZnFXemJVenlYcDQ3d3JWekk0b1I4NjBBQ0Naejg4a0VQa0U=
|
|
|
- //$data = '{"session_key":"aNAXk7nG\/DRYI\/G0KzJRsw==","openid":"oIZ895RZs2ZkywasoZIv6WavPZlQ"}';
|
|
|
- $data = Dever::json_decode($data);
|
|
|
-
|
|
|
- $user = $this->create($data);
|
|
|
-
|
|
|
- return $user;
|
|
|
+ return $data;
|
|
|
}
|
|
|
|
|
|
- /*
|
|
|
- public function test_sms()
|
|
|
- {
|
|
|
- $uid = Dever::input('uid');
|
|
|
- $mobile = Dever::input('mobile');
|
|
|
- $state = Dever::config('base', 'project')->regSendSms;
|
|
|
- if ($state) {
|
|
|
- Dever::setInput('skin', $state);
|
|
|
- $this->send($mobile, $uid);
|
|
|
- }
|
|
|
- }
|
|
|
- */
|
|
|
-
|
|
|
/**
|
|
|
* 生成用户,返回uid
|
|
|
*
|
|
@@ -101,7 +51,6 @@ class Applet extends Base
|
|
|
$uid = false;
|
|
|
} else {
|
|
|
# 判断用户是否存在,是否需要合并
|
|
|
- $uid = $info['uid'];
|
|
|
$uid = $this->combine($info['uid'], $data['unionid']);
|
|
|
}
|
|
|
$wechat['unionid'] = $data['unionid'];
|
|
@@ -127,7 +76,6 @@ class Applet extends Base
|
|
|
} else {
|
|
|
$uid = $info['uid'];
|
|
|
if (isset($data['unionid']) && $data['unionid']) {
|
|
|
- //$wechat['unionid'] = $data['unionid'];
|
|
|
# 判断用户是否存在,是否需要合并
|
|
|
$wechat['uid'] = $this->combine($uid, $data['unionid']);
|
|
|
}
|
|
@@ -144,67 +92,25 @@ class Applet extends Base
|
|
|
$result['uid'] = $uid;
|
|
|
$result['signature'] = Dever::login($uid);
|
|
|
|
|
|
- if ($user['mobile']) {
|
|
|
- $result['mobile'] = $user['mobile'];
|
|
|
+ if ($user['mobile']) {
|
|
|
+ $result['mobile'] = $user['mobile'];
|
|
|
}
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 更新用户信息 手机号
|
|
|
+ * 一次性登录:通过code或者openid、sessionkey来注册用户,此时已经授权,可以直接拿到unioinid
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function mobile()
|
|
|
+ public function login()
|
|
|
{
|
|
|
- $uid = $this->check();
|
|
|
- $vid = Dever::input('vid');
|
|
|
- $result = array();
|
|
|
- $mobile = $phoneNumber = '';
|
|
|
+ $data = $this->getLoginInfo();
|
|
|
|
|
|
- $iv = Dever::input('iv');
|
|
|
- $encryptedData = Dever::input('encryptedData');
|
|
|
-
|
|
|
- if ($iv && $encryptedData) {
|
|
|
- $vinfo = Dever::load('passport/wechat-one', $vid);
|
|
|
- $data = $this->decryptData($vinfo['session_key']);
|
|
|
- if ($data && isset($data->purePhoneNumber) && isset($data->phoneNumber)) {
|
|
|
- $mobile = $data->purePhoneNumber;
|
|
|
- $phoneNumber = $data->phoneNumber;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if ($mobile && $phoneNumber && $uid) {
|
|
|
- $uid = $this->combine($uid, $mobile, 'mobile');
|
|
|
-
|
|
|
- $info = Dever::load('passport/user-one', $uid);
|
|
|
- $result['mobile'] = $mobile;
|
|
|
- if ($info) {
|
|
|
-
|
|
|
- if (!$info['mobile']) {
|
|
|
- Dever::score($uid, 'bind_mobile', '绑定手机号');
|
|
|
- }
|
|
|
- $update['set_mobile'] = $mobile;
|
|
|
- $update['set_bind'] = 1;
|
|
|
- $update['where_id'] = $uid;
|
|
|
- Dever::load('passport/user-update', $update);
|
|
|
-
|
|
|
- $state = Dever::config('base', 'project')->regSendSms;
|
|
|
- if ($state) {
|
|
|
- Dever::setInput('skin', $state);
|
|
|
- $this->send($mobile, $uid);
|
|
|
- }
|
|
|
- } else {
|
|
|
- Dever::alert('无效的用户id,请重新登录');
|
|
|
- }
|
|
|
- }
|
|
|
+ $data += $this->getWechatData($data['session_key']);
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- $result['vid'] = $vid;
|
|
|
- $result['uid'] = $uid;
|
|
|
- $result['signature'] = Dever::login($uid);
|
|
|
+ $result = $this->create($data);
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
@@ -230,7 +136,8 @@ class Applet extends Base
|
|
|
if ($vid) {
|
|
|
$vinfo = Dever::load('passport/wechat-one', $vid);
|
|
|
|
|
|
- $unionid = $this->unionid($vinfo['session_key']);
|
|
|
+ $data = $this->getWechatData($vinfo['session_key']);
|
|
|
+ $unionid = $data['unionid'];
|
|
|
if ($unionid) {
|
|
|
$vinfo = Dever::load('passport/wechat-one', array('option_unionid' => $unionid));
|
|
|
if (!$vinfo) {
|
|
@@ -274,7 +181,7 @@ class Applet extends Base
|
|
|
|
|
|
if ($pic) {
|
|
|
//$update['set_avatar'] = $this->saveAvatar($pic);
|
|
|
- $update['set_avatar'] = $pic;
|
|
|
+ $update['set_avatar'] = $pic;
|
|
|
}
|
|
|
|
|
|
if ($city) {
|
|
@@ -309,6 +216,64 @@ class Applet extends Base
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新用户信息 手机号
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function mobile()
|
|
|
+ {
|
|
|
+ $uid = $this->check();
|
|
|
+ $vid = Dever::input('vid');
|
|
|
+ $result = array();
|
|
|
+ $mobile = $phoneNumber = '';
|
|
|
+
|
|
|
+ $iv = Dever::input('iv');
|
|
|
+ $encryptedData = Dever::input('encryptedData');
|
|
|
+
|
|
|
+ if ($iv && $encryptedData) {
|
|
|
+ $vinfo = Dever::load('passport/wechat-one', $vid);
|
|
|
+ $data = $this->getWechatData($vinfo['session_key']);
|
|
|
+ if ($data && $data['mobile']) {
|
|
|
+ $mobile = $data['mobile'];
|
|
|
+ $phoneNumber = $data['phone'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($mobile && $phoneNumber && $uid) {
|
|
|
+ $uid = $this->combine($uid, $mobile, 'mobile');
|
|
|
+
|
|
|
+ $info = Dever::load('passport/user-one', $uid);
|
|
|
+ $result['mobile'] = $mobile;
|
|
|
+ if ($info) {
|
|
|
+
|
|
|
+ if (!$info['mobile']) {
|
|
|
+ Dever::score($uid, 'bind_mobile', '绑定手机号');
|
|
|
+ }
|
|
|
+ $update['set_mobile'] = $mobile;
|
|
|
+ $update['set_bind'] = 1;
|
|
|
+ $update['where_id'] = $uid;
|
|
|
+ Dever::load('passport/user-update', $update);
|
|
|
+
|
|
|
+ $state = Dever::config('base', 'project')->regSendSms;
|
|
|
+ if ($state) {
|
|
|
+ Dever::setInput('skin', $state);
|
|
|
+ $this->send($mobile, $uid);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Dever::alert('无效的用户id,请重新登录');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $result['vid'] = $vid;
|
|
|
+ $result['uid'] = $uid;
|
|
|
+ $result['signature'] = Dever::login($uid);
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
private function unionid($session_key)
|
|
|
{
|
|
|
$data = $this->decryptData($session_key);
|
|
@@ -319,13 +284,37 @@ class Applet extends Base
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ private function getWechatData($session_key)
|
|
|
+ {
|
|
|
+ $result = array();
|
|
|
+ $data = $this->decryptData($session_key);
|
|
|
+ if ($data && isset($data->openId)) {
|
|
|
+ $result['openid'] = $data->openId;
|
|
|
+ $result['unionid'] = '';
|
|
|
+ $result['mobile'] = '';
|
|
|
+ $result['phone'] = '';
|
|
|
+ if (isset($data->unionId)) {
|
|
|
+ $result['unionid'] = $data->unionId;
|
|
|
+ }
|
|
|
+ if (isset($data->purePhoneNumber)) {
|
|
|
+ $result['mobile'] = $data->purePhoneNumber;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset($data->phoneNumber)) {
|
|
|
+ $result['phone'] = $data->phoneNumber;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
private function decryptData($session_key)
|
|
|
{
|
|
|
- $iv = Dever::input('iv');
|
|
|
+ $iv = Dever::input('iv');
|
|
|
$encryptedData = Dever::input('encryptedData');
|
|
|
|
|
|
if (!$iv || !$encryptedData) {
|
|
|
- return false;
|
|
|
+ return false;
|
|
|
}
|
|
|
if (strlen($session_key) != 24) {
|
|
|
return false;
|
|
@@ -347,11 +336,66 @@ class Applet extends Base
|
|
|
return false;
|
|
|
}
|
|
|
/*
|
|
|
- $applet = Dever::config('base', 'project')->applet;
|
|
|
+ $applet = Dever::config('base', 'project')->applet;
|
|
|
$appid = $applet['appid'];
|
|
|
if($dataObj->watermark->appid != $appid) {
|
|
|
return false;
|
|
|
}*/
|
|
|
return $dataObj;
|
|
|
}
|
|
|
+
|
|
|
+ private function getApplet()
|
|
|
+ {
|
|
|
+ $applet = Dever::config('base', 'project')->applet;
|
|
|
+ if (Dever::project('token')) {
|
|
|
+ $system = Dever::input('system', 1);
|
|
|
+ $project = Dever::db('token/project')->one($system);
|
|
|
+
|
|
|
+ if (!$project) {
|
|
|
+ if (isset($config['project']) && $config['project']) {
|
|
|
+ $project = Dever::db($config['project'])->one($system);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($project) {
|
|
|
+ $applet['appid'] = $project['appid'];
|
|
|
+ $applet['secret'] = $project['secret'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$applet || !$applet['appid'] || !$applet['secret']) {
|
|
|
+ Dever::alert('错误的appid');
|
|
|
+ }
|
|
|
+
|
|
|
+ return $applet;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getLoginInfo()
|
|
|
+ {
|
|
|
+ $applet = $this->getApplet();
|
|
|
+ $appid = $applet['appid'];
|
|
|
+ $secret = $applet['secret'];
|
|
|
+ $url = $applet['url'];
|
|
|
+ $code = Dever::input('code');
|
|
|
+ if (!$applet || !$applet['appid'] || !$applet['secret']) {
|
|
|
+ Dever::alert('错误的appid');
|
|
|
+ }
|
|
|
+
|
|
|
+ $url .= '?appid=' . $appid;
|
|
|
+ $url .= '&secret=' . $secret;
|
|
|
+ $url .= '&js_code=' . $code;
|
|
|
+ $url .= '&grant_type=authorization_code';
|
|
|
+
|
|
|
+ $data = Dever::curl($url);
|
|
|
+ Dever::log($data, 'passport_applet');
|
|
|
+ if (strstr($data, 'errcode')) {
|
|
|
+ Dever::alert($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ //YzJkOThpRFhwZ1lQTF9mZl9hLVZjZnFXemJVenlYcDQ3d3JWekk0b1I4NjBBQ0Naejg4a0VQa0U=
|
|
|
+ //$data = '{"session_key":"aNAXk7nG\/DRYI\/G0KzJRsw==","openid":"oIZ895RZs2ZkywasoZIv6WavPZlQ"}';
|
|
|
+ $data = Dever::json_decode($data);
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
}
|