login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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.showAuthorizeUser=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. }
  195. },
  196. async login(e){
  197. // console.log('e',e);
  198. let _this = this;
  199. let wxinfo = await this.$u.api.wxinfo({code:this.code});
  200. let {openid,sessionKey,unionid} = wxinfo.data;
  201. _this.$u.vuex('vuex_wechatOpenid', openid);
  202. const { errMsg, iv, encryptedData } = e.detail;
  203. if (errMsg !== 'getPhoneNumber:ok') return;
  204. let data = {
  205. code: this.code,
  206. encryptedData: encryptedData,
  207. ivStr: iv,
  208. wechatOpenid:openid,
  209. sessionKey,
  210. sourceType:1,
  211. mobile:this.mobile
  212. }
  213. // console.log('----------登陆中',data)
  214. this.$u.api.login(data).then(res=>{
  215. console.log('微信登录返回结果========',res.data)
  216. _this.$u.vuex('vuex_user_info', res.data);
  217. // _this.$u.vuex('vuex_member_info',res.data);
  218. // uni.setStorageSync('userInfo', res.data)
  219. this.showAuthorizePhone = false
  220. this.getMemberInfo(res.data.userid);
  221. }).catch(err=>{
  222. console.log('err',err);
  223. this.showAuthorizePhone = false
  224. })
  225. },
  226. getMemberInfo(userid){
  227. let _this = this;
  228. this.$u.api.memberInfo({id:userid}).then(res=> {
  229. if(res.code ===200) {
  230. _this.userInfo = res.data;
  231. this.$u.vuex('vuex_member_info', res.data);
  232. // this.$u.vuex('vuex_member_info.avatar', 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0');
  233. if(!res.data.name){
  234. this.$u.vuex('vuex_member_info.name', '微信用户');
  235. this.updateMemberInfo();
  236. }else{
  237. let url = this.backUrl&&this.backUrl.length>0?this.backUrl:'/pages/index/index';
  238. uni.navigateTo({
  239. url: url
  240. })
  241. }
  242. // 下面需要获取微信头像和昵称
  243. // if(res.data.name){
  244. // _this.hasUserInfo = true;
  245. // _this.userInfo = res.data;
  246. // uni.navigateTo({
  247. // url: '/pages/index/index'
  248. // })
  249. // }else{
  250. // _this.showAuthorizeUser = true
  251. // // uni.hideLoading()
  252. // }
  253. // console.log('---------登陆成功')
  254. }
  255. })
  256. },
  257. updateMemberInfo(){
  258. let params ={
  259. id:this.userInfo.id,
  260. // avatar:'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
  261. name:'微信用户'
  262. };
  263. this.$u.api.updateMemberInfo(params).then(res=>{
  264. let url = this.backUrl&&this.backUrl.length>0?this.backUrl:'/pages/index/index';
  265. uni.navigateTo({
  266. url: url
  267. })
  268. }).catch(err=>{
  269. console.log('err',err);
  270. })
  271. },
  272. authUser(){
  273. let userInfo = this.vuex_user_info || {};
  274. // console.log('this===',this);
  275. // console.log('vuex_user_info===',this.vuex_user_info);
  276. // console.log('userInfo',userInfo);
  277. // console.log('this.user',this.user);
  278. if(!this.user.name){
  279. uni.showToast({
  280. title: '请输入您的昵称!',
  281. icon:'error',
  282. duration: 2000
  283. });
  284. return;
  285. }
  286. userInfo.avatar = this.user.avatar;
  287. userInfo.name = this.user.name;
  288. this.userInfo = userInfo;
  289. uni.setStorageSync('userInfo', userInfo)
  290. this.user.id = userInfo.userid;
  291. let param = {
  292. id:this.user.id,
  293. name:this.user.name,
  294. avatar:this.user.avatar
  295. }
  296. this.$u.api.updateMemberInfo(param).then(res=>{
  297. this.hasUserInfo = true;
  298. this.showAuthorizeUser = false;
  299. uni.navigateTo({
  300. url: '/pages/index/index'
  301. })
  302. }).catch(err=>{
  303. console.log('err',err);
  304. })
  305. },
  306. nickNameInput(e){
  307. this.user.name = e.detail.value
  308. }
  309. }
  310. }
  311. </script>
  312. <style lang="scss" scoped>
  313. .body {
  314. // width: 100%;
  315. background-color: #f3f3f3;
  316. padding: 24px;
  317. box-sizing: border-box;
  318. .logo {
  319. padding-top: 200rpx;
  320. margin: auto;
  321. width: 120px;
  322. height: 120px;
  323. }
  324. .title {
  325. width: 90%;
  326. margin: 0 auto 30rpx;
  327. text-align: center;
  328. font-size: 18px;
  329. color: #111;
  330. }
  331. }
  332. //
  333. .auth-btncard{
  334. margin-top: 20px;
  335. .btn-unok{
  336. width: 45%;
  337. float: left;
  338. }
  339. .btn-ok{
  340. width: 45%;
  341. float: left;
  342. margin: 0;
  343. padding: 0;
  344. border: 0px solid transparent; //自定义边框
  345. outline: none; //消除默认点击蓝色边框效果
  346. u-button{
  347. font-size: 16px;
  348. margin: 0;
  349. padding: 0;
  350. // border: 0px solid transparent; //自定义边框
  351. outline: none; //消除默认点击蓝色边框效果
  352. }
  353. }
  354. }
  355. .auth-card{
  356. text-align: center;
  357. .avatar-img{
  358. width: 150rpx;
  359. height: 150rpx;
  360. overflow: hidden;
  361. border-radius: 100%;
  362. margin-top: 30rpx;
  363. }
  364. .title{
  365. font-size: 30rpx;
  366. }
  367. .content{
  368. margin-top: 10rpx;
  369. margin-bottom: 20rpx;
  370. }
  371. }
  372. .avatar-wrapper{
  373. width: 150rpx;
  374. height: 100rpx;
  375. color: #333 !important;
  376. text-align: center !important;
  377. border: none !important;
  378. border-radius:0 !important;
  379. background-color:transparent !important;
  380. }
  381. .avatar-wrapper::after {
  382. border: none !important;
  383. }
  384. .avatar{
  385. width: 100rpx;
  386. height: 100rpx;
  387. overflow: hidden;
  388. border-radius: 100%;
  389. }
  390. </style>