phoneLogin.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <view class="wrap">
  3. <view class="content">
  4. <view class="title">手机号登录</view>
  5. <input class="u-border-bottom" type="number" maxlength="11" v-model="tel" placeholder="请输入手机号" />
  6. <view class="u-text-center u-type-error u-m-t-20" v-if="telError">手机号输入错误</view>
  7. <button @tap="submit" :style="[inputStyle]" class="getCaptcha">获取短信验证码</button>
  8. <u-message-input v-if="show" :focus="true" :value="messageCode" @change="change" @finish="finish"
  9. mode="bottomLine" :maxlength="codelength"></u-message-input>
  10. </view>
  11. <view class="captcha">
  12. <text v-if="messageShow">{{ second }}秒后可重新获取验证码</text>
  13. </view>
  14. <view class="buttom">
  15. <view class="hint u-text-center">
  16. 登录代表同意
  17. <text class="link" @tap="jumpToPage(1)">《用户服务条款》</text>和
  18. <text class="link" @tap="jumpToPage(2)">《隐私政策》</text>
  19. 并授权使用您的账号信息(如昵称、头像、收货地址)以便您统一管理
  20. </view>
  21. </view>
  22. <u-toast ref="uToast" />
  23. </view>
  24. </template>
  25. <script>
  26. import getUrlParams from "./../../../utils/getUrlParams.js";
  27. export default {
  28. data() {
  29. return {
  30. tel: '',
  31. messageCode: '',
  32. messageShow: false,
  33. messageDisable: false,
  34. codelength: 4,
  35. show: false,
  36. second: 60,
  37. toastMsg: '',
  38. toastUrl: '',
  39. toastType: '',
  40. accessToken: '',
  41. userId: '',
  42. telError: false,
  43. openId: ''
  44. }
  45. },
  46. onLoad(page) {
  47. this.openId = this.vuex_wxinfo?.openId
  48. const backUrl = location.href
  49. if (!this.openId) {
  50. // 判断浏览器
  51. const ua = window.navigator.userAgent.toLowerCase()
  52. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  53. // 微信中打开
  54. this.$u.route('pages/wechatLogin/wechatLogin', {
  55. backUrl
  56. })
  57. }
  58. }
  59. },
  60. computed: {
  61. inputStyle() {
  62. let style = {};
  63. if (this.tel.length == 11 && this.messageDisable == false && this.$u.test.mobile(this.tel)) {
  64. style.color = "#fff";
  65. style.backgroundColor = '#5295F5';
  66. this.telError = false;
  67. } else if (this.tel.length == 11 && !this.$u.test.mobile(this.tel)) {
  68. this.telError = true;
  69. }
  70. return style;
  71. }
  72. },
  73. methods: {
  74. showToast() {
  75. this.$refs.uToast.show({
  76. title: this.toastMsg,
  77. type: this.toastType,
  78. url: this.toastUrl
  79. })
  80. },
  81. submit() {
  82. if (this.$u.test.mobile(this.tel) && this.messageDisable == false) {
  83. let that = this;
  84. let mobile = this.tel,
  85. openId = this.openId
  86. this.$u.api.codeV2Api.sendSmsCodeV2api({
  87. mobile,
  88. openId
  89. }).then(res => {
  90. this.messageDisable = true;
  91. this.messageShow = true;
  92. this.show = true;
  93. let interval = setInterval(() => {
  94. that.second--;
  95. if (that.second <= 0) {
  96. that.messageDisable = false
  97. that.messageShow = false;
  98. clearInterval(interval);
  99. that.second = 60;
  100. }
  101. }, 1000);
  102. this.accessToken = res.data.accessToken;
  103. this.userId = res.data.userId;
  104. })
  105. }
  106. },
  107. // change事件侦听
  108. change(value) {
  109. // console.log('change', value);
  110. },
  111. // 输入完验证码最后一位执行
  112. finish(value) {
  113. let params = {
  114. token: this.accessToken,
  115. code: value,
  116. openId: this.vuex_wxinfo?.openId,
  117. mobile: this.tel
  118. };
  119. this.$u.api.codeV2Api.verifyCodeV2Api(params).then(res => {
  120. if (res.code == '200') {
  121. this.$u.vuex('vuex_token', this.accessToken);
  122. this.$u.vuex('vuex_user', res.data);
  123. this.$u.vuex('vuex_hasLogin', true);
  124. setTimeout(() => {
  125. this.jumpIndex()
  126. }, 500)
  127. } else {
  128. this.$refs.uToast.show({
  129. title: res.msg,
  130. type: 'error'
  131. })
  132. }
  133. }).catch(err => {
  134. this.toastMsg = err.msg;
  135. this.showToast();
  136. });
  137. },
  138. // 微信登录
  139. wechatLogin() {
  140. this.jumpIndex()
  141. },
  142. // 微信已登录则跳转到首页
  143. jumpIndex() {
  144. let ret = localStorage.getItem('backUrl');
  145. if ((ret && ret.indexOf('wechatLogin') > (-1)) || (ret && ret.indexOf('phoneLogin') < 0)) {
  146. // 截取url
  147. const pagesIndex = ret.indexOf('pages')
  148. if (pagesIndex > (-1)) {
  149. const pageUrl = ret.slice(pagesIndex)
  150. const tabbarUrl = ['pages/center/index', 'pages/parkingLists/parkingLists', 'page/index/index']
  151. if (tabbarUrl.indexOf(pageUrl) > (-1)) {
  152. setTimeout(() => {
  153. uni.switchTab({
  154. url: '../../index/index'
  155. })
  156. }, 100)
  157. } else {
  158. setTimeout(() => {
  159. uni.redirectTo({
  160. url: '/' + pageUrl
  161. })
  162. }, 100)
  163. }
  164. } else {
  165. uni.switchTab({
  166. url: '../../index/index'
  167. })
  168. }
  169. } else {
  170. uni.switchTab({
  171. url: '/pages/index/index'
  172. })
  173. }
  174. },
  175. /**
  176. * 跳转页面
  177. * */
  178. jumpToPage(flag) {
  179. uni.navigateTo({
  180. url: "/pages/privacyPolicy/privacyPolicy?termsType=" + flag,
  181. })
  182. }
  183. }
  184. };
  185. </script>
  186. <style lang="scss" scoped>
  187. .hide {
  188. display: none !important;
  189. }
  190. .wrap {
  191. font-size: 28rpx;
  192. .content {
  193. width: 600rpx;
  194. margin: 80rpx auto 0;
  195. .title {
  196. text-align: left;
  197. font-size: 60rpx;
  198. font-weight: 500;
  199. margin-bottom: 100rpx;
  200. }
  201. input {
  202. text-align: left;
  203. margin-bottom: 10rpx;
  204. padding-bottom: 20rpx;
  205. border-bottom: 1px solid #ddd;
  206. }
  207. .getCaptcha {
  208. margin: 45rpx auto 130rpx;
  209. background-color: #a8c6f1;
  210. color: $u-tips-color;
  211. border: none;
  212. font-size: 30rpx;
  213. padding: 12rpx 0;
  214. &::after {
  215. border: none;
  216. }
  217. }
  218. }
  219. .buttom {
  220. .hint {
  221. padding: 20rpx 40rpx;
  222. font-size: 20rpx;
  223. color: $u-tips-color;
  224. .link {
  225. color: $u-type-warning;
  226. }
  227. }
  228. }
  229. .captcha {
  230. color: $u-type-warning;
  231. font-size: 30rpx;
  232. margin-top: 40rpx;
  233. text-align: center;
  234. }
  235. }
  236. </style>