phoneLogin.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. console.log('getcode res',res);
  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. console.log('err',err)
  102. this.toastMsg = err.code + ":" + err.msg;
  103. this.showToast();
  104. console.log('getcode err',err)
  105. });
  106. }
  107. },
  108. // 收不到验证码选择时的选择
  109. // noCaptcha() {
  110. // uni.showActionSheet({
  111. // itemList: ['重新获取验证码', '接听语音验证码'],
  112. // success: function(res) {
  113. // },
  114. // fail: function(res) {
  115. // }
  116. // });
  117. // },
  118. // change事件侦听
  119. change(value) {
  120. // console.log('change', value);
  121. },
  122. // 输入完验证码最后一位执行
  123. finish(value) {
  124. let params = {
  125. accessToken:this.accessToken,
  126. userId:this.userId,
  127. code:value
  128. };
  129. this.$u.api.phoneLoginAuth(params)
  130. .then(res =>{
  131. if(res.code=='200'){
  132. // console.log('finish res',res);
  133. this.$u.vuex('vuex_user', res.data);
  134. this.$u.vuex('vuex_hasLogin', true);
  135. this.wechatLogin()
  136. }else{
  137. this.toastMsg = res.msg;
  138. this.showToast();
  139. }
  140. }).catch(err=>{
  141. this.toastMsg = err.msg;
  142. this.showToast();
  143. });
  144. },
  145. // 微信登录
  146. wechatLogin() {
  147. // this.jumpIndex()
  148. const openId = this.$store.state.vuex_wxinfo?.openId
  149. if (openId) {
  150. this.jumpIndex()
  151. } else {
  152. this.getCode()
  153. }
  154. },
  155. // 微信已登录则跳转到首页
  156. jumpIndex() {
  157. let ret = localStorage.getItem('backUrl')
  158. if (ret && ret.indexOf('phoneLogin') < 0) {
  159. // 截取url
  160. const pagesIndex = ret.indexOf('pages')
  161. if (pagesIndex > (-1)) {
  162. const pageUrl = ret.slice(pagesIndex)
  163. setTimeout(() => {
  164. uni.navigateTo({
  165. url: '/' + pageUrl
  166. })
  167. }, 100)
  168. } else {
  169. uni.switchTab({
  170. url: '../../index/index'
  171. })
  172. }
  173. } else {
  174. uni.switchTab({
  175. url: '../../index/index'
  176. })
  177. }
  178. },
  179. // 获取code
  180. getCode () {
  181. const local = window.location.href // 获取页面url
  182. let locationLocaturl = window.location.search;
  183. this.code = getUrlParams(locationLocaturl, "code"); // 截取code
  184. if (this.code) { // 如果没有code,则去请求
  185. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  186. } else {
  187. 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`
  188. }
  189. },
  190. // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
  191. handleGetWXInfo (code) {
  192. uni.showLoading({
  193. title: '加载中'
  194. })
  195. this.$u.api.getWXInfo(code).then((res) => {
  196. if (res.code === 200 ) {
  197. this.$u.vuex('vuex_wxinfo', res.data);
  198. this.jumpIndex()
  199. } else {
  200. this.$refs.uToast.show({
  201. title: '获取用户信息失败!',
  202. type: 'error',
  203. });
  204. }
  205. uni.hideLoading()
  206. }).catch((err) => {
  207. this.$refs.uToast.show({
  208. title: '获取用户信息失败!',
  209. type: 'error',
  210. });
  211. })
  212. },
  213. }
  214. };
  215. </script>
  216. <style lang="scss" scoped>
  217. .hide{display: none!important;}
  218. .wrap {
  219. font-size: 28rpx;
  220. .content {
  221. width: 600rpx;
  222. margin: 80rpx auto 0;
  223. .title {
  224. text-align: left;
  225. font-size: 60rpx;
  226. font-weight: 500;
  227. margin-bottom: 100rpx;
  228. }
  229. input {
  230. text-align: left;
  231. margin-bottom: 10rpx;
  232. padding-bottom: 20rpx;
  233. border-bottom: 1px solid #ddd;
  234. }
  235. .getCaptcha {
  236. margin: 45rpx auto 130rpx;
  237. background-color: #a8c6f1;
  238. color: $u-tips-color;
  239. border: none;
  240. font-size: 30rpx;
  241. padding: 12rpx 0;
  242. &::after {
  243. border: none;
  244. }
  245. }
  246. }
  247. .buttom {
  248. .hint {
  249. padding: 20rpx 40rpx;
  250. font-size: 20rpx;
  251. color: $u-tips-color;
  252. .link {
  253. color: $u-type-warning;
  254. }
  255. }
  256. }
  257. .captcha {
  258. color: $u-type-warning;
  259. font-size: 30rpx;
  260. margin-top: 40rpx;
  261. text-align: center;
  262. }
  263. }
  264. </style>