login.vue 14 KB

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