login.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <view class="login-content">
  3. <view>
  4. <view>
  5. <view class="logo">
  6. <u-image src="/static/login_icon_logo" width="160" height="160" ></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. if (_this.path === '/pages/cleared/index' || _this.path === '/pages/userCenter/index') { // tabbar页面
  94. uni.switchTab({
  95. url: _this.path
  96. })
  97. } else {
  98. uni.redirectTo({
  99. url: _this.path
  100. })
  101. }
  102. }
  103. } else {
  104. uni.showToast({
  105. title: res.message,
  106. icon: 'none',
  107. duration: 2500
  108. });
  109. }
  110. });
  111. },300)
  112. } else {
  113. uni.showToast({
  114. title: '授权失败',
  115. icon: 'none',
  116. duration: 2500
  117. });
  118. }
  119. },
  120. // 调微信登录 获取code
  121. wxLogin () {
  122. let _this = this
  123. uni.login({
  124. provider: 'weixin',
  125. success(res) {
  126. _this.code = res.code
  127. }
  128. })
  129. },
  130. // 手机号登录
  131. pwLogin () {
  132. wx.navigateTo({
  133. url: '/pages/login/pwLogin'
  134. })
  135. }
  136. }
  137. }
  138. </script>
  139. <style lang="scss" scoped>
  140. .login-content {
  141. width: 100%;
  142. height: 100vh;
  143. padding: 40upx;
  144. display: flex;
  145. flex-direction: column;
  146. background-color: #fff;
  147. > view{
  148. text-align: center;
  149. &:first-child{
  150. flex-grow: 1;
  151. height: 80vh;
  152. padding: 20% 10%;
  153. display: flex;
  154. flex-direction: column;
  155. justify-content: space-between;
  156. }
  157. &:last-child{
  158. font-size: 24upx;
  159. text{
  160. color: #07c160;
  161. }
  162. }
  163. .logo{
  164. text-align: center;
  165. display: flex;
  166. justify-content: center;
  167. }
  168. .login-btns{
  169. padding: 60upx 0;
  170. .login-btn{
  171. margin: 30upx 0;
  172. }
  173. }
  174. }
  175. }
  176. </style>