phoneLogin.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. console.log(ret)
  154. if (ret && ret.indexOf('phoneLogin') < 0) {
  155. // 截取url
  156. const pagesIndex = ret.indexOf('pages')
  157. if (pagesIndex > (-1)) {
  158. const pageUrl = ret.slice(pagesIndex)
  159. console.log(pageUrl)
  160. setTimeout(() => {
  161. uni.navigateTo({
  162. url: '/' + pageUrl
  163. })
  164. }, 100)
  165. } else {
  166. uni.switchTab({
  167. url: '../../index/index'
  168. })
  169. }
  170. } else {
  171. uni.switchTab({
  172. url: '../../index/index'
  173. })
  174. }
  175. },
  176. // 获取code
  177. getCode () {
  178. var local = window.location.href // 获取页面url
  179. let locationLocaturl = window.location.search;
  180. this.code = getUrlParams(locationLocaturl,"code"); // 截取code
  181. if (this.code == null || this.code === '') { // 如果没有code,则去请求
  182. 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`
  183. } else {
  184. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  185. }
  186. },
  187. // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
  188. handleGetWXInfo (code) {
  189. this.$u.api.getWXInfo(code).then((res) => {
  190. if (res.code === 200 ) {
  191. this.$u.vuex('vuex_wxinfo', res.data);
  192. this.jumpIndex()
  193. }
  194. }).catch((err) => {
  195. this.$refs.uToast.show({
  196. title: err.msg,
  197. type: 'error',
  198. });
  199. })
  200. },
  201. }
  202. };
  203. </script>
  204. <style lang="scss" scoped>
  205. .hide{display: none!important;}
  206. .wrap {
  207. font-size: 28rpx;
  208. .content {
  209. width: 600rpx;
  210. margin: 80rpx auto 0;
  211. .title {
  212. text-align: left;
  213. font-size: 60rpx;
  214. font-weight: 500;
  215. margin-bottom: 100rpx;
  216. }
  217. input {
  218. text-align: left;
  219. margin-bottom: 10rpx;
  220. padding-bottom: 20rpx;
  221. border-bottom: 1px solid #ddd;
  222. }
  223. .getCaptcha {
  224. margin: 45rpx auto 130rpx;
  225. background-color: #a8c6f1;
  226. color: $u-tips-color;
  227. border: none;
  228. font-size: 30rpx;
  229. padding: 12rpx 0;
  230. &::after {
  231. border: none;
  232. }
  233. }
  234. }
  235. .buttom {
  236. .hint {
  237. padding: 20rpx 40rpx;
  238. font-size: 20rpx;
  239. color: $u-tips-color;
  240. .link {
  241. color: $u-type-warning;
  242. }
  243. }
  244. }
  245. .captcha {
  246. color: $u-type-warning;
  247. font-size: 30rpx;
  248. margin-top: 40rpx;
  249. text-align: center;
  250. }
  251. }
  252. </style>