login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. let errMsg = e.detail?.errMsg;
  199. if(errMsg!='getPhoneNumber:ok'){
  200. uni.showToast({
  201. title:'您已拒绝使用!',
  202. icon:'error'
  203. })
  204. return
  205. }
  206. this.showAuthorizePhone=false;
  207. this.loginBtn = false;
  208. let _this = this;
  209. await uni.getStorage({
  210. key: 'scene',
  211. success: function (res) {
  212. // console.log('scene====',res.data);
  213. if(res.data){
  214. _this.scene = res.data
  215. }
  216. }
  217. });
  218. // this.getMobile()
  219. let mobileData = await this.$u.api.getMobile({code:e.detail.code});
  220. console.log('mobileData',mobileData);
  221. let mobile = mobileData.data?.mobile;
  222. if(mobile){
  223. this.mobile = mobile
  224. this.login(e);
  225. }else{
  226. uni.showToast({
  227. title: '没有获取手机号',
  228. icon:'error',
  229. duration: 2000
  230. });
  231. this.loginBtn = true;
  232. }
  233. },
  234. async login(e){
  235. // console.log('e',e);
  236. let _this = this;
  237. let wxinfo = await this.$u.api.wxinfo({code:this.code});
  238. let {openid,sessionKey,unionid} = wxinfo.data;
  239. _this.$u.vuex('vuex_wechatOpenid', openid);
  240. const { errMsg, iv, encryptedData } = e.detail;
  241. if (errMsg !== 'getPhoneNumber:ok') return;
  242. let data = {
  243. code: this.code,
  244. encryptedData: encryptedData,
  245. ivStr: iv,
  246. wechatOpenid:openid,
  247. sessionKey,
  248. sourceType:1,
  249. mobile:this.mobile
  250. }
  251. if(this.scene&&this.scene!='undefined'){
  252. await this.$u.api.uncompress({scene:this.scene}).then(res=>{
  253. data.agentId = res.data.agentId;
  254. data.agentShopId = res.data.agentShopId;
  255. // console.log('uncompress',res.data);
  256. }).catch(err=>{
  257. console.log('uncompress',err);
  258. })
  259. }
  260. // console.log('----------登陆中',data)
  261. this.$u.api.login(data).then(res=>{
  262. // console.log('微信登录返回结果========',res.data)
  263. _this.$u.vuex('vuex_user_info', res.data);
  264. // _this.$u.vuex('vuex_member_info',res.data);
  265. // uni.setStorageSync('userInfo', res.data)
  266. this.showAuthorizePhone = false;
  267. uni.removeStorage({
  268. key:'scene'
  269. });
  270. this.getMemberInfo(res.data.userid);
  271. }).catch(err=>{
  272. console.log('err',err);
  273. this.showAuthorizePhone = false
  274. })
  275. },
  276. getMemberInfo(userid){
  277. let _this = this;
  278. this.$u.api.memberInfo({id:userid}).then(res=> {
  279. if(res.code ===200) {
  280. _this.userInfo = res.data;
  281. this.$u.vuex('vuex_member_info', res.data);
  282. // this.$u.vuex('vuex_member_info.avatar', 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0');
  283. if(!res.data.name){
  284. this.$u.vuex('vuex_member_info.name', '微信用户');
  285. this.updateMemberInfo();
  286. }else{
  287. let url = this.backUrl&&this.backUrl.length>0?this.backUrl:'/pages/index/index';
  288. uni.navigateTo({
  289. url: url
  290. })
  291. }
  292. // 下面需要获取微信头像和昵称
  293. // if(res.data.name){
  294. // _this.hasUserInfo = true;
  295. // _this.userInfo = res.data;
  296. // uni.navigateTo({
  297. // url: '/pages/index/index'
  298. // })
  299. // }else{
  300. // _this.showAuthorizeUser = true
  301. // // uni.hideLoading()
  302. // }
  303. // console.log('---------登陆成功')
  304. }
  305. })
  306. },
  307. updateMemberInfo(){
  308. let params ={
  309. id:this.userInfo.id,
  310. // avatar:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
  311. name:'微信用户'
  312. };
  313. this.$u.api.updateMemberInfo(params).then(res=>{
  314. let url = this.backUrl&&this.backUrl.length>0?this.backUrl:'/pages/index/index';
  315. uni.navigateTo({
  316. url: url
  317. })
  318. }).catch(err=>{
  319. console.log('err',err);
  320. })
  321. },
  322. authUser(){
  323. let userInfo = this.vuex_user_info || {};
  324. // console.log('this===',this);
  325. // console.log('vuex_user_info===',this.vuex_user_info);
  326. // console.log('userInfo',userInfo);
  327. // console.log('this.user',this.user);
  328. if(!this.user.name){
  329. uni.showToast({
  330. title: '请输入您的昵称!',
  331. icon:'error',
  332. duration: 2000
  333. });
  334. return;
  335. }
  336. userInfo.avatar = this.user.avatar;
  337. userInfo.name = this.user.name;
  338. this.userInfo = userInfo;
  339. uni.setStorageSync('userInfo', userInfo)
  340. this.user.id = userInfo.userid;
  341. let param = {
  342. id:this.user.id,
  343. name:this.user.name,
  344. avatar:this.user.avatar
  345. }
  346. this.$u.api.updateMemberInfo(param).then(res=>{
  347. this.hasUserInfo = true;
  348. this.showAuthorizeUser = false;
  349. uni.navigateTo({
  350. url: '/pages/index/index'
  351. })
  352. }).catch(err=>{
  353. console.log('err',err);
  354. })
  355. },
  356. nickNameInput(e){
  357. this.user.name = e.detail.value
  358. },
  359. checkboxChange(e){
  360. this.checkboxVal = e[0];
  361. },
  362. disabledClick(){
  363. uni.showToast({
  364. title:'请先同意使用条款!',
  365. icon:'none'
  366. })
  367. }
  368. }
  369. }
  370. </script>
  371. <style lang="scss" scoped>
  372. .body {
  373. // width: 100%;
  374. background-color: #f3f3f3;
  375. // padding: 24px;
  376. box-sizing: border-box;
  377. text-align: center;
  378. .logo {
  379. padding-top: 200rpx;
  380. margin: auto;
  381. width: 120px;
  382. height: 120px;
  383. }
  384. .title {
  385. width: 90%;
  386. margin: 0 auto 30rpx;
  387. font-size: 18px;
  388. color: #111;
  389. }
  390. }
  391. //
  392. .auth-btncard{
  393. margin-top: 20px;
  394. .btn-unok{
  395. width: 45%;
  396. float: left;
  397. }
  398. .btn-ok{
  399. width: 45%;
  400. float: left;
  401. margin: 0;
  402. padding: 0;
  403. border: 0px solid transparent; //自定义边框
  404. outline: none; //消除默认点击蓝色边框效果
  405. u-button{
  406. font-size: 16px;
  407. margin: 0;
  408. padding: 0;
  409. // border: 0px solid transparent; //自定义边框
  410. outline: none; //消除默认点击蓝色边框效果
  411. }
  412. }
  413. }
  414. .auth-card{
  415. text-align: center;
  416. .avatar-img{
  417. width: 150rpx;
  418. height: 150rpx;
  419. overflow: hidden;
  420. border-radius: 100%;
  421. margin-top: 30rpx;
  422. }
  423. .title{
  424. font-size: 30rpx;
  425. }
  426. .content{
  427. margin-top: 10rpx;
  428. margin-bottom: 20rpx;
  429. }
  430. }
  431. .avatar-wrapper{
  432. width: 150rpx;
  433. height: 100rpx;
  434. color: #333 !important;
  435. text-align: center !important;
  436. border: none !important;
  437. border-radius:0 !important;
  438. background-color:transparent !important;
  439. }
  440. .avatar-wrapper::after {
  441. border: none !important;
  442. }
  443. .avatar{
  444. width: 100rpx;
  445. height: 100rpx;
  446. overflow: hidden;
  447. border-radius: 100%;
  448. }
  449. .rule-wrap{
  450. margin: 40rpx auto;
  451. font-size: 26rpx;
  452. line-height: 1.5;
  453. .link{
  454. white-space: nowrap;
  455. color: #00A447;
  456. }
  457. }
  458. </style>