wxLogin.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <template>
  2. <view class="content">
  3. <!-- #ifndef H5 -->
  4. <!-- <image class="bg-img" :src="baseURL+urlFile+'/img/img09.png'" mode=" scaleToFill"></image> -->
  5. <view class="logo-img-box">
  6. <!-- <image class="logo-img" src="https://hongmd.liuniu946.com/static/img/hmdlogo.png" mode=" aspectFit"></image> -->
  7. <image class="logo-img" src="https://xmyl.qiniu1314.com/static/image/logo.png" mode=" aspectFit"></image>
  8. <button class="userInfo" type="warn" @click="isclick?'':userInfoData()" :class="{'nocaction': isclick}">
  9. <!-- <text class="iconfont iconweixin"></text> -->
  10. <text>
  11. 授权登录
  12. </text>
  13. </button>
  14. </view>
  15. <!-- #endif -->
  16. <view class="auth_wrap" :class="isAuth?'show':''">
  17. <view class="mask"></view>
  18. <view class="auth_content">
  19. <view class="auth_top">
  20. <view class="ptitle">获取您的昵称、头像、手机号</view>
  21. <view class="txt">获取用户头像、昵称、手机号信息,主要用于完善个人资料,向用户提供更好使用体验</view>
  22. <view class="close" @tap="closePopup">
  23. <image src="../../static/user/close_x.png" mode=""></image>
  24. </view>
  25. </view>
  26. <view class="auth_ul">
  27. <view class="auth_li">
  28. <view class="tit">头像</view>
  29. <view class="rit">
  30. <button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
  31. <image class="avatar" :src="avatarUrl"></image>
  32. </button>
  33. </view>
  34. </view>
  35. <view class="auth_li">
  36. <view class="tit">昵称</view>
  37. <view class="rit">
  38. <input type="nickname" class="weui-input" placeholder="请输入昵称" @blur="onNickname" />
  39. </view>
  40. </view>
  41. </view>
  42. <view class="confirm_btn" @tap.stop="editUser">保存</view>
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import {
  49. userEdit,
  50. } from '@/api/set.js';
  51. import {
  52. getUserInfo
  53. } from '@/api/login.js';
  54. // #ifdef H5
  55. import {
  56. loginWinxin
  57. } from '@/utils/wxAuthorized';
  58. // #endif
  59. // #ifdef MP-WEIXIN
  60. import {
  61. loginWinxinMp
  62. } from '@/utils/wxMinProgram';
  63. import {
  64. wechatMpAuth
  65. } from '@/api/wx';
  66. // #endif
  67. import {
  68. mapMutations,
  69. mapState
  70. } from 'vuex';
  71. export default {
  72. data() {
  73. return {
  74. isAuth: false, //控制授权弹框显示与隐藏
  75. avatarUrl: 'https://xmyl.qiniu1314.com/static/image/avt.png', //头像
  76. nickname: '', //昵称
  77. isFrist: '', //判断是否是第一次授权
  78. userInfo: {}, //授权用户信息
  79. code: '', //授权code
  80. isclick: false, //是否点击了
  81. };
  82. },
  83. onLoad(option) {
  84. this.loadData();
  85. let AlertLogin = uni.getStorageSync('AlertLogin');
  86. if (AlertLogin) {
  87. console.log('登录过一次')
  88. this.isFrist = 2
  89. } else {
  90. console.log('首次登录');
  91. this.isFrist = 1
  92. }
  93. },
  94. computed: {
  95. // ...mapState(['baseURL','urlFile'])
  96. },
  97. methods: {
  98. // ...mapMutations(['login', 'setUserInfo']),
  99. ...mapMutations('user', ['login', 'setUserInfo']),
  100. goSetBase(url) {
  101. this.backUrl = url;
  102. this.isAuth = true;
  103. },
  104. closePopup() {
  105. this.isAuth = false;
  106. let ur = this.backUrl;
  107. uni.navigateTo({
  108. url: ur,
  109. fail() {
  110. uni.switchTab({
  111. url: ur,
  112. fail() {
  113. uni.switchTab({
  114. url: '/pages/index/index'
  115. })
  116. }
  117. })
  118. }
  119. })
  120. },
  121. editUser() {
  122. userEdit({
  123. avatar: this.avatarUrl,
  124. nickname: this.nickname
  125. })
  126. .then(e => {
  127. this.closePopup()
  128. console.log(e);
  129. })
  130. .catch(e => {
  131. console.log(e);
  132. });
  133. },
  134. //监听昵称变化
  135. onNickname(e) {
  136. this.nickname = e.detail.value;
  137. },
  138. onChooseAvatar(e) {
  139. let _this = this;
  140. uni.showLoading({
  141. title: '上传中'
  142. })
  143. uni.uploadFile({
  144. url: _this.$store.state.baseURL + '/api/upload/image', //仅为示例,非真实的接口地址
  145. filePath: e.detail.avatarUrl,
  146. name: 'file',
  147. header: {
  148. "Authori-zation": 'Bearer ' + uni.getStorageSync('token')
  149. },
  150. success: (uploadFileRes) => {
  151. if ("string" === typeof uploadFileRes.data) {
  152. _this.avatarUrl = JSON.parse(uploadFileRes.data).data.url
  153. } else {
  154. _this.avatarUrl = uploadFileRes.data.data.url
  155. }
  156. },
  157. complete() {
  158. uni.hideLoading()
  159. }
  160. });
  161. },
  162. loadData() {
  163. let obj = this;
  164. // #ifdef H5
  165. loginWinxin();
  166. // #endif
  167. // #ifdef MP-WEIXIN
  168. loginWinxinMp().then(() => {
  169. wx.login({
  170. success(e) {
  171. console.log(e, 'loginWinxinMp');
  172. obj.code = e.code;
  173. },
  174. fill: function(e) {
  175. console.log(e)
  176. }
  177. })
  178. });
  179. // #endif
  180. },
  181. // 用户确认授权
  182. userInfoData() {
  183. let that = this
  184. if (that.isclick) {
  185. return
  186. }
  187. that.isclick = true
  188. wx.getUserProfile({
  189. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  190. success: res => {
  191. console.log(res, 'that.userInfo+++++++++++++++++++')
  192. that.userInfo = res;
  193. uni.showLoading({
  194. title: '授权中',
  195. mask: true
  196. });
  197. that.loadMp();
  198. },
  199. fail: err => {
  200. that.isclick = false
  201. uni.showToast({
  202. title: '您拒绝了请求,不能正常使用小程序',
  203. icon: 'error',
  204. duration: 2000
  205. });
  206. return;
  207. }
  208. });
  209. // this.userInfo = e;
  210. // console.log(e,'用户确认授权')
  211. // this.loadMp()
  212. },
  213. // #ifdef MP-WEIXIN
  214. loadMp() {
  215. let obj = this;
  216. // 获取登录授权页数据
  217. let user = obj.userInfo;
  218. console.log(user)
  219. // 获取推广人id
  220. let spread_spid = uni.getStorageSync('spread') || '';
  221. // #ifdef MP
  222. let spread_code = uni.getStorageSync('spread_code') || '';
  223. // #endif
  224. wechatMpAuth({
  225. code: obj.code,
  226. iv: user.iv,
  227. encryptedData: user.encryptedData,
  228. spread_spid: spread_spid,
  229. // #ifdef MP
  230. spread_code: spread_code,
  231. // #endif
  232. }).then(({
  233. data
  234. }) => {
  235. obj.wchatAuth(data);
  236. console.log(data, 'wechatMpAuth++++++++++++++++++++++++++')
  237. }).catch(err => {
  238. // obj.loding = false;
  239. // uni.hideLoading();
  240. });
  241. },
  242. // #endif
  243. wchatAuth(data) {
  244. let obj = this;
  245. // 保存token
  246. uni.setStorageSync('token', data.token);
  247. console.log(data.token, 'token++++++++++++++')
  248. // 获取用户基础信息
  249. getUserInfo({}).then(e => {
  250. console.log('userInfo+++++++++++', e)
  251. obj.login();
  252. uni.hideLoading();
  253. // 保存返回用户数据
  254. obj.setUserInfo(e.data);
  255. let ur = uni.getStorageSync('present') || '/pages/index/index';
  256. // 用于处理缓存bug
  257. if (ur == 'pages/product/product') {
  258. ur = '/pages/index/index'
  259. }
  260. if (e.data.nickname == '微信用户') {
  261. obj.goSetBase(ur)
  262. } else {
  263. uni.switchTab({
  264. url: ur,
  265. fail(e) {
  266. uni.navigateTo({
  267. url: ur,
  268. fail(e) {
  269. uni.navigateTo({
  270. url: '/pages/index/index',
  271. });
  272. }
  273. });
  274. }
  275. });
  276. }
  277. });
  278. }
  279. }
  280. };
  281. </script>
  282. <style lang="scss">
  283. page,
  284. .content {
  285. height: 100%;
  286. background-color: #fff;
  287. }
  288. .bg-img,
  289. .logo-img-box {
  290. position: absolute;
  291. top: 0;
  292. left: 0;
  293. width: 100%;
  294. height: 100%;
  295. }
  296. .logo-img {
  297. margin-top: 20vh;
  298. margin-left: 176rpx;
  299. width: 385rpx;
  300. height: 394rpx;
  301. }
  302. .userInfo {
  303. margin: 0 100rpx;
  304. margin-top: 50rpx;
  305. color: #FFFFFF;
  306. border-radius: 99rpx;
  307. background-color: $base-color !important;
  308. }
  309. .nocaction {
  310. background-color: #999;
  311. }
  312. .auth_wrap {
  313. position: fixed;
  314. width: 100%;
  315. bottom: -120%;
  316. transition: all 0.35s linear;
  317. &.show {
  318. bottom: 0;
  319. transition: all 0.35s linear;
  320. .mask {
  321. display: block;
  322. }
  323. }
  324. .mask {
  325. width: 100%;
  326. height: 100vh;
  327. position: fixed;
  328. background: rgba(0, 0, 0, 0.5);
  329. z-index: 98;
  330. top: 0;
  331. display: none;
  332. }
  333. .auth_content {
  334. padding: 32rpx 32rpx 72rpx 32rpx;
  335. position: relative;
  336. z-index: 99;
  337. background: #fff;
  338. border-radius: 16rpx 16rpx 0 0;
  339. .auth_top {
  340. position: relative;
  341. .ptitle {
  342. font-size: 30rpx;
  343. font-weight: bold;
  344. margin-bottom: 24rpx;
  345. }
  346. .txt {
  347. font-size: 26rpx;
  348. color: #999;
  349. }
  350. .close {
  351. width: 26rpx;
  352. height: 26rpx;
  353. position: absolute;
  354. right: 0;
  355. top: 0;
  356. image {
  357. width: 100%;
  358. height: 100%;
  359. }
  360. }
  361. }
  362. .auth_ul {
  363. margin-top: 16rpx;
  364. .auth_li {
  365. display: flex;
  366. align-items: center;
  367. border-top: solid 1px #f5f5f5;
  368. padding: 24rpx 0;
  369. &:last-child {
  370. border-bottom: solid 1px #f5f5f5;
  371. }
  372. .tit {
  373. width: 140rpx;
  374. font-size: 30rpx;
  375. }
  376. .rit {
  377. width: calc(100% - 140rpx);
  378. input {
  379. font-size: 28rpx;
  380. height: 72rpx;
  381. width: 100%;
  382. }
  383. image {
  384. width: 54rpx;
  385. height: 54rpx;
  386. border-radius: 50%;
  387. }
  388. button {
  389. width: 100%;
  390. height: 72rpx;
  391. background: #fff;
  392. text-align: left;
  393. padding: 0;
  394. &:after {
  395. border: solid 1px #fff;
  396. }
  397. // opacity: 0;
  398. }
  399. }
  400. }
  401. }
  402. .confirm_btn {
  403. width: 420rpx;
  404. margin: 46rpx auto 0 auto;
  405. background: #39b54a;
  406. color: #fff;
  407. border-radius: 8rpx;
  408. height: 94rpx;
  409. display: flex;
  410. align-items: center;
  411. justify-content: center;
  412. font-size: 30rpx;
  413. }
  414. }
  415. }
  416. </style>