login.vue 16 KB

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