uni-id-pages-bind-mobile.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <uni-popup ref="popup" type="bottom">
  3. <view class="box">
  4. <text class="headBox">绑定资料</text>
  5. <text class="tip">将一键获取你的手机号码绑定你的个人资料</text>
  6. <view class="btnBox">
  7. <text @click="closeMe" class="close">关闭</text>
  8. <button class="agree uni-btn" type="primary" open-type="getPhoneNumber"
  9. @getphonenumber="bindMobileByMpWeixin">获取</button>
  10. </view>
  11. </view>
  12. </uni-popup>
  13. </template>
  14. <script>
  15. const db = uniCloud.database();
  16. const usersTable = db.collection('uni-id-users')
  17. const uniIdCo = uniCloud.importObject("uni-id-co")
  18. export default {
  19. emits: ['success'],
  20. computed: {},
  21. data() {
  22. return {}
  23. },
  24. methods: {
  25. async beforeGetphonenumber() {
  26. return await new Promise((resolve,reject)=>{
  27. uni.showLoading({ mask: true })
  28. wx.checkSession({
  29. success() {
  30. // console.log('session_key 未过期');
  31. resolve()
  32. uni.hideLoading()
  33. },
  34. fail() {
  35. // console.log('session_key 已经失效,正在执行更新');
  36. wx.login({
  37. success({
  38. code
  39. }) {
  40. uniCloud.importObject("uni-id-co",{
  41. customUI:true
  42. }).loginByWeixin({code}).then(e=>{
  43. resolve()
  44. }).catch(e=>{
  45. console.log(e);
  46. reject()
  47. }).finally(e=>{
  48. uni.hideLoading()
  49. })
  50. },
  51. fail: (err) => {
  52. console.error(err);
  53. reject()
  54. }
  55. })
  56. }
  57. })
  58. })
  59. },
  60. async bindMobileByMpWeixin(e) {
  61. if (e.detail.errMsg == "getPhoneNumber:ok") {
  62. //检查登录信息是否过期,否则通过重新登录刷新session_key
  63. await this.beforeGetphonenumber()
  64. uniIdCo.bindMobileByMpWeixin(e.detail).then(e => {
  65. this.$emit('success')
  66. }).finally(e => {
  67. this.closeMe()
  68. })
  69. } else {
  70. this.closeMe()
  71. }
  72. },
  73. async open() {
  74. this.$refs.popup.open()
  75. },
  76. closeMe(e) {
  77. this.$refs.popup.close()
  78. }
  79. }
  80. }
  81. </script>
  82. <style lang="scss" scoped>
  83. @import "@/uni_modules/uni-id-pages/common/login-page.scss";
  84. view {
  85. display: flex;
  86. }
  87. .box {
  88. background-color: #FFFFFF;
  89. height: 200px;
  90. width: 750rpx;
  91. flex-direction: column;
  92. border-top-left-radius: 15px;
  93. border-top-right-radius: 15px;
  94. }
  95. .headBox {
  96. padding: 20rpx;
  97. height: 80rpx;
  98. line-height: 80rpx;
  99. text-align: left;
  100. font-size: 16px;
  101. color: #333333;
  102. margin-left: 15rpx;
  103. }
  104. .tip {
  105. color: #666666;
  106. text-align: left;
  107. justify-content: center;
  108. margin: 10rpx 30rpx;
  109. font-size: 18px;
  110. }
  111. .btnBox {
  112. margin-top: 45rpx;
  113. justify-content: center;
  114. flex-direction: row;
  115. }
  116. .close,
  117. .agree {
  118. text-align: center;
  119. width: 200rpx;
  120. height: 80upx;
  121. line-height: 80upx;
  122. border-radius: 5px;
  123. margin: 0 20rpx;
  124. font-size: 14px;
  125. }
  126. .close {
  127. color: #999999;
  128. border-color: #EEEEEE;
  129. border-style: solid;
  130. border-width: 1px;
  131. background-color: #FFFFFF;
  132. }
  133. .close:active {
  134. color: #989898;
  135. background-color: #E2E2E2;
  136. }
  137. .agree {
  138. color: #FFFFFF;
  139. }
  140. /* #ifdef MP */
  141. .agree::after {
  142. border: none;
  143. }
  144. /* #endif */
  145. .agree:active {
  146. background-color: #F5F5F6;
  147. }
  148. </style>