login.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. };
  134. },
  135. onLoad(e) {
  136. // console.log("onLoad e=======",e)
  137. if(e&&e.scene){
  138. this.scene = e.scene;
  139. // console.log('login e.scene',e.scene);
  140. this.scene&&uni.setStorage({
  141. key:'scene',
  142. data:e.scene,
  143. // timestamp: new Date().getTime(), // 添加时间戳
  144. success: function (res) {
  145. // console.log('onLoad success scene====',res);
  146. }
  147. });
  148. }
  149. // #ifdef H5
  150. if(e&&e.code) { // 微信第三方登录成功
  151. this.code = e.code
  152. this.login(e)
  153. }
  154. // #endif
  155. let that = this;
  156. //获取屏幕高度,我的项目再store里已经取到了
  157. uni.getSystemInfo({
  158. success: (res) => {
  159. this.screenHeight = res.windowHeight + "px"
  160. }
  161. });
  162. uni.getStorage({
  163. key: 'backUrl',
  164. success: function (res) {
  165. that.backUrl = '/'+res.data;
  166. }
  167. });
  168. },
  169. onShow() {
  170. // #ifdef H5
  171. let pages = getCurrentPages(); //当前页面栈
  172. // console.log("pages=====",pages)
  173. // #endif
  174. // #ifdef MP
  175. // uni.showLoading({
  176. // mask:true
  177. // });
  178. this.getChatCode();
  179. // setTimeout(() => {
  180. // this.openAuth();
  181. // // this.showAuthorizeUser = true //弹出头像用户名
  182. // }, 100);
  183. // #endif
  184. let userInfo = uni.getStorageSync('userInfo');
  185. this.user.name = userInfo.name;
  186. },
  187. methods: {
  188. openAuth(){
  189. this.showAuthorizePhone = true;
  190. // let userInfo = uni.getStorageSync('userInfo')
  191. // console.log('userInfo>>>>>>>>>>>>>>>>>>>>>',userInfo)
  192. // if(userInfo){
  193. // this.showAuthorizeUser = true;
  194. // }else{
  195. // this.showAuthorizePhone = true;
  196. // }
  197. // uni.hideLoading()
  198. },
  199. getChatCode() {
  200. //因为作用域先提前赋值
  201. var that = this
  202. //使用uni封装的一键登录方法
  203. uni.login({
  204. provider: 'weixin',
  205. success(res) {
  206. //成功后带着微信登录返回的code去请求我们的后端
  207. that.code = res.code;
  208. // console.log('code',that.code);
  209. }
  210. })
  211. },
  212. getUserProfile() {
  213. var that = this
  214. uni.getUserProfile({
  215. desc: "获取你的昵称、头像",
  216. success(res) {
  217. // console.log('getUserProfile success',res);
  218. if (res.errMsg == "getUserProfile:ok" && res.userInfo != undefined) {
  219. //我用store来存储一些数据,你可以放到你要放的地方
  220. // console.log(' res.userInfo', res.userInfo);
  221. // that.$u.vuex('wx_user_info.nickName', res.userInfo.nickName);
  222. // that.$u.vuex('wx_user_info.avatarUrl', res.userInfo.avatarUrl);
  223. // that.getChatCode()
  224. }
  225. },
  226. complete(res) {
  227. // console.log('complete',res);
  228. }
  229. })
  230. },
  231. //获取昵称输入内容
  232. // userNameInput(e){
  233. // this.user.nickName = e.detail.value
  234. // },
  235. onChooseAvatar(e) {
  236. console.log('头像信息》')
  237. console.log(e)
  238. this.user.avatar = e.detail.avatarUrl;
  239. },
  240. async authPhone(e){
  241. // console.log('checked',this.checked);
  242. // console.log('detail',e.detail);
  243. let errMsg = e.detail?.errMsg;
  244. if(errMsg!='getPhoneNumber:ok'){
  245. uni.showToast({
  246. title:'您已拒绝使用!',
  247. icon:'error'
  248. })
  249. return
  250. }
  251. this.showAuthorizePhone=false;
  252. this.loginBtn = false;
  253. let _this = this;
  254. console.log('this.scene',this.scene);
  255. if(!this.scene){
  256. await uni.getStorage({
  257. key: 'scene',
  258. success: function (res) {
  259. // console.log('scene====',res.data);
  260. if(res.data){
  261. _this.scene = res.data
  262. }
  263. }
  264. });
  265. }
  266. // this.getMobile()
  267. let mobileData = await this.$u.api.getMobile({code:e.detail.code});
  268. // console.log('mobileData',mobileData);
  269. let mobile = mobileData.data?.mobile;
  270. if(mobile){
  271. this.mobile = mobile
  272. }else{
  273. // uni.showToast({
  274. // title: '没有获取手机号',
  275. // icon:'error',
  276. // duration: 2000
  277. // });
  278. }
  279. this.login(e);
  280. this.loginBtn = true;
  281. },
  282. async login(e){
  283. // console.log('e',e);
  284. let _this = this;
  285. let wxinfo = null
  286. let data = {}
  287. // #ifdef MP
  288. wxinfo = await this.$u.api.wxinfo({code:this.code});
  289. let {openid,sessionKey,unionid} = wxinfo.data;
  290. _this.$u.vuex('vuex_wechatOpenid', openid);
  291. const { errMsg, iv, encryptedData } = e.detail;
  292. if (errMsg !== 'getPhoneNumber:ok') return;
  293. data = {
  294. code: this.code,
  295. encryptedData: encryptedData,
  296. ivStr: iv,
  297. openId:openid,
  298. sessionKey,
  299. sourceType:1,
  300. mobile:this.mobile
  301. }
  302. console.log('this.scene',this.scene);
  303. if(this.scene&&this.scene!='undefined'){
  304. await this.$u.api.uncompress({scene:this.scene}).then(res=>{
  305. // data.performId = res.data.performId;
  306. data.label = res.data.label;
  307. data.retailId = res.data.retailId;
  308. data.retailId&&uni.setStorage({
  309. key:'retailId',
  310. data:data.retailId
  311. });
  312. // console.log('uncompress',res.data);
  313. }).catch(err=>{
  314. console.log('uncompress',err);
  315. })
  316. }
  317. // #endif
  318. // #ifdef H5
  319. wxinfo = await this.$u.api.wxinfoH5({code:this.code});
  320. let { openid } = wxinfo.data;
  321. data = {
  322. "openId": "",
  323. "h5OpenId": openid,
  324. "mobile": ""
  325. }
  326. // #endif
  327. console.log('----------登陆中',data)
  328. this.$u.api.login(data).then(res=>{
  329. // console.log('微信登录返回结果========',res.data)
  330. _this.$u.vuex('vuex_user_info', res.data);
  331. // _this.$u.vuex('vuex_member_info',res.data);
  332. // uni.setStorageSync('userInfo', res.data)
  333. this.showAuthorizePhone = false;
  334. // uni.removeStorage({
  335. // key:'scene'
  336. // });
  337. // console.log('res.data.userid',res.data.userid);
  338. this.getMemberInfo(res.data.userId);
  339. }).catch(err=>{
  340. console.log('err',err);
  341. this.showAuthorizePhone = false
  342. })
  343. },
  344. getMemberInfo(userid){
  345. // console.log('userid',userid);
  346. let _this = this;
  347. this.$u.api.memberInfo({id:userid}).then(res=> {
  348. if(res.code ===200) {
  349. _this.userInfo = res.data;
  350. this.$u.vuex('vuex_member_info', res.data);
  351. // this.$u.vuex('vuex_member_info.avatar', 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0');
  352. if(!res.data.name){
  353. this.$u.vuex('vuex_member_info.name', '微信用户');
  354. this.updateMemberInfo();
  355. }else{
  356. this.goBack();
  357. }
  358. // 下面需要获取微信头像和昵称
  359. // if(res.data.name){
  360. // _this.hasUserInfo = true;
  361. // _this.userInfo = res.data;
  362. // uni.navigateTo({
  363. // url: '/pages/index/index'
  364. // })
  365. // }else{
  366. // _this.showAuthorizeUser = true
  367. // // uni.hideLoading()
  368. // }
  369. // console.log('---------登陆成功')
  370. }
  371. })
  372. },
  373. goBack(){
  374. let url = this.backUrl&&this.backUrl.length>0?this.backUrl:'/pages/index/index';
  375. // console.log('url',url);
  376. uni.removeStorage({
  377. key: 'backUrl',
  378. success: function (res) {
  379. console.log('goBackurl================',decodeURIComponent(url));
  380. uni.reLaunch({url: decodeURIComponent(url)});
  381. }
  382. });
  383. },
  384. updateMemberInfo(){
  385. let params ={
  386. id:this.userInfo.id,
  387. // avatar:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
  388. name:'微信用户'
  389. };
  390. this.$u.api.updateMemberInfo(params).then(res=>{
  391. this.goBack();
  392. }).catch(err=>{
  393. console.log('err',err);
  394. })
  395. },
  396. authUser(){
  397. let userInfo = this.vuex_user_info || {};
  398. // console.log('this===',this);
  399. // console.log('vuex_user_info===',this.vuex_user_info);
  400. // console.log('userInfo',userInfo);
  401. // console.log('this.user',this.user);
  402. if(!this.user.name){
  403. uni.showToast({
  404. title: '请输入您的昵称!',
  405. icon:'error',
  406. duration: 2000
  407. });
  408. return;
  409. }
  410. userInfo.avatar = this.user.avatar;
  411. userInfo.name = this.user.name;
  412. this.userInfo = userInfo;
  413. uni.setStorageSync('userInfo', userInfo)
  414. this.user.id = userInfo.userid;
  415. let param = {
  416. id:this.user.id,
  417. name:this.user.name,
  418. avatar:this.user.avatar
  419. }
  420. this.$u.api.updateMemberInfo(param).then(res=>{
  421. this.hasUserInfo = true;
  422. this.showAuthorizeUser = false;
  423. uni.navigateTo({
  424. url: '/pages/index/index'
  425. })
  426. }).catch(err=>{
  427. console.log('err',err);
  428. })
  429. },
  430. nickNameInput(e){
  431. this.user.name = e.detail.value
  432. },
  433. checkboxChange(e){
  434. this.checkboxVal = e[0];
  435. },
  436. disabledClick(){
  437. // console.log('checked',this.checked?.length);
  438. // console.log('loginBtn',this.loginBtn);
  439. // #ifdef MP
  440. if(!this.checked||this.checked?.length<=0){
  441. uni.showToast({
  442. title:'请先同意使用条款!',
  443. icon:'none'
  444. })
  445. }else if(this.loginBtn==false){
  446. uni.showToast({
  447. title:'登录中!',
  448. icon:'none'
  449. })
  450. }
  451. // #endif
  452. // #ifdef H5
  453. if(!this.checked||this.checked?.length<=0){
  454. uni.showToast({
  455. title:'请先同意使用条款!',
  456. icon:'none'
  457. })
  458. }else{
  459. uni.showToast({
  460. title:'登录中!',
  461. icon:'none'
  462. })
  463. this.redirectToAuth()
  464. }
  465. // #endif
  466. },
  467. /** 公众号 微信授权登录 */
  468. redirectToAuth() {
  469. const appid = 'wx6490eaa0d20d2be2';
  470. const redirectUri = encodeURIComponent('https://greath5.dev.gztjy.top/pages/login/login');
  471. 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`;
  472. window.location.href = authUrl;
  473. },
  474. }
  475. }
  476. </script>
  477. <style lang="scss" scoped>
  478. .login-bg{
  479. position: fixed;
  480. left: 0;
  481. right: 0;
  482. bottom: 0;
  483. width: 100%;
  484. }
  485. .body {
  486. background-color: #fff;
  487. box-sizing: border-box;
  488. text-align: center;
  489. .logo-wrap {
  490. padding-top: 25vh;
  491. img{
  492. display: block;
  493. margin: 0 auto;
  494. }
  495. .logo{
  496. width: 480rpx;
  497. height: 263rpx;
  498. }
  499. .sitename{
  500. width: 460rpx;
  501. height: 142rpx;
  502. }
  503. }
  504. }
  505. //
  506. .auth-btncard{
  507. margin-top: 20px;
  508. .btn-unok{
  509. width: 45%;
  510. float: left;
  511. }
  512. .btn-ok{
  513. width: 45%;
  514. float: left;
  515. margin: 0;
  516. padding: 0;
  517. border: 0px solid transparent; //自定义边框
  518. outline: none; //消除默认点击蓝色边框效果
  519. u-button{
  520. font-size: 16px;
  521. margin: 0;
  522. padding: 0;
  523. // border: 0px solid transparent; //自定义边框
  524. outline: none; //消除默认点击蓝色边框效果
  525. }
  526. }
  527. }
  528. .auth-card{
  529. text-align: center;
  530. .avatar-img{
  531. width: 150rpx;
  532. height: 150rpx;
  533. overflow: hidden;
  534. border-radius: 100%;
  535. margin-top: 30rpx;
  536. }
  537. .title{
  538. font-size: 30rpx;
  539. }
  540. .content{
  541. margin-top: 10rpx;
  542. margin-bottom: 20rpx;
  543. }
  544. }
  545. .avatar-wrapper{
  546. width: 150rpx;
  547. height: 100rpx;
  548. color: #333 !important;
  549. text-align: center !important;
  550. border: none !important;
  551. border-radius:0 !important;
  552. background-color:transparent !important;
  553. }
  554. .avatar-wrapper::after {
  555. border: none !important;
  556. }
  557. .avatar{
  558. width: 100rpx;
  559. height: 100rpx;
  560. overflow: hidden;
  561. border-radius: 100%;
  562. }
  563. .rule-wrap{
  564. position: fixed;
  565. left: 0;
  566. right: 0;
  567. bottom: 20px;
  568. margin: 40rpx auto;
  569. font-size: 24rpx;
  570. line-height: 1.5;
  571. .link{
  572. white-space: nowrap;
  573. color: #1677FF;
  574. }
  575. }
  576. </style>