login.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view class="container login-container">
  3. <view class="logo">
  4. <img src="../../static/image/login/logo.png" alt="" />
  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 type="person" size="24" color="#93959b"></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 type="email" size="24" color="#93959b"></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':''"
  28. @click="getImageCode">{{codeName}}</button>
  29. </view>
  30. </view>
  31. </view>
  32. <view class="form-btn-container">
  33. <button class="form-btn" @click="submitLogin">登录</button>
  34. </view>
  35. <!-- #ifdef MP-WEIXIN -->
  36. <view class="form-btn-container">
  37. <button type="primary" class="wx-btn" open-type="getPhoneNumber" @getphonenumber="wxPhoneLogin">
  38. 微信手机号一键登录
  39. </button>
  40. </view>
  41. <!-- #endif -->
  42. </view>
  43. <y-modal :show="captchaShow" title="请填写图形验证码" @onConfirm="confirmHandle" @onCancel="captchaShow = false">
  44. <view class="code-input-container">
  45. <input type="text" class="code-input" placeholder="输入图形验证码" maxlength="6" v-model="imgCode" />
  46. <view class="code-canvas-wrapper">
  47. <img v-if="codeImg" :src="codeImg" alt="图片验证码" class="code-image" @click="imgCodeFunc">
  48. </view>
  49. </view>
  50. </y-modal>
  51. </view>
  52. </template>
  53. <script>
  54. import {
  55. login,
  56. getImgCode,
  57. sendPhoneCode
  58. } from '@/request/api/login'
  59. import {
  60. isTel
  61. } from '@/uitls/validate'
  62. import yModal from "@/components/login/yModal.vue";
  63. export default {
  64. data() {
  65. return {
  66. loginForm: {
  67. phone: '',
  68. phoneCode: ''
  69. },
  70. captchaShow: false,
  71. imgCode: '',
  72. codeImg: '',
  73. codeName: '获取验证码', //获取验证码text
  74. isDisabled: false, //获取验证码按钮状态
  75. }
  76. },
  77. components: {
  78. yModal
  79. },
  80. methods: {
  81. getImageCode() {
  82. if (!isTel(this.loginForm.phone)) return uni.showToast({
  83. title: '请输入正确的手机号',
  84. icon: "none"
  85. })
  86. if (this.isDisabled) return;
  87. this.imgCodeFunc();
  88. this.captchaShow = true;
  89. },
  90. imgCodeFunc() {
  91. getImgCode().then(res => {
  92. if (res.code === 200) {
  93. this.codeImg = res.data.pngBase64;
  94. }
  95. })
  96. },
  97. confirmHandle() {
  98. if (!this.imgCode) return uni.showToast({
  99. title: '请输入图片验证码',
  100. icon: "none"
  101. })
  102. sendPhoneCode(this.loginForm.phone, this.imgCode).then(res => {
  103. if (res.code === 200) {
  104. this.codeReset();
  105. uni.showToast({
  106. title: '发送成功',
  107. icon: "none"
  108. })
  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. this.login({
  130. pCode: e.detail.code
  131. })
  132. },
  133. submitLogin() { //手机验证码登录
  134. if (!this.loginForm.phoneCode) return uni.showToast({
  135. title: '请输入正确的短信验证码',
  136. icon: "none"
  137. })
  138. this.login(this.loginForm);
  139. },
  140. login(postData) {
  141. // #ifdef MP-WEIXIN
  142. uni.login({
  143. provider: 'weixin',
  144. success: function(loginRes) {
  145. postData['code'] = loginRes.code;
  146. login(postData).then(res => {
  147. console.log(res);
  148. })
  149. }
  150. })
  151. // #endif
  152. // #ifndef MP-WEIXIN
  153. login(postData).then(res => {
  154. console.log(res);
  155. })
  156. // #endif
  157. }
  158. }
  159. }
  160. </script>
  161. <style scoped lang="scss">
  162. .login-container {
  163. align-items: center;
  164. .logo {
  165. margin-top: 80rpx;
  166. width: 150rpx;
  167. height: 150rpx;
  168. }
  169. //弹出输入框
  170. .code-input-container {
  171. display: flex;
  172. align-items: center;
  173. margin-top: 20rpx;
  174. .code-input {
  175. background: #F7F8FA;
  176. width: 306rpx;
  177. height: 84rpx;
  178. line-height: 84rpx;
  179. padding: 0 20rpx;
  180. border-radius: 20rpx;
  181. box-sizing: border-box;
  182. &::placeholder {
  183. line-height: 84rpx;
  184. }
  185. }
  186. .code-canvas-wrapper {
  187. flex: 1;
  188. .code-image {
  189. width: 100%;
  190. height: 84rpx;
  191. }
  192. }
  193. }
  194. .form {
  195. margin-top: 70rpx;
  196. display: flex;
  197. flex-direction: column;
  198. align-items: center;
  199. width: 686rpx;
  200. margin: 70rpx auto;
  201. // 登录表单项
  202. .form-item {
  203. width: 100%;
  204. border-radius: 200rpx;
  205. height: 100rpx;
  206. background-color: #fff;
  207. display: flex;
  208. margin-bottom: 50rpx;
  209. &:last-of-type {
  210. margin-bottom: 0;
  211. }
  212. }
  213. // 登录的input项
  214. .form-input-item {
  215. height: 100%;
  216. width: 100%;
  217. display: flex;
  218. align-items: center;
  219. box-sizing: border-box;
  220. // input左边区域
  221. .form-input-left {
  222. display: flex;
  223. align-items: center;
  224. margin-left: 30rpx;
  225. }
  226. // 手机号信息
  227. .phone-info {
  228. margin-left: 20rpx;
  229. // 手机号信息文字
  230. .phone-info-txt {
  231. margin-right: 8rpx;
  232. }
  233. // 下拉按钮图片大小
  234. .phone-info-img {
  235. width: 24rpx;
  236. height: 24rpx;
  237. }
  238. }
  239. // input内容区域
  240. .form-input-content {
  241. flex: 1 1 auto;
  242. padding-right: 20rpx;
  243. position: relative;
  244. // 具体的input样式
  245. .form-input {
  246. width: 100%;
  247. padding-left: 20rpx;
  248. // 覆盖uni的默认的提示
  249. ::v-deep .uni-input-placeholder {
  250. color: #C8C9CC;
  251. }
  252. }
  253. }
  254. }
  255. // 验证码
  256. .code-input-wrapper {
  257. display: flex;
  258. align-items: center;
  259. .code-input-btn {
  260. width: auto;
  261. width: 200rpx;
  262. height: 64rpx;
  263. font-size: 24rpx;
  264. padding: 12rpx 40rpx;
  265. white-space: nowrap;
  266. display: flex;
  267. align-items: center;
  268. justify-content: center;
  269. border-radius: 200rpx;
  270. background: $uni-primary;
  271. color: #fff;
  272. &.disabled {
  273. opacity: 0.5;
  274. }
  275. &::after {
  276. display: none;
  277. }
  278. }
  279. }
  280. // 登录按钮容器
  281. .form-btn-container {
  282. margin-top: 20rpx;
  283. width: 100%;
  284. }
  285. // 登录按钮
  286. .form-btn {
  287. height: 88rpx;
  288. border-radius: 200rpx;
  289. padding: 24rpx 32rpx;
  290. line-height: 40rpx;
  291. color: #fff;
  292. background-color: $uni-primary;
  293. font-size: 32rpx;
  294. &.disabled {
  295. opacity: 0.5;
  296. }
  297. &::after {
  298. display: none;
  299. }
  300. }
  301. .wx-btn {
  302. border-radius: 200rpx;
  303. font-size: 32rpx;
  304. &::after {
  305. display: none;
  306. }
  307. }
  308. }
  309. }
  310. </style>