123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace Passport\Src;
- use Dever;
- use Content\Lib\Base;
- class Login extends Base
- {
- /**
- * 用户绑定 生成用户信息
- *
- * @return mixed
- */
- public function bind()
- {
- $appid = $this->data['info']['appid'];
- $secret = $this->data['info']['secret'];
- $url = Dever::config('base')->wechat;
- $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 = json_decode($data);
- //$data = array();
- //$data['openid'] = 2;
- //$data['unionid'] = 1;
- //$data['session_key'] = 1;
- $user = $this->create($data);
- return $user;
- }
- /**
- * 更新用户信息
- *
- * @return mixed
- */
- public function update()
- {
- $this->check_user();
- $uid = Dever::input('uid');
- $name = 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');
- $info = Dever::load('passport/user-one', $uid);
- if ($info) {
- $update['set_name'] = $name;
- $update['set_pic'] = $pic;
- $update['set_sex'] = $sex;
- $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,请重新登录');
- }
- return $uid;
- }
- /**
- * 生成用户,返回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('option_unionid' => $data['unionid']));
- if (!$info) {
- $uid = false;
- } else {
- $uid = $info['uid'];
- }
- $wechat['add_unionid'] = $data['unionid'];
- }
- if (!$uid) {
- $user['add_name'] = '临时用户';
- $uid = Dever::load('passport/user-insert', $user);
- }
-
- $wechat['add_openid'] = $data['openid'];
- $wechat['add_session_key'] = $data['session_key'];
- $wechat['add_uid'] = $uid;
- $wechat['add_info_id'] = $this->data['info']['id'];
- Dever::load('passport/wechat-insert', $wechat);
- } else {
- $uid = $info['uid'];
- $wechat['set_session_key'] = $data['session_key'];
- $wechat['where_id'] = $info['id'];
- Dever::load('passport/wechat-update', $wechat);
- }
- $result['uid'] = $uid;
- $result['session'] = $this->session($uid);
- return $result;
- }
- }
|