phoneLogin.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. console.log('backUrl',localStorage.getItem('backUrl'))
  55. },
  56. onShow() {
  57. if(this.vuex_hasLogin){
  58. // uni.switchTab({
  59. // url: '../index/index'
  60. // })
  61. }
  62. },
  63. computed: {
  64. inputStyle() {
  65. let style = {};
  66. if(this.tel.length == 11&&this.messageDisable==false&&this.$u.test.mobile(this.tel)) {
  67. style.color = "#fff";
  68. style.backgroundColor = '#5295F5';
  69. this.telError = false;
  70. // style.backgroundColor = this.$u.color['warning'];
  71. }else if(this.tel.length==11&&!this.$u.test.mobile(this.tel)){
  72. this.telError = true;
  73. }
  74. return style;
  75. }
  76. },
  77. methods: {
  78. showToast() {
  79. this.$refs.uToast.show({
  80. title: this.toastMsg,
  81. type: this.toastType,
  82. url: this.toastUrl
  83. })
  84. },
  85. submit() {
  86. if(this.$u.test.mobile(this.tel)&&this.messageDisable==false) {
  87. let that = this;
  88. this.$u.api.getPhoneLoginCode({mobile:this.tel})
  89. .then(res =>{
  90. if(res.code == 200){
  91. this.messageDisable = true;
  92. this.messageShow = true;
  93. this.show = true;
  94. let interval = setInterval(() => {
  95. that.second--;
  96. if (that.second <= 0) {
  97. that.messageDisable=false
  98. that.messageShow = false;
  99. if (that.messageCode.lenth != 4) {
  100. // this.messageError = true;
  101. }
  102. clearInterval(interval);
  103. that.second=60;
  104. }
  105. }, 1000);
  106. this.accessToken = res.data.accessToken;
  107. this.$u.vuex('vuex_token', res.data.accessToken);
  108. this.userId = res.data.userId;
  109. }else{
  110. uni.showToast({
  111. title: res.msg,
  112. icon:'none',
  113. duration: 2000
  114. });
  115. }
  116. }).catch(err=>{
  117. this.toastMsg = err.code + ":" + err.msg;
  118. this.showToast();
  119. });
  120. }
  121. },
  122. // 收不到验证码选择时的选择
  123. // noCaptcha() {
  124. // uni.showActionSheet({
  125. // itemList: ['重新获取验证码', '接听语音验证码'],
  126. // success: function(res) {
  127. // },
  128. // fail: function(res) {
  129. // }
  130. // });
  131. // },
  132. // change事件侦听
  133. change(value) {
  134. // console.log('change', value);
  135. },
  136. // 输入完验证码最后一位执行
  137. finish(value) {
  138. let params = {
  139. accessToken:this.accessToken,
  140. userId:this.userId,
  141. code:value
  142. };
  143. this.$u.api.phoneLoginAuth(params)
  144. .then(res =>{
  145. if(res.code=='200'){
  146. // console.log('finish res',res);
  147. this.$u.vuex('vuex_user', res.data);
  148. this.$u.vuex('vuex_hasLogin', true);
  149. this.veteEducCheck();
  150. // this.wechatLogin()
  151. }else{
  152. this.toastMsg = res.msg;
  153. this.showToast();
  154. }
  155. }).catch(err=>{
  156. this.toastMsg = err.msg;
  157. this.showToast();
  158. });
  159. },
  160. // 微信登录
  161. wechatLogin() {
  162. this.jumpIndex()
  163. // const openId = this.$store.state.vuex_wxinfo?.openId
  164. // if (openId) {
  165. // this.jumpIndex()
  166. // } else {
  167. // this.getCode()
  168. // }
  169. },
  170. veteEducCheck(){
  171. this.$u.api.getVeteEducCheck().then(res=>{
  172. console.log('res',res);
  173. if(res.data!=9){
  174. this.jumpIndex();
  175. }else{
  176. uni.navigateTo({
  177. url: '/pages/authentication/authentication'
  178. })
  179. }
  180. })
  181. },
  182. // 微信已登录则跳转到首页
  183. jumpIndex() {
  184. let ret = localStorage.getItem('backUrl')
  185. if (ret && ret.indexOf('phoneLogin') < 0) {
  186. // 截取url
  187. const pagesIndex = ret.indexOf('pages')
  188. let switchTabList = ['/pages/index/index','pages/center/center','pages/mine/mine'];
  189. if (pagesIndex > (-1)) {
  190. const pageUrl = ret.slice(pagesIndex)
  191. if(switchTabList.includes(pageUrl)){
  192. setTimeout(() => {
  193. uni.switchTab({
  194. url: '/' + pageUrl
  195. })
  196. }, 100)
  197. }else{
  198. setTimeout(() => {
  199. uni.navigateTo({
  200. url: '/' + pageUrl
  201. })
  202. }, 100)
  203. }
  204. } else {
  205. uni.switchTab({
  206. url: '../index/index'
  207. })
  208. }
  209. } else {
  210. uni.switchTab({
  211. url: '../index/index'
  212. })
  213. }
  214. },
  215. // 获取code
  216. getCode () {
  217. const local = window.location.href // 获取页面url
  218. let locationLocaturl = window.location.search;
  219. this.code = getUrlParams(locationLocaturl, "code"); // 截取code
  220. if (this.code) { // 如果没有code,则去请求
  221. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  222. } else {
  223. 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`
  224. }
  225. },
  226. // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
  227. handleGetWXInfo (code) {
  228. uni.showLoading({
  229. title: '加载中'
  230. })
  231. this.$u.api.getWXInfo(code).then((res) => {
  232. if (res.code === 200 ) {
  233. this.$u.vuex('vuex_wxinfo', res.data);
  234. this.jumpIndex()
  235. } else {
  236. this.$refs.uToast.show({
  237. title: '获取用户信息失败!',
  238. type: 'error',
  239. });
  240. }
  241. uni.hideLoading()
  242. }).catch((err) => {
  243. this.$refs.uToast.show({
  244. title: '获取用户信息失败!',
  245. type: 'error',
  246. });
  247. })
  248. },
  249. /**
  250. * 跳转页面
  251. * */
  252. jumpToPage(flag){
  253. uni.navigateTo({
  254. url: "/pages/inbuild/inbuild",
  255. // url: "/pages/privacyPolicy/privacyPolicy?termsType=" + flag,
  256. })
  257. }
  258. }
  259. };
  260. </script>
  261. <style lang="scss" scoped>
  262. .hide{display: none!important;}
  263. .wrap {
  264. font-size: 28rpx;
  265. .content {
  266. width: 600rpx;
  267. margin: 80rpx auto 0;
  268. .title {
  269. text-align: left;
  270. font-size: 60rpx;
  271. font-weight: 500;
  272. margin-bottom: 100rpx;
  273. }
  274. input {
  275. text-align: left;
  276. margin-bottom: 10rpx;
  277. padding-bottom: 20rpx;
  278. border-bottom: 1px solid #ddd;
  279. }
  280. .getCaptcha {
  281. margin: 45rpx auto 130rpx;
  282. background-color: #a8c6f1;
  283. color: $u-tips-color;
  284. border: none;
  285. font-size: 30rpx;
  286. padding: 12rpx 0;
  287. &::after {
  288. border: none;
  289. }
  290. }
  291. }
  292. .buttom {
  293. .hint {
  294. padding: 20rpx 40rpx;
  295. font-size: 20rpx;
  296. color: $u-tips-color;
  297. .link {
  298. color: $u-type-warning;
  299. }
  300. }
  301. }
  302. .captcha {
  303. color: $u-type-warning;
  304. font-size: 30rpx;
  305. margin-top: 40rpx;
  306. text-align: center;
  307. }
  308. }
  309. </style>