login.vue 6.4 KB

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