login.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. if (!e.detail.code) return;
  126. this.login({
  127. pCode: e.detail.code
  128. })
  129. },
  130. submitLogin() { //手机验证码登录
  131. if (!this.loginForm.phoneCode) return this.$toast('请输入短信验证码');
  132. this.login(this.loginForm);
  133. },
  134. login(postData) {
  135. uni.showLoading({
  136. title: '登录中'
  137. })
  138. // #ifdef MP-WEIXIN
  139. uni.login({
  140. provider: 'weixin',
  141. success: loginRes => {
  142. if (postData.phone != '18888888888') postData['code'] = loginRes.code;
  143. login(postData).then(this.successFunc);
  144. }
  145. })
  146. // #endif
  147. // #ifndef MP-WEIXIN
  148. login(postData).then(this.successFunc);
  149. // #endif
  150. },
  151. successFunc(res) {
  152. if (res.code === 200) {
  153. uni.setStorageSync('token', res.data.token);
  154. getUserInfo().then(user => {
  155. if (user.code === 200) {
  156. let userInfo = user.data;
  157. this.$store.dispatch('app/changeOrganization', userInfo.organization);
  158. this.$store.dispatch('app/changeUser', userInfo);
  159. this.$toast('登录成功');
  160. setTimeout(() => {
  161. uni.hideLoading();
  162. uni.navigateBack();
  163. }, 400)
  164. } else {
  165. uni.hideLoading();
  166. }
  167. })
  168. } else {
  169. uni.hideLoading();
  170. }
  171. }
  172. }
  173. }
  174. </script>
  175. <style lang="scss">
  176. .login-container {
  177. width: 100%;
  178. height: 100%;
  179. display: flex;
  180. flex-direction: column;
  181. align-items: center;
  182. .logo {
  183. margin-top: 80rpx;
  184. width: 150rpx;
  185. height: 150rpx;
  186. image {
  187. width: 100%;
  188. height: 100%;
  189. }
  190. }
  191. //弹出输入框
  192. .code-input-container {
  193. display: flex;
  194. align-items: center;
  195. margin-top: 20rpx;
  196. .code-input {
  197. background: #F7F8FA;
  198. width: 306rpx;
  199. height: 84rpx;
  200. line-height: 84rpx;
  201. padding: 0 20rpx;
  202. border-radius: 20rpx;
  203. box-sizing: border-box;
  204. &::placeholder {
  205. line-height: 84rpx;
  206. }
  207. }
  208. .code-canvas-wrapper {
  209. flex: 1;
  210. .code-image {
  211. width: 100%;
  212. height: 84rpx;
  213. }
  214. }
  215. }
  216. .form {
  217. margin-top: 70rpx;
  218. display: flex;
  219. flex-direction: column;
  220. align-items: center;
  221. width: 686rpx;
  222. margin: 70rpx auto;
  223. // 登录表单项
  224. .form-item {
  225. width: 100%;
  226. border-radius: 200rpx;
  227. height: 100rpx;
  228. background-color: $uni-white;
  229. display: flex;
  230. margin-bottom: 50rpx;
  231. &:last-of-type {
  232. margin-bottom: 0;
  233. }
  234. }
  235. // 登录的input项
  236. .form-input-item {
  237. height: 100%;
  238. width: 100%;
  239. display: flex;
  240. align-items: center;
  241. box-sizing: border-box;
  242. // input左边区域
  243. .form-input-left {
  244. display: flex;
  245. align-items: center;
  246. margin-left: 30rpx;
  247. .form-input-icons {
  248. /* #ifdef H5 */
  249. color: $uni-secondary-color !important;
  250. /* #endif */
  251. /* #ifdef MP-WEIXIN */
  252. text {
  253. color: $uni-secondary-color !important;
  254. }
  255. /* #endif */
  256. }
  257. }
  258. // 手机号信息
  259. .phone-info {
  260. margin-left: 20rpx;
  261. // 手机号信息文字
  262. .phone-info-txt {
  263. margin-right: 8rpx;
  264. }
  265. // 下拉按钮图片大小
  266. .phone-info-img {
  267. width: 24rpx;
  268. height: 24rpx;
  269. }
  270. }
  271. // input内容区域
  272. .form-input-content {
  273. flex: 1 1 auto;
  274. padding-right: 20rpx;
  275. position: relative;
  276. // 具体的input样式
  277. .form-input {
  278. width: 100%;
  279. padding-left: 20rpx;
  280. // 覆盖uni的默认的提示
  281. ::v-deep .uni-input-placeholder {
  282. color: #C8C9CC;
  283. }
  284. }
  285. }
  286. }
  287. // 验证码
  288. .code-input-wrapper {
  289. display: flex;
  290. align-items: center;
  291. .code-input-btn {
  292. width: auto;
  293. width: 200rpx;
  294. height: 64rpx;
  295. font-size: 24rpx;
  296. padding: 12rpx 40rpx;
  297. white-space: nowrap;
  298. display: flex;
  299. align-items: center;
  300. justify-content: center;
  301. border-radius: 200rpx;
  302. background: $uni-primary;
  303. color: #fff;
  304. &.disabled {
  305. opacity: 0.5;
  306. }
  307. &::after {
  308. display: none;
  309. }
  310. }
  311. }
  312. // 登录按钮容器
  313. .form-btn-container {
  314. margin-top: 20rpx;
  315. width: 100%;
  316. }
  317. // 登录按钮
  318. .form-btn {
  319. height: 88rpx;
  320. border-radius: 200rpx;
  321. padding: 24rpx 32rpx;
  322. line-height: 40rpx;
  323. color: $uni-white;
  324. background-color: $uni-primary;
  325. font-size: 32rpx;
  326. &.disabled {
  327. opacity: 0.5;
  328. }
  329. &::after {
  330. display: none;
  331. }
  332. }
  333. .wx-btn {
  334. height: 88rpx;
  335. padding: 24rpx 32rpx;
  336. border-radius: 200rpx;
  337. line-height: 40rpx;
  338. font-size: 32rpx;
  339. color: $uni-white;
  340. background-color: $uni-success;
  341. &::after {
  342. display: none;
  343. }
  344. }
  345. }
  346. }
  347. </style>