login.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <view>
  3. <u-modal v-model="modelShow" :content="modelContent"></u-modal>
  4. <common :tips='commonTips'></common>
  5. <image class="login-bg" src="../../static/img/login-bg.png" mode="widthFix"></image>
  6. <view class="login-menu-box">
  7. <view class="login-menu">
  8. <view @click="tab(0)" class="login-button" :class="tabIndex==0 ? 'login-button-active':''">账号登录</view>
  9. <view @click="tab(1)" class="login-button" :class="tabIndex==1 ? 'login-button-active':''">手机登录</view>
  10. </view>
  11. </view>
  12. <u-form :model="form" class="login-form" label-width="150" :label-style="{textAlign: 'justify', textAlignLast: 'justify', display: 'inline-block', paddingRight:'10px'}" ref="uForm">
  13. <u-form-item v-if="tabIndex==0" label="账 号" prop="username">
  14. <u-input v-model="form.username" placeholder="请输入账号" name="username" />
  15. </u-form-item>
  16. <u-form-item v-if="tabIndex==0" label="密 码" prop="password">
  17. <u-input v-model="form.password" :password-icon="true" placeholder="请输入密码" type="password" name="password" />
  18. </u-form-item>
  19. <u-form-item v-if="tabIndex==0" label="验证码" prop="captcha">
  20. <u-input v-model="form.captcha" placeholder="请输入验证码" type="number"></u-input>
  21. <image @click="downloadCaptcha" class="captcha-img" slot="right" :src="captchaPath" mode="widthFix"></image>
  22. </u-form-item>
  23. <u-form-item v-if="tabIndex==1" label="手机号" prop="mobile">
  24. <u-input v-model="form.mobile" placeholder="请输入手机号" type="number" name="mobile" />
  25. </u-form-item>
  26. <u-form-item v-if="tabIndex==1" label="验证码" prop="code">
  27. <u-input placeholder="请输入验证码" v-model="form.code" type="text"></u-input>
  28. <u-button slot="right" type="primary" size="mini" @click="getCode">{{codeTips}}</u-button>
  29. </u-form-item>
  30. <u-button @click="submit" :custom-style="loginFormButtonStyle" :ripple="true" :disabled="loginFormButtonDisabled" ripple-bg-color="rgba(45,211,232, 0.8)" shape="circle">
  31. <u-icon name="arrow-rightward"></u-icon>
  32. </u-button>
  33. <u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
  34. </u-form>
  35. <view class="login-footer-box">
  36. <view @click="goRegister" class="login-footer-box-left">注册新用户</view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. export default {
  42. data() {
  43. return {
  44. tabIndex: 0,
  45. form: {
  46. mobile: '',
  47. username: '',
  48. password: '',
  49. captcha: ''
  50. },
  51. rules: {
  52. mobile: [
  53. {
  54. required: true,
  55. message: '请输入手机号',
  56. trigger: ['change', 'blur']
  57. },
  58. {
  59. validator: (rule, value, callback) => {
  60. return this.$u.test.mobile(value);
  61. },
  62. message: '手机号码不正确',
  63. trigger: ['blur'],
  64. }
  65. ],
  66. username: [
  67. {
  68. required: true,
  69. message: '请输入账号',
  70. trigger: ['change', 'blur']
  71. }
  72. ],
  73. password: [
  74. {
  75. required: true,
  76. min: 6,
  77. message: '请输入正确的密码',
  78. trigger: 'blur'
  79. }
  80. ],
  81. captcha: [
  82. {
  83. required: true,
  84. min: 4,
  85. message: '请输入正确的验证码',
  86. trigger: ['change', 'blur']
  87. }
  88. ]
  89. },
  90. loginFormButtonStyle: {
  91. backgroundImage: "linear-gradient(to bottom right, #34E4E8, #1FAEE8)",
  92. border: 'none',
  93. color: '#ffffff',
  94. outline: 'none',
  95. marginTop: '80rpx',
  96. height: '100rpx',
  97. width: '100rpx'
  98. },
  99. loginFormButtonDisabled: false,
  100. modelShow: false,
  101. modelContent: '',
  102. codeTips: '请输入验证码',
  103. commonTips: '',
  104. captchaPath: ''
  105. }
  106. },
  107. onReady() {
  108. this.downloadCaptcha()
  109. this.$refs.uForm.setRules(this.rules);
  110. },
  111. methods: {
  112. tab: function (index) {
  113. this.tabIndex = index
  114. },
  115. downloadCaptcha: function() {
  116. var that = this
  117. that.form.captcha = ''
  118. that.$u.post('/addons/fastim/api.user/captchaPre', {}).then(res => {
  119. that.ws.captchaId = res.data.captcha_id;
  120. that.captchaPath = this.ws.buildUrl('default') + '/addons/fastim/api.user/captcha?captcha_id=' + res.data.captcha_id
  121. }).catch(res => {
  122. that.$u.toast('验证码请求失败,请重试!');
  123. })
  124. },
  125. getCode: function () {
  126. var that = this
  127. if (!that.form.mobile) {
  128. that.$u.toast('请输入手机号');
  129. return ;
  130. }
  131. if(that.$refs.uCode.canGetCode) {
  132. uni.showLoading({
  133. title: '正在获取验证码',
  134. mask: true
  135. });
  136. that.$u.post('/api/sms/send', {
  137. event: 'login',
  138. mobile: that.form.mobile
  139. }).then(res => {
  140. if (res.code == 1) {
  141. uni.hideLoading();
  142. that.$u.toast('验证码已发送');
  143. that.$refs.uCode.start();
  144. } else {
  145. that.modelContent = res.msg
  146. that.modelShow = true
  147. }
  148. }).catch(res => {
  149. that.$u.toast('请求失败,请重试!');
  150. })
  151. } else {
  152. that.$u.toast('倒计时结束后再发送');
  153. }
  154. },
  155. codeChange(text) {
  156. this.codeTips = text;
  157. },
  158. submit: function() {
  159. var that = this
  160. that.$refs.uForm.validate(valid => {
  161. if (valid) {
  162. that.loginFormButtonDisabled = true
  163. that.form.tab = that.tabIndex
  164. that.form.captcha_id = that.ws.captchaId
  165. that.form.account = (that.tabIndex == 0) ? that.form.username:that.form.mobile;
  166. that.$u.post('/addons/fastim/api.user/login', that.form).then(res => {
  167. that.loginFormButtonDisabled = false
  168. if (res.code == 1) {
  169. uni.setStorageSync('userinfo', res.data.userinfo);
  170. that.ws.init(res.data.userinfo.token, res.data.userinfo.auth_token)
  171. uni.reLaunch({
  172. url: '/pages/message/message'
  173. })
  174. } else {
  175. that.downloadCaptcha()
  176. that.modelContent = res.msg
  177. that.modelShow = true
  178. }
  179. }).catch(res => {
  180. that.downloadCaptcha()
  181. that.loginFormButtonDisabled = false
  182. that.$u.toast('请求失败,请重试!');
  183. })
  184. }
  185. });
  186. },
  187. goRegister: function() {
  188. uni.navigateTo({
  189. url: '/pages/center/register'
  190. })
  191. }
  192. }
  193. }
  194. </script>
  195. <style lang="scss">
  196. page {
  197. background-color: $--bg-color;
  198. }
  199. .login-bg {
  200. width: 100vw;
  201. }
  202. .login-menu-box {
  203. position: relative;
  204. }
  205. .login-menu {
  206. display: flex;
  207. width: 80vw;
  208. margin: 0 auto;
  209. align-items: center;
  210. background-color: $--white;
  211. border-radius: 50rpx;
  212. position: absolute;
  213. top: -40rpx;
  214. left: 10vw;
  215. box-shadow: 0 0 10px rgba(0, 0, 0, .2);
  216. }
  217. .login-button {
  218. height: 80rpx;
  219. width: 50%;
  220. font-size: 30rpx;
  221. font-weight: bold;
  222. text-align: center;
  223. line-height: 80rpx;
  224. color: $--gray;
  225. border-radius: 50rpx;
  226. background: $--white;
  227. }
  228. .login-button-active {
  229. color: $--white;
  230. background-image: linear-gradient(to bottom right, #34E4E8, #1FAEE8);
  231. }
  232. .login-form {
  233. display: block;
  234. width: 78vw;
  235. padding-top: 140rpx;
  236. margin: 0 auto;
  237. }
  238. .login-footer-box {
  239. position: fixed;
  240. bottom: 20rpx;
  241. display: flex;
  242. align-items: center;
  243. justify-content: center;
  244. width: 100vw;
  245. }
  246. .login-footer-box-left {
  247. text-decoration: underline;
  248. font-size: 28rpx;
  249. color: $--blue;
  250. }
  251. .captcha-img {
  252. width: 200rpx;
  253. max-height: 100rpx;
  254. }
  255. </style>