use EasyWeChat\Factory;
use EasyWeChat\Factory;
code
encryptData
iv
public function mini_login()
{
$post = $this->request->post();
$app = Factory::miniProgram(config('wechat.miniprogram'));
$res = $app->auth->session($post['code']);
if (isset($res['errcode'])) {
trace(json_encode($res));
returnAPI('登录失败,请稍后重试');
}
// 获取手机号
$get_phone = $app->encryptor->decryptData($res['session_key'], str_replace(' ', '+', input('post.iv')), str_replace(' ', '+', urldecode(input('post.encryptData'))));
if ($app) {
$user = model('user')->get(['phone' => $get_phone['phoneNumber']]);
if (!is_null($user)) {
if (input('agent_id', 0) > 0) {
$user->save(['agent_id' => input('agent_id')]);
}
$this->log_suc($user);
} else {
if (input('agent_id', 0) > 0) {
$agent_id = input('agent_id', 0);
} else {
$agent_id = $this->default_agent()['id'];
}
$data = [];
$data['openid'] = $res['openid'];
$data['agent_id'] = $agent_id;
$data['parent_id'] = input('post.parent_id', 0);
$data['phone'] = $get_phone['phoneNumber'];
$data['nick'] = input('post.nick');
$data['head'] = input('post.head');
$user = User::create($data);
if ($user) {
$this->log_suc($user, '登录成功');
} else {
returnAPI('服务器错误');
}
}
} else {
returnAPI('网络错误');
}
}