// 实例化 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);
}
}