diff --git a/app/Http/Controllers/Api/AuthenticateController.php b/app/Http/Controllers/Api/AuthenticateController.php index 3eb69d8..53e9e42 100644 --- a/app/Http/Controllers/Api/AuthenticateController.php +++ b/app/Http/Controllers/Api/AuthenticateController.php @@ -6,9 +6,9 @@ use App\User; use Carbon\Carbon; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; +use Overtrue\LaravelWeChat\Facade as EasyWeChat; use Socialite; use Validator; -use EasyWeChat; class AuthenticateController extends ApiController { @@ -27,31 +27,23 @@ class AuthenticateController extends ApiController return 'openid'; } - /** * 处理小程序的自动登陆和注册 * @param Request $request * @return mixed + * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException */ public function auto_login(Request $request) { - $mini = EasyWeChat::miniProgram(); // 获取openid - if ($request->code) { - $wx_info = $mini->auth->session($request->code); - } - - if (!$request->openid && empty($wx_info['openid'])) { + $mini = EasyWeChat::miniProgram(); + $miniInfo = $mini->auth->session($request->get('code')); + // 判断用户是否合法 + if (!$request->get('openid') && empty($miniInfo['openid'])) { return $this->failed('用户openid没有获取到', 401); } - - $openid = empty($wx_info['openid'])?$request->openid:$wx_info['openid']; - - + $openid = empty($miniInfo['openid']) ? $request->get('openid') : $miniInfo['openid']; $userInfo = User::where('openid', $openid)->first(); - - return $userInfo; - if ($userInfo && $userInfo->toArray()) { //执行登录 $userInfo->login_ip = $this->getClientIP(); @@ -59,10 +51,10 @@ class AuthenticateController extends ApiController $userInfo->save(); // 直接创建token $token = $userInfo->createToken($openid)->accessToken; - return $this->success(compact('token','userInfo')); + return $this->success(compact('token', 'userInfo')); } else { //执行注册 - return $this->register($request,$openid); + return $this->register($request, $openid); } } @@ -70,17 +62,18 @@ class AuthenticateController extends ApiController * 用户注册 * @param Request $request */ - public function register($request,$openid) + public function register($request, $openid) { // 进行基本验证 - $user_info = \GuzzleHttp\json_decode($request->input('rawData'),true); + return $request->input('rawData'); + $user_info = \GuzzleHttp\json_decode($request->input('rawData'), true); //注册信息 字段名=》get到的值 $newUser = [ 'openid' => $openid, //openid 'nickname' => $user_info['nickName'],// 昵称 - 'email' => time().'sqc157400661@163.com',// 邮箱 + 'email' => time() . 'sqc157400661@163.com',// 邮箱 'name' => $user_info['nickName'],// 昵称 - 'avatar' =>$user_info['avatarUrl'], //头像 + 'avatar' => $user_info['avatarUrl'], //头像 'unionid' => '', // unionid (可空) 'state' => 1, 'role' => 0, @@ -92,7 +85,7 @@ class AuthenticateController extends ApiController $userInfo = User::create($newUser); // 直接创建token $token = $userInfo->createToken($openid)->accessToken; - return $this->success(compact('token','userInfo')); + return $this->success(compact('token', 'userInfo')); } protected function sendFailedLoginResponse(Request $request) diff --git a/mini/app.js b/mini/app.js index 2db4b73..5ba8714 100644 --- a/mini/app.js +++ b/mini/app.js @@ -5,6 +5,56 @@ var user = require('./services/user.js'); App({ onLaunch: function () { + wx.navigateTo({ + url: '/pages/welcome/welcome', + }) + + // wx.login({ + // success: res =>{ + // console.log(res); + // }, + // fail: error => { + // console.log(error); + // } + // }) + + // wx.getSetting({ + // success: res => { + // if(res.authSetting['scope.userInfo']){ + // console.log("已授权"); + // wx.getUserInfo({ + // success: res => { + // console.log(res); + // } + // }) + // }else{ + // console.log("未授权"); + // } + // } + // }) + + // wx.showModal({ + + // title: '提示', + + // content: '这是一个模态弹窗', + + // success: function (res) { + + // if (res.confirm) {//这里是点击了确定以后 + + // console.log('用户点击确定') + + // } else {//这里是点击了取消以后 + + // console.log('用户点击取消') + + // } + + // } + + // }) + // wx.getLocation({ // success: function(res) { // console.log(res); @@ -53,28 +103,28 @@ App({ // } // }); - wx.login({ - success: function (res) { - console.log('登录成功' + res); - if (res.code) { - //发起网络请求 - wx.request({ - url: 'http://127.0.0.1:8000/api/login', - data: { - code: res.code - }, - success: res => { - console.log(res); - }, - fail: error => { - console.log(error) - } - }) - } else { - console.log('登录失败!' + res.errMsg) - } - } - }); + // wx.login({ + // success: function (res) { + // console.log('登录成功' + res); + // if (res.code) { + // //发起网络请求 + // wx.request({ + // url: 'http://127.0.0.1:8000/api/login', + // data: { + // code: res.code + // }, + // success: res => { + // console.log(res); + // }, + // fail: error => { + // console.log(error) + // } + // }) + // } else { + // console.log('登录失败!' + res.errMsg) + // } + // } + // }); // //获取用户的登录信息 @@ -88,12 +138,37 @@ App({ }, + // globalData: { + // userInfo: { + // nickname: '游客', + // username: '去登录', + // avatar: 'http://mp.fengyuexingzi.top/images/default_head.png' + // }, + // token: '', + // } + + getUserInfo: function (cb) { + var that = this + if (this.globalData.userInfo) { + typeof cb == "function" && cb(this.globalData.userInfo) + } else { + //调用登录接口 + wx.login({ + success: function () { + wx.getUserInfo({ + success: function (res) { + console.log(res); + wx.setStorageSync('isFirst', res.userInfo); + that.globalData.userInfo = res.userInfo + typeof cb == "function" && cb(that.globalData.userInfo) + } + }) + } + }) + } + }, + globalData: { - userInfo: { - nickname: '游客', - username: '去登录', - avatar: 'http://mp.fengyuexingzi.top/images/default_head.png' - }, - token: '', + userInfo: null } }) \ No newline at end of file diff --git a/mini/app.json b/mini/app.json index 224ee13..187c6c5 100644 --- a/mini/app.json +++ b/mini/app.json @@ -34,7 +34,9 @@ "pages/shopping/address/address", "pages/shopping/addressAdd/addressAdd", "pages/goods/goods", - "pages/topicDetail/topicDetail" + "pages/topicDetail/topicDetail", + "pages/welcome", + "pages/welcome/welcome" ], "window": { "backgroundTextStyle": "dark", diff --git a/mini/pages/index/index.js b/mini/pages/index/index.js index 025b966..3249fef 100644 --- a/mini/pages/index/index.js +++ b/mini/pages/index/index.js @@ -12,7 +12,8 @@ Page({ brands: [], floorGoods: [], carouselInfo: [], - specialList: [] + specialList: [], + userInfo: {} }, onShareAppMessage: function () { return { @@ -39,7 +40,16 @@ Page({ }); }, onLoad: function (options) { + //this.getIndexData(); + var that = this + app.getUserInfo(function (userInfo) { + //更新数据 + that.setData({ + userInfo: userInfo + }) + }) + }, onReady: function () { // 页面渲染完成 diff --git a/mini/pages/index/index.wxml b/mini/pages/index/index.wxml index 7ad53cf..de5c664 100644 --- a/mini/pages/index/index.wxml +++ b/mini/pages/index/index.wxml @@ -1,6 +1,7 @@ +