login.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view class="login-content">
  3. <view>
  4. <view>
  5. <view class="logo">
  6. <u-image src="/static/logo.jpg" width="140" height="140" ></u-image>
  7. </view>
  8. <view class="login-btns">
  9. <u-button shape="circle" hover-class="none" :custom-style="wxButton" class="login-btn" type="success" :hair-line="false" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  10. 微信授权登录
  11. </u-button>
  12. <u-gap height="30"></u-gap>
  13. <u-button shape="circle" hover-class="none" :custom-style="phoneButton" class="login-btn" @click="pwLogin" type="primary">
  14. 账号密码登录
  15. </u-button>
  16. </view>
  17. </view>
  18. </view>
  19. <view @click="toYs">登录即代表同意<text>隐私政策</text>和<text>服务条款</text></view>
  20. </view>
  21. </template>
  22. <script>
  23. import { login } 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: '#00aaff'
  36. }
  37. }
  38. },
  39. mounted() {},
  40. onLoad(option) {
  41. // 隐藏小房子
  42. uni.hideHomeButton()
  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. cansel(){
  56. this.$store.state.vuex_noLogin = true
  57. uni.reLaunch({
  58. url: '/pages/index/index'
  59. })
  60. },
  61. toYs(){
  62. uni.navigateTo({
  63. url: '/pages/agreement/agreement'
  64. })
  65. },
  66. getPhoneNumber(e) {
  67. let _this = this
  68. uni.showLoading({
  69. mask: true,
  70. title: '加载中'
  71. });
  72. if (!this.code) {
  73. this.wxLogin()
  74. }
  75. if (e.target.errMsg === 'getPhoneNumber:ok') {
  76. setTimeout(()=>{
  77. login({
  78. code: _this.code,
  79. encryptedData: e.target.encryptedData,
  80. iv: e.target.iv
  81. }).then(res => {
  82. uni.hideLoading();
  83. _this.code = ''
  84. if (res.status == 200) {
  85. getApp().globalData.token = res.data
  86. _this.$u.vuex('vuex_token',res.data)
  87. uni.$emit("getUserInfo")
  88. if (_this.path === '/pages/index/index' || _this.lanuch) {
  89. uni.reLaunch({
  90. url: _this.path
  91. });
  92. } else {
  93. uni.redirectTo({
  94. url: _this.path
  95. });
  96. }
  97. } else {
  98. uni.showToast({
  99. title: res.message,
  100. icon: 'none',
  101. duration: 2500
  102. });
  103. }
  104. });
  105. },300)
  106. } else {
  107. uni.showToast({
  108. title: '授权失败',
  109. icon: 'none',
  110. duration: 2500
  111. });
  112. }
  113. },
  114. // 调微信登录 获取code
  115. wxLogin () {
  116. let _this = this
  117. uni.login({
  118. provider: 'weixin',
  119. success(res) {
  120. _this.code = res.code
  121. }
  122. })
  123. },
  124. // 手机号登录
  125. pwLogin () {
  126. wx.navigateTo({
  127. url: '/pages/login/pwLogin'
  128. })
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="scss" scoped>
  134. .login-content {
  135. width: 100%;
  136. height: 100vh;
  137. padding: 40upx;
  138. display: flex;
  139. flex-direction: column;
  140. background-color: #fff;
  141. > view{
  142. text-align: center;
  143. &:first-child{
  144. flex-grow: 1;
  145. height: 80vh;
  146. padding: 20% 10%;
  147. display: flex;
  148. flex-direction: column;
  149. justify-content: space-between;
  150. }
  151. &:last-child{
  152. font-size: 24upx;
  153. text{
  154. color: #07c160;
  155. }
  156. }
  157. .logo{
  158. text-align: center;
  159. display: flex;
  160. justify-content: center;
  161. }
  162. .login-btns{
  163. padding: 60upx 0;
  164. .login-btn{
  165. margin: 30upx 0;
  166. }
  167. }
  168. }
  169. }
  170. </style>