phoneLogin.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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" mode="bottomLine" :maxlength="codelength"></u-message-input>
  9. </view>
  10. <!-- <view class="u-text-center u-type-error" v-if="phoneError">手机号输入错误</view> -->
  11. <view class="captcha">
  12. <!-- <text v-if="show&&this.messageDisable==false" @tap="noCaptcha">收不到验证码点这里</text> -->
  13. <text v-if="messageShow">{{ second }}秒后可重新获取验证码</text>
  14. </view>
  15. <view class="buttom">
  16. <view class="hint u-text-center">
  17. 登录代表同意
  18. <text class="link">隐私政策,</text>
  19. 并授权使用您的账号信息(如昵称、头像、收货地址)以便您统一管理
  20. </view>
  21. </view>
  22. <u-toast ref="uToast" />
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. tel: '',
  30. messageCode:'',
  31. messageShow: false,
  32. messageDisable: false,
  33. codelength: 4,
  34. show: false,
  35. second:10,
  36. toastMsg:'',
  37. toastUrl:'',
  38. toastType:'',
  39. accessToken:'',
  40. userId:'',
  41. telError:false,
  42. // messageError:false
  43. }
  44. },
  45. onLoad() {
  46. },
  47. computed: {
  48. inputStyle() {
  49. let style = {};
  50. if(this.tel.length==11&&this.messageDisable==false&&this.$u.test.mobile(this.tel)) {
  51. style.color = "#fff";
  52. style.backgroundColor = '#5295F5';
  53. this.telError = false;
  54. // style.backgroundColor = this.$u.color['warning'];
  55. }else if(this.tel.length==11&&!this.$u.test.mobile(this.tel)){
  56. this.telError = true;
  57. }
  58. return style;
  59. }
  60. },
  61. methods: {
  62. showToast() {
  63. this.$refs.uToast.show({
  64. title: this.toastMsg,
  65. type: this.toastType,
  66. url: this.toastUrl
  67. })
  68. },
  69. submit() {
  70. if(this.$u.test.mobile(this.tel)&&this.messageDisable==false) {
  71. let that = this;
  72. this.$u.api.getPhoneLoginCode({mobile:this.tel})
  73. .then(res =>{
  74. this.messageDisable = true;
  75. this.messageShow = true;
  76. this.show = true;
  77. let interval = setInterval(() => {
  78. that.second--;
  79. if (that.second <= 0) {
  80. that.messageDisable=false
  81. that.messageShow = false;
  82. if (that.messageCode.lenth != 4) {
  83. // this.messageError = true;
  84. }
  85. clearInterval(interval);
  86. that.second=10;
  87. }
  88. }, 1000);
  89. console.log('getcode res',res);
  90. this.accessToken = res.data.accessToken;
  91. this.$u.vuex('vuex_token', res.data.accessToken);
  92. this.userId = res.data.userId;
  93. }).catch(err=>{
  94. console.log('err',err)
  95. this.toastMsg = err.code + ":" + err.msg;
  96. this.showToast();
  97. console.log('getcode err',err)
  98. });
  99. }
  100. },
  101. // 收不到验证码选择时的选择
  102. // noCaptcha() {
  103. // uni.showActionSheet({
  104. // itemList: ['重新获取验证码', '接听语音验证码'],
  105. // success: function(res) {
  106. // },
  107. // fail: function(res) {
  108. // }
  109. // });
  110. // },
  111. // change事件侦听
  112. change(value) {
  113. // console.log('change', value);
  114. },
  115. // 输入完验证码最后一位执行
  116. finish(value) {
  117. let params = {
  118. accessToken:this.accessToken,
  119. userId:this.userId,
  120. code:value
  121. };
  122. console.log('params',params);
  123. this.$u.api.phoneLoginAuth(params)
  124. .then(res =>{
  125. if(res.code=='200'){
  126. // console.log('finish res',res);
  127. this.$u.vuex('vuex_user', res.data);
  128. this.$u.vuex('vuex_hasLogin', true);
  129. let storage = window.sessionStorage;
  130. let backUrl = storage.getItem('backUrl');
  131. let switchTabList = ['/pages/use/use'];
  132. if(switchTabList.includes(res.data)){}
  133. console.log('backUrl',backUrl);
  134. location.href = backUrl;
  135. // this.$refs.uToast.show({
  136. // title: '操作成功',
  137. // url: backUrl,
  138. // // isTab:true,
  139. // callback :function(err){
  140. // console.log('err',err)
  141. // }
  142. // });
  143. // this.$u.api.getuseinfo().then(res => {
  144. // console.log('getuseinfo',res.retBody);
  145. // this.$u.vuex('vuex_user', res.retBody);
  146. // this.toastMsg = '操作成功';
  147. // this.showToast();
  148. // setTimeout(() => {
  149. // this.$u.route({
  150. // url: 'pages/template/wxCenter/index'
  151. // })
  152. // }, 2000);
  153. // });
  154. }else{
  155. this.toastMsg = res.msg;
  156. this.showToast();
  157. }
  158. }).catch(err=>{
  159. console.log('finish err',err);
  160. this.toastMsg = err.msg;
  161. this.showToast();
  162. });
  163. }
  164. }
  165. };
  166. </script>
  167. <style lang="scss" scoped>
  168. .hide{display: none!important;}
  169. .wrap {
  170. font-size: 28rpx;
  171. .content {
  172. width: 600rpx;
  173. margin: 80rpx auto 0;
  174. .title {
  175. text-align: left;
  176. font-size: 60rpx;
  177. font-weight: 500;
  178. margin-bottom: 100rpx;
  179. }
  180. input {
  181. text-align: left;
  182. margin-bottom: 10rpx;
  183. padding-bottom: 20rpx;
  184. border-bottom: 1px solid #ddd;
  185. }
  186. .getCaptcha {
  187. margin: 45rpx auto 130rpx;
  188. background-color: #a8c6f1;
  189. color: $u-tips-color;
  190. border: none;
  191. font-size: 30rpx;
  192. padding: 12rpx 0;
  193. &::after {
  194. border: none;
  195. }
  196. }
  197. }
  198. .buttom {
  199. .hint {
  200. padding: 20rpx 40rpx;
  201. font-size: 20rpx;
  202. color: $u-tips-color;
  203. .link {
  204. color: $u-type-warning;
  205. }
  206. }
  207. }
  208. .captcha {
  209. color: $u-type-warning;
  210. font-size: 30rpx;
  211. margin-top: 40rpx;
  212. text-align: center;
  213. }
  214. }
  215. </style>