login.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. getProjectList
  66. } from '@/request/api/project'
  67. import {
  68. isTel
  69. } from '@/uitls/validate'
  70. import imageCode from "@/components/login/imageCode.vue";
  71. export default {
  72. data() {
  73. return {
  74. form: {
  75. phone: '18888888888',
  76. phoneCode: '888888'
  77. },
  78. captchaShow: false,
  79. imgCode: '',
  80. codeImg: '',
  81. codeName: '获取验证码', //获取验证码text
  82. isDisabled: false, //获取验证码按钮状态
  83. }
  84. },
  85. components: {
  86. imageCode
  87. },
  88. methods: {
  89. getImageCode() {
  90. if (!isTel(this.form.phone)) return this.$toast('请输入正确的手机号');
  91. if (this.isDisabled) return;
  92. this.imgCodeFunc();
  93. this.captchaShow = true;
  94. },
  95. imgCodeFunc() {
  96. getImgCode().then(res => {
  97. if (res.code === 200) {
  98. this.codeImg = res.data.pngBase64;
  99. }
  100. })
  101. },
  102. confirmHandle() {
  103. if (!this.imgCode) return this.$toast('请输入图片验证码');
  104. sendPhoneCode(this.form.phone, this.imgCode).then(res => {
  105. if (res.code === 200) {
  106. this.codeReset();
  107. this.$toast('发送成功');
  108. this.imgCode = '';
  109. this.captchaShow = false;
  110. } else {
  111. this.imgCodeFunc();
  112. }
  113. })
  114. },
  115. codeReset() {
  116. //重置获取验证码倒计时
  117. let codeNumber = 60;
  118. codeNumber--;
  119. this.handleCode(codeNumber);
  120. let codeRestFn = setInterval(() => {
  121. codeNumber--;
  122. this.handleCode(codeNumber);
  123. if (codeNumber == 0) clearInterval(codeRestFn); //停止
  124. }, 1000);
  125. },
  126. handleCode(codeNumber) {
  127. //code操作
  128. this.codeName = codeNumber == 0 ? '获取验证码' : '重新获取' + codeNumber;
  129. this.isDisabled = codeNumber == 0 ? false : true;
  130. },
  131. wxPhoneLogin(e) { //微信手机号一键登录
  132. if (!e.detail.code) return;
  133. this.login({
  134. pCode: e.detail.code
  135. })
  136. },
  137. submitLogin() { //手机验证码登录
  138. if (!this.form.phoneCode) return this.$toast('请输入短信验证码');
  139. this.login(this.form);
  140. },
  141. login(postData) {
  142. uni.showLoading({
  143. title: '登录中'
  144. })
  145. // #ifdef MP-WEIXIN
  146. uni.login({
  147. provider: 'weixin',
  148. success: loginRes => {
  149. if (postData.pCode) postData['code'] = loginRes.code;
  150. login(postData).then(this.successFunc);
  151. }
  152. })
  153. // #endif
  154. // #ifndef MP-WEIXIN
  155. login(postData).then(this.successFunc);
  156. // #endif
  157. },
  158. successFunc(res) {
  159. if (res.code === 200) {
  160. uni.setStorageSync('token', res.data.token);
  161. getUserInfo().then(user => {
  162. if (user.code === 200) {
  163. let userInfo = user.data;
  164. this.$store.dispatch('app/changeOrganization', userInfo.organization);
  165. this.$store.dispatch('app/changeUser', userInfo);
  166. uni.removeStorageSync('chatToken');
  167. this.$chat.connect(userInfo.userId);
  168. this.initProjectData(userInfo.projectId);
  169. } else {
  170. uni.hideLoading();
  171. }
  172. })
  173. } else {
  174. uni.hideLoading();
  175. }
  176. },
  177. initProjectData(projectId) {
  178. getProjectList().then(res => {
  179. if (res.code == 200) {
  180. if (res.data.length === 0) return this.successLogin();
  181. let project = projectId === -1 ? res.data[0] : res.data.find(node => node.id ===
  182. projectId);
  183. this.$store.dispatch('app/changeProject', project);
  184. this.$store.dispatch('app/changeIdentity', project.projectListIdentity[0]);
  185. this.successLogin();
  186. } else {
  187. uni.hideLoading();
  188. }
  189. })
  190. },
  191. successLogin() {
  192. uni.setStorageSync('vuex_state', this.$store.state);
  193. this.$toast('登录成功');
  194. setTimeout(() => {
  195. uni.hideLoading();
  196. uni.navigateBack();
  197. }, 400)
  198. }
  199. }
  200. }
  201. </script>
  202. <style lang="scss" scoped>
  203. .login-container {
  204. height: 100vh;
  205. background: #fff;
  206. display: flex;
  207. flex-direction: column;
  208. .login-box-bg {
  209. height: 300rpx;
  210. image {
  211. width: 100%;
  212. height: 100%;
  213. }
  214. }
  215. .login-box {
  216. padding: 0 30rpx;
  217. flex: 1;
  218. min-width: 640rpx;
  219. }
  220. .title {
  221. padding-top: 30rpx;
  222. font-size: 48rpx;
  223. font-weight: bold;
  224. }
  225. .sub-title {
  226. font-size: 24rpx;
  227. color: $uni-extra-color;
  228. margin-top: 10rpx;
  229. }
  230. //弹出输入框
  231. .code-input-container {
  232. display: flex;
  233. align-items: center;
  234. margin-top: 20rpx;
  235. .code-input {
  236. background: #F7F8FA;
  237. width: 306rpx;
  238. height: 84rpx;
  239. line-height: 84rpx;
  240. padding: 0 20rpx;
  241. border-radius: 20rpx;
  242. box-sizing: border-box;
  243. &::placeholder {
  244. line-height: 84rpx;
  245. }
  246. }
  247. .code-canvas-wrapper {
  248. flex: 1;
  249. .code-image {
  250. width: 100%;
  251. height: 84rpx;
  252. }
  253. }
  254. }
  255. .form {
  256. display: flex;
  257. flex-direction: column;
  258. align-items: center;
  259. margin-top: 30rpx;
  260. .form-item {
  261. width: 100%;
  262. margin-bottom: 30rpx;
  263. }
  264. .form-item-label {
  265. font-weight: 500;
  266. margin-bottom: 20rpx;
  267. }
  268. // 登录的input项
  269. .form-input-item {
  270. height: 88rpx;
  271. border-radius: 88rpx;
  272. border: 1px solid $uni-border-1;
  273. width: 100%;
  274. display: flex;
  275. align-items: center;
  276. box-sizing: border-box;
  277. .form-input-icons {
  278. padding: 0 20rpx 0 30rpx;
  279. .uni-icons {
  280. color: $uni-secondary-color !important;
  281. }
  282. }
  283. .form-input-content {
  284. flex: 1;
  285. width: 0;
  286. }
  287. .code-input-btn {
  288. color: $uni-primary;
  289. padding: 0 30rpx;
  290. }
  291. }
  292. // 登录按钮容器
  293. .form-btn-container {
  294. margin-top: 20rpx;
  295. width: 100%;
  296. }
  297. // 登录按钮
  298. .form-btn {
  299. height: 88rpx;
  300. border-radius: 200rpx;
  301. padding: 24rpx 32rpx;
  302. line-height: 40rpx;
  303. color: $uni-white;
  304. background-color: $uni-primary;
  305. font-size: 32rpx;
  306. &.disabled {
  307. opacity: 0.5;
  308. }
  309. &::after {
  310. display: none;
  311. }
  312. }
  313. }
  314. .login-bottom {
  315. display: flex;
  316. flex-direction: column;
  317. justify-content: center;
  318. align-items: center;
  319. padding-bottom: 30px;
  320. .wx-btn {
  321. margin-top: 40rpx;
  322. height: 80rpx;
  323. width: 160rpx;
  324. text-align: center;
  325. line-height: 80rpx;
  326. background: #fff;
  327. &::after {
  328. border-radius: 80rpx;
  329. border-color: $uni-border-1;
  330. }
  331. }
  332. .line-text {
  333. position: relative;
  334. text-align: center;
  335. color: $uni-secondary-color;
  336. width: 100%;
  337. &::before {
  338. content: '';
  339. position: absolute;
  340. left: 60rpx;
  341. right: calc(50% + 20px);
  342. height: 1px;
  343. background: $uni-border-1;
  344. top: 50%;
  345. }
  346. &::after {
  347. content: '';
  348. position: absolute;
  349. right: 60rpx;
  350. left: calc(50% + 20px);
  351. height: 1px;
  352. background: $uni-border-1;
  353. top: 50%;
  354. }
  355. }
  356. }
  357. }
  358. </style>