index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <view class="content">
  3. <view class="welcome">
  4. <!-- <img :src="require('~/static/login-bg-top.png')" /> -->
  5. <!-- <view class="welcome-text">
  6. </view> -->
  7. <image :src="require('@/static/text.png')" mode="aspectFit" class="text" />
  8. <view class="btns">
  9. <view :class="{act: index === 0, btn: true}" @click="index = 0">
  10. <view>登录</view>
  11. <view class="arrow"></view>
  12. </view>
  13. <view :class="{act: index === 1, btn: true}" @click="index = 1">
  14. <view>注册</view>
  15. <view class="arrow"></view>
  16. </view>
  17. </view>
  18. </view>
  19. <view class="text-area">
  20. <view v-if="index === 0" class="login-form">
  21. <u--form labelPosition="top" :model="userInfo" labelWidth="100px">
  22. <u-form-item label="手机号" prop="userInfo.name">
  23. <u--input v-model="userInfo.name" placeholder="请输入您的账号"></u--input>
  24. </u-form-item>
  25. <u-form-item label="密码" prop="userInfo.password">
  26. <u--input v-model="userInfo.password" type="password" placeholder="请输入您的密码"></u--input>
  27. </u-form-item>
  28. </u--form>
  29. <view>
  30. <view class="login-btn" @click="login">登录</view>
  31. </view>
  32. </view>
  33. <view v-if="index === 1" class="login-form">
  34. <u--form labelPosition="top" :model="registInfo" labelWidth="100px">
  35. <u-form-item label="注册账号" prop="name">
  36. <u--input v-model="registInfo.name" placeholder="请输入您的账号"></u--input>
  37. </u-form-item>
  38. <u-form-item label="登录密码" prop="password">
  39. <u--input v-model="registInfo.password" type="password" placeholder="请输入您的密码"></u--input>
  40. </u-form-item>
  41. <u-form-item label="确认密码" prop="password">
  42. <u--input v-model="registInfo.password2" type="password" placeholder="请输入您的密码"></u--input>
  43. </u-form-item>
  44. </u--form>
  45. <view>
  46. <view class="login-btn" @click="register">注册</view>
  47. </view>
  48. </view>
  49. </view>
  50. <view style="text-align: center;">
  51. version: 1.6
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. import {
  57. login,
  58. cryptoPassword,
  59. register
  60. } from '@/api/login.js'
  61. import {
  62. validate
  63. } from '@/utils/common.js'
  64. import { setCache } from '@/utils/cache'
  65. export default {
  66. data() {
  67. return {
  68. index: 0,
  69. userInfo: {
  70. // name: null,
  71. // password: null,
  72. name: "13777777777",
  73. password: "123456",
  74. },
  75. registInfo: {
  76. name: null,
  77. password: null,
  78. password2: null,
  79. }
  80. }
  81. },
  82. onLoad() {
  83. },
  84. methods: {
  85. login() {
  86. if (
  87. validate(this.userInfo.name, '请输入用户名') &&
  88. validate(this.userInfo.password, '请输入密码')
  89. ) {
  90. const pwd = cryptoPassword(this.userInfo.password.trim())
  91. login({
  92. username: this.userInfo.name,
  93. //password: this.userInfo.password,
  94. password: pwd,
  95. }).then(resp => {
  96. if (resp.code === 200) {
  97. const {token, type} = resp.data
  98. setCache('token', token).then(_ => {
  99. getApp().globalData.userType = type
  100. getApp().globalData.tabIndex = 0
  101. return setCache('userType', type)
  102. }).then(_ => {
  103. uni.redirectTo({
  104. url: '/pages/home/home'
  105. })
  106. })
  107. } else {
  108. uni.showToast({
  109. title: resp.msg
  110. })
  111. }
  112. })
  113. }
  114. },
  115. register() {
  116. if (
  117. validate(this.registInfo.name, '请输入用户名') &&
  118. validate(this.registInfo.password, '请输入密码') &&
  119. validate(this.registInfo.password2, '请输入确认密码')
  120. ) {
  121. if(this.registInfo.password !== this.registInfo.password2) {
  122. uni.showToast({
  123. title: '两次密码不一致'
  124. })
  125. return
  126. }
  127. const pwd = cryptoPassword(this.registInfo.password.trim())
  128. register({
  129. username: this.registInfo.name,
  130. password: pwd
  131. }).then(resp => {
  132. if(resp.code === 200) {
  133. uni.showToast({
  134. title: '注册成功',
  135. icon: 'success'
  136. })
  137. this.index = 0
  138. }
  139. })
  140. }
  141. },
  142. }
  143. }
  144. </script>
  145. <style lang="scss" scoped>
  146. .welcome {
  147. background: url('@/static/login-bg-top.jpg');
  148. background-size: 100% 100%;
  149. height: 40vh;
  150. display: flex;
  151. flex-direction: column;
  152. align-items: center;
  153. justify-content: flex-end;
  154. .text {
  155. max-height: 80%;
  156. }
  157. .btns {
  158. width: 100%;
  159. display: flex;
  160. flex-direction: row;
  161. justify-content: space-around;
  162. margin-bottom: 10px;
  163. .btn {
  164. color: #bdc8ee;
  165. width: 100%;
  166. text-align: center;
  167. }
  168. .act {
  169. color: #fefffd;
  170. .arrow {
  171. /* background: url('~@/static/arrow.png') no-repeat;
  172. height: 15px;
  173. margin: 0px 19px; */
  174. }
  175. }
  176. }
  177. }
  178. .text-area {
  179. padding-left: 10px;
  180. padding-right: 10px;
  181. }
  182. .login-btn {
  183. background-image: url("~@/static/login-btn.png");
  184. background-size: 100% 100%;
  185. height: 50px;
  186. text-align: center;
  187. color: white;
  188. box-sizing: border-box;
  189. padding-top: 10px;
  190. }
  191. </style>