login.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. let url = this.backUrl&&this.backUrl.length>0?this.backUrl:'/pages/index/index';
  290. uni.navigateTo({
  291. url: url
  292. })
  293. }
  294. // 下面需要获取微信头像和昵称
  295. // if(res.data.name){
  296. // _this.hasUserInfo = true;
  297. // _this.userInfo = res.data;
  298. // uni.navigateTo({
  299. // url: '/pages/index/index'
  300. // })
  301. // }else{
  302. // _this.showAuthorizeUser = true
  303. // // uni.hideLoading()
  304. // }
  305. // console.log('---------登陆成功')
  306. }
  307. })
  308. },
  309. updateMemberInfo(){
  310. let params ={
  311. id:this.userInfo.id,
  312. // avatar:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
  313. name:'微信用户'
  314. };
  315. this.$u.api.updateMemberInfo(params).then(res=>{
  316. let url = this.backUrl&&this.backUrl.length>0?this.backUrl:'/pages/index/index';
  317. uni.navigateTo({
  318. url: url
  319. })
  320. }).catch(err=>{
  321. console.log('err',err);
  322. })
  323. },
  324. authUser(){
  325. let userInfo = this.vuex_user_info || {};
  326. // console.log('this===',this);
  327. // console.log('vuex_user_info===',this.vuex_user_info);
  328. // console.log('userInfo',userInfo);
  329. // console.log('this.user',this.user);
  330. if(!this.user.name){
  331. uni.showToast({
  332. title: '请输入您的昵称!',
  333. icon:'error',
  334. duration: 2000
  335. });
  336. return;
  337. }
  338. userInfo.avatar = this.user.avatar;
  339. userInfo.name = this.user.name;
  340. this.userInfo = userInfo;
  341. uni.setStorageSync('userInfo', userInfo)
  342. this.user.id = userInfo.userid;
  343. let param = {
  344. id:this.user.id,
  345. name:this.user.name,
  346. avatar:this.user.avatar
  347. }
  348. this.$u.api.updateMemberInfo(param).then(res=>{
  349. this.hasUserInfo = true;
  350. this.showAuthorizeUser = false;
  351. uni.navigateTo({
  352. url: '/pages/index/index'
  353. })
  354. }).catch(err=>{
  355. console.log('err',err);
  356. })
  357. },
  358. nickNameInput(e){
  359. this.user.name = e.detail.value
  360. },
  361. checkboxChange(e){
  362. this.checkboxVal = e[0];
  363. },
  364. disabledClick(){
  365. // console.log('checked',this.checked?.length);
  366. // console.log('loginBtn',this.loginBtn);
  367. if(!this.checked||this.checked?.length<=0){
  368. uni.showToast({
  369. title:'请先同意使用条款!',
  370. icon:'none'
  371. })
  372. }else if(this.loginBtn==false){
  373. uni.showToast({
  374. title:'请刷新重试!',
  375. icon:'none'
  376. })
  377. }
  378. }
  379. }
  380. }
  381. </script>
  382. <style lang="scss" scoped>
  383. .body {
  384. // width: 100%;
  385. background-color: #f3f3f3;
  386. // padding: 24px;
  387. box-sizing: border-box;
  388. text-align: center;
  389. .logo {
  390. padding-top: 200rpx;
  391. margin: auto;
  392. width: 120px;
  393. height: 120px;
  394. }
  395. .title {
  396. width: 90%;
  397. margin: 0 auto 30rpx;
  398. font-size: 18px;
  399. color: #111;
  400. }
  401. }
  402. //
  403. .auth-btncard{
  404. margin-top: 20px;
  405. .btn-unok{
  406. width: 45%;
  407. float: left;
  408. }
  409. .btn-ok{
  410. width: 45%;
  411. float: left;
  412. margin: 0;
  413. padding: 0;
  414. border: 0px solid transparent; //自定义边框
  415. outline: none; //消除默认点击蓝色边框效果
  416. u-button{
  417. font-size: 16px;
  418. margin: 0;
  419. padding: 0;
  420. // border: 0px solid transparent; //自定义边框
  421. outline: none; //消除默认点击蓝色边框效果
  422. }
  423. }
  424. }
  425. .auth-card{
  426. text-align: center;
  427. .avatar-img{
  428. width: 150rpx;
  429. height: 150rpx;
  430. overflow: hidden;
  431. border-radius: 100%;
  432. margin-top: 30rpx;
  433. }
  434. .title{
  435. font-size: 30rpx;
  436. }
  437. .content{
  438. margin-top: 10rpx;
  439. margin-bottom: 20rpx;
  440. }
  441. }
  442. .avatar-wrapper{
  443. width: 150rpx;
  444. height: 100rpx;
  445. color: #333 !important;
  446. text-align: center !important;
  447. border: none !important;
  448. border-radius:0 !important;
  449. background-color:transparent !important;
  450. }
  451. .avatar-wrapper::after {
  452. border: none !important;
  453. }
  454. .avatar{
  455. width: 100rpx;
  456. height: 100rpx;
  457. overflow: hidden;
  458. border-radius: 100%;
  459. }
  460. .rule-wrap{
  461. margin: 40rpx auto;
  462. font-size: 26rpx;
  463. line-height: 1.5;
  464. .link{
  465. white-space: nowrap;
  466. color: #00A447;
  467. }
  468. }
  469. </style>