master
fengyuexingzi 7 years ago
parent 56fe101227
commit 599413c8d8

@ -6,9 +6,9 @@ use App\User;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Overtrue\LaravelWeChat\Facade as EasyWeChat;
use Socialite; use Socialite;
use Validator; use Validator;
use EasyWeChat;
class AuthenticateController extends ApiController class AuthenticateController extends ApiController
{ {
@ -27,35 +27,22 @@ class AuthenticateController extends ApiController
return 'openid'; return 'openid';
} }
public function easyWechatGetSession($code)
{
// $config = config('wechat.mini_program.default');
// $app = Facade::miniProgram($config);
// return $app->auth->session($code);
}
/** /**
* 处理小程序的自动登陆和注册 * 处理小程序的自动登陆和注册
* @param Request $request * @param Request $request
* @return mixed * @return mixed
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/ */
public function auto_login(Request $request) public function auto_login(Request $request)
{ {
// 获取openid // 获取openid
if ($request->code) {
$wx_info = $this->easyWechatGetSession($request->code);
}
$mini = EasyWeChat::miniProgram(); $mini = EasyWeChat::miniProgram();
$miniInfo = $mini->auth->session($request->get('code'));
return $wx_info; // 判断用户是否合法
if (!$request->get('openid') && empty($miniInfo['openid'])) {
if (!$request->openid && empty($wx_info['openid'])) {
return $this->failed('用户openid没有获取到', 401); 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(); $userInfo = User::where('openid', $openid)->first();
if ($userInfo && $userInfo->toArray()) { if ($userInfo && $userInfo->toArray()) {
//执行登录 //执行登录
@ -78,6 +65,7 @@ class AuthenticateController extends ApiController
public function register($request, $openid) public function register($request, $openid)
{ {
// 进行基本验证 // 进行基本验证
return $request->input('rawData');
$user_info = \GuzzleHttp\json_decode($request->input('rawData'), true); $user_info = \GuzzleHttp\json_decode($request->input('rawData'), true);
//注册信息 字段名=》get到的值 //注册信息 字段名=》get到的值
$newUser = [ $newUser = [

131
mini/app.js vendored

@ -5,6 +5,56 @@ var user = require('./services/user.js');
App({ App({
onLaunch: function () { 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({ // wx.getLocation({
// success: function(res) { // success: function(res) {
// console.log(res); // console.log(res);
@ -53,28 +103,28 @@ App({
// } // }
// }); // });
wx.login({ // wx.login({
success: function (res) { // success: function (res) {
console.log('登录成功' + res); // console.log('登录成功' + res);
if (res.code) { // if (res.code) {
//发起网络请求 // //发起网络请求
wx.request({ // wx.request({
url: 'http://127.0.0.1:8000/api/login', // url: 'http://127.0.0.1:8000/api/login',
data: { // data: {
code: res.code // code: res.code
}, // },
success: res => { // success: res => {
console.log(res); // console.log(res);
}, // },
fail: error => { // fail: error => {
console.log(error) // console.log(error)
} // }
}) // })
} else { // } else {
console.log('登录失败!' + res.errMsg) // console.log('登录失败!' + res.errMsg)
} // }
} // }
}); // });
// //获取用户的登录信息 // //获取用户的登录信息
@ -88,12 +138,37 @@ App({
}, },
globalData: { // globalData: {
userInfo: { // userInfo: {
nickname: '游客', // nickname: '游客',
username: '去登录', // username: '去登录',
avatar: 'http://mp.fengyuexingzi.top/images/default_head.png' // 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)
}
})
}
})
}
}, },
token: '',
globalData: {
userInfo: null
} }
}) })

@ -34,7 +34,9 @@
"pages/shopping/address/address", "pages/shopping/address/address",
"pages/shopping/addressAdd/addressAdd", "pages/shopping/addressAdd/addressAdd",
"pages/goods/goods", "pages/goods/goods",
"pages/topicDetail/topicDetail" "pages/topicDetail/topicDetail",
"pages/welcome",
"pages/welcome/welcome"
], ],
"window": { "window": {
"backgroundTextStyle": "dark", "backgroundTextStyle": "dark",

@ -12,7 +12,8 @@ Page({
brands: [], brands: [],
floorGoods: [], floorGoods: [],
carouselInfo: [], carouselInfo: [],
specialList: [] specialList: [],
userInfo: {}
}, },
onShareAppMessage: function () { onShareAppMessage: function () {
return { return {
@ -39,7 +40,16 @@ Page({
}); });
}, },
onLoad: function (options) { onLoad: function (options) {
//this.getIndexData(); //this.getIndexData();
var that = this
app.getUserInfo(function (userInfo) {
//更新数据
that.setData({
userInfo: userInfo
})
})
}, },
onReady: function () { onReady: function () {
// 页面渲染完成 // 页面渲染完成

@ -1,6 +1,7 @@
<!--index.wxml--> <!--index.wxml-->
<view class="container"> <view class="container">
<button id='abc' open-type="getUserInfo">登录</button>
<swiper class="banner" indicator-dots="true" autoplay="true" interval="3000" duration="1000"> <swiper class="banner" indicator-dots="true" autoplay="true" interval="3000" duration="1000">
<swiper-item wx:for="{{carouselInfo}}" wx:key="{{item.id}}"> <swiper-item wx:for="{{carouselInfo}}" wx:key="{{item.id}}">
<navigator url="{{item.businessId}}"> <navigator url="{{item.businessId}}">

@ -2,7 +2,7 @@
<view class="profile-info" bindtap="goLogin"> <view class="profile-info" bindtap="goLogin">
<image class="avatar" src="{{userInfo.avatar}}"></image> <image class="avatar" src="{{userInfo.avatar}}"></image>
<view class="info"> <view class="info">
<text class="name">{{userInfo.nickname}}</text> <text class="name">{{userInfo.nickname}}11</text>
<!-- <text class="level">{{userInfo.username}}</text> --> <!-- <text class="level">{{userInfo.username}}</text> -->
</view> </view>
</view> </view>

@ -0,0 +1,66 @@
// pages/welcome.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})

@ -0,0 +1,2 @@
<!--pages/welcome.wxml-->
<text>pages/welcome.wxml</text>

@ -0,0 +1 @@
/* pages/welcome.wxss */

@ -0,0 +1,66 @@
// pages/welcome/welcome.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})

@ -0,0 +1,2 @@
<!--pages/welcome/welcome.wxml-->
<text>pages/welcome/welcome.wxml</text>

@ -0,0 +1 @@
/* pages/welcome/welcome.wxss */

@ -10,11 +10,8 @@ const api = require('../config/api.js');
* 调用微信登录 * 调用微信登录
*/ */
function loginByWeixin() { function loginByWeixin() {
let code = null; let code = null;
console.log('promise')
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
console.log('promise')
return util.login().then((res) => { return util.login().then((res) => {
code = res.code; code = res.code;
return util.getUserInfo(); return util.getUserInfo();

@ -112,13 +112,11 @@ function checkSession() {
* 调用微信登录 * 调用微信登录
*/ */
function login() { function login() {
console.log('util login');
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
wx.login({ wx.login({
success: function (res) { success: function (res) {
if (res.code) { if (res.code) {
//登录远程服务器 //登录远程服务器
console.log(res.code);
resolve(res); resolve(res);
} else { } else {
reject(res); reject(res);
@ -132,7 +130,6 @@ function login() {
} }
function getUserInfo() { function getUserInfo() {
console.log('abc');
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
wx.getUserInfo({ wx.getUserInfo({
withCredentials: true, withCredentials: true,
@ -141,7 +138,6 @@ function getUserInfo() {
resolve(res); resolve(res);
}, },
fail: function (err) { fail: function (err) {
console.log(1111111);
console.log(err); console.log(err);
reject(err); reject(err);
} }
@ -180,5 +176,3 @@ module.exports = {
login, login,
getUserInfo, getUserInfo,
} }

@ -15,7 +15,7 @@
"cross-env": "^5.1", "cross-env": "^5.1",
"jquery": "^3.2", "jquery": "^3.2",
"laravel-mix": "^1.0", "laravel-mix": "^1.0",
"lodash": "^4.17.4", "lodash": "^4.17.11",
"vue": "^2.5.7" "vue": "^2.5.7"
} }
} }

Loading…
Cancel
Save