Commit 42e9e73e by 汪睦雄

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

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