demo6.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view style="background-color: #FFFFFF;min-height: 100vh;">
  3. <!-- solt begin -->
  4. <view class="flex flex-direction align-center " style="padding-top: 160rpx;">
  5. <u-avatar size="130" src="http://pic2.sc.chinaz.com/Files/pic/pic9/202002/hpic2119_s.jpg"></u-avatar>
  6. <text style="margin:30rpx 0 100rpx 0rpx;color: #333333;font-size: 26rpx;font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;">Hi~登陆玩转社区吧</text>
  7. <button class="cu-btn round wx-btn " open-type="getPhoneNumber" @getphonenumber="tapGetPhoneNumber">
  8. <view class="text-center" style="position: relative;left: -100rpx;">
  9. <text class="cuIcon-weixin" style="font-size: 40rpx;color: #216a21;"></text>
  10. </view>
  11. <view style="position: relative;left: -16rpx;">
  12. <text class="text-center" style="font-size: 28rpx;">微信立即登陆</text>
  13. </view>
  14. </button>
  15. <view @click="hide" class="cu-btn round line-gray " style="font-size: 26rpx;width: 200rpx;margin-top: 80rpx;">
  16. <text style="color: #333333;">暂不登陆</text>
  17. </view>
  18. </view>
  19. <!-- solt end -->
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. data() {
  25. return {
  26. respWx:{},
  27. respLogin:{},
  28. timer:null,
  29. inviteCode:null,
  30. }
  31. },
  32. beforeDestroy() {
  33. if(this.timer){
  34. clearTimeout(this.timer)
  35. this.timer = null
  36. console.log('clearTimeout')
  37. }
  38. },
  39. onReady() {
  40. this.show()
  41. },
  42. methods:{
  43. hide () {
  44. if(this.timer){
  45. clearTimeout(this.timer)
  46. this.timer = null
  47. }
  48. },
  49. show(){
  50. let that=this
  51. this.inviteCode = this.$auth.getInviteCode()
  52. this.$mpi.wxLogin().then(res => {
  53. this.respWx = res
  54. console.log("res",res);
  55. })
  56. this.setTimer()
  57. },
  58. async setTimer(){
  59. this.respWx = await this.$mpi.wxLogin()
  60. this.timer = setTimeout(()=>{
  61. this.setTimer()
  62. },240000) // 每隔4分钟重新请求 jscode
  63. },
  64. async tapGetPhoneNumber (e) {
  65. if(e.detail.errMsg != 'getPhoneNumber:ok') {
  66. return
  67. }
  68. this.login(e.detail)
  69. },
  70. async login(detail){
  71. console.log("2:login",detail);
  72. try{
  73. this.$dialog.showLoading()
  74. if(!this.respLogin.sessionKey){
  75. this.respLogin = await this.$api.loginByCode({
  76. _isReject:true,
  77. code:this.respWx.code
  78. })
  79. }
  80. console.log(this.respLogin);
  81. let resp = await this.$api.loginByWxapp({
  82. _isReject:true,
  83. openId: this.respLogin.openId,
  84. sessionKey: this.respLogin.sessionKey,
  85. encryptedData: detail.encryptedData,
  86. iv: detail.iv,
  87. inviteCode: this.inviteCode
  88. })
  89. console.log(56,resp)
  90. resp.openId = this.respLogin.openId
  91. resp.sessionKey = this.respLogin.sessionKey
  92. let userType = this.$global.userType.member
  93. this.$auth.login(userType, resp.sessionId, resp.userId, resp)
  94. this.hide()
  95. this.$util.refreshPage(['/pagesM/pages/category','/pagesM/pages/shop_car'])
  96. // this.$auth.setInviteCode(null);
  97. this.$auth.removeInviteCode();
  98. let a = this.$auth.getInviteCode();
  99. if(this.inviteCode){
  100. this.$emit('getDistributorCoupon')
  101. }else{
  102. this.$emit('signIn', resp)
  103. }
  104. }catch(err){
  105. // console.log(72,err)
  106. this.$dialog.alert({
  107. content: err.errmsg || '服务繁忙~'
  108. })
  109. }finally{
  110. this.$dialog.hideLoading()
  111. if(this.timer){
  112. clearTimeout(this.timer)
  113. this.setTimer()
  114. }
  115. }
  116. },
  117. },
  118. }
  119. </script>
  120. <style lang="scss">
  121. .wx-btn{
  122. background-color: #2ba246;
  123. color: #FFFFFF;
  124. width: 600rpx;
  125. padding: 40rpx 0rpx;
  126. }
  127. </style>