index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <view class="container">
  3. <u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" title="微信授权"></u-navbar>
  4. <view class="flex login">
  5. <view class="flex login-image">
  6. <image src="../../static/logo.png" mode=""></image>
  7. <view class="login-image-txt">盲票</view>
  8. </view>
  9. <view class="login-txt">盲票将为您提供</view>
  10. <view class="login-title">商品兑换、盲票购买等服务 请先完成授权登录</view>
  11. <view class="login-btn">
  12. <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信授权登录</button>
  13. </view>
  14. </view>
  15. <auth :auth-show="authShow" :auth-token="token" @close="authClose" />
  16. </view>
  17. </template>
  18. <script>
  19. import $http from '@/utils/request.js'
  20. import Auth from '../../components/auth/auth.vue'
  21. export default {
  22. components: {
  23. Auth
  24. },
  25. data() {
  26. return {
  27. authShow: false,
  28. token: '',
  29. };
  30. },
  31. methods: {
  32. getPhoneNumber(e) {
  33. let _this = this
  34. uni.showLoading({
  35. title: '登录中'
  36. });
  37. console.log(e);
  38. if (e.detail & e.detail.code) {
  39. $http.post('/api/v1/mp/user/wxauth/mobile', {
  40. ...e.detail,
  41. identity: 1,
  42. noToken: true
  43. }).then(res => {
  44. uni.hideLoading();
  45. if (res.code == 0) {
  46. uni.setStorageSync('token', res.token)
  47. _this.getBaseInfo()
  48. }
  49. }).catch(() => {
  50. uni.hideLoading();
  51. })
  52. }else{
  53. uni.login({
  54. success(res) {
  55. console.log('login', res);
  56. $http.post('/api/v1/mp/user/wxauth/mobile', {
  57. ...e.detail,
  58. loginCode: res.code,
  59. identity: 1,
  60. noToken: true
  61. }).then(res => {
  62. uni.hideLoading();
  63. if (res.code == 0) {
  64. uni.setStorageSync('token', res.token)
  65. _this.getBaseInfo()
  66. }
  67. }).catch(() => {
  68. uni.hideLoading();
  69. })
  70. }
  71. })
  72. }
  73. },
  74. // 关闭授权
  75. authClose() {
  76. this.authShow = false
  77. setTimeout(() => {
  78. uni.navigateBack({
  79. delta: 1
  80. })
  81. }, 500)
  82. },
  83. getBaseInfo() {
  84. $http.post('/api/v1/mp/user/getLoginUserinfo', {}).then(res => {
  85. uni.hideLoading();
  86. if (res.code == 0) {
  87. uni.setStorageSync('userInfo', res.data)
  88. if (res.data.openId) {
  89. uni.$u.toast('登录成功');
  90. setTimeout(() => {
  91. uni.navigateBack({
  92. delta: 1
  93. })
  94. }, 500)
  95. } else {
  96. this.authShow = true
  97. }
  98. }
  99. }).catch(() => {
  100. uni.hideLoading();
  101. })
  102. },
  103. }
  104. }
  105. </script>
  106. <style lang="scss" scoped>
  107. .container {
  108. height: 100vh;
  109. width: 100%;
  110. }
  111. .login {
  112. flex-direction: column;
  113. &-image {
  114. flex-direction: column;
  115. padding: 100rpx 0 200rpx;
  116. image {
  117. width: 164rpx;
  118. height: 164rpx;
  119. border-radius: 10rpx;
  120. margin-bottom: 14rpx;
  121. }
  122. &-txt {
  123. text-align: center;
  124. line-height: 40rpx;
  125. font-weight: bold;
  126. margin-bottom: 20rpx;
  127. font-size: 36rpx;
  128. }
  129. }
  130. &-txt {
  131. text-align: center;
  132. line-height: 40rpx;
  133. font-weight: bold;
  134. margin-bottom: 20rpx;
  135. }
  136. &-title {
  137. text-align: center;
  138. font-size: 24rpx;
  139. font-weight: normal;
  140. margin-bottom: 50rpx;
  141. }
  142. &-btn {
  143. button {
  144. width: 600rpx;
  145. height: 80rpx;
  146. line-height: 80rpx;
  147. border-radius: 40rpx;
  148. background-color: rgba(235, 112, 9, 100);
  149. color: rgba(255, 255, 255, 100);
  150. text-align: center;
  151. font-size: 28rpx;
  152. }
  153. }
  154. }
  155. </style>