login.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <template>
  2. <view class="page">
  3. <view class="logTitle">
  4. <view class="welcome flex-row justify-center">
  5. <text>欢迎登录娇骄儿拓客端</text>
  6. </view>
  7. <view class="flex-row justify-center">
  8. <image src="../../static/logo.png" class="log"></image>
  9. </view>
  10. </view>
  11. <view class="form">
  12. <u-form labelPosition="left" :model="form" :rules="rules" ref="uForm">
  13. <view class="formItem">
  14. <u-form-item label="账号" prop="username" borderBottom ref="item1">
  15. <u--input v-model="form.username" placeholder="请输入账号" border="none"></u--input>
  16. </u-form-item>
  17. </view>
  18. <view class="formItem">
  19. <u-form-item label="密码" prop="password" borderBottom ref="item1">
  20. <u--input v-model="form.password" placeholder="请输入密码" border="none" type="password"></u--input>
  21. </u-form-item>
  22. </view>
  23. <view class="forgetPassword flex-row justify-between ">
  24. <view>
  25. <checkbox-group @change="checkboxChange">
  26. <checkbox :checked="savePassword" color="#FFCC33" :style="{'transform':'scale(0.8)'}"/>
  27. <text>记住账号密码</text>
  28. </checkbox-group>
  29. </view>
  30. <view>
  31. <text @click="forgetPassword">忘记密码?</text>
  32. </view>
  33. </view>
  34. <view class="loginButton" @click="login">
  35. <text>登录</text>
  36. </view>
  37. </u-form>
  38. </view>
  39. <!-- <view class="flex-row justify-center register">-->
  40. <!-- <text>无账号?</text>-->
  41. <!-- <text @click="goRegister" :style="{color:'#FFE05C'}">切换注册</text>-->
  42. <!-- </view>-->
  43. </view>
  44. </template>
  45. <script>
  46. export default {
  47. data() {
  48. return {
  49. requestStatus:false,
  50. savePassword:true,
  51. form: {
  52. username: '',
  53. password: ''
  54. },
  55. rules: {
  56. 'username': {
  57. type: 'string',
  58. required: true,
  59. min: 11,
  60. max: 11,
  61. pattern:this.$phonePattern,
  62. message: '请输入正确的电话号码',
  63. trigger: ['blur', 'change']
  64. },
  65. 'password': {
  66. type: 'string',
  67. required: true,
  68. min: 6,
  69. max: 18,
  70. message: '请输入6-18位密码',
  71. trigger: ['blur', 'change']
  72. }
  73. }
  74. }
  75. },
  76. onShow(){
  77. this.form.username = uni.getStorageSync('username')
  78. this.form.password = uni.getStorageSync('password')
  79. if (uni.getStorageSync('savePassword')){
  80. this.savePassword = uni.getStorageSync('savePassword')
  81. }
  82. },
  83. onLoad(e){
  84. console.log('重定向获取到的参数',e)
  85. console.log('重定向的url',location.href)
  86. let code = this.getUrlCode('code')
  87. if (code){
  88. uni.showToast({
  89. icon: 'success',
  90. duration: 5000,
  91. title: '获取到的code:'+code
  92. });
  93. }
  94. },
  95. methods: {
  96. getUrlCode(name) {
  97. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||[, ''])[1].replace(/\+/g, '%20')) || null
  98. },
  99. goRegister(){
  100. uni.redirectTo({
  101. url:'/pages/register/register'
  102. })
  103. },
  104. forgetPassword(){
  105. uni.navigateTo({
  106. url:'/pages/forgetPassword/forgetPassword'
  107. })
  108. },
  109. checkboxChange(e){
  110. this.savePassword = !this.savePassword
  111. console.log(this.savePassword)
  112. },
  113. login() {
  114. console.log('savePassword', this.savePassword)
  115. if (this.requestStatus){
  116. return
  117. }
  118. this.requestStatus=true
  119. this.$refs.uForm.validate().then(res => {
  120. console.log('savePassword', this.savePassword)
  121. if (this.savePassword){
  122. uni.setStorageSync('savePassword', this.savePassword)
  123. uni.setStorageSync('username', this.form.username)
  124. uni.setStorageSync('password', this.form.password)
  125. }else {
  126. uni.setStorageSync('savePassword', this.savePassword)
  127. uni.removeStorageSync('username');
  128. uni.removeStorageSync('password');
  129. }
  130. uni.showLoading({
  131. title: '登录中...'
  132. });
  133. this.$api.login.expandLogin(this.form).then(res => {
  134. uni.hideLoading();
  135. this.requestStatus=false
  136. uni.setStorageSync('accessToken', res.data.data.access_token)
  137. this.$api.user.getUserInfo().then(res=>{
  138. console.log('获取到的用户信息:',res.data.data,'获取到的用户头像',res.data.data.img)
  139. this.userInfo = res.data.data
  140. uni.setStorageSync('spreadUserInfo', this.userInfo)
  141. uni.showToast({
  142. icon: 'success',
  143. duration: 1000,
  144. title: '登陆成功'
  145. });
  146. setTimeout(()=>{
  147. uni.switchTab({
  148. url: "/pages/index/index"
  149. })
  150. },1000)
  151. })
  152. }).catch(err=>{
  153. this.requestStatus=false
  154. })
  155. }).catch(err=>{
  156. this.requestStatus=false
  157. })
  158. }
  159. }
  160. }
  161. </script>
  162. <style lang="scss" scoped>
  163. @import './index.rpx.css';
  164. </style>