login.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. },
  67. created() {
  68. this.getCode()
  69. },
  70. methods: {
  71. // 用户注册
  72. handleUserRegister() {
  73. this.$tab.redirectTo(`/pages/register`)
  74. },
  75. // 隐私协议
  76. handlePrivacy() {
  77. let site = this.globalConfig.appInfo.agreements[0]
  78. this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
  79. },
  80. // 用户协议
  81. handleUserAgrement() {
  82. let site = this.globalConfig.appInfo.agreements[1]
  83. this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
  84. },
  85. // 获取图形验证码
  86. getCode() {
  87. getCodeImg().then(res => {
  88. this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
  89. if (this.captchaEnabled) {
  90. this.codeUrl = 'data:image/gif;base64,' + res.img
  91. this.loginForm.uuid = res.uuid
  92. }
  93. })
  94. },
  95. // 用户名和密码赋值
  96. mima() {
  97. var that = this;
  98. const value = uni.getStorageSync('user_key');
  99. if (value != '') {
  100. that.loginForm.username = value.username;
  101. if (value.password != '') {
  102. that.isKeepKey[0] = true;
  103. that.loginForm.password = value.password;
  104. }
  105. }
  106. },
  107. // 登录方法
  108. async handleLogin() {
  109. if (this.loginForm.username === "") {
  110. this.$modal.msgError("请输入您的账号")
  111. } else if (this.loginForm.password === "") {
  112. this.$modal.msgError("请输入您的密码")
  113. // } else if (this.loginForm.code === "" && this.captchaEnabled) {
  114. // this.$modal.msgError("请输入验证码")
  115. } else {
  116. this.$modal.loading("登录中,请耐心等待...")
  117. this.pwdLogin()
  118. }
  119. },
  120. // 密码登录
  121. async pwdLogin() {
  122. this.$store.dispatch('Login', this.loginForm).then(() => {
  123. // 保存用户名或密码
  124. var user = {
  125. username: this.loginForm.username,
  126. password: '',
  127. isKeepKey: false
  128. };
  129. console.log(this.loginForm.isKeepKey);
  130. if (this.isKeepKey.length > 0) {
  131. user.password = this.loginForm.password;
  132. user.isKeepKey = true;
  133. }
  134. uni.setStorage({
  135. key: 'user_key',
  136. data: user
  137. });
  138. this.$modal.closeLoading()
  139. this.loginSuccess()
  140. }).catch(() => {
  141. if (this.captchaEnabled) {
  142. this.getCode()
  143. }
  144. })
  145. },
  146. // 登录成功后,处理函数
  147. loginSuccess(result) {
  148. // 设置用户信息
  149. this.$store.dispatch('GetInfo').then(res => {
  150. this.$tab.reLaunch('/pages/index')
  151. })
  152. }
  153. }
  154. }
  155. </script>
  156. <style lang="scss">
  157. page {
  158. background-color: #ffffff;
  159. }
  160. .normal-login-container {
  161. width: 100%;
  162. .logo-content {
  163. width: 100%;
  164. font-size: 21px;
  165. text-align: center;
  166. padding-top: 15%;
  167. image {
  168. border-radius: 4px;
  169. }
  170. .title {
  171. margin-left: 10px;
  172. }
  173. }
  174. .login-form-content {
  175. text-align: center;
  176. margin: 20px auto;
  177. margin-top: 15%;
  178. width: 80%;
  179. .input-item {
  180. margin: 20px auto;
  181. background-color: #f5f6f7;
  182. height: 45px;
  183. border-radius: 20px;
  184. .icon {
  185. font-size: 38rpx;
  186. margin-left: 10px;
  187. color: #999;
  188. }
  189. .input {
  190. width: 100%;
  191. font-size: 14px;
  192. line-height: 20px;
  193. text-align: left;
  194. padding-left: 15px;
  195. }
  196. }
  197. .login-btn {
  198. margin-top: 40px;
  199. height: 45px;
  200. }
  201. .reg {
  202. margin-top: 15px;
  203. }
  204. .xieyi {
  205. color: #333;
  206. margin-top: 20px;
  207. }
  208. .login-code {
  209. height: 38px;
  210. float: right;
  211. .login-code-img {
  212. height: 38px;
  213. position: absolute;
  214. margin-left: 10px;
  215. width: 200rpx;
  216. }
  217. }
  218. }
  219. }
  220. </style>