login.vue 8.5 KB

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