login.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <template>
  2. <view class="login-container">
  3. <view class="login-box-bg">
  4. <image
  5. src="https://images.unsplash.com/photo-1519751138087-5bf79df62d5b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wyMDUzMDJ8MHwxfHNlYXJjaHwyMHx8YmFja2dyb3VuZHxlbnwxfHx8fDE3MjExOTc4NjJ8MA&ixlib=rb-4.0.3&q=80&w=1080"
  6. mode="aspectFill"></image>
  7. </view>
  8. <view class="login-box">
  9. <view class="title">登录</view>
  10. <view class="sub-title">欢迎使用有极智慧园区管理软件</view>
  11. <view class="form">
  12. <view class="form-item">
  13. <view class="form-item-label">手机号</view>
  14. <view class="form-input-item">
  15. <uni-icons class="form-input-icons" type="person" size="24"></uni-icons>
  16. <view class="form-input-content">
  17. <input type="number" class="form-input" placeholder="请输入手机号" maxlength="11"
  18. v-model="form.phone" />
  19. </view>
  20. </view>
  21. </view>
  22. <!-- 获取验证码 -->
  23. <view class="form-item">
  24. <view class="form-item-label">验证码</view>
  25. <view class="form-input-item">
  26. <uni-icons class="form-input-icons" type="email" size="24"></uni-icons>
  27. <view class="form-input-content">
  28. <input type="number" class="form-input" maxlength="6" placeholder="请输入验证码"
  29. v-model="form.phoneCode" />
  30. </view>
  31. <text class="code-input-btn" :class="isDisabled ? 'disabled':''" @click="getImageCode">
  32. {{codeName}}
  33. </text>
  34. </view>
  35. </view>
  36. <view class="form-btn-container">
  37. <button class="form-btn" @click="submitLogin">登录</button>
  38. </view>
  39. </view>
  40. </view>
  41. <view class="login-bottom">
  42. <view class="line-text">or</view>
  43. <button class="wx-btn" open-type="getPhoneNumber" @getphonenumber="wxPhoneLogin">
  44. <uni-icons type="weixin" size="24" color="#43b156"></uni-icons>
  45. </button>
  46. </view>
  47. <image-code :show="captchaShow" title="请填写图形验证码" @onConfirm="confirmHandle" @onCancel="captchaShow = false">
  48. <view class="code-input-container">
  49. <input type="text" class="code-input" placeholder="输入图形验证码" maxlength="6" v-model="imgCode" />
  50. <view class="code-canvas-wrapper">
  51. <img v-if="codeImg" :src="codeImg" alt="图片验证码" class="code-image" @click="imgCodeFunc">
  52. </view>
  53. </view>
  54. </image-code>
  55. </view>
  56. </template>
  57. <script>
  58. import {
  59. login,
  60. getImgCode,
  61. sendPhoneCode,
  62. getUserInfo
  63. } from '@/request/api/login'
  64. import {
  65. isTel
  66. } from '@/uitls/validate'
  67. import imageCode from "@/components/login/imageCode.vue";
  68. export default {
  69. data() {
  70. return {
  71. form: {
  72. phone: '18888888888',
  73. phoneCode: '888888'
  74. },
  75. captchaShow: false,
  76. imgCode: '',
  77. codeImg: '',
  78. codeName: '获取验证码', //获取验证码text
  79. isDisabled: false, //获取验证码按钮状态
  80. }
  81. },
  82. components: {
  83. imageCode
  84. },
  85. methods: {
  86. getImageCode() {
  87. if (!isTel(this.form.phone)) return this.$toast('请输入正确的手机号');
  88. if (this.isDisabled) return;
  89. this.imgCodeFunc();
  90. this.captchaShow = true;
  91. },
  92. imgCodeFunc() {
  93. getImgCode().then(res => {
  94. if (res.code === 200) {
  95. this.codeImg = res.data.pngBase64;
  96. }
  97. })
  98. },
  99. confirmHandle() {
  100. if (!this.imgCode) return this.$toast('请输入图片验证码');
  101. sendPhoneCode(this.form.phone, this.imgCode).then(res => {
  102. if (res.code === 200) {
  103. this.codeReset();
  104. this.$toast('发送成功');
  105. this.imgCode = '';
  106. this.captchaShow = false;
  107. } else {
  108. this.imgCodeFunc();
  109. }
  110. })
  111. },
  112. codeReset() {
  113. //重置获取验证码倒计时
  114. let codeNumber = 60;
  115. codeNumber--;
  116. this.handleCode(codeNumber);
  117. let codeRestFn = setInterval(() => {
  118. codeNumber--;
  119. this.handleCode(codeNumber);
  120. if (codeNumber == 0) clearInterval(codeRestFn); //停止
  121. }, 1000);
  122. },
  123. handleCode(codeNumber) {
  124. //code操作
  125. this.codeName = codeNumber == 0 ? '获取验证码' : '重新获取' + codeNumber;
  126. this.isDisabled = codeNumber == 0 ? false : true;
  127. },
  128. wxPhoneLogin(e) { //微信手机号一键登录
  129. if (!e.detail.code) return;
  130. this.login({
  131. pCode: e.detail.code
  132. })
  133. },
  134. submitLogin() { //手机验证码登录
  135. if (!this.form.phoneCode) return this.$toast('请输入短信验证码');
  136. this.login(this.form);
  137. },
  138. login(postData) {
  139. uni.showLoading({
  140. title: '登录中'
  141. })
  142. // #ifdef MP-WEIXIN
  143. uni.login({
  144. provider: 'weixin',
  145. success: loginRes => {
  146. // if (postData.phone != '18888888888') postData['code'] = loginRes.code;
  147. login(postData).then(this.successFunc);
  148. }
  149. })
  150. // #endif
  151. // #ifndef MP-WEIXIN
  152. login(postData).then(this.successFunc);
  153. // #endif
  154. },
  155. successFunc(res) {
  156. if (res.code === 200) {
  157. uni.setStorageSync('token', res.data.token);
  158. getUserInfo().then(user => {
  159. if (user.code === 200) {
  160. let userInfo = user.data;
  161. this.$store.dispatch('app/changeOrganization', userInfo.organization);
  162. this.$store.dispatch('app/changeUser', userInfo);
  163. this.$chat.connect(userInfo.userId);
  164. uni.setStorageSync('vuex_state', this.$store.state);
  165. this.$toast('登录成功');
  166. setTimeout(() => {
  167. uni.hideLoading();
  168. uni.navigateBack();
  169. }, 400)
  170. } else {
  171. uni.hideLoading();
  172. }
  173. })
  174. } else {
  175. uni.hideLoading();
  176. }
  177. }
  178. }
  179. }
  180. </script>
  181. <style lang="scss" scoped>
  182. .login-container {
  183. height: 100vh;
  184. background: #fff;
  185. display: flex;
  186. flex-direction: column;
  187. .login-box-bg {
  188. height: 300rpx;
  189. image {
  190. width: 100%;
  191. height: 100%;
  192. }
  193. }
  194. .login-box {
  195. padding: 0 30rpx;
  196. flex: 1;
  197. min-width: 640rpx;
  198. }
  199. .title {
  200. padding-top: 30rpx;
  201. font-size: 48rpx;
  202. font-weight: bold;
  203. }
  204. .sub-title {
  205. font-size: 24rpx;
  206. color: $uni-extra-color;
  207. margin-top: 10rpx;
  208. }
  209. //弹出输入框
  210. .code-input-container {
  211. display: flex;
  212. align-items: center;
  213. margin-top: 20rpx;
  214. .code-input {
  215. background: #F7F8FA;
  216. width: 306rpx;
  217. height: 84rpx;
  218. line-height: 84rpx;
  219. padding: 0 20rpx;
  220. border-radius: 20rpx;
  221. box-sizing: border-box;
  222. &::placeholder {
  223. line-height: 84rpx;
  224. }
  225. }
  226. .code-canvas-wrapper {
  227. flex: 1;
  228. .code-image {
  229. width: 100%;
  230. height: 84rpx;
  231. }
  232. }
  233. }
  234. .form {
  235. display: flex;
  236. flex-direction: column;
  237. align-items: center;
  238. margin-top: 30rpx;
  239. .form-item {
  240. width: 100%;
  241. margin-bottom: 30rpx;
  242. }
  243. .form-item-label {
  244. font-weight: 500;
  245. margin-bottom: 20rpx;
  246. }
  247. // 登录的input项
  248. .form-input-item {
  249. height: 88rpx;
  250. border-radius: 88rpx;
  251. border: 1px solid $uni-border-1;
  252. width: 100%;
  253. display: flex;
  254. align-items: center;
  255. box-sizing: border-box;
  256. .form-input-icons {
  257. padding: 0 20rpx 0 30rpx;
  258. .uni-icons {
  259. color: $uni-secondary-color !important;
  260. }
  261. }
  262. .form-input-content {
  263. flex: 1;
  264. width: 0;
  265. }
  266. .code-input-btn {
  267. color: $uni-primary;
  268. padding: 0 30rpx;
  269. }
  270. }
  271. // 登录按钮容器
  272. .form-btn-container {
  273. margin-top: 20rpx;
  274. width: 100%;
  275. }
  276. // 登录按钮
  277. .form-btn {
  278. height: 88rpx;
  279. border-radius: 200rpx;
  280. padding: 24rpx 32rpx;
  281. line-height: 40rpx;
  282. color: $uni-white;
  283. background-color: $uni-primary;
  284. font-size: 32rpx;
  285. &.disabled {
  286. opacity: 0.5;
  287. }
  288. &::after {
  289. display: none;
  290. }
  291. }
  292. }
  293. .login-bottom {
  294. display: flex;
  295. flex-direction: column;
  296. justify-content: center;
  297. align-items: center;
  298. padding-bottom: 30px;
  299. .wx-btn {
  300. margin-top: 40rpx;
  301. height: 80rpx;
  302. width: 160rpx;
  303. text-align: center;
  304. line-height: 80rpx;
  305. background: #fff;
  306. &::after {
  307. border-radius: 80rpx;
  308. border-color: $uni-border-1;
  309. }
  310. }
  311. .line-text {
  312. position: relative;
  313. text-align: center;
  314. color: $uni-secondary-color;
  315. width: 100%;
  316. &::before {
  317. content: '';
  318. position: absolute;
  319. left: 60rpx;
  320. right: calc(50% + 20px);
  321. height: 1px;
  322. background: $uni-border-1;
  323. top: 50%;
  324. }
  325. &::after {
  326. content: '';
  327. position: absolute;
  328. right: 60rpx;
  329. left: calc(50% + 20px);
  330. height: 1px;
  331. background: $uni-border-1;
  332. top: 50%;
  333. }
  334. }
  335. }
  336. }
  337. </style>