main.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. }
  113. },
  114. // 表单验证
  115. formValidation(scene = GET_CAPTCHA) {
  116. const app = this
  117. // 验证获取短信验证码
  118. if (scene === GET_CAPTCHA) {
  119. if (!app.validteMobile(app.mobile) || !app.validteCaptchaCode(app.captchaCode)) {
  120. return false
  121. }
  122. }
  123. // 验证提交登录
  124. if (scene === SUBMIT_LOGIN) {
  125. if (!app.validteMobile(app.mobile) || !app.validteSmsCode(app.smsCode)) {
  126. return false
  127. }
  128. }
  129. return true
  130. },
  131. // 验证手机号
  132. validteMobile(str) {
  133. if (Verify.isEmpty(str)) {
  134. this.$toast('请先输入手机号')
  135. return false
  136. }
  137. if (!Verify.isMobile(str)) {
  138. this.$toast('请输入正确格式的手机号')
  139. return false
  140. }
  141. return true
  142. },
  143. // 验证图形验证码
  144. validteCaptchaCode(str) {
  145. if (Verify.isEmpty(str)) {
  146. this.$toast('请先输入图形验证码')
  147. return false
  148. }
  149. return true
  150. },
  151. // 验证短信验证码
  152. validteSmsCode(str) {
  153. if (Verify.isEmpty(str)) {
  154. this.$toast('请先输入短信验证码')
  155. return false
  156. }
  157. return true
  158. },
  159. // 请求发送短信验证码接口
  160. sendSmsCaptcha() {
  161. const app = this
  162. app.isLoading = true
  163. CaptchaApi.sendSmsCaptcha({
  164. form: {
  165. captchaKey: app.captcha.key,
  166. captchaCode: app.captchaCode,
  167. mobile: app.mobile
  168. }
  169. })
  170. .then(result => {
  171. // 显示发送成功
  172. app.$toast(result.message)
  173. // 执行定时器
  174. app.timer()
  175. })
  176. .catch(() => app.getCaptcha())
  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. // 跳转回原页面
  219. if (err.result.data.isBack) {
  220. setTimeout(() => app.onNavigateBack(1), 2000)
  221. }
  222. })
  223. .finally(() => app.isLoading = false)
  224. },
  225. /**
  226. * 登录成功-跳转回原页面
  227. */
  228. onNavigateBack(delta = 1) {
  229. const pages = getCurrentPages()
  230. if (pages.length > 1) {
  231. uni.navigateBack({
  232. delta: Number(delta || 1)
  233. })
  234. } else {
  235. this.$navTo('pages/index/index')
  236. }
  237. }
  238. }
  239. }
  240. </script>
  241. <style lang="scss" scoped>
  242. .container {
  243. padding: 100rpx 60rpx;
  244. min-height: 100vh;
  245. background-color: #fff;
  246. }
  247. // 页面头部
  248. .header {
  249. margin-bottom: 60rpx;
  250. .title {
  251. color: #191919;
  252. font-size: 54rpx;
  253. }
  254. .sub-title {
  255. margin-top: 20rpx;
  256. color: #b3b3b3;
  257. font-size: 28rpx;
  258. }
  259. }
  260. // 输入框元素
  261. .form-item {
  262. display: flex;
  263. padding: 18rpx;
  264. border-bottom: 1rpx solid #f3f1f2;
  265. margin-bottom: 30rpx;
  266. height: 96rpx;
  267. &--input {
  268. font-size: 28rpx;
  269. letter-spacing: 1rpx;
  270. flex: 1;
  271. height: 100%;
  272. }
  273. &--parts {
  274. min-width: 100rpx;
  275. height: 100%;
  276. }
  277. // 图形验证码
  278. .captcha {
  279. height: 100%;
  280. .image {
  281. display: block;
  282. width: 192rpx;
  283. height: 100%;
  284. }
  285. }
  286. // 短信验证码
  287. .captcha-sms {
  288. font-size: 28rpx;
  289. line-height: 50rpx;
  290. padding-right: 20rpx;
  291. .activate {
  292. color: #cea26a;
  293. }
  294. .un-activate {
  295. color: #9e9e9e;
  296. }
  297. }
  298. }
  299. // 登录按钮
  300. .login-button {
  301. width: 100%;
  302. height: 86rpx;
  303. margin-top: 80rpx;
  304. background: linear-gradient(to right, #ecb53c, #ff9211);
  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. display: flex;
  310. justify-content: center;
  311. align-items: center;
  312. }
  313. </style>