login.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. } from '@/request/api/login'
  60. import {
  61. isTel
  62. } from '@/uitls/validate'
  63. import model from "@/components/login/model.vue";
  64. export default {
  65. data() {
  66. return {
  67. logoImage: require('@/static/image/login/logo.png'),
  68. loginForm: {
  69. phone: '18888888888',
  70. phoneCode: '888888'
  71. },
  72. captchaShow: false,
  73. imgCode: '',
  74. codeImg: '',
  75. codeName: '获取验证码', //获取验证码text
  76. isDisabled: false, //获取验证码按钮状态
  77. }
  78. },
  79. components: {
  80. model
  81. },
  82. onLoad() {},
  83. methods: {
  84. getImageCode() {
  85. if (!isTel(this.loginForm.phone)) return uni.showToast({
  86. title: '请输入正确的手机号',
  87. icon: "none"
  88. })
  89. if (this.isDisabled) return;
  90. this.imgCodeFunc();
  91. this.captchaShow = true;
  92. },
  93. imgCodeFunc() {
  94. getImgCode().then(res => {
  95. if (res.code === 200) {
  96. this.codeImg = res.data.pngBase64;
  97. }
  98. })
  99. },
  100. confirmHandle() {
  101. if (!this.imgCode) return uni.showToast({
  102. title: '请输入图片验证码',
  103. icon: "none"
  104. })
  105. sendPhoneCode(this.loginForm.phone, this.imgCode).then(res => {
  106. if (res.code === 200) {
  107. this.codeReset();
  108. uni.showToast({
  109. title: '发送成功',
  110. icon: "none"
  111. })
  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. this.login({
  133. pCode: e.detail.code
  134. })
  135. },
  136. submitLogin() { //手机验证码登录
  137. if (!this.loginForm.phoneCode) return uni.showToast({
  138. title: '请输入短信验证码',
  139. icon: "none"
  140. })
  141. this.login(this.loginForm);
  142. },
  143. login(postData) {
  144. uni.showLoading({
  145. title: '登录中'
  146. })
  147. // #ifdef MP-WEIXIN
  148. uni.login({
  149. provider: 'weixin',
  150. success: loginRes => {
  151. if (postData.phone != '18888888888') postData['code'] = loginRes.code;
  152. login(postData).then(this.successFunc)
  153. }
  154. })
  155. // #endif
  156. // #ifndef MP-WEIXIN
  157. login(postData).then(this.successFunc);
  158. // #endif
  159. },
  160. successFunc(res) {
  161. if (res.code === 200) {
  162. uni.showToast({
  163. title: '登录成功',
  164. icon: "none"
  165. })
  166. console.log(res.data);
  167. setTimeout(() => {
  168. uni.hideLoading();
  169. // uni.navigateBack();
  170. }, 400)
  171. } else {
  172. uni.hideLoading();
  173. }
  174. }
  175. }
  176. }
  177. </script>
  178. <style lang="scss">
  179. .login-container {
  180. width: 100%;
  181. height: 100%;
  182. display: flex;
  183. flex-direction: column;
  184. align-items: center;
  185. .logo {
  186. margin-top: 80rpx;
  187. width: 150rpx;
  188. height: 150rpx;
  189. image {
  190. width: 100%;
  191. height: 100%;
  192. }
  193. }
  194. //弹出输入框
  195. .code-input-container {
  196. display: flex;
  197. align-items: center;
  198. margin-top: 20rpx;
  199. .code-input {
  200. background: #F7F8FA;
  201. width: 306rpx;
  202. height: 84rpx;
  203. line-height: 84rpx;
  204. padding: 0 20rpx;
  205. border-radius: 20rpx;
  206. box-sizing: border-box;
  207. &::placeholder {
  208. line-height: 84rpx;
  209. }
  210. }
  211. .code-canvas-wrapper {
  212. flex: 1;
  213. .code-image {
  214. width: 100%;
  215. height: 84rpx;
  216. }
  217. }
  218. }
  219. .form {
  220. margin-top: 70rpx;
  221. display: flex;
  222. flex-direction: column;
  223. align-items: center;
  224. width: 686rpx;
  225. margin: 70rpx auto;
  226. // 登录表单项
  227. .form-item {
  228. width: 100%;
  229. border-radius: 200rpx;
  230. height: 100rpx;
  231. background-color: $uni-white;
  232. display: flex;
  233. margin-bottom: 50rpx;
  234. &:last-of-type {
  235. margin-bottom: 0;
  236. }
  237. }
  238. // 登录的input项
  239. .form-input-item {
  240. height: 100%;
  241. width: 100%;
  242. display: flex;
  243. align-items: center;
  244. box-sizing: border-box;
  245. // input左边区域
  246. .form-input-left {
  247. display: flex;
  248. align-items: center;
  249. margin-left: 30rpx;
  250. .form-input-icons {
  251. /* #ifdef H5 */
  252. color: $uni-secondary-color !important;
  253. /* #endif */
  254. /* #ifdef MP-WEIXIN */
  255. text {
  256. color: $uni-secondary-color !important;
  257. }
  258. /* #endif */
  259. }
  260. }
  261. // 手机号信息
  262. .phone-info {
  263. margin-left: 20rpx;
  264. // 手机号信息文字
  265. .phone-info-txt {
  266. margin-right: 8rpx;
  267. }
  268. // 下拉按钮图片大小
  269. .phone-info-img {
  270. width: 24rpx;
  271. height: 24rpx;
  272. }
  273. }
  274. // input内容区域
  275. .form-input-content {
  276. flex: 1 1 auto;
  277. padding-right: 20rpx;
  278. position: relative;
  279. // 具体的input样式
  280. .form-input {
  281. width: 100%;
  282. padding-left: 20rpx;
  283. // 覆盖uni的默认的提示
  284. ::v-deep .uni-input-placeholder {
  285. color: #C8C9CC;
  286. }
  287. }
  288. }
  289. }
  290. // 验证码
  291. .code-input-wrapper {
  292. display: flex;
  293. align-items: center;
  294. .code-input-btn {
  295. width: auto;
  296. width: 200rpx;
  297. height: 64rpx;
  298. font-size: 24rpx;
  299. padding: 12rpx 40rpx;
  300. white-space: nowrap;
  301. display: flex;
  302. align-items: center;
  303. justify-content: center;
  304. border-radius: 200rpx;
  305. background: $uni-primary;
  306. color: #fff;
  307. &.disabled {
  308. opacity: 0.5;
  309. }
  310. &::after {
  311. display: none;
  312. }
  313. }
  314. }
  315. // 登录按钮容器
  316. .form-btn-container {
  317. margin-top: 20rpx;
  318. width: 100%;
  319. }
  320. // 登录按钮
  321. .form-btn {
  322. height: 88rpx;
  323. border-radius: 200rpx;
  324. padding: 24rpx 32rpx;
  325. line-height: 40rpx;
  326. color: $uni-white;
  327. background-color: $uni-primary;
  328. font-size: 32rpx;
  329. &.disabled {
  330. opacity: 0.5;
  331. }
  332. &::after {
  333. display: none;
  334. }
  335. }
  336. .wx-btn {
  337. height: 88rpx;
  338. padding: 24rpx 32rpx;
  339. border-radius: 200rpx;
  340. line-height: 40rpx;
  341. font-size: 32rpx;
  342. color: $uni-white;
  343. background-color: $uni-success;
  344. &::after {
  345. display: none;
  346. }
  347. }
  348. }
  349. }
  350. </style>