login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. };
  115. },
  116. onLoad() {
  117. let that = this;
  118. //获取屏幕高度,我的项目再store里已经取到了
  119. uni.getSystemInfo({
  120. success: (res) => {
  121. this.screenHeight = res.windowHeight + "px"
  122. }
  123. });
  124. uni.getStorage({
  125. key: 'backUrl',
  126. success: function (res) {
  127. that.backUrl = '/'+res.data;
  128. }
  129. });
  130. },
  131. onShow() {
  132. // uni.showLoading({
  133. // mask:true
  134. // });
  135. this.getChatCode();
  136. // setTimeout(() => {
  137. // this.openAuth();
  138. // // this.showAuthorizeUser = true //弹出头像用户名
  139. // }, 100);
  140. let userInfo = uni.getStorageSync('userInfo');
  141. this.user.name = userInfo.name;
  142. },
  143. methods: {
  144. openAuth(){
  145. this.showAuthorizePhone = true;
  146. // let userInfo = uni.getStorageSync('userInfo')
  147. // console.log('userInfo>>>>>>>>>>>>>>>>>>>>>',userInfo)
  148. // if(userInfo){
  149. // this.showAuthorizeUser = true;
  150. // }else{
  151. // this.showAuthorizePhone = true;
  152. // }
  153. // uni.hideLoading()
  154. },
  155. getChatCode() {
  156. //因为作用域先提前赋值
  157. var that = this
  158. //使用uni封装的一键登录方法
  159. uni.login({
  160. provider: 'weixin',
  161. success(res) {
  162. //成功后带着微信登录返回的code去请求我们的后端
  163. that.code = res.code;
  164. console.log('code',that.code);
  165. }
  166. })
  167. },
  168. getUserProfile() {
  169. var that = this
  170. uni.getUserProfile({
  171. desc: "获取你的昵称、头像",
  172. success(res) {
  173. console.log('getUserProfile success',res);
  174. if (res.errMsg == "getUserProfile:ok" && res.userInfo != undefined) {
  175. //我用store来存储一些数据,你可以放到你要放的地方
  176. console.log(' res.userInfo', res.userInfo);
  177. // that.$u.vuex('wx_user_info.nickName', res.userInfo.nickName);
  178. // that.$u.vuex('wx_user_info.avatarUrl', res.userInfo.avatarUrl);
  179. // that.getChatCode()
  180. }
  181. },
  182. complete(res) {
  183. // console.log('complete',res);
  184. }
  185. })
  186. },
  187. //获取昵称输入内容
  188. // userNameInput(e){
  189. // this.user.nickName = e.detail.value
  190. // },
  191. onChooseAvatar(e) {
  192. console.log('头像信息》')
  193. console.log(e)
  194. this.user.avatar = e.detail.avatarUrl;
  195. },
  196. async authPhone(e){
  197. let errMsg = e.detail?.errMsg;
  198. if(errMsg!='getPhoneNumber:ok'){
  199. uni.showToast({
  200. title:'您已拒绝使用!',
  201. icon:'error'
  202. })
  203. return
  204. }
  205. this.showAuthorizePhone=false;
  206. this.loginBtn = false;
  207. let _this = this;
  208. // this.getMobile()
  209. let mobileData = await this.$u.api.getMobile({code:e.detail.code});
  210. console.log('mobileData',mobileData);
  211. let mobile = mobileData.data?.mobile;
  212. if(mobile){
  213. this.mobile = mobile
  214. this.login(e);
  215. }else{
  216. uni.showToast({
  217. title: '没有获取手机号',
  218. icon:'error',
  219. duration: 2000
  220. });
  221. this.loginBtn = true;
  222. }
  223. },
  224. async login(e){
  225. // console.log('e',e);
  226. let _this = this;
  227. let wxinfo = await this.$u.api.wxinfo({code:this.code});
  228. let {openid,sessionKey,unionid} = wxinfo.data;
  229. _this.$u.vuex('vuex_wechatOpenid', openid);
  230. const { errMsg, iv, encryptedData } = e.detail;
  231. if (errMsg !== 'getPhoneNumber:ok') return;
  232. let data = {
  233. code: this.code,
  234. encryptedData: encryptedData,
  235. ivStr: iv,
  236. wechatOpenid:openid,
  237. sessionKey,
  238. sourceType:1,
  239. mobile:this.mobile
  240. }
  241. // console.log('----------登陆中',data)
  242. this.$u.api.login(data).then(res=>{
  243. console.log('微信登录返回结果========',res.data)
  244. _this.$u.vuex('vuex_user_info', res.data);
  245. // _this.$u.vuex('vuex_member_info',res.data);
  246. // uni.setStorageSync('userInfo', res.data)
  247. this.showAuthorizePhone = false
  248. this.getMemberInfo(res.data.userid);
  249. }).catch(err=>{
  250. console.log('err',err);
  251. this.showAuthorizePhone = false
  252. })
  253. },
  254. getMemberInfo(userid){
  255. let _this = this;
  256. this.$u.api.memberInfo({id:userid}).then(res=> {
  257. if(res.code ===200) {
  258. _this.userInfo = res.data;
  259. this.$u.vuex('vuex_member_info', res.data);
  260. // this.$u.vuex('vuex_member_info.avatar', 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0');
  261. if(!res.data.name){
  262. this.$u.vuex('vuex_member_info.name', '微信用户');
  263. this.updateMemberInfo();
  264. }else{
  265. let url = this.backUrl&&this.backUrl.length>0?this.backUrl:'/pages/index/index';
  266. uni.navigateTo({
  267. url: url
  268. })
  269. }
  270. // 下面需要获取微信头像和昵称
  271. // if(res.data.name){
  272. // _this.hasUserInfo = true;
  273. // _this.userInfo = res.data;
  274. // uni.navigateTo({
  275. // url: '/pages/index/index'
  276. // })
  277. // }else{
  278. // _this.showAuthorizeUser = true
  279. // // uni.hideLoading()
  280. // }
  281. // console.log('---------登陆成功')
  282. }
  283. })
  284. },
  285. updateMemberInfo(){
  286. let params ={
  287. id:this.userInfo.id,
  288. // avatar:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
  289. name:'微信用户'
  290. };
  291. this.$u.api.updateMemberInfo(params).then(res=>{
  292. let url = this.backUrl&&this.backUrl.length>0?this.backUrl:'/pages/index/index';
  293. uni.navigateTo({
  294. url: url
  295. })
  296. }).catch(err=>{
  297. console.log('err',err);
  298. })
  299. },
  300. authUser(){
  301. let userInfo = this.vuex_user_info || {};
  302. // console.log('this===',this);
  303. // console.log('vuex_user_info===',this.vuex_user_info);
  304. // console.log('userInfo',userInfo);
  305. // console.log('this.user',this.user);
  306. if(!this.user.name){
  307. uni.showToast({
  308. title: '请输入您的昵称!',
  309. icon:'error',
  310. duration: 2000
  311. });
  312. return;
  313. }
  314. userInfo.avatar = this.user.avatar;
  315. userInfo.name = this.user.name;
  316. this.userInfo = userInfo;
  317. uni.setStorageSync('userInfo', userInfo)
  318. this.user.id = userInfo.userid;
  319. let param = {
  320. id:this.user.id,
  321. name:this.user.name,
  322. avatar:this.user.avatar
  323. }
  324. this.$u.api.updateMemberInfo(param).then(res=>{
  325. this.hasUserInfo = true;
  326. this.showAuthorizeUser = false;
  327. uni.navigateTo({
  328. url: '/pages/index/index'
  329. })
  330. }).catch(err=>{
  331. console.log('err',err);
  332. })
  333. },
  334. nickNameInput(e){
  335. this.user.name = e.detail.value
  336. },
  337. checkboxChange(e){
  338. this.checkboxVal = e[0];
  339. },
  340. disabledClick(){
  341. uni.showToast({
  342. title:'请先同意使用条款!',
  343. icon:'none'
  344. })
  345. }
  346. }
  347. }
  348. </script>
  349. <style lang="scss" scoped>
  350. .body {
  351. // width: 100%;
  352. background-color: #f3f3f3;
  353. // padding: 24px;
  354. box-sizing: border-box;
  355. text-align: center;
  356. .logo {
  357. padding-top: 200rpx;
  358. margin: auto;
  359. width: 120px;
  360. height: 120px;
  361. }
  362. .title {
  363. width: 90%;
  364. margin: 0 auto 30rpx;
  365. font-size: 18px;
  366. color: #111;
  367. }
  368. }
  369. //
  370. .auth-btncard{
  371. margin-top: 20px;
  372. .btn-unok{
  373. width: 45%;
  374. float: left;
  375. }
  376. .btn-ok{
  377. width: 45%;
  378. float: left;
  379. margin: 0;
  380. padding: 0;
  381. border: 0px solid transparent; //自定义边框
  382. outline: none; //消除默认点击蓝色边框效果
  383. u-button{
  384. font-size: 16px;
  385. margin: 0;
  386. padding: 0;
  387. // border: 0px solid transparent; //自定义边框
  388. outline: none; //消除默认点击蓝色边框效果
  389. }
  390. }
  391. }
  392. .auth-card{
  393. text-align: center;
  394. .avatar-img{
  395. width: 150rpx;
  396. height: 150rpx;
  397. overflow: hidden;
  398. border-radius: 100%;
  399. margin-top: 30rpx;
  400. }
  401. .title{
  402. font-size: 30rpx;
  403. }
  404. .content{
  405. margin-top: 10rpx;
  406. margin-bottom: 20rpx;
  407. }
  408. }
  409. .avatar-wrapper{
  410. width: 150rpx;
  411. height: 100rpx;
  412. color: #333 !important;
  413. text-align: center !important;
  414. border: none !important;
  415. border-radius:0 !important;
  416. background-color:transparent !important;
  417. }
  418. .avatar-wrapper::after {
  419. border: none !important;
  420. }
  421. .avatar{
  422. width: 100rpx;
  423. height: 100rpx;
  424. overflow: hidden;
  425. border-radius: 100%;
  426. }
  427. .rule-wrap{
  428. margin: 40rpx auto;
  429. font-size: 26rpx;
  430. line-height: 1.5;
  431. .link{
  432. white-space: nowrap;
  433. color: #00A447;
  434. }
  435. }
  436. </style>