main.vue 8.0 KB

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