login.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <view class="normal-login-container">
  3. <view class="logo-content align-center justify-center flex">
  4. <!-- <image style="width: 100rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix">
  5. </image> -->
  6. <text class="title">途宝报单</text>
  7. </view>
  8. <view class="login-form-content">
  9. <view class="input-item flex align-center">
  10. <view class="iconfont icon-user icon"></view>
  11. <input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
  12. </view>
  13. <view class="input-item flex align-center">
  14. <view class="iconfont icon-password icon"></view>
  15. <input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
  16. </view>
  17. <!-- <view class="input-item flex align-center" style="width: 60%;margin: 0px;" v-if="captchaEnabled">
  18. <view class="iconfont icon-code icon"></view>
  19. <input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
  20. <view class="login-code">
  21. <image :src="codeUrl" @click="getCode" class="login-code-img"></image>
  22. </view>
  23. </view> -->
  24. <u-checkbox-group v-model="isKeepKey">
  25. <u-checkbox label-size="12" label="保存密码" :name="true"></u-checkbox>
  26. </u-checkbox-group>
  27. <view class="action-btn">
  28. <button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
  29. </view>
  30. <view class="reg text-center" v-if="register">
  31. <text class="text-grey1">没有账号?</text>
  32. <text @click="handleUserRegister" class="text-blue">立即注册</text>
  33. </view>
  34. <!-- <view class="xieyi text-center">
  35. <text class="text-grey1">登录即代表同意</text>
  36. <text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
  37. <text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
  38. </view> -->
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. import {
  44. getCodeImg
  45. } from '@/api/login'
  46. export default {
  47. data() {
  48. return {
  49. // 是否保存密码
  50. isKeepKey: [],
  51. codeUrl: "",
  52. captchaEnabled: true,
  53. // 用户注册开关
  54. register: false,
  55. globalConfig: getApp().globalData.config,
  56. loginForm: {
  57. username: "",
  58. password: "",
  59. code: "",
  60. uuid: ''
  61. },
  62. }
  63. },
  64. onLoad() {
  65. this.mima();
  66. // 判断是否有token
  67. if(uni.getStorageSync('App-Token')) {
  68. this.$tab.reLaunch('/pages/index')
  69. }
  70. },
  71. created() {
  72. this.getCode()
  73. },
  74. methods: {
  75. // 用户注册
  76. handleUserRegister() {
  77. this.$tab.redirectTo(`/pages/register`)
  78. },
  79. // 隐私协议
  80. handlePrivacy() {
  81. let site = this.globalConfig.appInfo.agreements[0]
  82. this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
  83. },
  84. // 用户协议
  85. handleUserAgrement() {
  86. let site = this.globalConfig.appInfo.agreements[1]
  87. this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
  88. },
  89. // 获取图形验证码
  90. getCode() {
  91. getCodeImg().then(res => {
  92. this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
  93. if (this.captchaEnabled) {
  94. this.codeUrl = 'data:image/gif;base64,' + res.img
  95. this.loginForm.uuid = res.uuid
  96. }
  97. })
  98. },
  99. // 用户名和密码赋值
  100. mima() {
  101. var that = this;
  102. const value = uni.getStorageSync('user_key');
  103. if (value != '') {
  104. that.loginForm.username = value.username;
  105. if (value.password != '') {
  106. that.isKeepKey[0] = true;
  107. that.loginForm.password = value.password;
  108. }
  109. }
  110. },
  111. // 登录方法
  112. async handleLogin() {
  113. if (this.loginForm.username === "") {
  114. this.$modal.msgError("请输入您的账号")
  115. } else if (this.loginForm.password === "") {
  116. this.$modal.msgError("请输入您的密码")
  117. // } else if (this.loginForm.code === "" && this.captchaEnabled) {
  118. // this.$modal.msgError("请输入验证码")
  119. } else {
  120. this.$modal.loading("登录中,请耐心等待...")
  121. this.pwdLogin()
  122. }
  123. },
  124. // 密码登录
  125. async pwdLogin() {
  126. this.$store.dispatch('Login', this.loginForm).then(() => {
  127. // 保存用户名或密码
  128. var user = {
  129. username: this.loginForm.username,
  130. password: '',
  131. isKeepKey: false
  132. };
  133. if (this.isKeepKey.length > 0) {
  134. user.password = this.loginForm.password;
  135. user.isKeepKey = true;
  136. }
  137. uni.setStorage({
  138. key: 'user_key',
  139. data: user
  140. });
  141. this.$modal.closeLoading()
  142. this.loginSuccess()
  143. }).catch(() => {
  144. if (this.captchaEnabled) {
  145. this.getCode()
  146. }
  147. })
  148. },
  149. // 登录成功后,处理函数
  150. loginSuccess(result) {
  151. // 设置用户信息
  152. this.$store.dispatch('GetInfo').then(res => {
  153. this.$tab.reLaunch('/pages/index')
  154. })
  155. }
  156. }
  157. }
  158. </script>
  159. <style lang="scss">
  160. page {
  161. background-color: #ffffff;
  162. }
  163. .normal-login-container {
  164. width: 100%;
  165. .logo-content {
  166. width: 100%;
  167. font-size: 21px;
  168. text-align: center;
  169. padding-top: 15%;
  170. image {
  171. border-radius: 4px;
  172. }
  173. .title {
  174. margin-left: 10px;
  175. }
  176. }
  177. .login-form-content {
  178. text-align: center;
  179. margin: 20px auto;
  180. margin-top: 15%;
  181. width: 80%;
  182. .input-item {
  183. margin: 20px auto;
  184. background-color: #f5f6f7;
  185. height: 45px;
  186. border-radius: 20px;
  187. .icon {
  188. font-size: 38rpx;
  189. margin-left: 10px;
  190. color: #999;
  191. }
  192. .input {
  193. width: 100%;
  194. font-size: 14px;
  195. line-height: 20px;
  196. text-align: left;
  197. padding-left: 15px;
  198. }
  199. }
  200. .login-btn {
  201. margin-top: 40px;
  202. height: 45px;
  203. }
  204. .reg {
  205. margin-top: 15px;
  206. }
  207. .xieyi {
  208. color: #333;
  209. margin-top: 20px;
  210. }
  211. .login-code {
  212. height: 38px;
  213. float: right;
  214. .login-code-img {
  215. height: 38px;
  216. position: absolute;
  217. margin-left: 10px;
  218. width: 200rpx;
  219. }
  220. }
  221. }
  222. }
  223. </style>