login.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <template>
  2. <view class="body" :style="{height:screenHeight}">
  3. <!-- <image class="login-bg" :src="staticUrl+'/img/login-bg.png'" mode="widthFix"></image> -->
  4. <u-toast ref="uToast"></u-toast>
  5. <view style="height: 40%;position: relative;z-index: 10;">
  6. <view class="logo-wrap">
  7. <img :src="staticUrl+'/img/logo.png'" class="logo" alt="">
  8. <!-- <img :src="sitename" class="sitename" alt=""> -->
  9. <!-- <u--image :src="logoSrc" width="200rpx" height="200rpx"></u--image>
  10. <u--image :src="sitename" width="460rpx" height="142rpx"></u--image> -->
  11. </view>
  12. <!-- <u-button text="getUserProfile" type="success" :hairline="false" throttleTime="2000" shape="circle" open-type="getUserProfile"
  13. @click="getUserProfile"></u-button> -->
  14. <!-- <u-button v-if="loginBtn" text="手机号快捷登录" type="success" :hairline="false" throttleTime="2000" shape="circle"
  15. @click="openAuth"></u-button> -->
  16. <view class="btn-wrap" style="margin:94rpx">
  17. <!-- #ifdef MP -->
  18. <u-button
  19. v-if="loginBtn&&checked.length>0"
  20. :hair-line='false'
  21. type="error"
  22. color="#ED0000"
  23. throttleTime="2000"
  24. shape="circle"
  25. open-type="getPhoneNumber"
  26. @getphonenumber="authPhone">登录
  27. </u-button>
  28. <u-button
  29. v-else
  30. :hair-line='false'
  31. type="error"
  32. color="#ED0000"
  33. @click="disabledClick"
  34. shape="circle">登录
  35. </u-button>
  36. <!-- #endif -->
  37. <!-- #ifdef H5 -->
  38. <u-button
  39. :hair-line='false'
  40. type="error"
  41. color="#ED0000"
  42. @click="disabledClick"
  43. shape="circle">登录
  44. </u-button>
  45. <!-- #endif -->
  46. </view>
  47. <view class="rule-wrap u-flex u-flex-wrap u-row-center">
  48. <u-checkbox-group v-model="checked" @change="checkboxChange" placement="row">
  49. <u-checkbox activeColor="#1677FF" name="同意" labelSize="24rpx" shape="circle" label="我已阅读并同意"></u-checkbox>
  50. </u-checkbox-group>
  51. <text class="link" @click="$u.route('/pages/login/regulation',{regulationName:'用户服务协议',type:1})">《用户服务协议》</text>
  52. 和<text class="link" @click="$u.route('/pages/login/regulation',{regulationName:'用户隐私政策',type:2})">《用户隐私政策》</text>
  53. </view>
  54. </view>
  55. <!-- 微信获取手机号弹窗 -->
  56. <!-- <u-modal :show="showAuthorizePhone" :show-title="false"
  57. :show-confirm-button="false">
  58. <view class="slot-content">
  59. <div class="auth-card">
  60. <div class="img">
  61. <img class="avatar-img" :src="logoSrc" mode="widthFix">
  62. </div>
  63. <div class="title">{{bname}}</div>
  64. <div class="content">申请获得您的手机号</div>
  65. </div>
  66. <div class="auth-btncard u-flex u-row-between">
  67. <div class="btn-unok"><u-button :hair-line='false' :custom-style="customStyleUnOk" @click="showAuthorizePhone=false">拒绝</u-button></div>
  68. <div class="btn-ok"><u-button :hair-line='false' :custom-style="customStyleOk" open-type="getPhoneNumber" @getphonenumber="authPhone"> 允许</u-button></div>
  69. </div>
  70. </view>
  71. </u-modal> -->
  72. <!-- 微信获取用户头像昵称弹窗 -->
  73. <!-- <u-modal :show="showAuthorizeUser" :show-title="false"
  74. :show-confirm-button="false">
  75. <view class="slot-content">
  76. <div class="auth-card">
  77. <div class="img">
  78. <img class="avatar-img"
  79. :src="logoSrc"
  80. mode="widthFix">
  81. </div>
  82. <div class="title">{{bname}}</div>
  83. <div class="content">邀请您补全个人信息<br></br>(昵称、头像)</div>
  84. <div style="margin-left: 100rpx;margin-right: 100rpx">
  85. <u-form :model="user" ref="uForm">
  86. <u-form-item label="头像">
  87. <button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar" slot="right">
  88. <image class="avatar" :src="user.avatar"></image>
  89. </button>
  90. </u-form-item>
  91. <u-form-item label="昵称">
  92. <input type="nickname" class="weui-input u-border-bottom" @blur="nickNameInput" placeholder="请输入昵称"/>
  93. </u-form-item>
  94. </u-form>
  95. </div>
  96. </div>
  97. <div class="auth-btncard">
  98. <div class="btn-unok"><u-button :hair-line='false' :custom-style="customStyleUnOk" @click="showAuthorizeUser=false"> 拒绝</u-button></div>
  99. <div class="btn-ok"><u-button :hair-line='false' :custom-style="customStyleOk" @click="authUser"> 允许</u-button></div>
  100. </div>
  101. </view>
  102. </u-modal> -->
  103. </view>
  104. </template>
  105. <script>
  106. export default {
  107. data() {
  108. return {
  109. staticUrl:this.$commonConfig.staticUrl,
  110. checked:false,
  111. checkboxVal:null,
  112. loginBtn:true,
  113. bname:'旭烁集团',
  114. //屏幕高度
  115. screenHeight: "",
  116. logoSrc: "/static/logo.png",
  117. // sitename:"/static/sitename.png",
  118. miniappLoginInfo:null,
  119. hasUserInfo:false,
  120. userInfo: null,
  121. user:{
  122. avatar:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
  123. name:'',
  124. mobile:'',
  125. },
  126. code: '',
  127. showAuthorizeUser: false,
  128. showAuthorizePhone: false,
  129. customStyleUnOk:{},
  130. customStyleOk:{'margin-left': '30px',color:'#00A447'},
  131. backUrl:null,
  132. scene:'',
  133. retailId:'',
  134. };
  135. },
  136. onLoad(e) {
  137. // console.log("onLoad e=======",e)
  138. if(e&&e.scene){
  139. this.scene = e.scene;
  140. // console.log('login e.scene',e.scene);
  141. this.scene&&uni.setStorage({
  142. key:'scene',
  143. data:e.scene,
  144. // timestamp: new Date().getTime(), // 添加时间戳
  145. success: function (res) {
  146. // console.log('onLoad success scene====',res);
  147. }
  148. });
  149. }
  150. // #ifdef H5
  151. if(e&&e.code) { // 微信第三方登录成功
  152. this.code = e.code
  153. this.login(e)
  154. }
  155. // #endif
  156. let that = this;
  157. //获取屏幕高度,我的项目再store里已经取到了
  158. uni.getSystemInfo({
  159. success: (res) => {
  160. this.screenHeight = res.windowHeight + "px"
  161. }
  162. });
  163. uni.getStorage({
  164. key: 'backUrl',
  165. success: function (res) {
  166. that.backUrl = '/'+res.data;
  167. }
  168. });
  169. },
  170. onShow() {
  171. // #ifdef H5
  172. let pages = getCurrentPages(); //当前页面栈
  173. // console.log("pages=====",pages)
  174. // #endif
  175. // #ifdef MP
  176. // uni.showLoading({
  177. // mask:true
  178. // });
  179. this.getChatCode();
  180. // setTimeout(() => {
  181. // this.openAuth();
  182. // // this.showAuthorizeUser = true //弹出头像用户名
  183. // }, 100);
  184. // #endif
  185. let userInfo = uni.getStorageSync('userInfo');
  186. this.user.name = userInfo.name;
  187. },
  188. methods: {
  189. openAuth(){
  190. this.showAuthorizePhone = true;
  191. // let userInfo = uni.getStorageSync('userInfo')
  192. // console.log('userInfo>>>>>>>>>>>>>>>>>>>>>',userInfo)
  193. // if(userInfo){
  194. // this.showAuthorizeUser = true;
  195. // }else{
  196. // this.showAuthorizePhone = true;
  197. // }
  198. // uni.hideLoading()
  199. },
  200. getChatCode() {
  201. //因为作用域先提前赋值
  202. var that = this
  203. //使用uni封装的一键登录方法
  204. uni.login({
  205. provider: 'weixin',
  206. success(res) {
  207. //成功后带着微信登录返回的code去请求我们的后端
  208. that.code = res.code;
  209. // console.log('code',that.code);
  210. }
  211. })
  212. },
  213. getUserProfile() {
  214. var that = this
  215. uni.getUserProfile({
  216. desc: "获取你的昵称、头像",
  217. success(res) {
  218. // console.log('getUserProfile success',res);
  219. if (res.errMsg == "getUserProfile:ok" && res.userInfo != undefined) {
  220. //我用store来存储一些数据,你可以放到你要放的地方
  221. // console.log(' res.userInfo', res.userInfo);
  222. // that.$u.vuex('wx_user_info.nickName', res.userInfo.nickName);
  223. // that.$u.vuex('wx_user_info.avatarUrl', res.userInfo.avatarUrl);
  224. // that.getChatCode()
  225. }
  226. },
  227. complete(res) {
  228. // console.log('complete',res);
  229. }
  230. })
  231. },
  232. //获取昵称输入内容
  233. // userNameInput(e){
  234. // this.user.nickName = e.detail.value
  235. // },
  236. onChooseAvatar(e) {
  237. console.log('头像信息》')
  238. console.log(e)
  239. this.user.avatar = e.detail.avatarUrl;
  240. },
  241. async authPhone(e){
  242. // console.log('checked',this.checked);
  243. // console.log('detail',e.detail);
  244. let errMsg = e.detail?.errMsg;
  245. if(errMsg!='getPhoneNumber:ok'){
  246. uni.showToast({
  247. title:'您已拒绝使用!',
  248. icon:'error'
  249. })
  250. return
  251. }
  252. this.showAuthorizePhone=false;
  253. this.loginBtn = false;
  254. let _this = this;
  255. console.log('this.scene',this.scene);
  256. if(!this.scene){
  257. await uni.getStorage({
  258. key: 'scene',
  259. success: function (res) {
  260. // console.log('scene====',res.data);
  261. if(res.data){
  262. _this.scene = res.data
  263. }
  264. }
  265. });
  266. }
  267. // this.getMobile()
  268. let mobileData = await this.$u.api.getMobile({code:e.detail.code});
  269. // console.log('mobileData',mobileData);
  270. let mobile = mobileData.data?.mobile;
  271. if(mobile){
  272. this.mobile = mobile
  273. }else{
  274. // uni.showToast({
  275. // title: '没有获取手机号',
  276. // icon:'error',
  277. // duration: 2000
  278. // });
  279. }
  280. this.login(e);
  281. this.loginBtn = true;
  282. },
  283. async login(e){
  284. // console.log('e',e);
  285. let _this = this;
  286. let wxinfo = null
  287. let data = {}
  288. // #ifdef MP
  289. wxinfo = await this.$u.api.wxinfo({code:this.code});
  290. let {openid,sessionKey,unionid} = wxinfo.data;
  291. _this.$u.vuex('vuex_wechatOpenid', openid);
  292. const { errMsg, iv, encryptedData } = e.detail;
  293. if (errMsg !== 'getPhoneNumber:ok') return;
  294. data = {
  295. code: this.code,
  296. encryptedData: encryptedData,
  297. ivStr: iv,
  298. openId:openid,
  299. sessionKey,
  300. sourceType:1,
  301. mobile:this.mobile
  302. }
  303. console.log('this.scene',this.scene);
  304. if(this.scene&&this.scene!='undefined'){
  305. await this.$u.api.uncompress({scene:this.scene}).then(res=>{
  306. // data.performId = res.data.performId;
  307. data.label = res.data.label;
  308. data.retailId = res.data.retailId;
  309. this.retailId = res.data.retailId;
  310. data.retailId&&uni.setStorage({
  311. key:'retailId',
  312. data:data.retailId
  313. });
  314. // console.log('uncompress',res.data);
  315. }).catch(err=>{
  316. console.log('uncompress err',err);
  317. })
  318. }
  319. // #endif
  320. // #ifdef H5
  321. wxinfo = await this.$u.api.wxinfoH5({code:this.code});
  322. let { openid } = wxinfo.data;
  323. data = {
  324. "openId": "",
  325. "h5OpenId": openid,
  326. "mobile": ""
  327. }
  328. // #endif
  329. console.log('----------登陆中',data)
  330. this.$u.api.login(data).then(res=>{
  331. // console.log('微信登录返回结果========',res.data)
  332. _this.$u.vuex('vuex_user_info', res.data);
  333. // _this.$u.vuex('vuex_member_info',res.data);
  334. // uni.setStorageSync('userInfo', res.data)
  335. this.showAuthorizePhone = false;
  336. // uni.removeStorage({
  337. // key:'scene'
  338. // });
  339. // console.log('res.data.userid',res.data.userid);
  340. this.getMemberInfo(res.data.userId);
  341. }).catch(err=>{
  342. console.log('err',err);
  343. this.showAuthorizePhone = false
  344. })
  345. },
  346. handleBindRetail(retailId){
  347. this.$u.api.bindRetail({retailId:retailId}).then(res=>{
  348. console.log('bindRetail',res);
  349. }).catch(err=>{
  350. console.log('bindRetail err',err);
  351. })
  352. },
  353. async getMemberInfo(userid){
  354. // console.log('userid',userid);
  355. let _this = this;
  356. if(this.retailId){
  357. await this.handleBindRetail(this.retailId);
  358. }
  359. this.$u.api.memberInfo({id:userid}).then(res=> {
  360. if(res.code ===200) {
  361. _this.userInfo = res.data;
  362. this.$u.vuex('vuex_member_info', res.data);
  363. // this.$u.vuex('vuex_member_info.avatar', 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0');
  364. if(!res.data.name){
  365. this.$u.vuex('vuex_member_info.name', '微信用户');
  366. this.updateMemberInfo();
  367. }else{
  368. this.goBack();
  369. }
  370. // 下面需要获取微信头像和昵称
  371. // if(res.data.name){
  372. // _this.hasUserInfo = true;
  373. // _this.userInfo = res.data;
  374. // uni.navigateTo({
  375. // url: '/pages/index/index'
  376. // })
  377. // }else{
  378. // _this.showAuthorizeUser = true
  379. // // uni.hideLoading()
  380. // }
  381. // console.log('---------登陆成功')
  382. }
  383. })
  384. },
  385. goBack(){
  386. let url = this.backUrl&&this.backUrl.length>0?this.backUrl:'/pages/index/index';
  387. // console.log('url',url);
  388. uni.removeStorage({
  389. key: 'backUrl',
  390. success: function (res) {
  391. console.log('goBackurl================',decodeURIComponent(url));
  392. uni.reLaunch({url: decodeURIComponent(url)});
  393. }
  394. });
  395. },
  396. updateMemberInfo(){
  397. let params ={
  398. id:this.userInfo.id,
  399. // avatar:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
  400. name:'微信用户'
  401. };
  402. this.$u.api.updateMemberInfo(params).then(res=>{
  403. this.goBack();
  404. }).catch(err=>{
  405. console.log('err',err);
  406. })
  407. },
  408. authUser(){
  409. let userInfo = this.vuex_user_info || {};
  410. // console.log('this===',this);
  411. // console.log('vuex_user_info===',this.vuex_user_info);
  412. // console.log('userInfo',userInfo);
  413. // console.log('this.user',this.user);
  414. if(!this.user.name){
  415. uni.showToast({
  416. title: '请输入您的昵称!',
  417. icon:'error',
  418. duration: 2000
  419. });
  420. return;
  421. }
  422. userInfo.avatar = this.user.avatar;
  423. userInfo.name = this.user.name;
  424. this.userInfo = userInfo;
  425. uni.setStorageSync('userInfo', userInfo)
  426. this.user.id = userInfo.userid;
  427. let param = {
  428. id:this.user.id,
  429. name:this.user.name,
  430. avatar:this.user.avatar
  431. }
  432. this.$u.api.updateMemberInfo(param).then(res=>{
  433. this.hasUserInfo = true;
  434. this.showAuthorizeUser = false;
  435. uni.navigateTo({
  436. url: '/pages/index/index'
  437. })
  438. }).catch(err=>{
  439. console.log('err',err);
  440. })
  441. },
  442. nickNameInput(e){
  443. this.user.name = e.detail.value
  444. },
  445. checkboxChange(e){
  446. this.checkboxVal = e[0];
  447. },
  448. disabledClick(){
  449. // console.log('checked',this.checked?.length);
  450. // console.log('loginBtn',this.loginBtn);
  451. // #ifdef MP
  452. if(!this.checked||this.checked?.length<=0){
  453. uni.showToast({
  454. title:'请先同意使用条款!',
  455. icon:'none'
  456. })
  457. }else if(this.loginBtn==false){
  458. uni.showToast({
  459. title:'登录中!',
  460. icon:'none'
  461. })
  462. }
  463. // #endif
  464. // #ifdef H5
  465. if(!this.checked||this.checked?.length<=0){
  466. uni.showToast({
  467. title:'请先同意使用条款!',
  468. icon:'none'
  469. })
  470. }else{
  471. uni.showToast({
  472. title:'登录中!',
  473. icon:'none'
  474. })
  475. this.redirectToAuth()
  476. }
  477. // #endif
  478. },
  479. /** 公众号 微信授权登录 */
  480. redirectToAuth() {
  481. const appid = 'wx6490eaa0d20d2be2';
  482. const redirectUri = encodeURIComponent('https://greath5.dev.gztjy.top/pages/login/login');
  483. const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
  484. window.location.href = authUrl;
  485. },
  486. }
  487. }
  488. </script>
  489. <style lang="scss" scoped>
  490. .login-bg{
  491. position: fixed;
  492. left: 0;
  493. right: 0;
  494. bottom: 0;
  495. width: 100%;
  496. }
  497. .body {
  498. background-color: #fff;
  499. box-sizing: border-box;
  500. text-align: center;
  501. .logo-wrap {
  502. padding-top: 25vh;
  503. img{
  504. display: block;
  505. margin: 0 auto;
  506. }
  507. .logo{
  508. width: 480rpx;
  509. height: 263rpx;
  510. }
  511. .sitename{
  512. width: 460rpx;
  513. height: 142rpx;
  514. }
  515. }
  516. }
  517. //
  518. .auth-btncard{
  519. margin-top: 20px;
  520. .btn-unok{
  521. width: 45%;
  522. float: left;
  523. }
  524. .btn-ok{
  525. width: 45%;
  526. float: left;
  527. margin: 0;
  528. padding: 0;
  529. border: 0px solid transparent; //自定义边框
  530. outline: none; //消除默认点击蓝色边框效果
  531. u-button{
  532. font-size: 16px;
  533. margin: 0;
  534. padding: 0;
  535. // border: 0px solid transparent; //自定义边框
  536. outline: none; //消除默认点击蓝色边框效果
  537. }
  538. }
  539. }
  540. .auth-card{
  541. text-align: center;
  542. .avatar-img{
  543. width: 150rpx;
  544. height: 150rpx;
  545. overflow: hidden;
  546. border-radius: 100%;
  547. margin-top: 30rpx;
  548. }
  549. .title{
  550. font-size: 30rpx;
  551. }
  552. .content{
  553. margin-top: 10rpx;
  554. margin-bottom: 20rpx;
  555. }
  556. }
  557. .avatar-wrapper{
  558. width: 150rpx;
  559. height: 100rpx;
  560. color: #333 !important;
  561. text-align: center !important;
  562. border: none !important;
  563. border-radius:0 !important;
  564. background-color:transparent !important;
  565. }
  566. .avatar-wrapper::after {
  567. border: none !important;
  568. }
  569. .avatar{
  570. width: 100rpx;
  571. height: 100rpx;
  572. overflow: hidden;
  573. border-radius: 100%;
  574. }
  575. .rule-wrap{
  576. position: fixed;
  577. left: 0;
  578. right: 0;
  579. bottom: 20px;
  580. margin: 40rpx auto;
  581. font-size: 24rpx;
  582. line-height: 1.5;
  583. .link{
  584. white-space: nowrap;
  585. color: #1677FF;
  586. }
  587. }
  588. </style>