check(); $user = Dever::load('passport/user-one', $uid); if ($user && $user['birthday']) { $user['birthday'] = date('Y-m-d', $user['birthday']); } return $user; } /** * 用户绑定 生成用户信息 * * @return mixed */ public function bind_commit() { $create = Dever::input('create', 3); $data = $this->getLoginInfo(); if ($create == 1) { # 直接返回用户信息 $info = Dever::db('passport/wechat')->one(array('openid' => $data['openid'])); $data = array('uid' => false); if ($info) { //$user = Dever::db('passport/user')->one($uid); $data = $this->getSign($info['uid'], $info['id']); } } elseif ($create == 2) { # 未授权,会生成临时用户,针对有的项目,不需要授权,但是还要生成用户 $data = $this->create($data, false); } return $data; } /** * 一次性登录:通过code或者openid、sessionkey来注册用户,此时已经授权,可以直接拿到unioinid,相当于注册 * * @return mixed */ public function login_commit() { $data = array(); $data = $this->getLoginInfo(); $data += $this->getWechatData($data['session_key']); $mobile = Dever::input('mobile'); if ($mobile) { $data['mobile'] = Dever::load('passport/reg')->checkMobileExists(false); } $data['username'] = Dever::input('nickname'); $data['avatar'] = Dever::input('avatarurl'); $data['sex'] = Dever::input('gender'); $data['city'] = Dever::input('city'); $data['province'] = Dever::input('province'); $data['country'] = Dever::input('country'); $user['county'] = Dever::input('county'); $result = $this->create($data); return $result; } /** * 更新用户信息 * * @return mixed */ public function update_commit() { $uid = $this->check(); $vid = Dever::input('vid'); $info = Dever::db('passport/user')->one($uid); if ($info) { if ($info['temp'] == 1) { # 针对未授权,生成临时用户的用户进行设置积分 Dever::score($uid, 'bind_wechat', '用户微信授权'); } $data['temp'] = 2; $data['username'] = Dever::input('nickname'); $data['avatar'] = Dever::input('avatar'); $data['sex'] = Dever::input('gender'); $data['city'] = Dever::input('city'); $data['province'] = Dever::input('province'); $data['country'] = Dever::input('country'); if ($data['sex'] == 1) { $data['sex'] = 1; } elseif ($data['sex'] == 2) { $data['sex'] = 2; } else { $data['sex'] = 3; } $this->updateUser($uid, $data); } else { Dever::alert('无效的用户id,请重新登录'); } $result = $this->getSign($uid, $vid); return $result; } /** * 更新用户信息 手机号 * * @return mixed */ public function mobile() { $uid = $this->check(); $vid = Dever::input('vid'); if (!$vid) { $mobile = Dever::input('mobile'); if (!$mobile) { Dever::alert('无效的用户id,请重新登录'); } } $code = Dever::input('code'); if ($code) { Dever::load('passport/applet.login_commit'); } $result = array(); $mobile = $phoneNumber = ''; $iv = Dever::input('iv'); $encryptedData = Dever::input('encryptedData'); if ($iv && $encryptedData) { $key = $this->cache . $vid; $session_key = Dever::cache($key); if (!$session_key) { $vinfo = Dever::db('passport/wechat')->one($vid); $session_key = $vinfo['session_key']; } $data = $this->getWechatData($session_key); if ($data && $data['mobile']) { $mobile = $data['mobile']; $phoneNumber = $data['phone']; } } if ($mobile && $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', '绑定手机号'); } $avatar = Dever::input('avatar'); if ($avatar) { $update['avatar'] = $avatar; } $update['mobile'] = $mobile; $update['bind'] = 1; $update['where_id'] = $uid; $name = '商城会员' . substr($mobile, -5); if (!$info['username'] || strstr($info['username'], 'G')) { $update['username'] = $name; } Dever::db('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 = $this->getSign($uid, $vid, $mobile); $user = Dever::load('passport/user-one', array('id' => $uid, 'clear' => true)); $result['user'] = $user; return $result; } /** * 生成用户,返回uid * * @return int */ public function create($data, $state = true) { $uid = 0; $data['system'] = Dever::input('system', 1); $system_source = Dever::input('system_source', 5); $info = Dever::db('passport/wechat')->one(array('openid' => $data['openid'])); $wechat = array(); if (!$info) { if (isset($data['unionid']) && $data['unionid']) { $info = Dever::db('passport/wechat')->one(array('unionid' => $data['unionid'])); if (!$info) { $uid = 0; } else { # 判断用户是否存在,是否需要合并 //$uid = $this->combine($info['uid'], $data['unionid']); $uid = $info['uid']; } $wechat['unionid'] = $data['unionid']; } if (!$uid) { $uid = $this->reg('applet', $system_source, $data); if ($state) { Dever::score($uid, 'bind_wechat', '用户微信授权'); } } $wechat['openid'] = $data['openid']; $wechat['session_key'] = $data['session_key']; $wechat['uid'] = $uid; # 微信小程序 $wechat['type'] = 1;//即将废弃,统一 $wechat['system_source'] = $system_source; $wechat['system_id'] = $data['system']; $id = Dever::db('passport/wechat')->insert($wechat); $key = $this->cache . $id; $cache = Dever::cache($key, $data['session_key']); } else { $uid = $info['uid']; $id = $info['id']; /* if (isset($data['unionid']) && $data['unionid']) { # 判断用户是否存在,是否需要合并 $wechat['uid'] = $this->combine($uid, $data['unionid']); if ($wechat['uid'] != $uid) { $uid = $wechat['uid']; } } */ $key = $this->cache . $id; $cache = Dever::cache($key, $data['session_key']); $wechat['session_key'] = $data['session_key']; if ($wechat) { $wechat['where_id'] = $id; Dever::db('passport/wechat')->update($wechat); } } $user = Dever::db('passport/user')->one($uid); $result = $this->getSign($uid, $id); if (isset($user['mobile']) && $user['mobile']) { $result['mobile'] = $user['mobile']; } $result['user'] = $user; return $result; } 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; $project = false; $system = Dever::input('system', 1); if (Dever::project('token')) { $project = Dever::db('token/project')->find($system); } if (!$project) { if (isset($applet['project']) && $applet['project']) { $project = Dever::db($applet['project'])->find($system); } } if ($project) { $applet['appid'] = $project['appid']; $applet['secret'] = $project['secret']; } if (!$applet || !$applet['appid'] || !$applet['secret']) { Dever::alert('错误的appid'); } return $applet; } public function getLoginInfo() { $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']; $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'); //YzJkOThpRFhwZ1lQTF9mZl9hLVZjZnFXemJVenlYcDQ3d3JWekk0b1I4NjBBQ0Naejg4a0VQa0U= //$data = '{"session_key":"aNAXk7nG\/DRYI\/G0KzJRsw==","openid":"oIZ895RZs2ZkywasoZIv6WavPZlQ"}'; if (strstr($data, 'errcode')) { Dever::alert($data); } $data = Dever::json_decode($data); return $data; } }