共计 2336 个字符,预计需要花费 6 分钟才能阅读完成。
后面更新了两篇,这个是第三篇,也就是盲盒购物网站零碎开发建设 第三篇,间接更新代码就能够了。
在公网接口处重写此办法:验证服务器有效性
public function serverValidation()
{ | |
$TOKEN = 'I8cezsHeF1buiCBPwD'; | |
$signature = $_GET["signature"] ?? ""; | |
$timestamp = $_GET["timestamp"] ?? ""; | |
$nonce = $_GET["nonce"] ?? ""; | |
$tmpArr = array($TOKEN, $timestamp, $nonce); | |
sort($tmpArr, SORT_STRING); | |
$tmpStr = implode($tmpArr); | |
$tmpStr = sha1($tmpStr); | |
if ($tmpStr == $signature) {return $_GET["echostr"] ?? ''; | |
} | |
return 'error'; | |
} |
获取用户信息
if (‘snsapi_userinfo’ == $res[‘scope’]) {
$userinfo = file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token={$res['access_token']}&openid={$res['openid']}&lang=zh_CN"); | |
$userinfo = json_decode($userinfo, true); | |
if (!empty($userinfo['errcode']) && !empty($userinfo['errmsg'])) {$redirect = $redirect . (strpos($redirect, '?') ? '&' : '?') . 'status=0&token=&errmsg=' . $userinfo['errmsg']; | |
$this->redirect($redirect ? urldecode($redirect) : '/h5/#/'); | |
exit();} |
绑定过手机,间接去首页
if ($this->has_mobile) {
$this->redirect('/h5/#/?status=1&errmsg=&token=' . $token . "&is_notice=". $this->is_notice); | |
} | |
$redirect = $redirect . (strpos($redirect, '?') ? '&' : '?') . 'status=1&errmsg=&token=' . $token . "&is_notice=". $this->is_notice; | |
$this->redirect($redirect ? urldecode($redirect) : '/h5/#/'); | |
} | |
$errmsg = $this->_error ?: '受权失败'; | |
$redirect = $redirect . (strpos($redirect, '?') ? '&' : '?') . 'status=0&token=&errmsg=' . $errmsg; | |
$this->redirect($redirect ? urldecode($redirect) : '/h5/#/'); | |
} |
登录
private function login($openid, $nickname = ”, $avatar = ”, $invite_code = ”, $is_channel =”)
{$user = \app\common\model\User::getByWechatOpenid($openid); | |
if ($user) {if ($user->status != 'normal') { | |
$this->_error = '账号被锁定'; | |
return false; | |
} | |
// 如果曾经有账号则间接登录 | |
$ret = $this->auth->direct($user->id); | |
} else { | |
$extend = [ | |
'invite_code' => $invite_code, | |
'wechat_openid' => $openid, | |
'nickname' => $nickname, | |
'avatar' => $avatar, | |
]; | |
$ret = $this->auth->register('', Random::alnum(),'', '', $extend); | |
if($ret && $is_channel == '49ba59abbe56e057'){ // 赠送 10 金币 | |
\app\common\model\User::where(['id'=>$this->auth->id])->setInc("coin", 10); | |
$this->is_notice = 1; | |
} | |
} | |
if ($ret) {$data = $this->auth->getUserinfo(); | |
$this->has_mobile = !empty($data['mobile']) ? true : false; | |
return $data['token']; | |
} | |
$this->_error = $this->auth->getError(); | |
return false; | |
} |
微信登录并重定向
public function wechatLoginRedirect()
{$redirect = input('redirect'); | |
if (empty('')) {throw new HttpResponseException(Response::create(['code' => 403, 'msg' => '重定向地址为空', 'data' => null], 'json', 200)); | |
} | |
$this->redirect('/index/wechat/bootToUrl?url=' . base64_encode($redirect)); | |
} |
}
正文完