Commit 10f2a528 by ArronYR

modify auth

parent 4a4e4a7f
......@@ -22,139 +22,18 @@ App({
*/
checkUserLogin: function(callback) {
let _app = this
if (_app.globalData.userInfo == null) {
_app.requestWxLogin(function(res) {
typeof callback == "function" && callback()
if (_app.globalData.userInfo == null || !wx.getStorageSync('token')) {
wx.navigateTo({
url: '/pages/auth/index',
})
} else {
console.log("用户已登录", _app.globalData.userInfo)
typeof callback == "function" && callback()
api.me.info().then(res => {
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: {
userInfo: null,
token: ''
userInfo: null
}
})
\ No newline at end of file
const Util = require('../utils/Util.js')
const app = getApp()
import api from '../utils/Api.js'
module.exports = Behavior({
behaviors: [],
......@@ -7,26 +6,24 @@ module.exports = Behavior({
data: {
formApi: 'user/save_formid'
},
attached: function () { },
attached: function() {},
methods: {
/**
* 绑定页面中收集 formId 的 submit 事件
*/
__collectFormId: function (event) {
__collectFormId: function(event) {
let formId = event.detail.formId
let token = app.globalData ? app.globalData.token : ''
this.__postFormId(formId, token)
this.__postFormId(formId)
},
/**
* 提交 formId 到服务器
*/
__postFormId: function (formId, token) {
__postFormId: function(formId) {
let that = this
app.requestData(that.data.formApi, Util.sign({
token: token,
api.saveFormId({
formid: formId
}), 'POST', function (res) {
}).then(res => {
console.log('colletion formid: ', res)
})
}
......
const Util = require('../utils/Util.js')
const app = getApp()
import api from '../utils/Api.js'
module.exports = Behavior({
behaviors: [],
properties: {},
data: {},
attached: function () { },
attached: function() {},
methods: {
/**
* 获取手机号按钮回调事件
*/
__getPhoneNumber: function (event, callback) {
__getPhoneNumber: function(event, callback) {
let that = this
wx.login({
success: function (res) {
success: function(res) {
let encryptedData = event.detail.encryptedData
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)
})
}
})
},
/**
* 解密用户数据
*/
__decodePhoneData: function (code, encryptedData, iv, callback) {
__decodePhoneData: function(code, encryptedData, iv, callback) {
let that = this
app.requestData('user/auth_phone', Util.sign({
token: app.globalData.token,
api.getPhoneNumber({
code: code,
encryptedData: encryptedData,
iv: iv
}), 'POST', function (res) {
}).then(res => {
if (!res.code) {
typeof callback == "function" && callback(res)
} else {
app.showToastWithImg('授权失败')
wx.showToast({
title: '授权失败'
})
}
})
}
......
// 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({
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
onLoad: function(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
onReady: function() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
onShow: function() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
handleWxAuth: function(event) {
Wx.login().then(res => {
api.login({
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'
});
}
})
})
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
handlePhoneAuth: function(event) {
Wx.login().then(res => {
let encryptedData = event.detail.encryptedData
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 @@
<view class='v-page'>
<view class='container'>
<image src='/images/icon_location.png' mode='scaleToFill'></image>
<view></view>
<image class='logo' src='/images/icon_location.png' mode='scaleToFill'></image>
<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>
\ No newline at end of file
</view>
<i-toast id="toast" />
\ No newline at end of file
/* pages/auth/index.wxss */
page {
position: relative;
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 @@
"backgroundColor": "#f5f5f5",
"usingComponents": {
"i-avatar": "/iview/avatar/index",
"i-cell-group": "/iview/cell-group/index",
"i-cell": "/iview/cell/index"
"i-cell-group": "/iview/cell-group/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 @@
<i-cell title="我的订单" is-link url="/pages/dashboard/index">
<text slot="footer" class='text-gray'>全部</text>
</i-cell>
<view class='order-status-box'>
<view class='order-items'>
<i-row i-class="order-status-box">
<i-col span="8" i-class="col-class">
<view class='order-item'>
<image src='/images/personal_center/icon_be_in_progress.png'></image>
<text>进行中</text>
</view>
</i-col>
<i-col span="8" i-class="col-class">
<view class='order-item'>
<image src='/images/personal_center/icon_to_be_evaluated.png'></image>
<text>待评价</text>
</view>
</i-col>
<i-col span="8" i-class="col-class">
<view class='order-item'>
<image src='/images/personal_center/icon_completed.png'></image>
<text>已完成</text>
</view>
</view>
</view>
</i-col>
</i-row>
</i-cell-group>
<i-cell-group i-class='vip'>
<i-cell title="VIP会员" is-link url="/pages/dashboard/index">
<text slot="footer" class='text-gray'>全部</text>
</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>
......
......@@ -35,7 +35,7 @@ page {
transform: translateX(-50%);
}
.avatar_box>image {
.avatar_box > image {
width: 120rpx;
height: 120rpx;
border-radius: 50%;
......@@ -50,7 +50,7 @@ page {
align-items: center;
}
.user-info-box>text {
.user-info-box > text {
color: #fff;
font-size: 30rpx;
}
......@@ -65,20 +65,11 @@ page {
.order-status-box {
background-color: #fff;
height: 182rpx;
padding: 20px 0;
margin-bottom: 20rpx;
width: 100%;
}
.order-items {
background-color: #fff;
padding: 32rpx 0 0 0;
margin: auto;
display: flex;
width: 480rpx;
justify-content: space-between;
}
.order-item {
display: flex;
flex-direction: column;
......@@ -90,8 +81,8 @@ page {
}
.order-item > image {
width: 40rpx;
height: 44rpx;
width: 48rpx;
height: 48rpx;
}
.vip {
......
......@@ -14,6 +14,10 @@ export const shareTicket = params => {
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 = {
banner: params => {
return req.post('banner/index', params).then(res => res.data)
......@@ -59,10 +63,16 @@ export const search = {
}
}
export const saveFormId = params => {
return req.post('user/save_formid', params).then(res => res.data)
}
export default {
login,
shareTicket,
getPhoneNumber,
home,
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