login.vue 12 KB

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