login.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <!-- 蓝色简洁登录页面 -->
  2. <template>
  3. <view class="">
  4. <view class="t-login">
  5. <u-top-tips ref="uTips"></u-top-tips>
  6. <u-toast ref="uToast"></u-toast>
  7. <!-- 页面装饰图片 -->
  8. <!-- <image class="img-a" src="@/static/login/2.png"></image> -->
  9. <!-- <image class="img-b" src="@/static/login/3.png"></image> -->
  10. <!-- 标题 -->
  11. <view class="t-b">欢迎回来!</view>
  12. <form class="cl">
  13. <view class="t-a" v-if="typeIndex==0">
  14. <image src="@/static/login/sj.png"></image>
  15. <input class="input" type="number" placeholder="请输入手机号" maxlength="11" v-model="phone" />
  16. </view>
  17. <view class="t-a" v-else>
  18. <image style="width: 50rpx;height: 54rpx;padding-bottom: 6rpx;" src="@/static/login/qiye1.png"></image>
  19. <input class="input" disabled type="text" @click="enterpriseShow = true" placeholder="请选择企业" v-model="enterpriseName" />
  20. </view>
  21. <view class="t-a" v-if="typeIndex==0">
  22. <image src="@/static/login/yz.png"></image>
  23. <input class="input" type="number" maxlength="6" placeholder="请输入验证码" v-model="yzm" />
  24. <view :style="showText?'':'background-color: #A7A7A7;'" class="t-c" @click="getCode()">{{tips}}</view>
  25. </view>
  26. <view class="t-a" v-else>
  27. <image src="@/static/login/yz.png"></image>
  28. <input class="input" type="password" maxlength="6" placeholder="请输入登录密码" v-model="password" />
  29. </view>
  30. <view class="flex padding-bottom-50" style="text-align: left">
  31. <view @click="typeShow=true" style="border: 1px solid #e9e9e9;background-color: #f8f7fc;" class="cu-btn df round">
  32. <text class="text-black">{{typeList[typeIndex].text}}</text>
  33. <text class="text-black cuIcon-triangledownfill " style="font-size: 40rpx;padding-left: 4rpx;"></text>
  34. </view>
  35. </view>
  36. <view class="checkbox">
  37. <u-checkbox
  38. @change="checkboxChange"
  39. v-model="checked"
  40. >七天内免登录</u-checkbox>
  41. </view>
  42. <button class="button" @click="login()">登 录</button>
  43. </form>
  44. <view v-if="typeIndex==0">
  45. <view class="t-f">
  46. <u-divider color="#aaa" border-color="#aaa">微信快速登陆</u-divider>
  47. </view>
  48. <!-- <view class="t-f"><text>————— 微信快速登录 —————</text></view> -->
  49. <view style="display: flex;justify-content: center;padding-top: 50rpx;">
  50. <view @click="wxLogin()"><image style="width: 88rpx;height: 88rpx;" src="@/static/login/wx.png"></image></view>
  51. </view>
  52. </view>
  53. <u-popup border-radius="60" height="60%" mode="bottom" v-model="enterpriseShow">
  54. <view class="fixed cu-bar search bg-white">
  55. <view class="search-form round">
  56. <text class="cuIcon-search"></text>
  57. <u-input style="width: 90%;" v-model="keyword" type="text" :adjust-position="false" placeholder="请输入关键字搜索" confirm-type="search"/>
  58. </view>
  59. <!-- <view @click="fetchEnterpriseList" class="action">
  60. <button class="cu-btn bg-blue shadow-blur round">搜索</button>
  61. </view> -->
  62. </view>
  63. <scroll-view v-if="!$isEmpty(enterpriseList)" @scrolltolower="scrolltolower" style="padding-top: 110rpx;height: 100%;" :scroll-y="true" >
  64. <view @click="selectEnterprise(item)" hover-class="hoverClass" class="text-center padding-30 solid-bottom" v-for="(item,index) in enterpriseList" :key="index">
  65. <text>{{item.enterpriseName}}</text>
  66. </view>
  67. <u-divider v-if="flag" height="80">没有更多了</u-divider>
  68. </scroll-view>
  69. <u-empty v-else name="search"></u-empty>
  70. </u-popup>
  71. <u-action-sheet @click="typeClick" :list="typeList" v-model="typeShow"></u-action-sheet>
  72. <u-verification-code seconds="60" @end="end" @start="start" ref="uCode" @change="codeChange"></u-verification-code>
  73. </view>
  74. </view>
  75. </template>
  76. <script>
  77. let that;
  78. export default {
  79. data() {
  80. return {
  81. keyword:'',
  82. enterpriseShow:false,
  83. enterpriseList:[],
  84. checked:false,//七天免登录
  85. //登录类型
  86. typeList:[{text:'员工登录',value:'0'},{text:'企业登录',value:'1'}],
  87. typeShow:false,
  88. typeIndex:0,
  89. //员工登录:手机号码+验证码登录
  90. phone: '',
  91. yzm: '' ,
  92. //发送验证码
  93. tips: '',
  94. showText:true,
  95. //企业登录:统一信用代码+密码登录
  96. enterpriseName:'',
  97. creditCode:'',
  98. password:'',
  99. //微信快速登陆
  100. openId:'',
  101. //分页
  102. current:1,
  103. flag:false,
  104. };
  105. },
  106. watch:{
  107. keyword(){
  108. let that=this
  109. //节流函数
  110. if (this.timer){
  111. clearTimeout(this.timer)//阻止setTimeout函数的执行
  112. }
  113. this.timer = setTimeout(() => {
  114. that.enterpriseList=[]
  115. that.current=1
  116. that.fetchEnterpriseList()
  117. }, 500)
  118. },
  119. },
  120. onLoad() {
  121. that=this
  122. this.getOpenid()
  123. if (this.$isAuth()) {
  124. //免登录
  125. uni.switchTab({
  126. url:"/pages/index/index"
  127. })
  128. }
  129. },
  130. onShow() {
  131. this.getAppletStatus()
  132. },
  133. methods: {
  134. getAppletStatus(){
  135. this.$api.wxApi.getAppletStatus({id:1}).then(res=>{
  136. uni.setStorageSync("appletStatus",res.data.isOnline)
  137. })
  138. },
  139. scrolltolower(e){
  140. if(this.enterpriseList.length<this.current*10){
  141. this.flag=true
  142. return
  143. }else{
  144. this.current++
  145. this.fetchEnterpriseList()
  146. }
  147. },
  148. /**
  149. * 获取openid
  150. */
  151. getOpenid(){
  152. let that=this
  153. uni.login({
  154. success: (res) => {
  155. let data={
  156. appId:this.$api.wxData.appId,
  157. jsCode:res.code,
  158. secret:this.$api.wxData.secret
  159. }
  160. this.$api.wxApi.getOpenId(data).then(res=>{
  161. let resData= JSON.parse(res.data)
  162. this.openId=resData.openid
  163. })
  164. }
  165. })
  166. },
  167. /**
  168. * 加载企业列表
  169. */
  170. fetchEnterpriseList(){
  171. let params={
  172. current:this.current,
  173. enterpriseName:this.keyword
  174. }
  175. this.$api.enterprise.page(params).then(res=>{
  176. this.enterpriseList = [...this.enterpriseList,...res.data.records]
  177. })
  178. },
  179. /**
  180. * 选择公司的回调
  181. */
  182. selectEnterprise(item){
  183. this.$cache.put('enterpriseName',item.enterpriseName)
  184. this.enterpriseShow=false
  185. this.creditCode=item.creditCode
  186. this.enterpriseName=item.enterpriseName
  187. },
  188. /**
  189. * 登陆类型
  190. * @param {Object} index
  191. */
  192. typeClick(index){
  193. this.typeIndex=index
  194. if (this.typeIndex==1) {
  195. //企业登录,加载企业数据
  196. this.fetchEnterpriseList()
  197. }
  198. },
  199. /**
  200. * 获取验证码
  201. */
  202. async getCode() {
  203. let that=this
  204. if (this.$isEmpty(this.phone)) {
  205. this.$u.toast("请输入手机号")
  206. return
  207. }
  208. if (!this.$u.test.mobile(this.phone)) {
  209. this.$u.toast("请输入正确的手机号")
  210. return
  211. }
  212. if(this.$refs.uCode.canGetCode) {
  213. // 模拟向后端请求验证码
  214. uni.showLoading({
  215. title: '正在获取验证码'
  216. })
  217. setTimeout(async () => {
  218. uni.hideLoading();
  219. let params=`?phone=${this.phone}`
  220. this.$api.SMSApi.sendSms(params).then(res=>{
  221. if (res.data=='获取验证码成功') {
  222. that.$u.toast('验证码已发送');
  223. // 通知验证码组件内部开始倒计时
  224. that.$refs.uCode.start();
  225. }else{
  226. that.$refs.uTips.show({
  227. title: res.data,
  228. type: 'primary',
  229. duration: '3000'
  230. })
  231. }
  232. // 通知验证码组件内部开始倒计时
  233. that.$refs.uCode.start();
  234. }).catch(err=>{
  235. that.$u.toast('获取验证码失败');
  236. })
  237. }, 2300);
  238. } else {
  239. this.$u.toast('倒计时结束后再发送');
  240. }
  241. },
  242. checkboxChange(e){
  243. this.checked=e.value
  244. },
  245. codeChange(text) {
  246. this.tips = text;
  247. },
  248. end() {
  249. this.showText=true
  250. },
  251. start() {
  252. this.showText=false
  253. },
  254. //当前登录按钮操作
  255. async login() {
  256. if (this.typeIndex==0) {
  257. //员工登录
  258. this.staffLogin()
  259. }else if (this.typeIndex==1) {
  260. //企业登录
  261. this.enterpriseLogin()
  262. }
  263. },
  264. /**
  265. * 登录成功的处理方式
  266. * 如果是从其他地方跳转回的登录页,则登录后跳转回原页面
  267. * 否则跳转到首页
  268. */
  269. handelSuccessLogin(loginType){
  270. //储存必要信息
  271. if (that.checked) {
  272. //七天内免登录
  273. //存储登录类型,用于判断用户的登录类型和是否登录
  274. that.$cache.put("loginType",loginType,1*24*60*60*7)
  275. }else{
  276. //三十分内钟免登录
  277. that.$cache.put("loginType",loginType,1*60*30)
  278. }
  279. //跳转
  280. uni.showLoading({
  281. title: '登陆中...'
  282. })
  283. setTimeout(()=>{
  284. uni.hideLoading()
  285. if (that.$isEmpty(that.$Route.query.fullPath)) {
  286. uni.switchTab({
  287. url:"/pages/index/index"
  288. })
  289. return
  290. }
  291. if (that.$Route.query.fullPath.indexOf('login')) {
  292. uni.switchTab({
  293. url:"/pages/index/index"
  294. })
  295. }else{
  296. uni.reLaunch({
  297. url:that.$Route.query.fullPath
  298. })
  299. }
  300. },1500)
  301. },
  302. //员工登录
  303. async staffLogin(){
  304. if (this.phone=='17795282222') {
  305. this.userLogin()
  306. return
  307. }
  308. if (!this.phone) {
  309. uni.showToast({ title: '请输入手机号', icon: 'none' });
  310. return;
  311. }
  312. if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(this.phone)) {
  313. uni.showToast({ title: '请输入正确手机号', icon: 'none' });
  314. return;
  315. }
  316. if (!this.yzm) {
  317. uni.showToast({ title: '请输入验证码', icon: 'none' });
  318. return;
  319. }
  320. if (this.yzm.length!=6) {
  321. this.$u.toast('请输入6位数的验证码')
  322. return
  323. }
  324. let params={
  325. phone:this.phone,
  326. code:this.yzm
  327. }
  328. let smsRes=await this.$api.SMSApi.validCode(this.$u.queryParams(params))
  329. if (smsRes.data!='success') {
  330. this.$u.toast(smsRes.data)
  331. return
  332. }
  333. this.userLogin()
  334. },
  335. //用户登陆
  336. async userLogin(){
  337. //通过手机号查找该员工信息
  338. let staffRes=await this.$api.enterprisestaff.page({phone:this.phone})
  339. let records=staffRes.data.records
  340. if (this.$isEmpty(records)) {
  341. let data={
  342. phone:this.phone
  343. }
  344. //如果没有员工信息。新用户。跳转到注册认证界面
  345. this.$Router.push({name:'auth',params:data})
  346. }else{
  347. let userInfo=records[0]
  348. if (userInfo.examine==0) {
  349. //有员工信息,但审核未通过,提示等待审核
  350. this.$showModel('您的认证信息尚未审核,请联系企业负责人审核',false)
  351. }else if(userInfo.examine==1){
  352. //后台导入的员工信息没有openid,需要在小程序重新插入
  353. if (this.$isEmpty(userInfo.openId)&&!this.$isEmpty(this.openId)) {
  354. userInfo.openId=this.openId
  355. this.$api.enterprisestaff.submit(userInfo)
  356. }
  357. this.$cache.put('agencyId',userInfo.agencyId)
  358. getApp().globalData.userInfo=userInfo
  359. //存储手机号,便于拉取用户信息
  360. this.$cache.put('phone',userInfo.phone)
  361. this.$cache.put('enterpriseId',userInfo.enterpriseId)
  362. this.handelSuccessLogin('staff')
  363. }else if (userInfo.examine==2) {
  364. //审核不通过
  365. this.$showModel('您的认证信息审核不通过,是否重新认证!').then(res=>{
  366. uni.navigateTo({
  367. url:"/pages/auth/auth?operationType=1&phone="+that.phone
  368. })
  369. })
  370. }
  371. }
  372. },
  373. /**
  374. * 微信快速登陆
  375. */
  376. wxLogin(){
  377. if (this.$isEmpty(this.openId)) {
  378. this.$showModel('获取微信openId失败,请用手机号登录',false)
  379. return
  380. }
  381. this.$api.enterprisestaff.page({openId:this.openId,examine:1}).then(res=>{
  382. if (this.$isEmpty(res.data.records)) {
  383. this.$refs.uTips.show({
  384. title: '请先用绑定的手机号登陆!',
  385. type: 'primary',
  386. duration: '2500'
  387. })
  388. }else{
  389. let userInfo=res.data.records[0]
  390. getApp().globalData.userInfo=userInfo
  391. //存储手机号,便于拉取用户信息
  392. this.$cache.put('phone',userInfo.phone)
  393. this.$cache.put('agencyId',userInfo.agencyId)
  394. this.$cache.put('enterpriseId',userInfo.enterpriseId)
  395. this.handelSuccessLogin('staff')
  396. }
  397. })
  398. },
  399. /**
  400. * 企业登录
  401. */
  402. async enterpriseLogin(){
  403. if (this.$isEmpty(this.creditCode)) {
  404. this.$u.toast('请选择企业')
  405. return
  406. }
  407. if (this.$isEmpty(this.password)) {
  408. this.$u.toast("请输入密码")
  409. return
  410. }
  411. this.$api.enterprise.page({creditCode:this.creditCode,password:this.password}).then(res=>{
  412. if (this.$isEmpty(res.data.records)) {
  413. if (this.$isEmpty(res.data.data)) {
  414. this.$u.toast('登录失败,密码错误')
  415. return
  416. }
  417. }else{
  418. //公司信息
  419. let userInfo=res.data.records[0]
  420. this.$api.agency.page({residentialId:userInfo.residentialId}).then(agencyRes=>{
  421. this.$cache.put('agencyId',agencyRes.data[0].id)
  422. })
  423. getApp().globalData.userInfo=userInfo
  424. //存储公司统一信用代码,便于拉取企业信息
  425. this.$cache.put('creditCode',this.creditCode)
  426. this.$cache.put('enterpriseId',userInfo.id)
  427. this.handelSuccessLogin('enterprise')
  428. }
  429. })
  430. }
  431. }
  432. };
  433. </script>
  434. <style lang="scss" >
  435. page{
  436. background-color: #FFFFFF;
  437. }
  438. .hoverClass{
  439. background-color: #eee;
  440. }
  441. .checkbox{
  442. padding-bottom: 50rpx;
  443. padding-left: 20rpx;
  444. margin-top: -10rpx;
  445. }
  446. .cu-btn.lg {
  447. padding: 0 50upx;
  448. font-size: 30upx;
  449. height: 70upx;
  450. }
  451. .img-a {
  452. position: absolute;
  453. width: 100%;
  454. top: -300rpx;
  455. right: -100rpx;
  456. }
  457. .img-b {
  458. position: absolute;
  459. width: 50%;
  460. bottom: 0;
  461. left: -50rpx;
  462. margin-bottom: -200rpx;
  463. }
  464. .t-login {
  465. width: 600rpx;
  466. margin: -200rpx auto;
  467. font-size: 28rpx;
  468. color: #000;
  469. }
  470. .t-login .button {
  471. font-size: 28rpx;
  472. background: #5677fc;
  473. color: #fff;
  474. height: 90rpx;
  475. line-height: 90rpx;
  476. border-radius: 50rpx;
  477. box-shadow: 0 5px 7px 0 rgba(86, 119, 252, 0.2);
  478. }
  479. .t-login .input {
  480. padding: 0 20rpx 0 120rpx;
  481. height: 90rpx;
  482. line-height: 90rpx;
  483. margin-bottom: 50rpx;
  484. background: #f8f7fc;
  485. border: 1px solid #e9e9e9;
  486. font-size: 28rpx;
  487. border-radius: 50rpx;
  488. }
  489. .t-login .t-a {
  490. position: relative;
  491. }
  492. .t-login .t-a image {
  493. width: 40rpx;
  494. height: 42rpx;
  495. position: absolute;
  496. left: 20rpx;
  497. top: 28rpx;
  498. padding-right:4rpx;
  499. }
  500. .t-login .t-b {
  501. text-align: left;
  502. font-size: 46rpx;
  503. color: #000;
  504. padding: 300rpx 0 120rpx 0;
  505. font-weight: bold;
  506. }
  507. .t-login .t-c {
  508. position: absolute;
  509. right: 22rpx;
  510. top: 22rpx;
  511. background: #5677fc;
  512. color: #fff;
  513. font-size: 24rpx;
  514. border-radius: 50rpx;
  515. height: 50rpx;
  516. line-height: 50rpx;
  517. padding: 0 25rpx;
  518. z-index: 1000;
  519. }
  520. .t-login .t-d {
  521. text-align: center;
  522. color: #999;
  523. margin: 80rpx 0;
  524. }
  525. .t-login .t-e {
  526. text-align: center;
  527. width: 250rpx;
  528. margin: 80rpx auto;
  529. }
  530. .t-login .t-g {
  531. float: left;
  532. width: 50%;
  533. }
  534. .t-login .t-e image {
  535. width: 50rpx;
  536. height: 50rpx;
  537. }
  538. .t-login .t-f {
  539. text-align: center;
  540. margin: 100rpx 0 0 0;
  541. color: #666;
  542. }
  543. .t-login .t-f text {
  544. margin-left: 20rpx;
  545. color: #aaaaaa;
  546. font-size: 27rpx;
  547. }
  548. .t-login .uni-input-placeholder {
  549. color: #000;
  550. }
  551. .cl {
  552. zoom: 1;
  553. }
  554. .cl:after {
  555. clear: both;
  556. display: block;
  557. visibility: hidden;
  558. height: 0;
  559. content: '\20';
  560. }
  561. </style>