phoneLogin.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. const pages = getCurrentPages();
  60. if (pages.length >= 2) {
  61. uni.setStorageSync('last_page', pages[pages.length - 2].route);
  62. if (pages[pages.length - 2].options) {
  63. uni.setStorageSync('last_page_options', pages[pages.length - 2].options);
  64. }
  65. }
  66. },
  67. computed: {
  68. inputStyle() {
  69. let style = {};
  70. if (this.tel.length == 11 && this.messageDisable == false && this.$u.test.mobile(this.tel)) {
  71. style.color = "#fff";
  72. style.backgroundColor = '#5295F5';
  73. this.telError = false;
  74. } else if (this.tel.length == 11 && !this.$u.test.mobile(this.tel)) {
  75. this.telError = true;
  76. }
  77. return style;
  78. }
  79. },
  80. methods: {
  81. showToast() {
  82. this.$refs.uToast.show({
  83. title: this.toastMsg,
  84. type: this.toastType,
  85. url: this.toastUrl
  86. })
  87. },
  88. submit() {
  89. if (this.$u.test.mobile(this.tel) && this.messageDisable == false) {
  90. let that = this;
  91. let mobile = this.tel,
  92. openId = this.openId
  93. this.$u.api.codeV2Api.sendSmsCodeV2api({
  94. mobile,
  95. openId
  96. }).then(res => {
  97. this.messageDisable = true;
  98. this.messageShow = true;
  99. this.show = true;
  100. let interval = setInterval(() => {
  101. that.second--;
  102. if (that.second <= 0) {
  103. that.messageDisable = false
  104. that.messageShow = false;
  105. clearInterval(interval);
  106. that.second = 60;
  107. }
  108. }, 1000);
  109. this.accessToken = res.data.accessToken;
  110. this.userId = res.data.userId;
  111. })
  112. }
  113. },
  114. // change事件侦听
  115. change(value) {
  116. // console.log('change', value);
  117. },
  118. // 输入完验证码最后一位执行
  119. finish(value) {
  120. let params = {
  121. token: this.accessToken,
  122. code: value,
  123. openId: this.vuex_wxinfo?.openId,
  124. mobile: this.tel
  125. };
  126. this.$u.api.codeV2Api.verifyCodeV2Api(params).then(res => {
  127. if (res.code == '200') {
  128. this.$u.vuex('vuex_token', this.accessToken);
  129. this.$u.vuex('vuex_user', res.data);
  130. this.$u.vuex('vuex_hasLogin', true);
  131. setTimeout(() => {
  132. this.jumpIndex()
  133. }, 500)
  134. } else {
  135. this.$refs.uToast.show({
  136. title: res.msg,
  137. type: 'error'
  138. })
  139. }
  140. }).catch(err => {
  141. this.toastMsg = err.msg;
  142. this.showToast();
  143. });
  144. },
  145. // 微信已登录则跳转到首页
  146. jumpIndex() {
  147. let ret = localStorage.getItem('backUrl');
  148. if ((ret && ret.indexOf('wechatLogin') > (-1)) || (ret && ret.indexOf('phoneLogin') < 0)) {
  149. // 截取url
  150. const pagesIndex = ret.indexOf('pages')
  151. if (pagesIndex > (-1)) {
  152. const pageUrl = ret.slice(pagesIndex)
  153. const tabbarUrl = ['pages/center/index', 'pages/parkingLists/parkingLists', 'page/index/index']
  154. if (tabbarUrl.indexOf(pageUrl) > (-1)) {
  155. setTimeout(() => {
  156. uni.switchTab({
  157. url: '../../index/index'
  158. })
  159. }, 100)
  160. } else {
  161. setTimeout(() => {
  162. uni.redirectTo({
  163. url: '/' + pageUrl
  164. })
  165. }, 100)
  166. }
  167. } else {
  168. uni.switchTab({
  169. url: '../../index/index'
  170. })
  171. }
  172. } else {
  173. uni.switchTab({
  174. url: '/pages/index/index'
  175. })
  176. }
  177. },
  178. /**
  179. * 跳转页面
  180. * */
  181. jumpToPage(flag) {
  182. uni.navigateTo({
  183. url: "/pages/privacyPolicy/privacyPolicy?termsType=" + flag,
  184. })
  185. }
  186. }
  187. };
  188. </script>
  189. <style lang="scss" scoped>
  190. .hide {
  191. display: none !important;
  192. }
  193. .wrap {
  194. font-size: 28rpx;
  195. .content {
  196. width: 600rpx;
  197. margin: 80rpx auto 0;
  198. .title {
  199. text-align: left;
  200. font-size: 60rpx;
  201. font-weight: 500;
  202. margin-bottom: 100rpx;
  203. }
  204. input {
  205. text-align: left;
  206. margin-bottom: 10rpx;
  207. padding-bottom: 20rpx;
  208. border-bottom: 1px solid #ddd;
  209. }
  210. .getCaptcha {
  211. margin: 45rpx auto 130rpx;
  212. background-color: #a8c6f1;
  213. color: $u-tips-color;
  214. border: none;
  215. font-size: 30rpx;
  216. padding: 12rpx 0;
  217. &::after {
  218. border: none;
  219. }
  220. }
  221. }
  222. .buttom {
  223. .hint {
  224. padding: 20rpx 40rpx;
  225. font-size: 20rpx;
  226. color: $u-tips-color;
  227. .link {
  228. color: $u-type-warning;
  229. }
  230. }
  231. }
  232. .captcha {
  233. color: $u-type-warning;
  234. font-size: 30rpx;
  235. margin-top: 40rpx;
  236. text-align: center;
  237. }
  238. }
  239. </style>