login.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <home-container>
  3. <view class="normal-login-container">
  4. <view class="logo-content">
  5. <!-- <image style="width: 100rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix">
  6. </image> -->
  7. <text class="title" style="display: block;">您好,</text>
  8. <text class="title" style="display: block;">欢迎使用机场安检</text>
  9. </view>
  10. <view class="login-form-content">
  11. <view class="input-item flex align-center">
  12. <view class="iconfont icon-user icon"></view>
  13. <input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
  14. </view>
  15. <view class="input-item flex align-center">
  16. <view class="iconfont icon-password icon"></view>
  17. <input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
  18. </view>
  19. <view class="input-item flex align-center" style="width: 60%;margin: 0px;" v-if="captchaEnabled">
  20. <view class="iconfont icon-code icon"></view>
  21. <input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
  22. <view class="login-code">
  23. <image v-if="codeUrl" :src="codeUrl" @click="getCode" class="login-code-img"></image>
  24. <view v-else class="refresh-btn" @click="getCode">
  25. <text class="iconfont icon-refresh refresh-icon"></text>
  26. <text class="refresh-text">点击刷新</text>
  27. </view>
  28. </view>
  29. </view>
  30. <view class="action-btn">
  31. <button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
  32. </view>
  33. <view class="reg text-center" v-if="register">
  34. <text class="text-grey1">没有账号?</text>
  35. <text @click="handleUserRegister" class="text-blue">立即注册</text>
  36. </view>
  37. <view class="xieyi text-center">
  38. <text class="text-grey1">登录即代表同意</text>
  39. <text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
  40. <text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
  41. </view>
  42. </view>
  43. </view>
  44. </home-container>
  45. </template>
  46. <script>
  47. import HomeContainer from "@/components/HomeContainer.vue";
  48. import { bufferConverter } from '@/utils/handler'
  49. import { getCodeImg } from '@/api/login'
  50. import { getToken } from '@/utils/auth'
  51. import { showMessageTabRedDot } from "@/utils/common.js"
  52. export default {
  53. components: { HomeContainer },
  54. data() {
  55. return {
  56. codeUrl: "",
  57. captchaEnabled: true,
  58. // 用户注册开关
  59. register: false,
  60. globalConfig: getApp().globalData.config,
  61. loginForm: {
  62. username: "",
  63. password: "",
  64. code: "",
  65. uuid: ""
  66. }
  67. }
  68. },
  69. created() {
  70. this.getCode()
  71. },
  72. onLoad() {
  73. //#ifdef H5
  74. if (getToken()) {
  75. this.$tab.reLaunch('/pages/home-new/index')
  76. }
  77. this.getStorage()
  78. //#endif
  79. },
  80. methods: {
  81. getStorage() {
  82. let info = uni.getStorageSync('loginInfo')
  83. if (info) {
  84. info = bufferConverter.base64ToString(info)
  85. try {
  86. info = JSON.parse(info)
  87. this.loginForm.username = info.username
  88. this.loginForm.password = info.password
  89. } catch {
  90. this.loginForm.username = ''
  91. this.loginForm.password = ''
  92. }
  93. }
  94. },
  95. // 用户注册
  96. handleUserRegister() {
  97. this.$tab.redirectTo(`/pages/register`)
  98. },
  99. // 隐私协议
  100. handlePrivacy() {
  101. let site = this.globalConfig.appInfo.agreements[0]
  102. this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
  103. },
  104. // 用户协议
  105. handleUserAgrement() {
  106. let site = this.globalConfig.appInfo.agreements[1]
  107. this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
  108. },
  109. // 获取图形验证码
  110. getCode() {
  111. getCodeImg().then(res => {
  112. this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
  113. if (this.captchaEnabled) {
  114. this.codeUrl = 'data:image/gif;base64,' + res.img
  115. this.loginForm.uuid = res.uuid
  116. }
  117. })
  118. },
  119. // 登录方法
  120. async handleLogin() {
  121. if (this.loginForm.username === "") {
  122. this.$modal.msgError("请输入账号")
  123. } else if (this.loginForm.password === "") {
  124. this.$modal.msgError("请输入密码")
  125. } else if (this.loginForm.code === "" && this.captchaEnabled) {
  126. this.$modal.msgError("请输入验证码")
  127. } else {
  128. this.$modal.loading("登录中,请耐心等待...")
  129. this.pwdLogin()
  130. }
  131. },
  132. // 密码登录
  133. async pwdLogin() {
  134. this.$store.dispatch('Login', this.loginForm).then(() => {
  135. // 密码现在是明文传输 展示转base64 存本地 做保存密码需求
  136. const info = bufferConverter.stringToBase64(JSON.stringify({
  137. username: this.loginForm.username,
  138. password: this.loginForm.password,
  139. }))
  140. uni.setStorageSync('loginInfo', info)
  141. this.$modal.closeLoading()
  142. this.loginSuccess()
  143. }).catch((res) => {
  144. uni.showToast({
  145. title: res.msg,
  146. icon: 'none'
  147. });
  148. if (this.captchaEnabled) {
  149. this.getCode()
  150. }
  151. }).finally(() => {
  152. this.$modal.closeLoading()
  153. })
  154. },
  155. // 登录成功后,处理函数
  156. loginSuccess(result) {
  157. // 设置用户信息
  158. this.$store.dispatch('GetInfo').then(res => {
  159. // 登录成功后调用showMessageTabRedDot更新消息tab红点状态
  160. showMessageTabRedDot()
  161. this.$tab.reLaunch('/pages/home-new/index')
  162. })
  163. }
  164. }
  165. }
  166. </script>
  167. <style lang="scss" scoped>
  168. page {
  169. background-color: #ffffff;
  170. }
  171. .normal-login-container {
  172. width: 100%;
  173. .logo-content {
  174. width: 100%;
  175. font-size: 21px;
  176. text-align: left;
  177. padding-top: 15%;
  178. padding-left: 30rpx;
  179. image {
  180. border-radius: 4px;
  181. }
  182. .title {
  183. margin-left: 10px;
  184. }
  185. }
  186. .login-form-content {
  187. text-align: center;
  188. margin: 20px auto;
  189. margin-top: 15%;
  190. width: 80%;
  191. .input-item {
  192. margin: 20px auto;
  193. background-color: #f5f6f7;
  194. height: 45px;
  195. border-radius: 20px;
  196. .icon {
  197. font-size: 38rpx;
  198. margin-left: 10px;
  199. color: #999;
  200. }
  201. .input {
  202. width: 100%;
  203. font-size: 14px;
  204. line-height: 20px;
  205. text-align: left;
  206. padding-left: 15px;
  207. }
  208. }
  209. .login-btn {
  210. margin-top: 40px;
  211. height: 45px;
  212. }
  213. .reg {
  214. margin-top: 15px;
  215. }
  216. .xieyi {
  217. color: #333;
  218. margin-top: 20px;
  219. }
  220. .login-code {
  221. height: 38px;
  222. float: right;
  223. .login-code-img {
  224. height: 38px;
  225. position: absolute;
  226. margin-left: 10px;
  227. width: 200rpx;
  228. }
  229. .refresh-btn {
  230. display: flex;
  231. flex-direction: row;
  232. align-items: center;
  233. justify-content: center;
  234. background-color: #f0f0f0;
  235. cursor: pointer;
  236. height: 38px;
  237. position: absolute;
  238. margin-left: 10px;
  239. width: 200rpx;
  240. .refresh-icon {
  241. font-size: 16px;
  242. color: #666;
  243. }
  244. .refresh-text {
  245. font-size: 12px;
  246. color: #666;
  247. margin-left: 5rpx;
  248. }
  249. &:active {
  250. background-color: #e0e0e0;
  251. }
  252. }
  253. }
  254. }
  255. }
  256. </style>