index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <view>
  3. <view class="login-title">
  4. <u-icon name="qichexiangguan-chongdianzhan" custom-prefix="custom-icon" color="#1677ff" size="56"></u-icon>
  5. <h3>电动自行车智能充电系统</h3>
  6. </view>
  7. <view class="login-main">
  8. <u-form :model="form" ref="uForm">
  9. <u-form-item label="手机号码" prop="phone" label-width="150" label-position="top">
  10. <u-input placeholder="请输入手机号" v-model="form.phone" type="number"></u-input>
  11. </u-form-item>
  12. <u-form-item label="验证码" prop="code" label-width="150" label-position="top">
  13. <u-input placeholder="请输入验证码" v-model="form.code" type="text"></u-input>
  14. <view class="login-code" @click="getCode">
  15. {{codeTips}}
  16. </view>
  17. </u-form-item>
  18. </u-form>
  19. </view>
  20. <view class="login-btn">
  21. <u-button type="primary" :custom-style="customStyle" @click="finish" shape="square">登录</u-button>
  22. </view>
  23. <u-verification-code :seconds="sendMsgSecond" ref="uCode" @change="codeChange" @end="end" @start="start">
  24. </u-verification-code>
  25. </view>
  26. </template>
  27. <script>
  28. import * as API from '@/apis/login.js'
  29. import {
  30. checkPhone
  31. } from '@/utils'
  32. export default {
  33. data() {
  34. return {
  35. form: {
  36. phone: '',
  37. code: '',
  38. },
  39. backUrl:"",
  40. message:"",
  41. codeTips: '',
  42. isSendMsgIng: false,
  43. sendMsgSecond: 60 * 2,
  44. customStyle: {
  45. background: '#1677ff'
  46. }
  47. }
  48. },
  49. onLoad(op){
  50. this.message=op.message;
  51. this.backUrl=op.back;
  52. },
  53. methods: {
  54. codeChange(text) {
  55. this.codeTips = text;
  56. },
  57. //倒计时
  58. end() {
  59. this.sendMsgSecond = 2 * 60;
  60. this.isSendMsgIng = false;
  61. },
  62. finish(){
  63. uni.redirectTo({
  64. url:'pages/user/index'
  65. })
  66. return
  67. if(!this.carhelp.getOpenId()){
  68. uni.showToast({
  69. title:"请使用“微信”访问本系统登录"
  70. })
  71. return
  72. }
  73. uni.showLoading({
  74. title:"加载中",mask:true,
  75. })
  76. API.validateCode({
  77. verifyCode: this.form.code,
  78. telephone:this.form.phone,
  79. openId:this.carhelp.getOpenId(),
  80. }).then((response) => {
  81. var token = response ? response.data.token : '';
  82. this.carhelp.setToken(token);
  83. this.carhelp.setPersonInfo(response.data.jobUser );
  84. //this.gotoUrl("pages/user/index")
  85. uni.redirectTo({
  86. url:'/pages/user/index'
  87. })
  88. // if(this.backUrl){
  89. // window.location.href="#"+this.backUrl.split(",").find(function(item){
  90. // return item.indexOf("pages/login/")==-1
  91. // });
  92. // }else{
  93. // }
  94. }).catch(error => {
  95. uni.showToast({
  96. title:error,
  97. icon:"none"
  98. })
  99. })
  100. },
  101. start() {
  102. if (!this.isSendMsgIng) {
  103. this.carhelp.set("getvcodetime", new Date().getTime());
  104. console.log("模拟发送成功")
  105. return
  106. uni.showLoading({
  107. title: "加载中",
  108. mask: true,
  109. })
  110. API.getVerifyCode(this.form.phone).then((response) => {
  111. uni.hideLoading();
  112. this.carhelp.set("getvcodetime", new Date().getTime());
  113. if (!"") {
  114. //倒计时
  115. uni.showToast({
  116. title: "发送成功"
  117. })
  118. } else {
  119. uni.showToast({
  120. title: "您的验证码已经发送[5分钟有效],请勿重复点击"
  121. })
  122. }
  123. }).catch(error => {
  124. uni.showToast({
  125. title: error,
  126. icon: "none"
  127. })
  128. })
  129. }
  130. },
  131. // 获取验证码
  132. getCode() {
  133. if(this.$refs.uCode.canGetCode) {
  134. } else {
  135. uni.showToast({
  136. title: '倒计时结束后再发送',
  137. icon: "none"
  138. })
  139. return
  140. }
  141. var checkPhoneResult = checkPhone(this.form.phone);
  142. if (false&&checkPhoneResult !== true) {
  143. uni.showToast({
  144. title: checkPhoneResult,
  145. })
  146. return;
  147. }
  148. this.$refs.uCode.start();
  149. }
  150. },onReady(){
  151. if(!this.carhelp.getOpenId()){
  152. uni.showToast({
  153. title:"请使用“微信”访问本系统登录"
  154. })
  155. return
  156. }else if(this.message){
  157. uni.showToast({
  158. title:this.message.split(",")[0],
  159. icon:"none"
  160. })
  161. }
  162. var time = this.carhelp.get("getvcodetime");
  163. if (time) {
  164. //this.$refs.uCode.start();
  165. var nowtime = new Date().getTime()
  166. var differ = (nowtime - time) / 1000
  167. if (differ < 2 * 60) {
  168. this.sendMsgSecond = 2 * 60 - parseInt(differ)
  169. this.isSendMsgIng = true;
  170. this.$refs.uCode.start();
  171. }
  172. }
  173. }
  174. }
  175. </script>
  176. <style lang="scss" scoped>
  177. .login-title {
  178. display: flex;
  179. align-items: center;
  180. margin: 40px 30px;
  181. h3 {
  182. font-size: 20px;
  183. margin-left: 10rpx;
  184. color: #1677ff;
  185. font-weight: normal;
  186. }
  187. }
  188. .login-main {
  189. margin: 0 30px;
  190. }
  191. .login-btn {
  192. margin: 30px;
  193. }
  194. .login-code {
  195. color: #1677ff;
  196. }
  197. </style>