phoneLogin.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. this.wechatLogin()
  125. } else {
  126. this.$refs.uToast.show({
  127. title: res.msg,
  128. type: 'error'
  129. })
  130. }
  131. }).catch(err => {
  132. this.toastMsg = err.msg;
  133. this.showToast();
  134. });
  135. },
  136. // 微信登录
  137. wechatLogin() {
  138. this.jumpIndex()
  139. },
  140. // 微信已登录则跳转到首页
  141. jumpIndex() {
  142. let ret = localStorage.getItem('backUrl');
  143. if (ret.indexOf('wechatLogin') > (-1) || (ret && ret.indexOf('phoneLogin') < 0)) {
  144. // 截取url
  145. const pagesIndex = ret.indexOf('pages')
  146. if (pagesIndex > (-1)) {
  147. const pageUrl = ret.slice(pagesIndex)
  148. const tabbarUrl = ['pages/center/index', 'pages/parkingLists/parkingLists', 'page/index/index']
  149. if (tabbarUrl.indexOf(pageUrl) > (-1)) {
  150. setTimeout(() => {
  151. uni.switchTab({
  152. url: '../../index/index'
  153. })
  154. }, 100)
  155. } else {
  156. setTimeout(() => {
  157. uni.redirectTo({
  158. url: '/' + pageUrl
  159. })
  160. }, 100)
  161. }
  162. } else {
  163. uni.switchTab({
  164. url: '../../index/index'
  165. })
  166. }
  167. } else {
  168. uni.switchTab({
  169. url: '/pages/index/index'
  170. })
  171. }
  172. },
  173. /**
  174. * 跳转页面
  175. * */
  176. jumpToPage(flag) {
  177. uni.navigateTo({
  178. url: "/pages/privacyPolicy/privacyPolicy?termsType=" + flag,
  179. })
  180. }
  181. }
  182. };
  183. </script>
  184. <style lang="scss" scoped>
  185. .hide {
  186. display: none !important;
  187. }
  188. .wrap {
  189. font-size: 28rpx;
  190. .content {
  191. width: 600rpx;
  192. margin: 80rpx auto 0;
  193. .title {
  194. text-align: left;
  195. font-size: 60rpx;
  196. font-weight: 500;
  197. margin-bottom: 100rpx;
  198. }
  199. input {
  200. text-align: left;
  201. margin-bottom: 10rpx;
  202. padding-bottom: 20rpx;
  203. border-bottom: 1px solid #ddd;
  204. }
  205. .getCaptcha {
  206. margin: 45rpx auto 130rpx;
  207. background-color: #a8c6f1;
  208. color: $u-tips-color;
  209. border: none;
  210. font-size: 30rpx;
  211. padding: 12rpx 0;
  212. &::after {
  213. border: none;
  214. }
  215. }
  216. }
  217. .buttom {
  218. .hint {
  219. padding: 20rpx 40rpx;
  220. font-size: 20rpx;
  221. color: $u-tips-color;
  222. .link {
  223. color: $u-type-warning;
  224. }
  225. }
  226. }
  227. .captcha {
  228. color: $u-type-warning;
  229. font-size: 30rpx;
  230. margin-top: 40rpx;
  231. text-align: center;
  232. }
  233. }
  234. </style>