uni-bindMobileByMpWeixin.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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" type="warn" @click="beforeGetphonenumber" open-type="getPhoneNumber"
  9. @getphonenumber="bindMobileByMpWeixin">获取</button>
  10. </view>
  11. </view>
  12. </uni-popup>
  13. </template>
  14. <script>
  15. import {
  16. mapMutations,
  17. mapGetters
  18. } from 'vuex';
  19. const db = uniCloud.database();
  20. const usersTable = db.collection('uni-id-users')
  21. let userId = ''
  22. export default {
  23. emits: ['next'],
  24. computed: {
  25. ...mapGetters({
  26. userInfo: 'user/info',
  27. login: 'user/hasLogin'
  28. })
  29. },
  30. data() {
  31. return {}
  32. },
  33. methods: {
  34. ...mapMutations({
  35. setUserInfo: 'user/login'
  36. }),
  37. beforeGetphonenumber() {
  38. uni.showLoading({
  39. mask:true
  40. })
  41. wx.checkSession({
  42. success() {
  43. console.log('session_key 未过期');
  44. uni.hideLoading()
  45. },
  46. fail() {
  47. console.log('session_key 已经失效,正在执行更新');
  48. wx.login({
  49. success({code}) {
  50. uniCloud.callFunction({
  51. name: "uni-id-cf",
  52. data: {
  53. "action": "refreshSessionKey",
  54. "params": {
  55. code
  56. }
  57. },
  58. complete: (e) => {
  59. console.log(e);
  60. uni.hideLoading()
  61. }
  62. })
  63. },
  64. fail: (err) => {
  65. console.error(err);
  66. }
  67. })
  68. }
  69. })
  70. },
  71. bindMobileByMpWeixin(e) {
  72. console.log(e.detail);
  73. if(e.errMsg == "getPhoneNumber:ok"){
  74. uniCloud.callFunction({
  75. name: "uni-id-cf",
  76. data: {
  77. "action": "bindMobileByMpWeixin",
  78. "params": e.detail
  79. },
  80. complete: (e) => {
  81. console.log(e);
  82. },
  83. success: (e) => {
  84. uni.showToast({
  85. title: e.result.msg||'绑定成功',
  86. icon: 'none'
  87. });
  88. if(e.result.code === 0){
  89. this.setUserInfo({
  90. "mobile": e.result.mobile
  91. })
  92. }
  93. this.closeMe()
  94. }
  95. })
  96. }else{
  97. this.closeMe()
  98. }
  99. },
  100. async open(uid) {
  101. userId = uid
  102. this.$refs.popup.open()
  103. this.beforeGetphonenumber()
  104. },
  105. closeMe(e) {
  106. this.$refs.popup.close()
  107. },
  108. }
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. view {
  113. display: flex;
  114. }
  115. .box {
  116. background-color: #FFFFFF;
  117. height: 200px;
  118. width: 750rpx;
  119. flex-direction: column;
  120. border-top-left-radius: 15px;
  121. border-top-right-radius: 15px;
  122. }
  123. .headBox {
  124. padding: 20rpx;
  125. height: 80rpx;
  126. line-height: 80rpx;
  127. text-align: left;
  128. font-size: 32upx;
  129. color: #333333;
  130. margin-left: 15rpx;
  131. }
  132. .tip {
  133. color: #666666;
  134. text-align: left;
  135. justify-content: center;
  136. margin: 10rpx 30rpx;
  137. font-size: 36rpx;
  138. }
  139. .btnBox {
  140. margin-top: 45rpx;
  141. justify-content: center;
  142. flex-direction: row;
  143. }
  144. .close,
  145. .agree {
  146. text-align: center;
  147. width: 200rpx;
  148. height: 80upx;
  149. line-height: 80upx;
  150. border-radius: 50px;
  151. margin: 0 20rpx;
  152. font-size: 36rpx;
  153. }
  154. .close {
  155. color: #999999;
  156. border-color: #EEEEEE;
  157. border-style: solid;
  158. border-width: 1px;
  159. background-color: #FFFFFF;
  160. }
  161. .close:active {
  162. color: #989898;
  163. background-color: #E2E2E2;
  164. }
  165. .agree {
  166. background-color: #DD524D;
  167. color: #FFFFFF;
  168. }
  169. /* #ifdef MP */
  170. .agree::after {
  171. border: none;
  172. }
  173. .agree {
  174. background-color: #DD524D;
  175. }
  176. /* #endif */
  177. .agree:active {
  178. background-color: #F5F5F6;
  179. }
  180. </style>