login.vue 5.1 KB

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