login.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <view class="content">
  3. <view>
  4. <view class="logo">
  5. <u-image src="/static/logo.png" width="140" height="140" ></u-image>
  6. </view>
  7. <view class="login-btns">
  8. <u-button shape="circle" :custom-style="wxButton" class="login-btn" type="success" :hair-line="false" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  9. 微信登陆
  10. </u-button>
  11. <u-gap height="30"></u-gap>
  12. <u-button shape="circle" plain :custom-style="phoneButton" class="login-btn" @click="phoneLogin" type="info">
  13. 手机号码登录
  14. </u-button>
  15. </view>
  16. </view>
  17. <view>登录即代表同意<text>隐私政策</text>和<text>服务条款</text></view>
  18. </view>
  19. </template>
  20. <script>
  21. import {
  22. login
  23. } from "@/api/login"
  24. export default {
  25. components: {},
  26. data() {
  27. return {
  28. path: '/pages/index/index',
  29. lanuch: true,
  30. code: '',
  31. wxButton: {
  32. background: '#07c160'
  33. },
  34. phoneButton: {
  35. background: '#ffffff'
  36. }
  37. }
  38. },
  39. onShow() {
  40. },
  41. mounted() {},
  42. onLoad(option) {
  43. // 为解决首次登录异常问题 进入页面先调微信登录获取code
  44. this.wxLogin()
  45. const {
  46. path,
  47. lanuch
  48. } = option;
  49. this.lanuch = (lanuch + '') === 'true';
  50. if (path) {
  51. this.path = decodeURIComponent(path);
  52. }
  53. },
  54. methods: {
  55. getPhoneNumber(e) {
  56. let _this = this
  57. uni.showLoading({
  58. mask: true,
  59. title: '加载中'
  60. });
  61. if (!this.code) {
  62. this.wxLogin()
  63. }
  64. if (e.target.errMsg === 'getPhoneNumber:ok') {
  65. setTimeout(()=>{
  66. login({
  67. code: _this.code,
  68. encryptedData: e.target.encryptedData,
  69. iv: e.target.iv
  70. }).then(res => {
  71. uni.hideLoading();
  72. _this.code = ''
  73. console.log(res,_this.path, 'login data')
  74. if (res.status == '200') {
  75. getApp().globalData.token = res.data
  76. _this.$u.vuex('vuex_token',res.data)
  77. if (_this.path === '/pages/index/index' || _this.lanuch) {
  78. uni.reLaunch({
  79. url: _this.path
  80. });
  81. } else {
  82. uni.redirectTo({
  83. url: _this.path
  84. });
  85. }
  86. } else {
  87. uni.showToast({
  88. title: res.message,
  89. icon: 'none',
  90. duration: 2500
  91. });
  92. }
  93. });
  94. },300)
  95. } else {
  96. uni.showToast({
  97. title: '授权失败',
  98. icon: 'none',
  99. duration: 2500
  100. });
  101. }
  102. },
  103. // 调微信登录 获取code
  104. wxLogin () {
  105. let _this = this
  106. uni.login({
  107. provider: 'weixin',
  108. success(res) {
  109. _this.code = res.code
  110. }
  111. })
  112. },
  113. // 手机号登录
  114. phoneLogin () {
  115. wx.navigateTo({
  116. url: '/pages/login/phoneLogin'
  117. })
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss">
  123. .content {
  124. width: 100%;
  125. height: 100%;
  126. padding: 40upx;
  127. display: flex;
  128. flex-direction: column;
  129. > view{
  130. text-align: center;
  131. &:first-child{
  132. flex-grow: 1;
  133. height: 80vh;
  134. padding: 20% 10%;
  135. }
  136. &:last-child{
  137. font-size: 24upx;
  138. text{
  139. color: #07c160;
  140. }
  141. }
  142. .logo{
  143. text-align: center;
  144. display: flex;
  145. justify-content: center;
  146. }
  147. .login-btns{
  148. padding: 30upx 0;
  149. .login-btn{
  150. margin: 30upx 0;
  151. }
  152. }
  153. }
  154. }
  155. </style>