|
@@ -0,0 +1,256 @@
|
|
|
+<?php
|
|
|
+namespace Passport\Src;
|
|
|
+
|
|
|
+use Dever;
|
|
|
+use Passport\Src\Lib\Base;
|
|
|
+
|
|
|
+# 小程序
|
|
|
+
|
|
|
+class Applet extends Base
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 用户绑定 生成用户信息
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function bind()
|
|
|
+ {
|
|
|
+ $applet = Dever::config('base')->applet;
|
|
|
+ $appid = $applet['appid'];
|
|
|
+ $secret = $applet['secret'];
|
|
|
+ $url = $applet['url'];
|
|
|
+ $code = Dever::input('code');
|
|
|
+
|
|
|
+ $url .= '?appid=' . $appid;
|
|
|
+ $url .= '&secret=' . $secret;
|
|
|
+ $url .= '&js_code=' . $code;
|
|
|
+ $url .= '&grant_type=authorization_code';
|
|
|
+
|
|
|
+ $data = Dever::curl($url);
|
|
|
+ if (strstr($data, 'errcode')) {
|
|
|
+ Dever::alert($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ //$data = '{"session_key":"aNAXk7nG\/DRYI\/G0KzJRsw==","openid":"oIZ895RZs2ZkywasoZIv6WavPZlQ"}';
|
|
|
+ $data = Dever::decode($data);
|
|
|
+
|
|
|
+ $user = $this->create($data);
|
|
|
+
|
|
|
+ return $user;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成用户,返回uid
|
|
|
+ *
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ private function create($data)
|
|
|
+ {
|
|
|
+ $uid = false;
|
|
|
+ $info = Dever::load('passport/wechat-one', array('option_openid' => $data['openid']));
|
|
|
+
|
|
|
+ if (!$info) {
|
|
|
+ if (isset($data['unionid']) && $data['unionid']) {
|
|
|
+ $info = Dever::load('passport/wechat-one', array('unionid' => $data['unionid']));
|
|
|
+ if (!$info) {
|
|
|
+ $uid = false;
|
|
|
+ } else {
|
|
|
+ $uid = $info['uid'];
|
|
|
+ }
|
|
|
+ $wechat['unionid'] = $data['unionid'];
|
|
|
+ }
|
|
|
+ if (!$uid) {
|
|
|
+ $user['username'] = '临时用户';
|
|
|
+ $uid = Dever::load('passport/user-insert', $user);
|
|
|
+ }
|
|
|
+
|
|
|
+ $wechat['openid'] = $data['openid'];
|
|
|
+ $wechat['session_key'] = $data['session_key'];
|
|
|
+ $wechat['uid'] = $uid;
|
|
|
+ $id = Dever::load('passport/wechat-insert', $wechat);
|
|
|
+ } else {
|
|
|
+ $uid = $info['uid'];
|
|
|
+ if (isset($data['unionid']) && $data['unionid']) {
|
|
|
+ $wechat['unionid'] = $data['unionid'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $wechat['session_key'] = $data['session_key'];
|
|
|
+ $wechat['where_id'] = $info['id'];
|
|
|
+ $id = $info['id'];
|
|
|
+ Dever::load('passport/wechat-update', $wechat);
|
|
|
+ }
|
|
|
+
|
|
|
+ $user = Dever::load('passport/user-one', $uid);
|
|
|
+
|
|
|
+ $result['vid'] = $id;
|
|
|
+ $result['uid'] = $uid;
|
|
|
+ $result['signature'] = Dever::login($uid);
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检测用户有效性
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ private function check()
|
|
|
+ {
|
|
|
+ $signature = Dever::input('signature');
|
|
|
+ $user = Dever::loginResult($signature);
|
|
|
+ if (!isset($user['uid'])) {
|
|
|
+ Dever::alert('user error');
|
|
|
+ }
|
|
|
+ return $user['uid'];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户信息 手机号
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function mobile()
|
|
|
+ {
|
|
|
+ $uid = $this->check();
|
|
|
+ $vid = Dever::input('vid');
|
|
|
+ $result = array();
|
|
|
+ $mobile = $phoneNumber = '';
|
|
|
+ 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) {
|
|
|
+ $info = Dever::load('passport/user-one', $uid);
|
|
|
+ $result['mobile'] = $mobile;
|
|
|
+ if ($info) {
|
|
|
+ $update['set_mobile'] = $mobile;
|
|
|
+ $update['where_id'] = $uid;
|
|
|
+ Dever::load('passport/user-update', $update);
|
|
|
+ } else {
|
|
|
+ Dever::alert('无效的用户id,请重新登录');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $result['vid'] = $vid;
|
|
|
+ $result['uid'] = $uid;
|
|
|
+ $result['signature'] = Dever::login($uid);
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户信息
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function update()
|
|
|
+ {
|
|
|
+ $uid = $this->check();
|
|
|
+ $name = Dever::emoji(Dever::input('nickname'));
|
|
|
+ $pic = Dever::input('avatarurl');
|
|
|
+ $sex = Dever::input('gender');
|
|
|
+ $city = Dever::input('city');
|
|
|
+ $mobile = Dever::input('mobile');
|
|
|
+ $province = Dever::input('province');
|
|
|
+ $country = Dever::input('country');
|
|
|
+ $vid = Dever::input('vid');
|
|
|
+
|
|
|
+ $update['set_temp'] = 3;
|
|
|
+ if ($vid) {
|
|
|
+ $vinfo = Dever::load('passport/wechat-one', $vid);
|
|
|
+
|
|
|
+ $unionid = $this->unionid($vinfo['session_key'], $iv);
|
|
|
+ if ($unionid) {
|
|
|
+ $vinfo = Dever::load('passport/wechat-one', array('option_unionid' => $unionid));
|
|
|
+ if (!$vinfo) {
|
|
|
+ $wechat['set_unionid'] = $unionid;
|
|
|
+ $wechat['where_id'] = $vid;
|
|
|
+ Dever::load('passport/wechat-update', $wechat);
|
|
|
+ } elseif ($vinfo && $vinfo['id'] != $vid) {
|
|
|
+ $wechat['set_unionid'] = $unionid;
|
|
|
+ $wechat['set_uid'] = $vinfo['uid'];
|
|
|
+ $wechat['where_id'] = $vid;
|
|
|
+
|
|
|
+ # 删除user信息
|
|
|
+ if ($uid != $vinfo['uid']) {
|
|
|
+ Dever::load('passport/user-update', array('set_state' => 2, 'where_id' => $uid));
|
|
|
+ }
|
|
|
+
|
|
|
+ $uid = $vinfo['uid'];
|
|
|
+ Dever::load('passport/wechat-update', $wechat);
|
|
|
+ }
|
|
|
+
|
|
|
+ $update['set_temp'] = 2;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $info = Dever::load('passport/user-one', $uid);
|
|
|
+ if ($info) {
|
|
|
+ $update['set_username'] = $name;
|
|
|
+ $update['set_sex'] = $this->saveSex($sex);
|
|
|
+ $update['set_avatar'] = $this->saveAvatar($pic);
|
|
|
+ //$update['set_city'] = $city;
|
|
|
+ //$update['set_mobile'] = $mobile;
|
|
|
+ //$update['set_province'] = $province;
|
|
|
+ //$update['set_country'] = $country;
|
|
|
+ //$update['where_id'] = $uid;
|
|
|
+ Dever::load('passport/user-update', $update);
|
|
|
+ } 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);
|
|
|
+ if ($data && isset($data->unionId)) {
|
|
|
+ return $data->unionId;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function decryptData($session_key)
|
|
|
+ {
|
|
|
+ $iv = Dever::input('iv');
|
|
|
+ $encryptedData = Dever::input('encryptedData');
|
|
|
+
|
|
|
+ if (!$iv || !$encryptedData) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (strlen($session_key) != 24) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (strlen($iv) != 24) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $aesKey = base64_decode($session_key);
|
|
|
+
|
|
|
+ $aesIV = base64_decode($iv);
|
|
|
+
|
|
|
+ $aesCipher = base64_decode($encryptedData);
|
|
|
+
|
|
|
+ $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
|
|
|
+
|
|
|
+ $dataObj = json_decode($result);
|
|
|
+ if ($dataObj == NULL) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if($dataObj->watermark->appid != $appid) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return $dataObj;
|
|
|
+ }
|
|
|
+}
|