Commit 10f2a528 by ArronYR

modify auth

parent 4a4e4a7f
...@@ -22,139 +22,18 @@ App({ ...@@ -22,139 +22,18 @@ App({
*/ */
checkUserLogin: function(callback) { checkUserLogin: function(callback) {
let _app = this let _app = this
if (_app.globalData.userInfo == null) { if (_app.globalData.userInfo == null || !wx.getStorageSync('token')) {
_app.requestWxLogin(function(res) { wx.navigateTo({
typeof callback == "function" && callback() url: '/pages/auth/index',
}) })
} else { } else {
console.log("用户已登录", _app.globalData.userInfo) api.me.info().then(res => {
typeof callback == "function" && callback() console.log(res)
})
} }
}, },
/**
* 请求微信授权登陆
*/
requestWxLogin: function(callback) {
let that = this
wx.login({
success: function(res) {
if (res.code) {
//发起网络请求
let wxcode = res.code
wx.getUserInfo({
withCredentials: true,
lang: 'zh_CN',
success: function(data) {
console.info("1成功获取用户返回数据", wxcode, data);
that.requestServerLogin(wxcode, data, callback)
},
fail: function() {
console.info("1授权失败返回数据");
// 显示提示弹窗
wx.showModal({
title: '授权失败',
content: '你的微信授权未开启,将无法使用小程序!',
success: function(res) {
if (res.confirm) {
wx.openSetting({
success: function(data) {
if (data && data.authSetting["scope.userInfo"] == true) {
wx.getUserInfo({
withCredentials: true,
success: function(data) {
console.info("3成功获取用户返回数据");
that.requestServerLogin(wxcode, data, callback)
},
fail: function() {
console.info("3授权失败返回数据");
}
});
}
},
fail: function() {
console.info("设置失败返回数据");
}
});
} else if (res.cancel) {
console.log('用户坚持取消授权')
}
}
});
}
});
}
},
fail: function() {
console.info("登录失败返回数据");
}
});
},
/**
* 请求获取群id openGid 所需参数
*/
requestShareTicket: function(callback) {
let that = this
wx.login({
success: function(res) {
let wxcode = res.code
wx.getShareInfo({
shareTicket: that.options.shareTicket,
success: function(res) {
that.requestServerWithShareData(wxcode, {
encryptedData: res.encryptedData,
iv: res.iv
}, callback)
}
})
}
})
},
/**
* 请求服务器登陆
*/
requestServerLogin: function(wxcode, data, callback) {
let _app = this
api.login({
code: wxcode,
signature: data.signature,
rawData: data.rawData,
encryptedData: data.encryptedData,
iv: data.iv,
scene: _app.options.query && _app.options.query.scene ? decodeURIComponent(_app.options.query.scene) : ''
}).then(res => {
if (!res.code) {
_app.globalData.userInfo = res.user
_app.globalData.token = res.token
typeof callback == "function" && callback(res.user)
}
if (_app.options.scene == 1044 && _app.options.shareTicket) {
_app.requestShareTicket(function(ret) {
console.log("Request Share Ticket", ret)
})
}
})
},
/**
* 请求服务器解析数据获取 openGid
*/
requestServerWithShareData(wxcode, data, callback) {
let _app = this
api.shareTicket({
token: _app.globalData.token,
code: wxcode,
encryptedData: data.encryptedData,
iv: data.iv
}).then(res => res.data).then(res => {
typeof callback == "function" && callback(res)
})
},
globalData: { globalData: {
userInfo: null, userInfo: null
token: ''
} }
}) })
\ No newline at end of file
const Util = require('../utils/Util.js') import api from '../utils/Api.js'
const app = getApp()
module.exports = Behavior({ module.exports = Behavior({
behaviors: [], behaviors: [],
...@@ -7,26 +6,24 @@ module.exports = Behavior({ ...@@ -7,26 +6,24 @@ module.exports = Behavior({
data: { data: {
formApi: 'user/save_formid' formApi: 'user/save_formid'
}, },
attached: function () { }, attached: function() {},
methods: { methods: {
/** /**
* 绑定页面中收集 formId 的 submit 事件 * 绑定页面中收集 formId 的 submit 事件
*/ */
__collectFormId: function (event) { __collectFormId: function(event) {
let formId = event.detail.formId let formId = event.detail.formId
let token = app.globalData ? app.globalData.token : '' this.__postFormId(formId)
this.__postFormId(formId, token)
}, },
/** /**
* 提交 formId 到服务器 * 提交 formId 到服务器
*/ */
__postFormId: function (formId, token) { __postFormId: function(formId) {
let that = this let that = this
app.requestData(that.data.formApi, Util.sign({ api.saveFormId({
token: token,
formid: formId formid: formId
}), 'POST', function (res) { }).then(res => {
console.log('colletion formid: ', res) console.log('colletion formid: ', res)
}) })
} }
......
const Util = require('../utils/Util.js') import api from '../utils/Api.js'
const app = getApp()
module.exports = Behavior({ module.exports = Behavior({
behaviors: [], behaviors: [],
properties: {}, properties: {},
data: {}, data: {},
attached: function () { }, attached: function() {},
methods: { methods: {
/** /**
* 获取手机号按钮回调事件 * 获取手机号按钮回调事件
*/ */
__getPhoneNumber: function (event, callback) { __getPhoneNumber: function(event, callback) {
let that = this let that = this
wx.login({ wx.login({
success: function (res) { success: function(res) {
let encryptedData = event.detail.encryptedData let encryptedData = event.detail.encryptedData
let iv = event.detail.iv let iv = event.detail.iv
that.__decodePhoneData(res.code, encryptedData, iv, function (res) { that.__decodePhoneData(res.code, encryptedData, iv, function(res) {
typeof callback == "function" && callback(res) typeof callback == "function" && callback(res)
}) })
} }
}) })
}, },
/** /**
* 解密用户数据 * 解密用户数据
*/ */
__decodePhoneData: function (code, encryptedData, iv, callback) { __decodePhoneData: function(code, encryptedData, iv, callback) {
let that = this let that = this
app.requestData('user/auth_phone', Util.sign({ api.getPhoneNumber({
token: app.globalData.token,
code: code, code: code,
encryptedData: encryptedData, encryptedData: encryptedData,
iv: iv iv: iv
}), 'POST', function (res) { }).then(res => {
if (!res.code) { if (!res.code) {
typeof callback == "function" && callback(res) typeof callback == "function" && callback(res)
} else { } else {
app.showToastWithImg('授权失败') wx.showToast({
title: '授权失败'
})
} }
}) })
} }
......
// pages/auth/index.js // pages/auth/index.js
Page({ import Wx from '../../utils/Wx.js'
import api from '../../utils/Api.js'
const {
$Toast
} = require('../../iview/base/index');
const app = getApp()
Page({
/** /**
* 页面的初始数据 * 页面的初始数据
*/ */
...@@ -11,56 +17,93 @@ Page({ ...@@ -11,56 +17,93 @@ Page({
/** /**
* 生命周期函数--监听页面加载 * 生命周期函数--监听页面加载
*/ */
onLoad: function (options) { onLoad: function(options) {
}, },
/** /**
* 生命周期函数--监听页面初次渲染完成 * 生命周期函数--监听页面初次渲染完成
*/ */
onReady: function () { onReady: function() {
}, },
/** /**
* 生命周期函数--监听页面显示 * 生命周期函数--监听页面显示
*/ */
onShow: function () { onShow: function() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
}, },
/** handleWxAuth: function(event) {
* 页面相关事件处理函数--监听用户下拉动作 Wx.login().then(res => {
*/ api.login({
onPullDownRefresh: function () { code: res.code,
signature: event.detail.signature,
rawData: event.detail.rawData,
encryptedData: event.detail.encryptedData,
iv: event.detail.iv,
scene: app.options.query && app.options.query.scene ? decodeURIComponent(app.options.query.scene) : ''
}).then(res => {
if (!res.code) {
app.globalData.userInfo = res.user
wx.setStorage({
key: 'token',
data: res.token,
})
Wx.showToast({
title: '微信授权成功',
icon: 'success',
duration: 2000,
}).then(() => {
wx.navigateBack({
delta: 1
})
})
} else {
$Toast({
content: '微信授权失败,请重试',
type: 'error'
});
}
})
})
}, },
/** handlePhoneAuth: function(event) {
* 页面上拉触底事件的处理函数 Wx.login().then(res => {
*/ let encryptedData = event.detail.encryptedData
onReachBottom: function () { let iv = event.detail.iv
this.decodePhoneData(res.code, encryptedData, iv)
})
}, },
/** /**
* 用户点击右上角分享 * 解密用户数据
*/ */
onShareAppMessage: function () { decodePhoneData: function(code, encryptedData, iv) {
let that = this
api.getPhoneNumber({
code: code,
encryptedData: encryptedData,
iv: iv
}).then(res => {
if (!res.code) {
$Toast({
content: '手机号授权成功',
type: 'success'
});
let userInfo = app.globalData.userInfo
app.globalData.userInfo = {
...userInfo,
...res.data
}
} else {
$Toast({
content: '手机号授权失败',
type: 'error'
});
}
})
} }
}) })
\ No newline at end of file
{ {
"navigationBarTitleText": "微信授权" "navigationBarTitleText": "微信授权",
"usingComponents": {
"i-button": "/iview/button/index",
"i-toast": "/iview/toast/index"
}
} }
\ No newline at end of file
...@@ -2,7 +2,17 @@ ...@@ -2,7 +2,17 @@
<view class='v-page'> <view class='v-page'>
<view class='container'> <view class='container'>
<image src='/images/icon_location.png' mode='scaleToFill'></image> <image class='logo' src='/images/icon_location.png' mode='scaleToFill'></image>
<view></view> <view class='slogan'>
<view>没行者亲子游</view>
<view>美好的教育,行走在路上</view>
</view>
<view class='action-wrapper'>
<i-button bindgetuserinfo="handleWxAuth" type="success" shape="circle" open-type='getUserInfo'>微信授权</i-button>
<i-button bindgetphonenumber="handlePhoneAuth" type="info" shape="circle" open-type="getPhoneNumber">手机号授权</i-button>
<view class="text-small text-hint tip">请完成2步,微信授权</view>
</view>
</view> </view>
</view> </view>
\ No newline at end of file
<i-toast id="toast" />
\ No newline at end of file
/* pages/auth/index.wxss */ /* pages/auth/index.wxss */
page { page {
position: relative;
background: #fff; background: #fff;
height: 100%;
}
.v-page {
position: relative;
height: 100%;
}
.container {
position: relative;
display: flex;
flex-direction: column;
align-content: center;
align-items: center;
}
.logo {
width: 68%;
}
.slogan {
text-align: center;
width: 100%;
color: #767676;
font-size: 16px;
margin-top: 30px;
margin-bottom: 40px;
line-height: 24px;
}
.action-wrapper {
width: 100%;
padding: 0 40px;
box-sizing: border-box;
}
.action-wrapper .tip {
text-align: center;
} }
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
"backgroundColor": "#f5f5f5", "backgroundColor": "#f5f5f5",
"usingComponents": { "usingComponents": {
"i-avatar": "/iview/avatar/index", "i-avatar": "/iview/avatar/index",
"i-cell-group": "/iview/cell-group/index", "i-cell-group": "/iview/cell-group/index",
"i-cell": "/iview/cell/index" "i-cell": "/iview/cell/index",
"i-row": "/iview/row/index",
"i-col": "/iview/col/index"
} }
} }
\ No newline at end of file
...@@ -17,29 +17,33 @@ ...@@ -17,29 +17,33 @@
<i-cell title="我的订单" is-link url="/pages/dashboard/index"> <i-cell title="我的订单" is-link url="/pages/dashboard/index">
<text slot="footer" class='text-gray'>全部</text> <text slot="footer" class='text-gray'>全部</text>
</i-cell> </i-cell>
<view class='order-status-box'> <i-row i-class="order-status-box">
<view class='order-items'> <i-col span="8" i-class="col-class">
<view class='order-item'> <view class='order-item'>
<image src='/images/personal_center/icon_be_in_progress.png'></image> <image src='/images/personal_center/icon_be_in_progress.png'></image>
<text>进行中</text> <text>进行中</text>
</view> </view>
</i-col>
<i-col span="8" i-class="col-class">
<view class='order-item'> <view class='order-item'>
<image src='/images/personal_center/icon_to_be_evaluated.png'></image> <image src='/images/personal_center/icon_to_be_evaluated.png'></image>
<text>待评价</text> <text>待评价</text>
</view> </view>
</i-col>
<i-col span="8" i-class="col-class">
<view class='order-item'> <view class='order-item'>
<image src='/images/personal_center/icon_completed.png'></image> <image src='/images/personal_center/icon_completed.png'></image>
<text>已完成</text> <text>已完成</text>
</view> </view>
</view> </i-col>
</view> </i-row>
</i-cell-group> </i-cell-group>
<i-cell-group i-class='vip'> <i-cell-group i-class='vip'>
<i-cell title="VIP会员" is-link url="/pages/dashboard/index"> <i-cell title="VIP会员" is-link url="/pages/dashboard/index">
<text slot="footer" class='text-gray'>全部</text> <text slot="footer" class='text-gray'>全部</text>
</i-cell> </i-cell>
<i-cell title="我的收藏" is-link url="/pages/dashboard/index"></i-cell> <i-cell title="我的收藏" is-link url="/pages/collection/index"></i-cell>
</i-cell-group> </i-cell-group>
<i-cell-group> <i-cell-group>
......
...@@ -35,7 +35,7 @@ page { ...@@ -35,7 +35,7 @@ page {
transform: translateX(-50%); transform: translateX(-50%);
} }
.avatar_box>image { .avatar_box > image {
width: 120rpx; width: 120rpx;
height: 120rpx; height: 120rpx;
border-radius: 50%; border-radius: 50%;
...@@ -50,7 +50,7 @@ page { ...@@ -50,7 +50,7 @@ page {
align-items: center; align-items: center;
} }
.user-info-box>text { .user-info-box > text {
color: #fff; color: #fff;
font-size: 30rpx; font-size: 30rpx;
} }
...@@ -65,20 +65,11 @@ page { ...@@ -65,20 +65,11 @@ page {
.order-status-box { .order-status-box {
background-color: #fff; background-color: #fff;
height: 182rpx; padding: 20px 0;
margin-bottom: 20rpx; margin-bottom: 20rpx;
width: 100%; width: 100%;
} }
.order-items {
background-color: #fff;
padding: 32rpx 0 0 0;
margin: auto;
display: flex;
width: 480rpx;
justify-content: space-between;
}
.order-item { .order-item {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
...@@ -90,8 +81,8 @@ page { ...@@ -90,8 +81,8 @@ page {
} }
.order-item > image { .order-item > image {
width: 40rpx; width: 48rpx;
height: 44rpx; height: 48rpx;
} }
.vip { .vip {
......
...@@ -14,6 +14,10 @@ export const shareTicket = params => { ...@@ -14,6 +14,10 @@ export const shareTicket = params => {
return req.post('user/get_groupid', params).then(res => res.data) return req.post('user/get_groupid', params).then(res => res.data)
} }
export const getPhoneNumber = params => {
return req.post('user/getPhoneNumber', params).then(res => res.data)
}
export const home = { export const home = {
banner: params => { banner: params => {
return req.post('banner/index', params).then(res => res.data) return req.post('banner/index', params).then(res => res.data)
...@@ -59,10 +63,16 @@ export const search = { ...@@ -59,10 +63,16 @@ export const search = {
} }
} }
export const saveFormId = params => {
return req.post('user/save_formid', params).then(res => res.data)
}
export default { export default {
login, login,
shareTicket, shareTicket,
getPhoneNumber,
home, home,
guide, guide,
search search,
saveFormId
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment