phoneLogin.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <template>
  2. <view class="phonelogin">
  3. <u-navbar title-color="#FFFFFF" :custom-back="customBack" :bpay-bottom="false" back-icon-color="#FFFFFF" :background="{background: 'transparent' }" title="手机登录" class="phonelogin-unavbar" />
  4. <view class="phonelogin-title">
  5. <view>欢迎您!</view>
  6. <view>登录退役军人服务移动端</view>
  7. </view>
  8. <view class="phonelogin-content">
  9. <!-- <input class="u-border-bottom" type="number" maxlength="11" v-model="tel" placeholder="请输入手机号" /> -->
  10. <u-input class="u-border-bottom phonelogin-content-input" v-model="tel" :type="number" maxlength="11" placeholder="请输入手机号" />
  11. <view class="u-text-center u-type-error u-m-t-20" v-if="telError">手机号输入错误</view>
  12. <button @tap="submit" :style="[inputStyle]" class="phonelogin-content-getcaptcha">获取验证码</button>
  13. <u-message-input v-if="show" :focus="true" :value="messageCode" @change="change" @finish="finish" mode="bottomLine" :maxlength="codelength" class="phonelogin-content-messageinput"></u-message-input>
  14. </view>
  15. <!-- <view class="u-text-center u-type-error" v-if="phoneError">手机号输入错误</view> -->
  16. <view class="phonelogin-captcha">
  17. <!-- <text v-if="show&&this.messageDisable==false" @tap="noCaptcha">收不到验证码点这里</text> -->
  18. <text v-if="messageShow">{{ second }}秒后可重新获取验证码</text>
  19. </view>
  20. <view class="phonelogin-pact" :class="messageShow?'phonelogin-msgpact':''">
  21. <view class="phonelogin-pact-hint u-text-center">
  22. 进入即代表同意
  23. <text class="link" @tap="jumpToPage(1)">《用户协议》</text>及
  24. <text class="link" @tap="jumpToPage(2)">《隐私协议》</text>
  25. </view>
  26. </view>
  27. <u-toast ref="uToast" />
  28. </view>
  29. </template>
  30. <script>
  31. import getUrlParams from "utils/getUrlParams.js";
  32. export default {
  33. data() {
  34. return {
  35. tel: '',
  36. messageCode:'',
  37. messageShow: false,
  38. messageDisable: false,
  39. codelength: 4,
  40. show: false,
  41. second:60,
  42. toastMsg:'',
  43. toastUrl:'',
  44. toastType:'',
  45. accessToken:'',
  46. userId:'',
  47. telError:false,
  48. // messageError:false
  49. }
  50. },
  51. onLoad(page) {
  52. // 如果存在code 则认为是微信登录完成后跳转过来的,直接获取信息跳转首页或者指定页面
  53. let locationLocaturl = window.location.search;
  54. this.code = getUrlParams(locationLocaturl, "code"); // 截取code
  55. if (this.code) {
  56. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  57. }
  58. console.log('backUrl',localStorage.getItem('backUrl'))
  59. },
  60. onShow() {
  61. if(this.vuex_hasLogin){
  62. // uni.switchTab({
  63. // url: '../index/index'
  64. // })
  65. }
  66. },
  67. computed: {
  68. inputStyle() {
  69. let style = {};
  70. if(this.tel.length == 11&&this.messageDisable==false&&this.$u.test.mobile(this.tel)) {
  71. style.color = "#fff";
  72. style.backgroundColor = '#5295F5';
  73. this.telError = false;
  74. // style.backgroundColor = this.$u.color['warning'];
  75. }else if(this.tel.length==11&&!this.$u.test.mobile(this.tel)){
  76. this.telError = true;
  77. }
  78. return style;
  79. }
  80. },
  81. methods: {
  82. showToast() {
  83. this.$refs.uToast.show({
  84. title: this.toastMsg,
  85. type: this.toastType,
  86. url: this.toastUrl
  87. })
  88. },
  89. submit() {
  90. if(this.$u.test.mobile(this.tel)&&this.messageDisable==false) {
  91. let that = this;
  92. this.$u.api.getPhoneLoginCode({mobile:this.tel})
  93. .then(res =>{
  94. if(res.code == 200){
  95. this.messageDisable = true;
  96. this.messageShow = true;
  97. this.show = true;
  98. let interval = setInterval(() => {
  99. that.second--;
  100. if (that.second <= 0) {
  101. that.messageDisable=false
  102. that.messageShow = false;
  103. if (that.messageCode.lenth != 4) {
  104. // this.messageError = true;
  105. }
  106. clearInterval(interval);
  107. that.second=60;
  108. }
  109. }, 1000);
  110. this.accessToken = res.data.accessToken;
  111. this.$u.vuex('vuex_token', res.data.accessToken);
  112. this.userId = res.data.userId;
  113. }else{
  114. uni.showToast({
  115. title: res.msg,
  116. icon:'none',
  117. duration: 2000
  118. });
  119. }
  120. }).catch(err=>{
  121. this.toastMsg = err.code + ":" + err.msg;
  122. this.showToast();
  123. });
  124. }
  125. },
  126. // 收不到验证码选择时的选择
  127. // noCaptcha() {
  128. // uni.showActionSheet({
  129. // itemList: ['重新获取验证码', '接听语音验证码'],
  130. // success: function(res) {
  131. // },
  132. // fail: function(res) {
  133. // }
  134. // });
  135. // },
  136. // change事件侦听
  137. change(value) {
  138. // console.log('change', value);
  139. },
  140. // 输入完验证码最后一位执行
  141. finish(value) {
  142. let params = {
  143. accessToken:this.accessToken,
  144. userId:this.userId,
  145. code:value
  146. };
  147. this.$u.api.phoneLoginAuth(params)
  148. .then(res =>{
  149. if(res.code=='200'){
  150. // console.log('finish res',res);
  151. this.$u.vuex('vuex_user', res.data);
  152. this.$u.vuex('vuex_hasLogin', true);
  153. this.veteEducCheck();
  154. // this.wechatLogin()
  155. }else{
  156. this.toastMsg = res.msg;
  157. this.showToast();
  158. }
  159. }).catch(err=>{
  160. this.toastMsg = err.msg;
  161. this.showToast();
  162. });
  163. },
  164. // 微信登录
  165. wechatLogin() {
  166. this.jumpIndex()
  167. // const openId = this.$store.state.vuex_wxinfo?.openId
  168. // if (openId) {
  169. // this.jumpIndex()
  170. // } else {
  171. // this.getCode()
  172. // }
  173. },
  174. veteEducCheck(){
  175. this.$u.api.getVeteEducCheck().then(res=>{
  176. console.log('res',res);
  177. if(res.data!=9){
  178. this.jumpIndex();
  179. }else{
  180. uni.navigateTo({
  181. url: '/pages/authentication/authentication'
  182. })
  183. }
  184. })
  185. },
  186. // 微信已登录则跳转到首页
  187. jumpIndex() {
  188. let ret = localStorage.getItem('backUrl')
  189. if (ret && ret.indexOf('phoneLogin') < 0) {
  190. // 截取url
  191. const pagesIndex = ret.indexOf('pages')
  192. let switchTabList = ['/pages/index/index','pages/center/center','pages/mine/mine'];
  193. if (pagesIndex > (-1)) {
  194. const pageUrl = ret.slice(pagesIndex)
  195. if(switchTabList.includes(pageUrl)){
  196. setTimeout(() => {
  197. uni.switchTab({
  198. url: '/' + pageUrl
  199. })
  200. }, 100)
  201. }else{
  202. setTimeout(() => {
  203. uni.navigateTo({
  204. url: '/' + pageUrl
  205. })
  206. }, 100)
  207. }
  208. } else {
  209. uni.switchTab({
  210. url: '../index/index'
  211. })
  212. }
  213. } else {
  214. uni.switchTab({
  215. url: '../index/index'
  216. })
  217. }
  218. },
  219. // 获取code
  220. getCode () {
  221. const local = window.location.href // 获取页面url
  222. let locationLocaturl = window.location.search;
  223. this.code = getUrlParams(locationLocaturl, "code"); // 截取code
  224. if (this.code) { // 如果没有code,则去请求
  225. this.handleGetWXInfo(this.code) //把code传给后台获取用户信息
  226. } else {
  227. 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`
  228. }
  229. },
  230. // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
  231. handleGetWXInfo (code) {
  232. uni.showLoading({
  233. title: '加载中'
  234. })
  235. this.$u.api.getWXInfo(code).then((res) => {
  236. if (res.code === 200 ) {
  237. this.$u.vuex('vuex_wxinfo', res.data);
  238. this.jumpIndex()
  239. } else {
  240. this.$refs.uToast.show({
  241. title: '获取用户信息失败!',
  242. type: 'error',
  243. });
  244. }
  245. uni.hideLoading()
  246. }).catch((err) => {
  247. this.$refs.uToast.show({
  248. title: '获取用户信息失败!',
  249. type: 'error',
  250. });
  251. })
  252. },
  253. /**
  254. * 跳转页面
  255. * */
  256. jumpToPage(flag){
  257. uni.navigateTo({
  258. url: "/pages/inbuild/inbuild",
  259. // url: "/pages/privacyPolicy/privacyPolicy?termsType=" + flag,
  260. })
  261. }
  262. }
  263. };
  264. </script>
  265. <style lang="scss" scoped>
  266. .hide{display: none!important;}
  267. .phonelogin{
  268. width: 100%;
  269. height: 100vh;
  270. background-image: url(../../static/img/phonelogin-bg-png.png);
  271. background-repeat: no-repeat;
  272. background-size: cover;
  273. &-unavbar{
  274. ::v-deep .u-border-bottom:after {
  275. border-bottom-width: 0 !important;
  276. }
  277. }
  278. &-title{
  279. padding: 10% 4% 6%;
  280. font-family: PingFangSC-Light, PingFang SC;
  281. font-weight: 300;
  282. color: #FFFFFF;
  283. line-height: 98rpx;
  284. letter-spacing: 2rpx;
  285. view:first-child{
  286. font-size: 70rpx;
  287. }
  288. view:last-child{
  289. font-size: 56rpx;
  290. }
  291. }
  292. &-content{
  293. padding: 0 4%;
  294. &-input{
  295. ::v-deep {
  296. .uni-input-placeholder {
  297. text-align: center;
  298. font-size: 40rpx;
  299. font-family: PingFangSC-Heavy, PingFang SC;
  300. color: #FFFFFF !important;
  301. letter-spacing: 1px;
  302. line-height: 60rpx;
  303. opacity: 0.6;
  304. }
  305. .uni-input-input{
  306. text-align: center;
  307. font-size: 60rpx;
  308. font-family: PingFangSC-Heavy, PingFang SC;
  309. font-weight: 800;
  310. color: #FFFFFF;
  311. line-height: 83rpx;
  312. letter-spacing: 1px;
  313. }
  314. }
  315. &:after {
  316. top: 21rpx;
  317. }
  318. }
  319. &-getcaptcha{
  320. margin-top: 10%;
  321. color: $u-tips-color;
  322. border: none;
  323. padding: 14rpx 0;
  324. background-color: rgba(255, 255, 255, 0.5);
  325. border-radius: 55rpx;
  326. font-size: 40rpx;
  327. font-family: PingFangSC-Medium, PingFang SC;
  328. font-weight: 500;
  329. color: #6A5C52;
  330. line-height: 83rpx;
  331. &::after {
  332. border: none;
  333. }
  334. }
  335. &-messageinput{
  336. ::v-deep .u-char-item{
  337. color: #FFFFFF !important;
  338. .u-bottom-line, .u-placeholder-line{
  339. background: #FFFFFF !important;
  340. }
  341. }
  342. }
  343. }
  344. &-captcha{
  345. color: $u-type-warning;
  346. font-size: 30rpx;
  347. margin-top: 40rpx;
  348. text-align: center;
  349. }
  350. &-pact{
  351. width: 100%;
  352. &-hint{
  353. font-size: 28rpx;
  354. font-family: PingFangSC-Regular, PingFang SC;
  355. font-weight: 400;
  356. color: #FFFFFF;
  357. line-height: 83rpx;
  358. opacity: 0.7;
  359. .link {
  360. color: $u-type-warning;
  361. }
  362. }
  363. }
  364. }
  365. </style>