123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace addons\yexam\library;
- use addons\ygame\model\YgameWechat;
- use app\common\model\User;
- use fast\Random;
- use think\Db;
- use think\exception\PDOException;
- /**
- *
- */
- class Service
- {
- public static function connect($type,$params = [])
- {
- $data = [
- 'type' => $type,
- 'openid' => $params['openid'],
- 'avatar' => $params['headimgurl'],
- 'nickname' => isset($params['nickname']) ? $params['nickname'] : '',
- ];
- $auth = \app\common\library\Auth::instance();
- $wechatUser = \app\admin\model\yexam\Third::get(['type'=>$type,'openid' => $params['openid']]);
- if ($wechatUser) {
- $user = User::get($wechatUser['user_id']);
- if (!$user) {
- return false;
- }
- $user->save(['nickname'=>$data['nickname'],'avatar'=>$data['avatar']]);
- $wechatUser->save($data);
- } else {
- // 先随机一个用户名,随后再变更为u+数字id
- $username = Random::alnum(20);
- $password = Random::alnum(6);
- $domain = request()->host();
- Db::startTrans();
- try {
- $result = $auth->register($username, $password, $username . '@' . $domain, '');
- if (!$result) {
- return false;
- }
- $user = $auth->getUser();
- $fields = [
- 'username' => 'wx' . $user->id,
- 'email' => 'wx' . $user->id . '@' . $domain,
- 'nickname'=>$data['nickname'],
- 'avatar'=>$data['avatar']
- ];
- // 更新会员资料
- $user = User::get($user->id);
- $user->save($fields);
- $data['user_id'] = $user->id;
- \app\admin\model\yexam\Third::create($data);
- Db::commit();
- } catch (PDOException $e) {
- Db::rollback();
- return false;
- }
- }
- return $auth->direct($user->id);
- }
- }
|