login.vue 11 KB

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