phoneLogin.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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(page) {
  47. // 如果存在code 则认为是微信登录完成后跳转过来的,直接获取信息跳转首页或者指定页面
  48. let locationLocaturl = window.location.search;
  49. this.code = getUrlParams(locationLocaturl, "code"); // 截取code
  50. if (this.code) {
  51. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  52. }
  53. },
  54. computed: {
  55. inputStyle() {
  56. let style = {};
  57. if(this.tel.length == 11&&this.messageDisable==false&&this.$u.test.mobile(this.tel)) {
  58. style.color = "#fff";
  59. style.backgroundColor = '#5295F5';
  60. this.telError = false;
  61. // style.backgroundColor = this.$u.color['warning'];
  62. }else if(this.tel.length==11&&!this.$u.test.mobile(this.tel)){
  63. this.telError = true;
  64. }
  65. return style;
  66. }
  67. },
  68. methods: {
  69. showToast() {
  70. this.$refs.uToast.show({
  71. title: this.toastMsg,
  72. type: this.toastType,
  73. url: this.toastUrl
  74. })
  75. },
  76. submit() {
  77. if(this.$u.test.mobile(this.tel)&&this.messageDisable==false) {
  78. let that = this;
  79. this.$u.api.getPhoneLoginCode({mobile:this.tel})
  80. .then(res =>{
  81. this.messageDisable = true;
  82. this.messageShow = true;
  83. this.show = true;
  84. let interval = setInterval(() => {
  85. that.second--;
  86. if (that.second <= 0) {
  87. that.messageDisable=false
  88. that.messageShow = false;
  89. if (that.messageCode.lenth != 4) {
  90. // this.messageError = true;
  91. }
  92. clearInterval(interval);
  93. that.second=60;
  94. }
  95. }, 1000);
  96. this.accessToken = res.data.accessToken;
  97. this.$u.vuex('vuex_token', res.data.accessToken);
  98. this.userId = res.data.userId;
  99. }).catch(err=>{
  100. this.toastMsg = err.code + ":" + err.msg;
  101. this.showToast();
  102. });
  103. }
  104. },
  105. // 收不到验证码选择时的选择
  106. // noCaptcha() {
  107. // uni.showActionSheet({
  108. // itemList: ['重新获取验证码', '接听语音验证码'],
  109. // success: function(res) {
  110. // },
  111. // fail: function(res) {
  112. // }
  113. // });
  114. // },
  115. // change事件侦听
  116. change(value) {
  117. // console.log('change', value);
  118. },
  119. // 输入完验证码最后一位执行
  120. finish(value) {
  121. let params = {
  122. accessToken:this.accessToken,
  123. userId:this.userId,
  124. code:value
  125. };
  126. this.$u.api.phoneLoginAuth(params)
  127. .then(res =>{
  128. if(res.code=='200'){
  129. // console.log('finish res',res);
  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. const openId = this.$store.state.vuex_wxinfo?.openId
  146. if (openId) {
  147. this.jumpIndex()
  148. } else {
  149. this.getCode()
  150. }
  151. },
  152. // 微信已登录则跳转到首页
  153. jumpIndex() {
  154. let ret = localStorage.getItem('backUrl')
  155. if (ret && ret.indexOf('phoneLogin') < 0) {
  156. // 截取url
  157. const pagesIndex = ret.indexOf('pages')
  158. if (pagesIndex > (-1)) {
  159. const pageUrl = ret.slice(pagesIndex)
  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. const local = window.location.href // 获取页面url
  179. let locationLocaturl = window.location.search;
  180. this.code = getUrlParams(locationLocaturl, "code"); // 截取code
  181. if (this.code) { // 如果没有code,则去请求
  182. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  183. } else {
  184. 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`
  185. }
  186. },
  187. // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
  188. handleGetWXInfo (code) {
  189. uni.showLoading({
  190. title: '加载中'
  191. })
  192. this.$u.api.getWXInfo(code).then((res) => {
  193. if (res.code === 200 ) {
  194. this.$u.vuex('vuex_wxinfo', res.data);
  195. this.jumpIndex()
  196. } else {
  197. this.$refs.uToast.show({
  198. title: '获取用户信息失败!',
  199. type: 'error',
  200. });
  201. }
  202. uni.hideLoading()
  203. }).catch((err) => {
  204. this.$refs.uToast.show({
  205. title: '获取用户信息失败!',
  206. type: 'error',
  207. });
  208. })
  209. },
  210. }
  211. };
  212. </script>
  213. <style lang="scss" scoped>
  214. .hide{display: none!important;}
  215. .wrap {
  216. font-size: 28rpx;
  217. .content {
  218. width: 600rpx;
  219. margin: 80rpx auto 0;
  220. .title {
  221. text-align: left;
  222. font-size: 60rpx;
  223. font-weight: 500;
  224. margin-bottom: 100rpx;
  225. }
  226. input {
  227. text-align: left;
  228. margin-bottom: 10rpx;
  229. padding-bottom: 20rpx;
  230. border-bottom: 1px solid #ddd;
  231. }
  232. .getCaptcha {
  233. margin: 45rpx auto 130rpx;
  234. background-color: #a8c6f1;
  235. color: $u-tips-color;
  236. border: none;
  237. font-size: 30rpx;
  238. padding: 12rpx 0;
  239. &::after {
  240. border: none;
  241. }
  242. }
  243. }
  244. .buttom {
  245. .hint {
  246. padding: 20rpx 40rpx;
  247. font-size: 20rpx;
  248. color: $u-tips-color;
  249. .link {
  250. color: $u-type-warning;
  251. }
  252. }
  253. }
  254. .captcha {
  255. color: $u-type-warning;
  256. font-size: 30rpx;
  257. margin-top: 40rpx;
  258. text-align: center;
  259. }
  260. }
  261. </style>