getInfo($code); if (isset($info['openid'])) { $openid = $info['openid']; Dever::load('user/lib/wechat')->update($uid, $openid, $system_source, $project_id); } } if (!$openid && $uid > 0) { $wechat = Dever::load('user/lib/wechat')->getInfo($uid, $system_source, $project_id); if ($wechat) { $openid = $wechat['openid']; } } return $openid; } private function getInfo($code) { $session_key = Dever::input('session_key'); if ($session_key) { return array('session_key' => $session_key, 'openid' => $openid); } $applet = $this->getApplet(); $appid = $applet['appid']; $secret = $applet['secret']; $url = $applet['url']; 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); } $data = Dever::json_decode($data); return $data; } private function unionid($session_key) { $data = $this->decryptData($session_key); if ($data && isset($data->unionId)) { return $data->unionId; } return false; } private function getWechatData($session_key) { $result = array(); $data = $this->decryptData($session_key); $result['openid'] = ''; $result['unionid'] = ''; $result['mobile'] = ''; $result['phone'] = ''; if ($data && isset($data->openId)) { $result['openid'] = $data->openId; if (isset($data->unionId)) { $result['unionid'] = $data->unionId; } } if ($data && isset($data->phoneNumber)) { if (isset($data->phoneNumber)) { $result['phone'] = $data->phoneNumber; } } if ($data && isset($data->purePhoneNumber)) { if (isset($data->purePhoneNumber)) { $result['mobile'] = $data->purePhoneNumber; } } return $result; } 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; } /* $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; $system = Dever::input('system', 1); $system = Dever::db('passport/system')->find($system); $project = false; if ($system && $system['token_project_id'] > 0 && Dever::project('token')) { $project = Dever::db('token/project')->find($system['token_project_id']); } if ($project) { $applet['appid'] = $project['appid']; $applet['secret'] = $project['secret']; } if (!$applet || !$applet['appid'] || !$applet['secret']) { Dever::alert('错误的appid'); } return $applet; } }