login.vue 14 KB

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