laravel框架使用easyweachat获取用户信息

	// 实例化 easyweachat 类
	protected function app() {
		$url = get_domain();
		$config = [
			'app_id' => get_setting('wechat_appid'),
			'secret' => get_setting('wechat_secret'),
			//'token' => 'TestToken',
			'response_type' => 'array',

			'oauth' => [
				'scopes' => ['snsapi_userinfo'],
				'callback' => $url . '/api/login/oauth?pid='.request()->input("pid"),
			],

		];

		$wechat_app = Factory::officialAccount($config);

		return $wechat_app;
	}

	//登陆授权
	public function index() {

		$response = $this->app()->oauth->redirect();

		return $response;
	}

	// 登陆回调
	public function oauth(Request $request) {
		$oauth = $this->app()->oauth;
		// 获取 OAuth 授权结果用户信息
		$user = $oauth->user()->toArray();
		if (!empty($user)) {
			if (!empty($request->input('pid'))) {
				$pid = $request->input('pid');
			} else {
				$pid = 0;
			}
			$users = new User;
			$index = $users::where("openid", $user['id'])->select("openid", "recuid")->first();

			if (empty($index)) {
				$ip = $request->getClientIp();
				$info = [
					"pid" => $pid,
					'openid' => $user['id'],
					'nickname' => $user['nickname'],
					'avatar' => $user['avatar'],
					'share_money' => 0,
					'joinuser_money' => 0,
					'user_acount' => 0,
					'add_time' => date("Y-m-d H:i:s"),
					'status' => 1,
					'login_last_ip' => $ip,
				];
				$id = User::insertGetId($info);

			
				$data['status'] = 1;
				$data['msg'] = "授权成功";
			} else {
				$recuid = $index->recuid;
			}
		
			$url = 'http://' . $domain['data'] . "/dist/index.html#/My?recuid=" . $recuid . "&openid=" . $user['id'];
			return redirect($url);

		} else {
			$data['status'] = 0;
			$data['msg'] = '服务器错误';
			return json_encode($data);
		}
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值