login.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <view class="page" :style="{height:height}">
  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" type="number"></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="debounce(login,500)">
  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. timer: null, //定时任务
  50. requestStatus:false,
  51. height:'',
  52. savePassword:true,
  53. form: {
  54. username: '',
  55. password: ''
  56. },
  57. rules: {
  58. 'username': {
  59. type: 'string',
  60. required: true,
  61. min: 11,
  62. max: 11,
  63. pattern:this.$phonePattern,
  64. message: '请输入正确的电话号码',
  65. trigger: ['blur', 'change']
  66. },
  67. 'password': {
  68. type: 'string',
  69. required: true,
  70. min: 6,
  71. max: 18,
  72. message: '请输入6-18位密码',
  73. trigger: ['blur', 'change']
  74. }
  75. }
  76. }
  77. },
  78. onShow(){
  79. let sysInfo = uni.getSystemInfoSync()
  80. this.height = sysInfo.windowHeight + 'px'
  81. this.form.username = uni.getStorageSync('username')
  82. this.form.password = uni.getStorageSync('password')
  83. let isLogOut = uni.getStorageSync('isLogOut')
  84. if (uni.getStorageSync('savePassword')){
  85. this.savePassword = uni.getStorageSync('savePassword')
  86. }
  87. if (this.form.username && this.form.password && isLogOut !== true){
  88. setTimeout(()=>{
  89. this.login()
  90. },100)
  91. }
  92. },
  93. onLoad(e){
  94. console.log('重定向获取到的参数',e)
  95. console.log('重定向的url',location.href)
  96. let code = this.getUrlCode('code')
  97. if (code){
  98. uni.showToast({
  99. icon: 'success',
  100. duration: 5000,
  101. title: '获取到的code:'+code
  102. });
  103. }
  104. },
  105. //
  106. // onPullDownRefresh() {
  107. // console.log('refresh');
  108. // setTimeout(()=>{
  109. // location.reload()
  110. // },1000)
  111. // },
  112. methods: {
  113. getUrlCode(name) {
  114. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||[, ''])[1].replace(/\+/g, '%20')) || null
  115. },
  116. goRegister(){
  117. uni.redirectTo({
  118. url:'/pages/register/register'
  119. })
  120. },
  121. forgetPassword(){
  122. uni.navigateTo({
  123. url:'/pages/forgetPassword/forgetPassword'
  124. })
  125. },
  126. checkboxChange(e){
  127. this.savePassword = !this.savePassword
  128. console.log(this.savePassword)
  129. },
  130. login() {
  131. uni.removeStorageSync('isLogOut');
  132. console.log('savePassword', this.savePassword)
  133. if (this.requestStatus){
  134. return
  135. }
  136. this.requestStatus=true
  137. this.$refs.uForm.validate().then(res => {
  138. console.log('savePassword', this.savePassword)
  139. if (this.savePassword){
  140. uni.setStorageSync('savePassword', this.savePassword)
  141. uni.setStorageSync('username', this.form.username)
  142. uni.setStorageSync('password', this.form.password)
  143. }else {
  144. uni.setStorageSync('savePassword', this.savePassword)
  145. uni.removeStorageSync('username');
  146. uni.removeStorageSync('password');
  147. }
  148. uni.showLoading({
  149. title: '登录中...'
  150. });
  151. this.$api.login.expandLogin(this.form).then(res => {
  152. uni.hideLoading();
  153. uni.setStorageSync('accessToken', res.data.data.access_token)
  154. this.$api.user.getUserInfo().then(res=>{
  155. console.log('获取到的用户信息:',res.data.data,'获取到的用户头像',res.data.data.img)
  156. this.userInfo = res.data.data
  157. uni.setStorageSync('spreadUserInfo', this.userInfo)
  158. uni.showToast({
  159. icon: 'success',
  160. duration: 1000,
  161. title: '登陆成功'
  162. });
  163. setTimeout(()=>{
  164. this.requestStatus=false
  165. uni.switchTab({
  166. url: "/pages/index/index"
  167. })
  168. },1000)
  169. })
  170. }).catch(err=>{
  171. this.requestStatus=false
  172. })
  173. }).catch(err=>{
  174. this.requestStatus=false
  175. })
  176. },
  177. debounce(fn, delay) {
  178. console.log(delay)
  179. clearTimeout(this.timer);
  180. this.timer = setTimeout(() => {
  181. fn.apply();
  182. }, delay);
  183. }
  184. }
  185. }
  186. </script>
  187. <style lang="scss" scoped>
  188. @import './index.rpx.css';
  189. </style>