phoneLogin.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. 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. // messageError:false
  44. }
  45. },
  46. onLoad() {
  47. },
  48. computed: {
  49. inputStyle() {
  50. let style = {};
  51. if(this.tel.length == 11&&this.messageDisable==false&&this.$u.test.mobile(this.tel)) {
  52. style.color = "#fff";
  53. style.backgroundColor = '#5295F5';
  54. this.telError = false;
  55. // style.backgroundColor = this.$u.color['warning'];
  56. }else if(this.tel.length==11&&!this.$u.test.mobile(this.tel)){
  57. this.telError = true;
  58. }
  59. return style;
  60. }
  61. },
  62. methods: {
  63. showToast() {
  64. this.$refs.uToast.show({
  65. title: this.toastMsg,
  66. type: this.toastType,
  67. url: this.toastUrl
  68. })
  69. },
  70. submit() {
  71. if(this.$u.test.mobile(this.tel)&&this.messageDisable==false) {
  72. let that = this;
  73. this.$u.api.getPhoneLoginCode({mobile:this.tel})
  74. .then(res =>{
  75. this.messageDisable = true;
  76. this.messageShow = true;
  77. this.show = true;
  78. let interval = setInterval(() => {
  79. that.second--;
  80. if (that.second <= 0) {
  81. that.messageDisable=false
  82. that.messageShow = false;
  83. if (that.messageCode.lenth != 4) {
  84. // this.messageError = true;
  85. }
  86. clearInterval(interval);
  87. that.second=60;
  88. }
  89. }, 1000);
  90. console.log('getcode res',res);
  91. this.accessToken = res.data.accessToken;
  92. this.$u.vuex('vuex_token', res.data.accessToken);
  93. this.userId = res.data.userId;
  94. }).catch(err=>{
  95. console.log('err',err)
  96. this.toastMsg = err.code + ":" + err.msg;
  97. this.showToast();
  98. console.log('getcode err',err)
  99. });
  100. }
  101. },
  102. // 收不到验证码选择时的选择
  103. // noCaptcha() {
  104. // uni.showActionSheet({
  105. // itemList: ['重新获取验证码', '接听语音验证码'],
  106. // success: function(res) {
  107. // },
  108. // fail: function(res) {
  109. // }
  110. // });
  111. // },
  112. // change事件侦听
  113. change(value) {
  114. // console.log('change', value);
  115. },
  116. // 输入完验证码最后一位执行
  117. finish(value) {
  118. let params = {
  119. accessToken:this.accessToken,
  120. userId:this.userId,
  121. code:value
  122. };
  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. this.wechatLogin()
  130. }else{
  131. this.toastMsg = res.msg;
  132. this.showToast();
  133. }
  134. }).catch(err=>{
  135. console.log('finish err',err);
  136. this.toastMsg = err.msg;
  137. this.showToast();
  138. });
  139. },
  140. // 微信登录
  141. wechatLogin() {
  142. this.jumpIndex()
  143. // const openId = this.$store.state.vuex_wxinfo.openId
  144. // if (openId) {
  145. // this.jumpIndex()
  146. // } else {
  147. // this.getCode()
  148. // }
  149. },
  150. // 微信已登录则跳转到首页
  151. jumpIndex() {
  152. let ret = localStorage.getItem('backUrl')
  153. if (ret && ret.indexOf('phoneLogin') < 0) {
  154. // 截取url
  155. const pagesIndex = ret.indexOf('pages')
  156. const pageUrl = ret.slice(pagesIndex)
  157. setTimeout(() => {
  158. uni.navigateTo({
  159. url: '/' + pageUrl
  160. })
  161. }, 100)
  162. } else {
  163. uni.switchTab({
  164. url: '../../index/index'
  165. })
  166. }
  167. },
  168. // 获取code
  169. getCode () {
  170. var local = window.location.href // 获取页面url
  171. let locationLocaturl = window.location.search;
  172. this.code = getUrlParams(locationLocaturl,"code"); // 截取code
  173. if (this.code == null || this.code === '') { // 如果没有code,则去请求
  174. window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wxAppid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&#wechat_redirect`
  175. } else {
  176. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  177. }
  178. },
  179. // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
  180. handleGetWXInfo (code) {
  181. this.$u.api.getWXInfo(code).then((res) => {
  182. if (res.code === 200 ) {
  183. this.$u.vuex('vuex_wxinfo', res.data);
  184. this.jumpIndex()
  185. }
  186. }).catch((err) => {
  187. this.$refs.uToast.show({
  188. title: err.msg,
  189. type: 'error',
  190. });
  191. })
  192. },
  193. }
  194. };
  195. </script>
  196. <style lang="scss" scoped>
  197. .hide{display: none!important;}
  198. .wrap {
  199. font-size: 28rpx;
  200. .content {
  201. width: 600rpx;
  202. margin: 80rpx auto 0;
  203. .title {
  204. text-align: left;
  205. font-size: 60rpx;
  206. font-weight: 500;
  207. margin-bottom: 100rpx;
  208. }
  209. input {
  210. text-align: left;
  211. margin-bottom: 10rpx;
  212. padding-bottom: 20rpx;
  213. border-bottom: 1px solid #ddd;
  214. }
  215. .getCaptcha {
  216. margin: 45rpx auto 130rpx;
  217. background-color: #a8c6f1;
  218. color: $u-tips-color;
  219. border: none;
  220. font-size: 30rpx;
  221. padding: 12rpx 0;
  222. &::after {
  223. border: none;
  224. }
  225. }
  226. }
  227. .buttom {
  228. .hint {
  229. padding: 20rpx 40rpx;
  230. font-size: 20rpx;
  231. color: $u-tips-color;
  232. .link {
  233. color: $u-type-warning;
  234. }
  235. }
  236. }
  237. .captcha {
  238. color: $u-type-warning;
  239. font-size: 30rpx;
  240. margin-top: 40rpx;
  241. text-align: center;
  242. }
  243. }
  244. </style>