phoneLogin.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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" @tap="jumpToPage(1)">用户服务条款,</text>
  19. <text class="link" @tap="jumpToPage(2)">隐私政策,</text>
  20. 并授权使用您的账号信息(如昵称、头像、收货地址)以便您统一管理
  21. </view>
  22. </view>
  23. <u-toast ref="uToast" />
  24. </view>
  25. </template>
  26. <script>
  27. import getUrlParams from "./../../../utils/getUrlParams.js";
  28. export default {
  29. data() {
  30. return {
  31. tel: '',
  32. messageCode:'',
  33. messageShow: false,
  34. messageDisable: false,
  35. codelength: 4,
  36. show: false,
  37. second:60,
  38. toastMsg:'',
  39. toastUrl:'',
  40. toastType:'',
  41. accessToken:'',
  42. userId:'',
  43. telError:false,
  44. // messageError:false
  45. }
  46. },
  47. onLoad(page) {
  48. // 如果存在code 则认为是微信登录完成后跳转过来的,直接获取信息跳转首页或者指定页面
  49. let locationLocaturl = window.location.search;
  50. this.code = getUrlParams(locationLocaturl, "code"); // 截取code
  51. if (this.code) {
  52. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  53. }
  54. },
  55. computed: {
  56. inputStyle() {
  57. let style = {};
  58. if(this.tel.length == 11&&this.messageDisable==false&&this.$u.test.mobile(this.tel)) {
  59. style.color = "#fff";
  60. style.backgroundColor = '#5295F5';
  61. this.telError = false;
  62. // style.backgroundColor = this.$u.color['warning'];
  63. }else if(this.tel.length==11&&!this.$u.test.mobile(this.tel)){
  64. this.telError = true;
  65. }
  66. return style;
  67. }
  68. },
  69. methods: {
  70. showToast() {
  71. this.$refs.uToast.show({
  72. title: this.toastMsg,
  73. type: this.toastType,
  74. url: this.toastUrl
  75. })
  76. },
  77. submit() {
  78. if(this.$u.test.mobile(this.tel)&&this.messageDisable==false) {
  79. let that = this;
  80. this.$u.api.getPhoneLoginCode({mobile:this.tel})
  81. .then(res =>{
  82. this.messageDisable = true;
  83. this.messageShow = true;
  84. this.show = true;
  85. let interval = setInterval(() => {
  86. that.second--;
  87. if (that.second <= 0) {
  88. that.messageDisable=false
  89. that.messageShow = false;
  90. if (that.messageCode.lenth != 4) {
  91. // this.messageError = true;
  92. }
  93. clearInterval(interval);
  94. that.second=60;
  95. }
  96. }, 1000);
  97. this.accessToken = res.data.accessToken;
  98. this.$u.vuex('vuex_token', res.data.accessToken);
  99. this.userId = res.data.userId;
  100. }).catch(err=>{
  101. this.toastMsg = err.code + ":" + err.msg;
  102. this.showToast();
  103. });
  104. }
  105. },
  106. // 收不到验证码选择时的选择
  107. // noCaptcha() {
  108. // uni.showActionSheet({
  109. // itemList: ['重新获取验证码', '接听语音验证码'],
  110. // success: function(res) {
  111. // },
  112. // fail: function(res) {
  113. // }
  114. // });
  115. // },
  116. // change事件侦听
  117. change(value) {
  118. // console.log('change', value);
  119. },
  120. // 输入完验证码最后一位执行
  121. finish(value) {
  122. let params = {
  123. accessToken:this.accessToken,
  124. userId:this.userId,
  125. code:value
  126. };
  127. this.$u.api.phoneLoginAuth(params)
  128. .then(res =>{
  129. if(res.code=='200'){
  130. // console.log('finish res',res);
  131. this.$u.vuex('vuex_user', res.data);
  132. this.$u.vuex('vuex_hasLogin', true);
  133. this.wechatLogin()
  134. }else{
  135. this.toastMsg = res.msg;
  136. this.showToast();
  137. }
  138. }).catch(err=>{
  139. this.toastMsg = err.msg;
  140. this.showToast();
  141. });
  142. },
  143. // 微信登录
  144. wechatLogin() {
  145. this.jumpIndex()
  146. // const openId = this.$store.state.vuex_wxinfo?.openId
  147. // if (openId) {
  148. // this.jumpIndex()
  149. // } else {
  150. // this.getCode()
  151. // }
  152. },
  153. // 微信已登录则跳转到首页
  154. jumpIndex() {
  155. let ret = localStorage.getItem('backUrl')
  156. if (ret && ret.indexOf('phoneLogin') < 0) {
  157. // 截取url
  158. const pagesIndex = ret.indexOf('pages')
  159. if (pagesIndex > (-1)) {
  160. const pageUrl = ret.slice(pagesIndex)
  161. setTimeout(() => {
  162. uni.navigateTo({
  163. url: '/' + pageUrl
  164. })
  165. }, 100)
  166. } else {
  167. uni.switchTab({
  168. url: '../../index/index'
  169. })
  170. }
  171. } else {
  172. uni.switchTab({
  173. url: '../../index/index'
  174. })
  175. }
  176. },
  177. // 获取code
  178. getCode () {
  179. const local = window.location.href // 获取页面url
  180. let locationLocaturl = window.location.search;
  181. this.code = getUrlParams(locationLocaturl, "code"); // 截取code
  182. if (this.code) { // 如果没有code,则去请求
  183. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  184. } else {
  185. 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`
  186. }
  187. },
  188. // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
  189. handleGetWXInfo (code) {
  190. uni.showLoading({
  191. title: '加载中'
  192. })
  193. this.$u.api.getWXInfo(code).then((res) => {
  194. if (res.code === 200 ) {
  195. this.$u.vuex('vuex_wxinfo', res.data);
  196. this.jumpIndex()
  197. } else {
  198. this.$refs.uToast.show({
  199. title: '获取用户信息失败!',
  200. type: 'error',
  201. });
  202. }
  203. uni.hideLoading()
  204. }).catch((err) => {
  205. this.$refs.uToast.show({
  206. title: '获取用户信息失败!',
  207. type: 'error',
  208. });
  209. })
  210. },
  211. /**
  212. * 跳转页面
  213. * */
  214. jumpToPage(flag){
  215. uni.navigateTo({
  216. url: "/pages/privacyPolicy/privacyPolicy?termsType=" + flag,
  217. })
  218. }
  219. }
  220. };
  221. </script>
  222. <style lang="scss" scoped>
  223. .hide{display: none!important;}
  224. .wrap {
  225. font-size: 28rpx;
  226. .content {
  227. width: 600rpx;
  228. margin: 80rpx auto 0;
  229. .title {
  230. text-align: left;
  231. font-size: 60rpx;
  232. font-weight: 500;
  233. margin-bottom: 100rpx;
  234. }
  235. input {
  236. text-align: left;
  237. margin-bottom: 10rpx;
  238. padding-bottom: 20rpx;
  239. border-bottom: 1px solid #ddd;
  240. }
  241. .getCaptcha {
  242. margin: 45rpx auto 130rpx;
  243. background-color: #a8c6f1;
  244. color: $u-tips-color;
  245. border: none;
  246. font-size: 30rpx;
  247. padding: 12rpx 0;
  248. &::after {
  249. border: none;
  250. }
  251. }
  252. }
  253. .buttom {
  254. .hint {
  255. padding: 20rpx 40rpx;
  256. font-size: 20rpx;
  257. color: $u-tips-color;
  258. .link {
  259. color: $u-type-warning;
  260. }
  261. }
  262. }
  263. .captcha {
  264. color: $u-type-warning;
  265. font-size: 30rpx;
  266. margin-top: 40rpx;
  267. text-align: center;
  268. }
  269. }
  270. </style>