phoneLogin.vue 6.0 KB

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