login.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. <view class="action-btn">
  25. <button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
  26. </view>
  27. <view class="reg text-center" v-if="register">
  28. <text class="text-grey1">没有账号?</text>
  29. <text @click="handleUserRegister" class="text-blue">立即注册</text>
  30. </view>
  31. <view class="xieyi text-center">
  32. <text class="text-grey1">登录即代表同意</text>
  33. <text @click="handleUserAgrement" class="text-blue">《用户协议》</text>
  34. <text @click="handlePrivacy" class="text-blue">《隐私协议》</text>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import { getCodeImg } from '@/api/login'
  41. import { getToken } from '@/utils/auth'
  42. import { encrypt, decrypt } from '@/utils/jsencrypt'
  43. import config from '@/config'
  44. export default {
  45. data() {
  46. return {
  47. codeUrl: "",
  48. captchaEnabled: true,
  49. // 用户注册开关
  50. register: false,
  51. globalConfig: getApp().globalData.config,
  52. loginForm: {
  53. username: "admin",
  54. password: "admin123",
  55. code: "",
  56. uuid: ""
  57. },
  58. jzflag:true
  59. }
  60. },
  61. created() {
  62. this.getCode()
  63. },
  64. onLoad() {
  65. //#ifdef H5
  66. if (getToken()) {
  67. this.$tab.reLaunch('/pages/index')
  68. }
  69. //#endif
  70. },
  71. methods: {
  72. // 用户注册
  73. handleUserRegister() {
  74. this.$tab.redirectTo(`/pages/register`)
  75. },
  76. // 隐私协议
  77. handlePrivacy() {
  78. let site = this.globalConfig.appInfo.agreements[0]
  79. this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
  80. },
  81. // 用户协议
  82. handleUserAgrement() {
  83. let site = this.globalConfig.appInfo.agreements[1]
  84. this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
  85. },
  86. // 获取图形验证码
  87. getCode() {
  88. getCodeImg().then(res => {
  89. this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
  90. if (this.captchaEnabled) {
  91. this.codeUrl = 'data:image/gif;base64,' + res.img
  92. this.loginForm.uuid = res.uuid
  93. }
  94. })
  95. },
  96. // 登录方法
  97. async handleLogin() {
  98. if (this.loginForm.username === "") {
  99. this.$modal.msgError("请输入账号")
  100. } else if (this.loginForm.password === "") {
  101. this.$modal.msgError("请输入密码")
  102. } else if (this.loginForm.code === "" && this.captchaEnabled) {
  103. this.$modal.msgError("请输入验证码")
  104. } else {
  105. this.$modal.loading("登录中,请耐心等待...")
  106. this.pwdLogin()
  107. }
  108. },
  109. // 密码登录
  110. async pwdLogin() {
  111. var that=this;
  112. this.$store.dispatch('Login', this.loginForm).then(() => {
  113. // 判断是否记住密码
  114. if(that.jzflag){
  115. var newObj={
  116. username:that.loginForm.username,
  117. password:encrypt(that.loginForm.password),
  118. captchaEnabled:that.captchaEnabled,
  119. }
  120. uni.setStorageSync('account', JSON.parse(JSON.stringify(newObj)))
  121. }else{
  122. uni.removeStorageSync('account')
  123. }
  124. this.$modal.closeLoading()
  125. this.loginSuccess()
  126. this.$modal.msg('登录成功')
  127. setTimeout(function(){
  128. that.loginSuccess()
  129. },1500)
  130. }).catch(() => {
  131. if (this.captchaEnabled) {
  132. this.getCode()
  133. }
  134. })
  135. },
  136. // 登录成功后,处理函数
  137. loginSuccess(result) {
  138. // 设置用户信息
  139. this.$store.dispatch('GetInfo').then(res => {
  140. this.$tab.reLaunch('/pages/index')
  141. })
  142. }
  143. }
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. page {
  148. background-color: #ffffff;
  149. }
  150. .normal-login-container {
  151. width: 100%;
  152. .logo-content {
  153. width: 100%;
  154. font-size: 21px;
  155. text-align: center;
  156. padding-top: 15%;
  157. image {
  158. border-radius: 4px;
  159. }
  160. .title {
  161. margin-left: 10px;
  162. }
  163. }
  164. .login-form-content {
  165. text-align: center;
  166. margin: 20px auto;
  167. margin-top: 15%;
  168. width: 80%;
  169. .input-item {
  170. margin: 20px auto;
  171. background-color: #f5f6f7;
  172. height: 45px;
  173. border-radius: 20px;
  174. .icon {
  175. font-size: 38rpx;
  176. margin-left: 10px;
  177. color: #999;
  178. }
  179. .input {
  180. width: 100%;
  181. font-size: 14px;
  182. line-height: 20px;
  183. text-align: left;
  184. padding-left: 15px;
  185. }
  186. }
  187. .login-btn {
  188. margin-top: 40px;
  189. height: 45px;
  190. }
  191. .reg {
  192. margin-top: 15px;
  193. }
  194. .xieyi {
  195. color: #333;
  196. margin-top: 20px;
  197. }
  198. .login-code {
  199. height: 38px;
  200. float: right;
  201. .login-code-img {
  202. height: 38px;
  203. position: absolute;
  204. margin-left: 10px;
  205. width: 200rpx;
  206. }
  207. }
  208. }
  209. }
  210. </style>