main.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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()
  110. .then(result => app.captcha = result.data)
  111. },
  112. // 点击发送短信验证码
  113. handelSmsCaptcha() {
  114. const app = this
  115. if (!app.isLoading && !app.smsState && app.formValidation(GET_CAPTCHA)) {
  116. app.sendSmsCaptcha()
  117. }
  118. },
  119. // 表单验证
  120. formValidation(scene = GET_CAPTCHA) {
  121. const app = this
  122. // 验证获取短信验证码
  123. if (scene === GET_CAPTCHA) {
  124. if (!app.validteMobile(app.mobile) || !app.validteCaptchaCode(app.captchaCode)) {
  125. return false
  126. }
  127. }
  128. // 验证提交登录
  129. if (scene === SUBMIT_LOGIN) {
  130. // 短信通道尚未接入,后端已关闭验证码校验(yoshop.sms.verify-code=false),
  131. // 前端同步放开:只校验手机号。接入短信平台后恢复 validteSmsCode(app.smsCode)
  132. if (!app.validteMobile(app.mobile)) {
  133. return false
  134. }
  135. }
  136. return true
  137. },
  138. // 验证手机号
  139. validteMobile(str) {
  140. if (Verify.isEmpty(str)) {
  141. this.$toast('请先输入手机号')
  142. return false
  143. }
  144. if (!Verify.isMobile(str)) {
  145. this.$toast('请输入正确格式的手机号')
  146. return false
  147. }
  148. return true
  149. },
  150. // 验证图形验证码
  151. validteCaptchaCode(str) {
  152. if (Verify.isEmpty(str)) {
  153. this.$toast('请先输入图形验证码')
  154. return false
  155. }
  156. return true
  157. },
  158. // 验证短信验证码
  159. validteSmsCode(str) {
  160. if (Verify.isEmpty(str)) {
  161. this.$toast('请先输入短信验证码')
  162. return false
  163. }
  164. return true
  165. },
  166. // 请求发送短信验证码接口
  167. sendSmsCaptcha() {
  168. const app = this
  169. app.isLoading = true
  170. CaptchaApi.sendSmsCaptcha({
  171. form: {
  172. captchaKey: app.captcha.key,
  173. captchaCode: app.captchaCode,
  174. mobile: app.mobile
  175. }
  176. })
  177. .then(result => {
  178. // 显示发送成功
  179. app.$toast(result.message)
  180. // 执行定时器
  181. app.timer()
  182. })
  183. .catch(() => app.getCaptcha())
  184. .finally(() => app.isLoading = false)
  185. },
  186. // 执行定时器
  187. timer() {
  188. const app = this
  189. app.smsState = true
  190. const inter = setInterval(() => {
  191. app.times = app.times - 1
  192. if (app.times <= 0) {
  193. app.smsState = false
  194. app.times = times
  195. clearInterval(inter)
  196. }
  197. }, 1000)
  198. },
  199. // 点击登录
  200. handleLogin() {
  201. const app = this
  202. if (!app.isLoading && !app.disabled && app.formValidation(SUBMIT_LOGIN)) {
  203. app.submitLogin()
  204. }
  205. },
  206. // 确认登录
  207. submitLogin() {
  208. const app = this
  209. app.isLoading = true
  210. app.disabled = true
  211. store.dispatch('Login', {
  212. smsCode: app.smsCode,
  213. mobile: app.mobile,
  214. isParty: app.isParty,
  215. partyData: app.partyData
  216. })
  217. .then(result => {
  218. // 显示登录成功
  219. app.$toast(result.message)
  220. // 相应全局事件订阅: 刷新上级页面数据
  221. uni.$emit('syncRefresh', true)
  222. // 跳转回原页面
  223. setTimeout(() => app.onNavigateBack(1), 2000)
  224. })
  225. .catch(err => {
  226. app.disabled = false
  227. })
  228. .finally(() => app.isLoading = false)
  229. },
  230. /**
  231. * 登录成功-跳转回原页面
  232. */
  233. onNavigateBack(delta = 1) {
  234. const pages = getCurrentPages()
  235. if (pages.length > 1) {
  236. uni.navigateBack({
  237. delta: Number(delta || 1)
  238. })
  239. } else {
  240. this.$navTo('pages/index/index')
  241. }
  242. }
  243. }
  244. }
  245. </script>
  246. <style lang="scss" scoped>
  247. .container {
  248. padding: 100rpx 60rpx;
  249. min-height: 100vh;
  250. background-color: #fff;
  251. }
  252. // 页面头部
  253. .header {
  254. margin-bottom: 60rpx;
  255. .title {
  256. color: #191919;
  257. font-size: 54rpx;
  258. }
  259. .sub-title {
  260. margin-top: 20rpx;
  261. color: #b3b3b3;
  262. font-size: 28rpx;
  263. }
  264. }
  265. // 输入框元素
  266. .form-item {
  267. display: flex;
  268. align-items: center;
  269. padding: 18rpx;
  270. border-bottom: 1rpx solid #f3f1f2;
  271. margin-bottom: 30rpx;
  272. height: 96rpx;
  273. &--input {
  274. font-size: 28rpx;
  275. letter-spacing: 1rpx;
  276. flex: 1;
  277. height: 100%;
  278. }
  279. &--parts {
  280. min-width: 100rpx;
  281. }
  282. // 图形验证码
  283. .captcha {
  284. height: 60rpx;
  285. .image {
  286. display: block;
  287. width: 192rpx;
  288. height: 100%;
  289. }
  290. }
  291. // 短信验证码
  292. .captcha-sms {
  293. font-size: 28rpx;
  294. line-height: 50rpx;
  295. padding-right: 20rpx;
  296. .activate {
  297. color: $main-bg;
  298. }
  299. .un-activate {
  300. color: #9e9e9e;
  301. }
  302. }
  303. }
  304. // 登录按钮
  305. .login-button {
  306. width: 100%;
  307. height: 86rpx;
  308. margin-top: 80rpx;
  309. background: linear-gradient(to right, $main-bg, $main-bg2);
  310. color: $main-text;
  311. border-radius: 80rpx;
  312. box-shadow: 0px 10px 20px 0px rgba(0, 0, 0, 0.1);
  313. letter-spacing: 5rpx;
  314. display: flex;
  315. justify-content: center;
  316. align-items: center;
  317. // 禁用按钮
  318. &.disabled {
  319. opacity: 0.6;
  320. }
  321. }
  322. </style>