login.vue 6.1 KB

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