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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. console.log(e);
  44. resolve()
  45. }).catch(e=>{
  46. console.log(e);
  47. reject()
  48. }).finally(e=>{
  49. console.log(e);
  50. uni.hideLoading()
  51. })
  52. },
  53. fail: (err) => {
  54. console.error(err);
  55. reject()
  56. }
  57. })
  58. }
  59. })
  60. })
  61. },
  62. async bindMobileByMpWeixin(e) {
  63. console.log(e);
  64. if (e.detail.errMsg == "getPhoneNumber:ok") {
  65. console.log(e.detail);
  66. //检查登录信息是否过期,否则通过重新登录刷新session_key
  67. await this.beforeGetphonenumber()
  68. uniIdCo.bindMobileByMpWeixin(e.detail).then(e => {
  69. console.log(e);
  70. this.$emit('success')
  71. }).finally(e => {
  72. this.closeMe()
  73. })
  74. } else {
  75. this.closeMe()
  76. }
  77. },
  78. async open() {
  79. this.$refs.popup.open()
  80. },
  81. closeMe(e) {
  82. this.$refs.popup.close()
  83. }
  84. }
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. @import "@/uni_modules/uni-id-pages/common/login-page.scss";
  89. view {
  90. display: flex;
  91. }
  92. .box {
  93. background-color: #FFFFFF;
  94. height: 200px;
  95. width: 750rpx;
  96. flex-direction: column;
  97. border-top-left-radius: 15px;
  98. border-top-right-radius: 15px;
  99. }
  100. .headBox {
  101. padding: 20rpx;
  102. height: 80rpx;
  103. line-height: 80rpx;
  104. text-align: left;
  105. font-size: 16px;
  106. color: #333333;
  107. margin-left: 15rpx;
  108. }
  109. .tip {
  110. color: #666666;
  111. text-align: left;
  112. justify-content: center;
  113. margin: 10rpx 30rpx;
  114. font-size: 18px;
  115. }
  116. .btnBox {
  117. margin-top: 45rpx;
  118. justify-content: center;
  119. flex-direction: row;
  120. }
  121. .close,
  122. .agree {
  123. text-align: center;
  124. width: 200rpx;
  125. height: 80upx;
  126. line-height: 80upx;
  127. border-radius: 5px;
  128. margin: 0 20rpx;
  129. font-size: 14px;
  130. }
  131. .close {
  132. color: #999999;
  133. border-color: #EEEEEE;
  134. border-style: solid;
  135. border-width: 1px;
  136. background-color: #FFFFFF;
  137. }
  138. .close:active {
  139. color: #989898;
  140. background-color: #E2E2E2;
  141. }
  142. .agree {
  143. color: #FFFFFF;
  144. }
  145. /* #ifdef MP */
  146. .agree::after {
  147. border: none;
  148. }
  149. /* #endif */
  150. .agree:active {
  151. background-color: #F5F5F6;
  152. }
  153. </style>