login.vue 11 KB

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