Commit 42e9e73e by 汪睦雄

'修复了授权手机号的问题'

parent 8e1653f6
......@@ -20,6 +20,7 @@ Page({
date: '',
remark: '',
loading: false,
sessionId: ''
},
/**
......@@ -31,6 +32,14 @@ Page({
id: options.id
})
}
// 获取session id
let that = this
this.getSessionKey(function(sid) {
that.setData({
sessionId: sid
})
})
},
/**
......@@ -82,10 +91,109 @@ Page({
})
},
handlePhoneAuth: function(event) {
Wx.login().then(res => {
// 获取session key
let encryptedData = event.detail.encryptedData
let iv = event.detail.iv
this.decodePhoneData(res.code, encryptedData, iv)
let that = this
// 检查session key是否过期,如果过期的话,那么就获取新的再请求,否则直接请求
wx.checkSession({
success: function(res) {
//session_key未过期,直接操作
let sessionId = that.data.sessionId
api.getPhoneNumber({
sessionId: sessionId,
encryptedData: encryptedData,
iv: encodeURIComponent(iv)
}).then(res => {
if (!res.code) {
$Toast({
content: '手机号授权成功',
type: 'success'
});
that.setData({
phone: res.data.phone
})
// 将刚拿到的手机号码放到全局数据中,否则phone数据只会是当前页面数据
if (app.globalData.userInfo) {
app.globalData.userInfo.phone = res.data.phone
}
} else {
$Toast({
content: res.msg,
type: 'error'
});
}
})
},
fail: function() {
console.log('checkSession fail!')
// session_key 已过期,进行登录操作,提示错误
// 获取session id
let that = this
this.getSessionKey(function(sid) {
that.setData({
sessionId: sid
})
// 授权手机号
api.getPhoneNumber({
sessionId: sid,
encryptedData: encryptedData,
iv: encodeURIComponent(iv)
}).then(res => {
if (!res.code) {
$Toast({
content: '手机号授权成功',
type: 'success'
});
that.setData({
phone: res.data.phone
})
// 将刚拿到的手机号码放到全局数据中,否则phone数据只会是当前页面数据
if (app.globalData.userInfo) {
app.globalData.userInfo.phone = res.data.phone
}
} else {
$Toast({
content: res.msg,
type: 'error'
});
}
})
})
}
})
// Wx.login().then(res => {
// let encryptedData = event.detail.encryptedData
// let iv = event.detail.iv
// this.decodePhoneData(res.code, encryptedData, iv)
// })
},
/**
* 获取服务器端 session key
*/
getSessionKey(callback) {
Wx.login().then(res => {
if (res.errMsg == 'login:ok') {
api.getSessionKey({
code: res.code
}).then(res => {
if (res.code == 0) {
typeof callback == "function" && callback(res.data)
} else {
$Toast({
content: res.msg,
type: 'error'
});
}
})
} else {
$Toast({
content: 'wx.login失败',
type: 'error'
});
}
})
},
......
......@@ -31,12 +31,12 @@
<input slot="footer" class='input' type='text' placeholder='请输入您的姓名' bindinput='handleNickname' value='{{nickname}}'></input>
</i-cell>
<i-cell title="手机号" i-class="cell-class">
<block wx:if="{{phone.length == 0}}">
<!-- <block wx:if="{{phone.length == 0}}"> -->
<button slot="footer" bindgetphonenumber="handlePhoneAuth" type="info" open-type="getPhoneNumber" class="btn-auth-phone">授权手机号</button>
</block>
<!-- </block>
<block wx:else>
<text slot="footer">{{phone}}</text>
</block>
</block> -->
</i-cell>
<i-cell title="期望时间" is-link i-class="cell-class">
<picker slot="footer" class='input' mode="date" value="{{date}}" bindchange="handleDateChange" class="picker">
......
......@@ -41,28 +41,11 @@ Page({
}
// 获取session id
Wx.login().then(res => {
if (res.errMsg == 'login:ok') {
api.getSessionKey({
code: res.code
}).then(res => {
if (res.code == 0) {
this.setData({
sessionid: res.data
let that = this
this.getSessionKey(function(sid) {
that.setData({
sessionId: sid
})
} else {
$Toast({
content: res.msg,
type: 'error'
});
}
})
} else {
$Toast({
content: 'wx.login失败',
type: 'error'
});
}
})
},
......@@ -110,23 +93,22 @@ Page({
})
},
// 获取手机号
handlePhoneAuth: function(event) {
// 获取session key
let encryptedData = event.detail.encryptedData
let iv = event.detail.iv
let that = this
// 检查session key是否过期,如果过期的话,那么就获取新的再请求,否则直接请求
wx.checkSession({
success: function(res) {
if (res.errMsg == 'checkSession:ok'){
let sessionId = that.data.sessionid
console.log( 'sessionId', sessionId )
//session_key未过期,直接操作
let sessionId = that.data.sessionId
api.getPhoneNumber({
sessionId: sessionId,
encryptedData: encryptedData,
iv: encodeURIComponent(iv)
}).then(res => {
console.log('iv: ', iv, ' res.code: ', res.code)
if (!res.code) {
$Toast({
content: '手机号授权成功',
......@@ -146,72 +128,71 @@ Page({
});
}
})
}else{
},
fail: function() {
console.log('checkSession fail!')
// session_key 已过期,进行登录操作,提示错误
// 获取session id
let that = this
this.getSessionKey(function(sid) {
that.setData({
sessionId: sid
})
// 授权手机号
api.getPhoneNumber({
sessionId: sid,
encryptedData: encryptedData,
iv: encodeURIComponent(iv)
}).then(res => {
if (!res.code) {
$Toast({
content: res.errMsg,
content: '手机号授权成功',
type: 'success'
});
console.log('checkSession', res.errMsg)
that.setData({
phone: res.data.phone
})
// 将刚拿到的手机号码放到全局数据中,否则phone数据只会是当前页面数据
if (app.globalData.userInfo) {
app.globalData.userInfo.phone = res.data.phone
}
},
fail: function() {
console.log('checkSession', 'fail')
} else {
$Toast({
content: res.msg,
type: 'error'
});
}
})
})
}
})
},
// Wx.login().then(res => {
// console.log('wx.login', res, ' event:', event)
// if (res.errMsg == 'login:ok') {
// api.getSessionKey({
// code: res.code
// }).then(res => {
// if (!res.code) {
// let sessionId = res.data
// let that = this
// api.getPhoneNumber({
// sessionId: sessionId,
// encryptedData: encryptedData,
// iv: encodeURIComponent(iv)
// }).then(res => {
// console.log('iv: ', iv, ' res.code: ', res.code)
// if (!res.code) {
// $Toast({
// content: '手机号授权成功',
// type: 'success'
// });
// this.setData({
// phone: res.data.phone
// })
// // 将刚拿到的手机号码放到全局数据中,否则phone数据只会是当前页面数据
// if (app.globalData.userInfo) {
// app.globalData.userInfo.phone = res.data.phone
// }
// } else {
// $Toast({
// content: res.msg,
// type: 'error'
// });
// }
// })
// } else {
// $Toast({
// content: res.msg,
// type: 'error'
// });
// }
// })
// } else {
// $Toast({
// content: 'wx.login失败',
// type: 'error'
// });
// }
// // let encryptedData = event.detail.encryptedData
// // let iv = event.detail.iv
// // this.decodePhoneData(res.code, encryptedData, iv)
// })
/**
* 获取服务器端 session key
*/
getSessionKey(callback) {
Wx.login().then(res => {
if (res.errMsg == 'login:ok') {
api.getSessionKey({
code: res.code
}).then(res => {
if (res.code == 0) {
typeof callback == "function" && callback(res.data)
} else {
$Toast({
content: res.msg,
type: 'error'
});
}
})
} else {
$Toast({
content: 'wx.login失败',
type: 'error'
});
}
})
},
/**
......
<!--pages/detail/enroll/index.wxml-->
<wxs src="../../common.wxs" module="common"></wxs>
<view class='page-wrapper'>
<view class='project-title'>
......@@ -41,12 +43,12 @@
<input slot="footer" class='input' type='text' placeholder='请输入您的姓名' bindinput='handleNickname' value='{{nickname}}'></input>
</i-cell>
<i-cell title="手机号" i-class="cell-class">
<!-- <block wx:if="{{phone.length == 0}}"> -->
<block wx:if="{{phone.length == 0}}">
<button slot="footer" bindgetphonenumber="handlePhoneAuth" type="info" open-type="getPhoneNumber" class="btn-auth-phone">授权手机号</button>
<!-- </block>
</block>
<block wx:else>
<text slot="footer">{{phone}}</text>
</block> -->
</block>
</i-cell>
<i-cell title="成人人数" is-link i-class="cell-class">
<picker slot="footer" class="num-picker" bindchange="handleAdultNum" range="{{nums}}" value="{{adultNum}}">
......@@ -74,7 +76,7 @@
<i-col span="20" offset='2' i-class="col-class">
<i-button bind:click="handleSubmit" type="info" shape="circle" loading="{{loading}}">
立即支付
<text class='margin-left-10'>¥ {{user.is_vip != 0 ? detail.vip_price*(familyNum+1) : detail.price*(familyNum+1)}}</text>
<text class='margin-left-10'>¥ {{user.is_vip != 0 ? common.numFixed(detail.vip_price*(familyNum+1), 2) : common.numFixed(detail.price*(familyNum+1), 2)}}</text>
</i-button>
</i-col>
</i-row>
......
......@@ -20,7 +20,7 @@ Page({
name: '',
address: '',
wx_number: '',
sessionId: ''
},
handleChange1({
......@@ -71,6 +71,14 @@ Page({
onLoad: function(options) {
this.getUserInfo()
this.getVipPrice()
// 获取session id
let that = this
this.getSessionKey(function (sid) {
that.setData({
sessionId: sid
})
})
},
// 获取个人资料
......@@ -91,28 +99,59 @@ Page({
},
handlePhoneAuth: function(event) {
Wx.login().then(res => {
// 获取session key
let encryptedData = event.detail.encryptedData
let iv = event.detail.iv
this.decodePhoneData(res.code, encryptedData, iv)
let that = this
// 检查session key是否过期,如果过期的话,那么就获取新的再请求,否则直接请求
wx.checkSession({
success: function (res) {
//session_key未过期,直接操作
let sessionId = that.data.sessionId
api.getPhoneNumber({
sessionId: sessionId,
encryptedData: encryptedData,
iv: encodeURIComponent(iv)
}).then(res => {
if (!res.code) {
that.setData({
visible1: false
})
if (res.is_relation) {
that.setData({
visible2: true
})
}
//更新下用户数据
that.getUserInfo()
} else {
$Toast({
content: res.msg,
type: 'error'
});
}
})
},
/**
* 解密手机号数据
*/
decodePhoneData: function(code, encryptedData, iv) {
fail: function () {
// session_key 已过期,进行登录操作,提示错误
// 获取session id
console.log('checkSession fail!')
let that = this
this.getSessionKey(function (sid) {
that.setData({
sessionId: sid
})
// 授权手机号
api.getPhoneNumber({
code: code,
sessionId: sid,
encryptedData: encryptedData,
iv: iv
iv: encodeURIComponent(iv)
}).then(res => {
if (!res.code) {
that.setData({
visible1: false
})
if (res.is_relation){
if (res.is_relation) {
that.setData({
visible2: true
})
......@@ -121,14 +160,42 @@ Page({
that.getUserInfo()
} else {
$Toast({
content: '手机号授权失败',
content: res.msg,
type: 'error'
});
}
})
})
}
})
},
/**
* 获取服务器端 session key
*/
getSessionKey(callback) {
Wx.login().then(res => {
if (res.errMsg == 'login:ok') {
api.getSessionKey({
code: res.code
}).then(res => {
if (res.code == 0) {
typeof callback == "function" && callback(res.data)
} else {
$Toast({
content: res.msg,
type: 'error'
});
}
})
} else {
$Toast({
content: 'wx.login失败',
type: 'error'
});
}
})
},
// 获取VIP单价
getVipPrice: function() {
......
......@@ -37,7 +37,7 @@
"list": []
},
"miniprogram": {
"current": 10,
"current": 11,
"list": [
{
"id": 0,
......@@ -109,7 +109,7 @@
"id": 11,
"name": "预约下次",
"pathName": "pages/detail/appoint/index",
"query": "id=173"
"query": "id=298"
},
{
"id": -1,
......
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