index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!-- 顶部蓝色 -->
  2. <template>
  3. <view class="contaier">
  4. <view class="top-bg">
  5. <image class="logo-icon" src="/static/logo.png"></image>
  6. </view>
  7. <view class="input-box padding-lr">
  8. <uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData">
  9. <view class="cu-form-group margin-top">
  10. <uni-icons class="uni-icon" type="person" size="30" color="#999"></uni-icons>
  11. <uni-easyinput placeholder="账号" v-model="valiFormData.account" />
  12. </view>
  13. <view class="cu-form-group">
  14. <uni-icons class="uni-icon" type="locked" size="30" color="#999"></uni-icons>
  15. <uni-easyinput placeholder="密码" type="password" v-model="valiFormData.password" />
  16. </view>
  17. <view class="cu-form-group solid-bottom">
  18. <uni-icons class="uni-icon" type="more-filled" size="30" color="#999"></uni-icons>
  19. <uni-easyinput placeholder="验证码" v-model="valiFormData.captcha" />
  20. <!-- <button class='cu-btn bg-login-zl shadow'>验证码</button> -->
  21. <image class='cu-btn captchaImg' :src="verifyCodeImg" @click="getVerifyCodeImg"></image>
  22. </view>
  23. </uni-forms>
  24. </view>
  25. <view class="padding-lr margin-top-xs">
  26. <button class="cu-btn block round bg-login-zl margin-tb-sm lg" @click="formSubmit('valiForm')">立即登录</button>
  27. </view>
  28. <uni-popup ref="message" type="message">
  29. <uni-popup-message type="error" :message="messageText" :duration="2000"></uni-popup-message>
  30. </uni-popup>
  31. </view>
  32. </template>
  33. <script>
  34. import {
  35. computed,
  36. ref
  37. } from 'vue';
  38. import {loginURL, getcaptchaURL} from "@/utils/api.js"
  39. export default {
  40. data() {
  41. return {
  42. messageText: "",
  43. captcha_id: 'f2dd39ce-0d3e-4c1e-916c-b50090a7590s',
  44. verifyCodeImg: '', //验证码图片
  45. verifyCodeToken: '', //图形验证码的验证token
  46. isVerifyCodeTure: false, //验证码输入是否正确
  47. valiFormData: {
  48. account: '',
  49. password: '',
  50. captcha: ''
  51. },
  52. rules: {
  53. name: {
  54. rules: [{
  55. required: true,
  56. errorMessage: '姓名不能为空'
  57. }]
  58. },
  59. },
  60. }
  61. },
  62. onLoad() {
  63. this.getVerifyCodeImg()
  64. },
  65. methods: {
  66. getVerifyCodeImg() {
  67. uni.request({
  68. url: `${getcaptchaURL}?server=1&id=${this.captcha_id}&m=` +
  69. Math.random(),
  70. method: 'GET',
  71. responseType: 'arraybuffer', //设置响应类型
  72. success: res => {
  73. const arrayBuffer = new Uint8Array(res.data)
  74. const base64 = "data:image/png;base64," + uni.arrayBufferToBase64(
  75. arrayBuffer) //这里需要添加前缀
  76. this.verifyCodeImg = base64
  77. this.verifyCodeToken = res.header["Set-Cookie"]
  78. }
  79. })
  80. },
  81. formSubmit() {
  82. // 请求
  83. uni.request({
  84. //api地址
  85. url: loginURL,
  86. data: {
  87. "username": this.valiFormData.account,
  88. "password": this.valiFormData.password,
  89. "captcha": this.valiFormData.captcha,
  90. "keep": false,
  91. "loading": true,
  92. "captcha_id": this.captcha_id
  93. },
  94. //请求类型
  95. method: 'POST',
  96. success: (res) => {
  97. console.log(res)
  98. if (res.data.code == 1) {
  99. const userInfo = res.data.data.userInfo;
  100. //登录成功之后获取Token
  101. uni.setStorage({
  102. key: 'token',
  103. data: userInfo.token,
  104. success() {
  105. //存储完Token之后再存储一下用户信息
  106. uni.setStorage({
  107. key: 'user',
  108. data: userInfo,
  109. success() {
  110. //返回上一页
  111. uni.navigateBack();
  112. }
  113. })
  114. }
  115. })
  116. } else {
  117. console.log(res)
  118. this.messageText = res.data.msg
  119. this.$refs.message.open()
  120. }
  121. }
  122. });
  123. }
  124. },
  125. };
  126. </script>
  127. <style lang="scss" scoped>
  128. .contaier {
  129. height: 100vh;
  130. background-color: #ffffff;
  131. }
  132. .margin-top {
  133. margin-top: 10rpx;
  134. }
  135. .margin-top-xs {
  136. margin-top: 60rpx;
  137. }
  138. .padding-lr {
  139. padding-left: 40rpx;
  140. padding-right: 40rpx;
  141. }
  142. .cu-btn {
  143. margin-top: 10rpx;
  144. margin-bottom: 10rpx;
  145. height: 60rpx;
  146. font-size: 24rpx;
  147. text-align: center;
  148. }
  149. .round {
  150. border-radius: 30rpx;
  151. }
  152. .input-box {
  153. .cu-form-group {
  154. display: flex;
  155. flex-direction: row;
  156. align-items: center;
  157. border-bottom: #ccc solid 0.5px;
  158. .uni-icon {
  159. color: #333;
  160. padding: 20rpx;
  161. }
  162. }
  163. }
  164. .top-bg {
  165. margin-top: 20rpx;
  166. width: 750rpx;
  167. height: 120rpx;
  168. display: flex;
  169. justify-content: center;
  170. align-items: center;
  171. .logo-icon {
  172. width: 248rpx;
  173. height: 100rpx;
  174. object-fit: contain;
  175. }
  176. }
  177. .captchaImg {
  178. width: 160rpx;
  179. height: 60rpx;
  180. }
  181. .bg-login-zl {
  182. background-image: linear-gradient(45deg, #727CFB, #46D0ED);
  183. color: #ffffff;
  184. }
  185. </style>