main.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <view class="container">
  3. <!-- 页面头部 -->
  4. <view class="header">
  5. <view class="title">
  6. <text>手机号登录</text>
  7. </view>
  8. <view class="sub-title">
  9. <text>未注册的手机号登录后将自动注册</text>
  10. </view>
  11. </view>
  12. <!-- 表单 -->
  13. <view class="login-form">
  14. <!-- 手机号 -->
  15. <view class="form-item">
  16. <input class="form-item--input" type="number" v-model="mobile" maxlength="11" placeholder="请输入手机号码" />
  17. </view>
  18. <!-- 图形验证码 -->
  19. <view class="form-item">
  20. <input class="form-item--input" type="text" v-model="captchaCode" maxlength="5" placeholder="请输入图形验证码" />
  21. <view class="form-item--parts">
  22. <view class="captcha" @click="getCaptcha()">
  23. <image class="image" :src="captcha.base64"></image>
  24. </view>
  25. </view>
  26. </view>
  27. <!-- 短信验证码 -->
  28. <view class="form-item">
  29. <input class="form-item--input" type="number" v-model="smsCode" maxlength="6" placeholder="请输入短信验证码" />
  30. <view class="form-item--parts">
  31. <view class="captcha-sms" @click="handelSmsCaptcha()">
  32. <text v-if="!smsState" class="activate">获取验证码</text>
  33. <text v-else class="un-activate">重新发送({{ times }})秒</text>
  34. </view>
  35. </view>
  36. </view>
  37. <!-- 登录按钮 -->
  38. <view class="login-button" :class="{disabled}" @click="handleLogin()">
  39. <text>登录</text>
  40. </view>
  41. </view>
  42. <!-- 微信授权手机号一键登录 -->
  43. <!-- #ifdef MP-WEIXIN -->
  44. <!-- <MpWeixinMobile v-if="isMpWeixinMobile" :isParty="isParty" :partyData="partyData" /> -->
  45. <!-- #endif -->
  46. </view>
  47. </template>
  48. <script>
  49. import store from '@/store'
  50. import * as CaptchaApi from '@/api/captcha'
  51. import * as Verify from '@/utils/verify'
  52. import MpWeixinMobile from './mp-weixin-mobile'
  53. // 倒计时时长(秒)
  54. const times = 60
  55. // 表单验证场景
  56. const GET_CAPTCHA = 10
  57. const SUBMIT_LOGIN = 20
  58. export default {
  59. components: {
  60. MpWeixinMobile
  61. },
  62. props: {
  63. // 是否存在第三方用户信息
  64. isParty: {
  65. type: Boolean,
  66. default: () => false
  67. },
  68. // 第三方用户信息数据
  69. partyData: {
  70. type: Object
  71. },
  72. // 是否显示微信小程序端 一键授权手机号
  73. isMpWeixinMobile: {
  74. type: Boolean,
  75. default: () => false
  76. }
  77. },
  78. data() {
  79. return {
  80. // 正在加载
  81. isLoading: false,
  82. // 按钮禁用
  83. disabled: false,
  84. // 图形验证码信息
  85. captcha: {},
  86. // 短信验证码发送状态
  87. smsState: false,
  88. // 倒计时
  89. times,
  90. // 手机号
  91. mobile: '',
  92. // 图形验证码
  93. captchaCode: '',
  94. // 短信验证码
  95. smsCode: ''
  96. }
  97. },
  98. /**
  99. * 生命周期函数--监听页面加载
  100. */
  101. created() {
  102. // 获取图形验证码
  103. // this.getCaptcha()
  104. },
  105. methods: {
  106. // 获取图形验证码
  107. getCaptcha() {
  108. const app = this
  109. CaptchaApi.image().then((result) => (app.captcha = result.data))
  110. },
  111. // 点击发送短信验证码
  112. handelSmsCaptcha() {
  113. const app = this
  114. if (!app.isLoading && !app.smsState && app.formValidation(GET_CAPTCHA)) {
  115. app.sendSmsCaptcha()
  116. }
  117. },
  118. // 表单验证
  119. formValidation(scene = GET_CAPTCHA) {
  120. const app = this
  121. // 验证获取短信验证码
  122. if (scene === GET_CAPTCHA) {
  123. if (!app.validteMobile(app.mobile) || !app.validteCaptchaCode(app.captchaCode)) {
  124. return false
  125. }
  126. }
  127. // 验证提交登录
  128. if (scene === SUBMIT_LOGIN) {
  129. if (!app.validteMobile(app.mobile) || !app.validteSmsCode(app.smsCode)) {
  130. return false
  131. }
  132. }
  133. return true
  134. },
  135. // 验证手机号
  136. validteMobile(str) {
  137. if (Verify.isEmpty(str)) {
  138. this.$toast('请先输入手机号')
  139. return false
  140. }
  141. if (!Verify.isMobile(str)) {
  142. this.$toast('请输入正确格式的手机号')
  143. return false
  144. }
  145. return true
  146. },
  147. // 验证图形验证码
  148. validteCaptchaCode(str) {
  149. if (Verify.isEmpty(str)) {
  150. this.$toast('请先输入图形验证码')
  151. return false
  152. }
  153. return true
  154. },
  155. // 验证短信验证码
  156. validteSmsCode(str) {
  157. if (Verify.isEmpty(str)) {
  158. this.$toast('请先输入短信验证码')
  159. return false
  160. }
  161. return true
  162. },
  163. // 请求发送短信验证码接口
  164. sendSmsCaptcha() {
  165. const app = this
  166. app.isLoading = true
  167. CaptchaApi.sendSmsCaptcha({
  168. form: {
  169. captchaKey: app.captcha.key,
  170. captchaCode: app.captchaCode,
  171. mobile: app.mobile
  172. }
  173. })
  174. .then((result) => {
  175. // 显示发送成功
  176. app.$toast(result.message)
  177. // 执行定时器
  178. app.timer()
  179. })
  180. .catch(() => app.getCaptcha())
  181. .finally(() => (app.isLoading = false))
  182. },
  183. // 执行定时器
  184. timer() {
  185. const app = this
  186. app.smsState = true
  187. const inter = setInterval(() => {
  188. app.times = app.times - 1
  189. if (app.times <= 0) {
  190. app.smsState = false
  191. app.times = times
  192. clearInterval(inter)
  193. }
  194. }, 1000)
  195. },
  196. // 点击登录
  197. handleLogin() {
  198. const app = this
  199. if (!app.isLoading && !app.disabled && app.formValidation(SUBMIT_LOGIN)) {
  200. app.submitLogin()
  201. }
  202. },
  203. // 确认登录
  204. submitLogin() {
  205. const app = this
  206. app.isLoading = true
  207. app.disabled = true
  208. store
  209. .dispatch('Login', {
  210. smsCode: app.smsCode,
  211. mobile: app.mobile,
  212. isParty: app.isParty,
  213. partyData: app.partyData
  214. })
  215. .then((result) => {
  216. // 显示登录成功
  217. app.$toast(result.message)
  218. // 相应全局事件订阅: 刷新上级页面数据
  219. uni.$emit('syncRefresh', true)
  220. // 跳转回原页面
  221. setTimeout(() => app.onNavigateBack(1), 2000)
  222. })
  223. .catch((err) => {
  224. app.disabled = false
  225. // 跳转回原页面
  226. if (err.result.data.isBack) {
  227. setTimeout(() => app.onNavigateBack(1), 2000)
  228. }
  229. })
  230. .finally(() => (app.isLoading = false))
  231. },
  232. /**
  233. * 登录成功-跳转回原页面
  234. */
  235. onNavigateBack(delta = 1) {
  236. const pages = getCurrentPages()
  237. if (pages.length > 1) {
  238. uni.navigateBack({
  239. delta: Number(delta || 1)
  240. })
  241. } else {
  242. this.$navTo('pages/index/index')
  243. }
  244. }
  245. }
  246. }
  247. </script>
  248. <style lang="scss" scoped>
  249. .container {
  250. padding: 100rpx 60rpx;
  251. min-height: 100vh;
  252. background-color: #fff;
  253. }
  254. // 页面头部
  255. .header {
  256. margin-bottom: 60rpx;
  257. .title {
  258. color: #191919;
  259. font-size: 54rpx;
  260. }
  261. .sub-title {
  262. margin-top: 20rpx;
  263. color: #b3b3b3;
  264. font-size: 28rpx;
  265. }
  266. }
  267. // 输入框元素
  268. .form-item {
  269. display: flex;
  270. align-items: center;
  271. padding: 18rpx;
  272. border-bottom: 1rpx solid #f3f1f2;
  273. margin-bottom: 30rpx;
  274. height: 96rpx;
  275. &--input {
  276. font-size: 28rpx;
  277. letter-spacing: 1rpx;
  278. flex: 1;
  279. height: 100%;
  280. }
  281. &--parts {
  282. min-width: 100rpx;
  283. }
  284. // 图形验证码
  285. .captcha {
  286. height: 60rpx;
  287. .image {
  288. display: block;
  289. width: 192rpx;
  290. height: 100%;
  291. }
  292. }
  293. // 短信验证码
  294. .captcha-sms {
  295. font-size: 30rpx;
  296. line-height: 50rpx;
  297. padding-right: 20rpx;
  298. .activate {
  299. color: $main-bg;
  300. }
  301. .un-activate {
  302. color: #9e9e9e;
  303. }
  304. }
  305. }
  306. // 登录按钮
  307. .login-button {
  308. width: 100%;
  309. height: 86rpx;
  310. margin-top: 80rpx;
  311. background: linear-gradient(to right, $main-bg, $main-bg2);
  312. color: $main-text;
  313. border-radius: 80rpx;
  314. box-shadow: 0px 10px 20px 0px rgba(0, 0, 0, 0.1);
  315. letter-spacing: 5rpx;
  316. display: flex;
  317. justify-content: center;
  318. align-items: center;
  319. // 禁用按钮
  320. &.disabled {
  321. opacity: 0.6;
  322. }
  323. }
  324. </style>