123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace User\Lib;
- use Dever;
- # 小程序
- class Applet
- {
- public function getOpenid($uid, $system_source = 5, $project_id = 1)
- {
- $openid = false;
- $code = Dever::input('code');
- if ($code) {
- $info = $this->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;
- }
- }
|