login.vue 16 KB

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