phoneLogin.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <view class="phone-login">
  3. <view class="login-content">
  4. <view class="login-logo">
  5. <u-image src="/static/logo.png" width="160" height="160" class="logo"></u-image>
  6. </view>
  7. <u-form ref="uForm">
  8. <u-form-item>
  9. <u-input v-model.trim="mobile" placeholder="请输入手机号" type="number" maxlength='11' />
  10. </u-form-item>
  11. <u-form-item>
  12. <u-input placeholder="请输入验证码" maxlength='4' type="number" v-model.trim="code" />
  13. <u-button slot="right" class="yzmBtn" :custom-style="yzmBtn" shape="circle" :hair-line="false" size="mini"
  14. @click="checkMobile" :disabled="isDisabled">{{codeText}}</u-button>
  15. </u-form-item>
  16. <u-form-item class="form-btn" :border-bottom="false">
  17. <u-button :custom-style="submitBtn" type="warring" form-type="submit" shape="circle" :hair-line="false"
  18. @click="bindLogin">登录</u-button>
  19. </u-form-item>
  20. </u-form>
  21. </view>
  22. <view class="xieyi" @click="gotoXieyi()">
  23. 首次登录会自动创建账号且代表同意 <text>《用户协议》</text>及<text>《隐私政策》</text>
  24. </view>
  25. <!-- 图文验证码 -->
  26. <uni-popup @change="changeModal" ref="imageTxtPopup" type="center">
  27. <popup-con title="图文验证码" :popBtn="popBtn" :changeImg="changeImg" @captchaImg="captchaVerify"></popup-con>
  28. </uni-popup>
  29. </view>
  30. </template>
  31. <script>
  32. import service from '../../service.js';
  33. import {
  34. code2Session,
  35. login,
  36. sendLoginVerifyCode
  37. } from '../../api/login.js'
  38. import {
  39. mapState,
  40. mapMutations
  41. } from 'vuex'
  42. import uniPopup from '@/components/uni-popup/uni-popup.vue'
  43. import popupCon from '@/components/uni-popup/popup-con.vue'
  44. import {
  45. isvalidPhone
  46. } from '@/libs/validate.js'
  47. export default {
  48. components: {
  49. uniPopup,
  50. popupCon
  51. },
  52. data() {
  53. return {
  54. mobile: '', // 手机号码
  55. code: '', // 短信验证码
  56. popBtn: [], // name为操作按钮名称,color为操作按钮颜色值
  57. changeImg: false, // 是否重新更换图形验证码
  58. randomCode: '', // 图片验证码随机码
  59. codeText: '获取验证码',
  60. getCodeing: false,
  61. totalTime: 60,
  62. yzmBtn: {
  63. background: "#EFEFEF"
  64. },
  65. submitBtn: {
  66. background: "#1283d4",
  67. marginTop: '15px',
  68. color: '#ffffff'
  69. },
  70. isDisabled: false // 倒计时按钮是否禁用
  71. }
  72. },
  73. computed: mapState(['forcedLogin']),
  74. mounted() {
  75. //微信平台登录
  76. //#ifdef MP-WEIXIN
  77. this.$store.dispatch('wxLogin', 'weixin')
  78. //#endif
  79. },
  80. methods: {
  81. ...mapMutations(['login']),
  82. // 获取验证码
  83. getCode() {
  84. let sid = null
  85. sid = setInterval(() => {
  86. if (this.totalTime > 0) {
  87. this.totalTime = this.totalTime - 1
  88. this.codeText = '已发送' + this.totalTime + 's'
  89. if (this.totalTime < 60) {
  90. this.isDisabled = true
  91. }
  92. } else {
  93. this.isDisabled = false
  94. clearInterval(sid)
  95. this.getCodeing = false
  96. this.codeText = '获取验证码'
  97. this.totalTime = 60
  98. }
  99. }, 1000)
  100. },
  101. // 验证手机
  102. checkMobile() {
  103. if (!this.getCodeing) {
  104. if (this.mobile) {
  105. if (!isvalidPhone(this.mobile)) {
  106. uni.showToast({
  107. title: '请输入正确的手机号码',
  108. icon: 'none'
  109. })
  110. return false
  111. } else {
  112. this.getCodeing = true
  113. this.retsetCode()
  114. // 图文验证码
  115. this.$refs.imageTxtPopup.open()
  116. }
  117. }
  118. }
  119. },
  120. changeModal(val) {
  121. if (val.show == false) {
  122. this.getCodeing = false
  123. }
  124. },
  125. // 验证图片验证码
  126. captchaVerify(code, nowRandomCode) {
  127. const _this = this
  128. let obj = {}
  129. obj.mobile = this.mobile
  130. obj.random = nowRandomCode
  131. obj.code = code
  132. obj.openid = this.$store.state.openid
  133. // 发送短信验证码
  134. sendLoginVerifyCode(obj).then(res => {
  135. console.log(JSON.stringify(res.data))
  136. if (res.status == 200) { // 验证码输入正确
  137. _this.randomCode = nowRandomCode // 图片验证码随机码
  138. // 关闭 图形验证码 弹框
  139. _this.$refs.imageTxtPopup.close()
  140. // 开启倒计时
  141. _this.getCode()
  142. uni.showToast({
  143. icon: 'none',
  144. title: '验证码发送成功'
  145. })
  146. } else { // 验证码输入错误
  147. _this.retsetCode()
  148. uni.showToast({
  149. icon: 'none',
  150. title: res.message
  151. })
  152. }
  153. })
  154. },
  155. // 重新触发获取图片验证码
  156. retsetCode() {
  157. const _this = this
  158. _this.code = ''
  159. _this.randomCode = ''
  160. // 切换验证码重新校验
  161. _this.changeImg = false
  162. _this.$nextTick(function() {
  163. _this.changeImg = true
  164. })
  165. },
  166. // 登录
  167. bindLogin() {
  168. let mobile = this.mobile
  169. let openid = this.$store.state.openid
  170. let code = this.code
  171. let random = this.randomCode
  172. let data = {
  173. 'mobile': mobile,
  174. 'code': code,
  175. 'openid': openid,
  176. 'random': random
  177. }
  178. login(data).then(res => {
  179. console.log(res)
  180. if (res.status == 200) {
  181. this.toMain(res.data)
  182. } else {
  183. uni.showToast({
  184. title: res.message,
  185. icon: 'none'
  186. })
  187. }
  188. })
  189. },
  190. toMain(data) {
  191. this.login(data);
  192. uni.reLaunch({
  193. url: '/pages/index/index',
  194. });
  195. },
  196. // 用户协议
  197. gotoXieyi(url) {
  198. uni.navigateTo({
  199. url: '/pagesMe/xieyi/index?id=0'
  200. })
  201. },
  202. },
  203. watch: {
  204. mobile(val) {
  205. this.mobile = val
  206. }
  207. }
  208. }
  209. </script>
  210. <style lang="scss">
  211. .phone-login {
  212. display: flex;
  213. flex-direction: column;
  214. width: 100%;
  215. height: 100vh;
  216. padding: 0 125upx;
  217. .login-content {
  218. flex: 1;
  219. .login-logo {
  220. text-align: center;
  221. .logo {
  222. display: inline-block;
  223. margin: 200upx 0 100upx;
  224. }
  225. }
  226. }
  227. .xieyi {
  228. font-size: 24upx;
  229. text-align: center;
  230. padding-bottom: 60upx;
  231. text {
  232. color: #1283d4
  233. }
  234. }
  235. }
  236. </style>