login.vue 14 KB

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