login.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view>
  3. <view class="margin-top padding">
  4. <text class="page-title">{{title}}</text>
  5. </view>
  6. <view class="margin-top">
  7. <view class="cu-form-group">
  8. <view class="title">用户名</view>
  9. <input placeholder="请输入用户名" v-model="username" v-on:input="valuechanging"></input>
  10. </view>
  11. <view class="cu-form-group">
  12. <view class="title">密码</view>
  13. <input v-if="isshowpwd" placeholder="请输入密码" type="text" v-model="password" v-on:input="valuechanging" @confirm="login"></input>
  14. <input v-if="!isshowpwd" placeholder="请输入密码" type="password" v-model="password" v-on:input="valuechanging" @confirm="login"></input>
  15. <text class="text-blue" :class="isshowpwd ? 'cuIcon-attentionfill' : 'cuIcon-attentionforbidfill'" @click="changePassword"></text>
  16. </view>
  17. </view>
  18. <view class="margin-top padding">
  19. <button class="round loginBtn" :disabled="isnull" @click="login">登录</button>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. data() {
  26. return {
  27. title: '鲁泰纺织',
  28. username: '',
  29. password: '',
  30. isshowpwd: false,
  31. isnull: true,
  32. isback: false
  33. }
  34. },
  35. onLoad(param) {
  36. ///获取是否返回上级页面
  37. if (Object.keys(param).length > 0 && param.isback == 1) this.isback = true
  38. uni.getStorage({
  39. key: 'token',
  40. success: function(res) {
  41. //若用户已登录,则无需重复登录,跳转到菜单页面
  42. uni.redirectTo({
  43. url: '../menu/menu'
  44. })
  45. }
  46. });
  47. },
  48. methods: {
  49. changePassword() {
  50. //显示/隐藏密码
  51. this.isshowpwd = !this.isshowpwd;
  52. },
  53. valuechanging() {
  54. //根据是否输入用户名密码,设置登录按钮是否可用
  55. if (this.username != "" && this.password != "") {
  56. this.isnull = false;
  57. } else {
  58. this.isnull = true;
  59. }
  60. },
  61. login() {
  62. //判断用户名密码是否为空(设置了在输入密码按回车键的时候执行登录)
  63. if (this.username == "" || this.password == "") {
  64. uni.showToast({
  65. title: '用户名或密码为空!',
  66. icon: 'none'
  67. })
  68. return;
  69. }
  70. //访问网络登录
  71. uni.request({
  72. url: "https://api.lttc.cn/sys/user/login",
  73. data: {
  74. "userName": this.username,
  75. "password": this.password
  76. },
  77. method: 'POST',
  78. success: (res) => {
  79. if (res.data.code == 0) {
  80. //登录成功后,将用户的userKey存储缓存(键:token,值:userKey)
  81. var vm = this
  82. uni.setStorage({
  83. key: 'token',
  84. data: res.data.data.userKey,
  85. success: function() {
  86. ////判断是否跳回原网页
  87. if (vm.isback) {
  88. uni.navigateBack({
  89. delta: 1,
  90. })
  91. return;
  92. }
  93. //登录成功后,跳转到菜单页面
  94. uni.redirectTo({
  95. url: '../menu/menu'
  96. })
  97. },
  98. fail() {
  99. //用户UserKey存储缓存失败时提示
  100. uni.showToast({
  101. title: '登录成功,但调用接口缓存token失败!',
  102. icon: 'none'
  103. })
  104. }
  105. });
  106. } else {
  107. //登录失败清空密码
  108. this.password = '';
  109. this.isnull = true;
  110. //弹出提示
  111. uni.showToast({
  112. title: '登录失败!原因是:' + res.data.msg,
  113. icon: 'none'
  114. })
  115. }
  116. },
  117. fail: (res) => {
  118. uni.showToast({
  119. title: '访问网络登录失败!原因是:' + res.errMsg,
  120. icon: 'none'
  121. })
  122. }
  123. })
  124. }
  125. }
  126. }
  127. </script>
  128. <style>
  129. page {
  130. background-image: linear-gradient(to bottom right, #0081ff, #5ac9ea);
  131. }
  132. .cu-form-group .title {
  133. color: #000000;
  134. font-size: medium;
  135. min-width: calc(4em);
  136. }
  137. .page-title {
  138. color: #ffffff;
  139. font-size: 60upx;
  140. text-align: center;
  141. display: block;
  142. }
  143. .loginBtn {
  144. background-color: #3c59ff;
  145. color: #FFFFFF;
  146. }
  147. </style>