phoneLogin.vue 5.7 KB

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