loginh5.vue 15 KB

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