login-withpwd.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!-- 账号密码登录页 -->
  2. <template>
  3. <view class="uni-content">
  4. <view class="login-logo">
  5. <image :src="logo"></image>
  6. </view>
  7. <!-- 顶部文字 -->
  8. <text class="title title-box">账号密码登录</text>
  9. <uni-forms>
  10. <uni-forms-item name="username">
  11. <uni-easyinput :focus="focusUsername" @blur="focusUsername = false" class="input-box"
  12. :inputBorder="false" v-model="username" placeholder="请输入手机号/用户名/邮箱" />
  13. </uni-forms-item>
  14. <uni-forms-item name="password">
  15. <uni-easyinput :focus="focusPassword" @blur="focusPassword = false" class="input-box" clearable
  16. type="password" :inputBorder="false" v-model="password" placeholder="请输入密码" />
  17. </uni-forms-item>
  18. </uni-forms>
  19. <uni-captcha v-if="needCaptcha" focus ref="captcha" scene="login-by-pwd" v-model="captcha" />
  20. <!-- 带选择框的隐私政策协议组件 -->
  21. <uni-id-pages-agreements scope="login" ref="agreements"></uni-id-pages-agreements>
  22. <button class="uni-btn" type="primary" @click="pwdLogin">登录</button>
  23. <!-- 忘记密码 -->
  24. <view class="link-box">
  25. <view v-if="!config.isAdmin">
  26. <text class="forget">忘记了?</text>
  27. <text class="link" @click="toRetrievePwd">找回密码</text>
  28. </view>
  29. <text v-if="!existAdmin" class="link" @click="toRegister">{{config.isAdmin ? '注册管理员账号': '注册账号'}}</text>
  30. </view>
  31. <!-- 悬浮登录方式组件 -->
  32. <!-- #ifndef MP-TOUTIAO -->
  33. <uni-id-pages-fab-login ref="uniFabLogin"></uni-id-pages-fab-login>
  34. <!-- #endif -->
  35. </view>
  36. </template>
  37. <script>
  38. import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
  39. const uniIdCo = uniCloud.importObject("uni-id-co", {
  40. errorOptions: {
  41. type: 'toast'
  42. }
  43. })
  44. export default {
  45. mixins: [mixin],
  46. data() {
  47. return {
  48. "password": "",
  49. "username": "",
  50. "captcha": "",
  51. "needCaptcha": false,
  52. "focusUsername": false,
  53. "focusPassword": false,
  54. "logo": "/static/logo.png",
  55. "existAdmin": true
  56. }
  57. },
  58. onShow() {
  59. // #ifdef H5
  60. document.onkeydown = event => {
  61. let e = event || window.event;
  62. if (e && e.keyCode == 13) { //回车键的键值为13
  63. this.pwdLogin()
  64. }
  65. };
  66. // #endif
  67. },
  68. async onLoad() {
  69. // 查询是否已经有管理员注册了,如果有,则隐藏注册管理员的入口
  70. try {
  71. const db = uniCloud.database();
  72. let countRes = await db.collection("uni-id-users").where({role:"admin"}).count();
  73. let count = countRes.result.total;
  74. this.existAdmin = count > 0 ? true : false;
  75. } catch(err){
  76. this.existAdmin = false;
  77. }
  78. },
  79. methods: {
  80. // 页面跳转,找回密码
  81. toRetrievePwd() {
  82. let url = '/uni_modules/uni-id-pages/pages/retrieve/retrieve'
  83. //如果刚好用户名输入框的值为手机号码,就把它传到retrieve页面,根据该手机号找回密码
  84. if (/^1\d{10}$/.test(this.username)) {
  85. url += `?phoneNumber=${this.username}`
  86. }
  87. uni.navigateTo({
  88. url
  89. })
  90. },
  91. /**
  92. * 密码登录
  93. */
  94. pwdLogin() {
  95. if (!this.password.length) {
  96. this.focusPassword = true
  97. return uni.showToast({
  98. title: '请输入密码',
  99. icon: 'none',
  100. duration: 3000
  101. });
  102. }
  103. if (!this.username.length) {
  104. this.focusUsername = true
  105. return uni.showToast({
  106. title: '请输入手机号/用户名/邮箱',
  107. icon: 'none',
  108. duration: 3000
  109. });
  110. }
  111. if (this.needCaptcha && this.captcha.length != 4) {
  112. this.$refs.captcha.getImageCaptcha()
  113. return uni.showToast({
  114. title: '请输入验证码',
  115. icon: 'none',
  116. duration: 3000
  117. });
  118. }
  119. if (this.needAgreements && !this.agree) {
  120. return this.$refs.agreements.popup(this.pwdLogin)
  121. }
  122. let data = {
  123. "password": this.password,
  124. "captcha": this.captcha
  125. }
  126. if (/^1\d{10}$/.test(this.username)) {
  127. data.mobile = this.username
  128. } else if (/@/.test(this.username)) {
  129. data.email = this.username
  130. } else {
  131. data.username = this.username
  132. }
  133. uniIdCo.login(data).then(e => {
  134. this.loginSuccess(e)
  135. }).catch(e => {
  136. if (e.errCode == 'uni-id-captcha-required') {
  137. this.needCaptcha = true
  138. } else if (this.needCaptcha) {
  139. //登录失败,自动重新获取验证码
  140. this.$refs.captcha.getImageCaptcha()
  141. }
  142. })
  143. },
  144. /* 前往注册 */
  145. toRegister() {
  146. uni.navigateTo({
  147. url: this.config.isAdmin ? '/uni_modules/uni-id-pages/pages/register/register-admin' :
  148. '/uni_modules/uni-id-pages/pages/register/register',
  149. fail(e) {
  150. console.error(e);
  151. }
  152. })
  153. }
  154. }
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. @import "@/uni_modules/uni-id-pages/common/login-page.scss";
  159. @media screen and (min-width: 690px) {
  160. .uni-content {
  161. height: auto;
  162. }
  163. }
  164. .forget {
  165. font-size: 12px;
  166. color: #8a8f8b;
  167. }
  168. .link-box {
  169. /* #ifndef APP-NVUE */
  170. display: flex;
  171. /* #endif */
  172. flex-direction: row;
  173. justify-content: space-between;
  174. margin-top: 20px;
  175. }
  176. .link {
  177. font-size: 12px;
  178. }
  179. </style>