login.vue 13 KB

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