index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <template>
  2. <view class="container">
  3. <!--用户信息-->
  4. <view class="header main-between cross-center">
  5. <view class="user-box main-left cross-center">
  6. <view class="head-img">
  7. <!-- #ifdef MP-KUAISHOU | MP-TOUTIAO -->
  8. <image :src="userInfo.avatar?userInfo.avatar:'/static/image/default-head-img.png'" />
  9. <!-- #endif -->
  10. <!-- #ifdef MP-WEIXIN -->
  11. <button class="avatar cross-center" @click="handleGetWechatUserInfo">
  12. <image :src="userInfo.avatar?userInfo.avatar:'/static/image/default-head-img.png'" />
  13. </button>
  14. <!-- #endif -->
  15. </view>
  16. <view class="base">
  17. <!-- #ifdef MP-KUAISHOU | MP-TOUTIAO -->
  18. <view v-if="!userInfo.nickname" class="nickname cross-center" @click="handleGetUserInfo">未授权登陆</view>
  19. <!-- #endif -->
  20. <!-- #ifdef MP-WEIXIN -->
  21. <view v-if="!userInfo.nickname" class="nickname cross-center" @click="handleGetWechatUserInfo">未授权登陆</view>
  22. <!-- #endif -->
  23. <view v-if="userInfo.nickname" class="nickname cross-center">{{ userInfo.nickname }}</view>
  24. <view v-if="userInfo.info.is_vip" class="vip cross-center">
  25. VIP到期时间: {{ userInfo.info.end_at }}
  26. </view>
  27. </view>
  28. </view>
  29. <!-- #ifdef MP-KUAISHOU | MP-TOUTIAO -->
  30. <button open-type="getUserInfo" class="refresh" @click="handleGetUserInfo">刷新</button>
  31. <!-- #endif -->
  32. <!-- #ifdef MP-WEIXIN -->
  33. <button open-type="getUserInfo" class="refresh" @click="handleGetWechatUserInfo">刷新</button>
  34. <!-- #endif -->
  35. </view>
  36. <!--充值-->
  37. <view class="recharge main-between cross-center">
  38. <view class="static-box main-left cross-center">
  39. <view class="icon">
  40. <image src="/static/image/gold-bag.png" mode="aspectFill" />
  41. </view>
  42. <view class="overage">{{ userInfo.info.integral }}金币</view>
  43. </view>
  44. <view class="recharge-btn" @click="recharge.show = true">充值</view>
  45. </view>
  46. <!--历史-->
  47. <view class="history" @click="$u.route('/pages/my/history')">
  48. <view class="header main-between cross-center">
  49. <text>历史观看记录</text>
  50. <u-icon name="arrow-right" :color="$colors.infoColor" bold />
  51. </view>
  52. <view class="content dir-left-nowrap cross-center">
  53. <view
  54. v-for="(item,index) in history"
  55. :key="index"
  56. class="episode"
  57. >
  58. <view class="cover-image">
  59. <image :src="item.detail.episode.cover_img" mode="aspectFill" />
  60. </view>
  61. </view>
  62. </view>
  63. </view>
  64. <!--菜单-->
  65. <view class="menu-group">
  66. <template v-for="(menu,index) in menus">
  67. <button
  68. v-if="menu.href !== '/pages/share/index' || (menu.href === '/pages/share/index' && userInfo.is_share !== '0')"
  69. :key="index"
  70. class="menu-item main-between cross-center"
  71. :open-type="menu.type ? menu.type : ''"
  72. @bindcontact="handleMenu"
  73. @click="handleMenu(menu)"
  74. >
  75. <view class="left-box dir-left-nowrap cross-center">
  76. <view class="icon">
  77. <image :src="menu.icon" />
  78. </view>
  79. <text>{{ menu.name }}</text>
  80. </view>
  81. <u-icon name="arrow-right" :color="$colors.infoColor" bold />
  82. </button>
  83. </template>
  84. </view>
  85. <!--tab bar-->
  86. <tab-bar />
  87. <!--充值-->
  88. <recharge :show.sync="recharge.show" />
  89. <!-- #ifdef MP-WEIXIN -->
  90. <u-modal
  91. :show="modal.show"
  92. :title="modal.title"
  93. :show-confirm-button="false"
  94. >
  95. <view class="slot-content dir-top-wrap cross-center">
  96. <button open-type="chooseAvatar" class="avatar cross-center" @chooseavatar="handleChooseAvatar">
  97. <image :src="modal.avatar?modal.avatar:'/static/image/default-head-img.png'" />
  98. </button>
  99. <input
  100. class="nickname cross-center"
  101. type="nickname"
  102. :value="modal.nickname"
  103. placeholder="填写昵称"
  104. @change="handleChangeNickname"
  105. >
  106. <button class="confirm" @click="handleConfirmWechatUserInfo">提交</button>
  107. </view>
  108. </u-modal>
  109. <!-- #endif -->
  110. </view>
  111. </template>
  112. <script>
  113. import TabBar from '../../components/TabBar/index'
  114. import { mapState } from 'vuex'
  115. import Recharge from '../../components/Recharge/index'
  116. export default {
  117. name: 'My',
  118. components: { Recharge, TabBar },
  119. data() {
  120. return {
  121. history: [],
  122. menus: [
  123. { icon: '/static/image/my-page/order.png', name: '消费记录', href: '/pages/my/consume' },
  124. { icon: '/static/image/my-page/recharge.png', name: '充值记录', href: '/pages/my/recharge' },
  125. { icon: '/static/image/my-page/contact.png', name: '联系客服', type: 'contact' },
  126. { icon: '/static/image/my-page/share.png', name: '分享中心', href: '/pages/share/index' },
  127. { icon: '/static/image/my-page/protocol.png', name: '用户协议', href: '/pages/my/protocol' }
  128. ],
  129. recharge: {
  130. show: false
  131. },
  132. code: null,
  133. config: {},
  134. modal: {
  135. show: false,
  136. title: '用户信息获取',
  137. nickname: '',
  138. avatar: ''
  139. },
  140. btnLock: false
  141. }
  142. },
  143. computed: {
  144. ...mapState({
  145. userInfo: seate => seate.user.info
  146. })
  147. },
  148. methods: {
  149. getHistory() {
  150. this.$api.user.episode.record({ limit: 3 }).then(res => {
  151. this.history = res.data
  152. })
  153. },
  154. handleMenu(menu) {
  155. // #ifdef MP-KUAISHOU
  156. if (menu.type === 'contact') {
  157. ks.makePhoneCall({
  158. phoneNumber: this.config.contact,
  159. success(res) {
  160. console.log('-->success', res)
  161. },
  162. fail(err) {
  163. console.log('-->error', err)
  164. }
  165. })
  166. }
  167. // #endif
  168. if (menu.href) {
  169. this.$u.route(menu.href)
  170. }
  171. },
  172. // 微信获取头像
  173. handleGetWechatUserInfo() {
  174. this.modal.show = true
  175. this.modal.nickname = this.userInfo.nickname
  176. this.modal.avatar = this.userInfo.avatar
  177. },
  178. // 微信获取头像
  179. handleChooseAvatar({ detail }) {
  180. const _this = this
  181. uni.getFileSystemManager().readFile({
  182. filePath: detail.avatarUrl, // 选择图片返回的相对路径
  183. encoding: 'base64', // 编码格式
  184. success: res => { // 成功的回调
  185. uni.uploadFile({
  186. url: _this.$setting.BASE_URL + '/avatar/upload',
  187. // filePath: 'data:image/png;base64,' + res.data,
  188. filePath: detail.avatarUrl,
  189. name: 'file',
  190. success({ data }) {
  191. const res = JSON.parse(data)
  192. const { url } = res.data
  193. _this.modal.avatar = url
  194. console.log('-->data', _this.modal.avatar)
  195. }
  196. })
  197. // this.modal.avatar = detail.avatarUrl
  198. // console.log('data:image/png;base64,' + res.data)
  199. }
  200. })
  201. },
  202. handleChangeNickname(res) {
  203. this.modal.nickname = res.detail.value
  204. },
  205. // 微信获取头像
  206. handleConfirmWechatUserInfo() {
  207. console.log('-->data', this.modal)
  208. if (!this.modal.avatar) {
  209. this.$u.toast('请上传头像')
  210. return
  211. }
  212. if (!this.modal.nickname) {
  213. this.$u.toast('请填写昵称')
  214. return
  215. }
  216. this.$loading('数据提交中...')
  217. this.$api.user.update({ avatar: this.modal.avatar, nickname: this.modal.nickname }).then(res => {
  218. this.code = null
  219. this.$hideLoading()
  220. this.$store.dispatch('user/info', res.data)
  221. this.modal.show = false
  222. }).catch(() => {
  223. this.$hideLoading()
  224. })
  225. },
  226. handleGetUserInfo(res) {
  227. if (this.btnLock) return
  228. this.btnLock = true
  229. // #ifdef MP-KUAISHOU
  230. this.$loading('数据刷新中...')
  231. uni.authorize({
  232. scope: 'scope.userInfo',
  233. success: () => {
  234. uni.getUserInfo({
  235. withCredentials: true,
  236. success: res => {
  237. this.getCode().then(code => {
  238. const params = {
  239. encryptedData: res.encryptedData,
  240. iv: res.iv,
  241. signature: res.signature,
  242. code: code
  243. }
  244. this.$api.user.update(params).then(res => {
  245. this.btnLock = false
  246. this.code = null
  247. this.$hideLoading()
  248. this.$store.dispatch('user/info', res.data)
  249. }).catch(() => {
  250. this.code = null
  251. this.btnLock = false
  252. this.$hideLoading()
  253. })
  254. })
  255. },
  256. fail: () => {
  257. this.btnLock = false
  258. }
  259. })
  260. },
  261. fail: () => {
  262. this.btnLock = false
  263. }
  264. })
  265. // #endif
  266. // #ifdef MP-TOUTIAO
  267. uni.getUserProfile({
  268. success: res => {
  269. this.getCode().then(code => {
  270. const params = {
  271. encryptedData: res.encryptedData,
  272. iv: res.iv,
  273. signature: res.signature,
  274. code: code
  275. }
  276. this.$loading('数据刷新中...')
  277. this.$api.user.update(params).then(res => {
  278. this.btnLock = false
  279. this.code = null
  280. this.$hideLoading()
  281. this.$store.dispatch('user/info', res.data)
  282. }).catch(() => {
  283. this.code = null
  284. this.btnLock = false
  285. this.$hideLoading()
  286. })
  287. })
  288. },
  289. fail: () => {
  290. this.btnLock = false
  291. }
  292. })
  293. // #endif
  294. },
  295. getCode() {
  296. return new Promise((resolve, reject) => {
  297. if (this.code) {
  298. return resolve(this.code)
  299. }
  300. uni.login({
  301. provider: uni.$u.platform,
  302. success: loginRes => {
  303. this.code = loginRes.code
  304. // 四分钟失效
  305. setTimeout(() => {
  306. this.code = null
  307. }, 1000 * 4 * 60)
  308. resolve(this.code)
  309. },
  310. fail: () => {
  311. this.btnLock = false
  312. reject()
  313. }
  314. })
  315. })
  316. },
  317. getConfig() {
  318. this.$api.setting.config().then(res => {
  319. this.config = res.data
  320. })
  321. }
  322. },
  323. onShow() {
  324. this.getHistory()
  325. this.getConfig()
  326. }
  327. }
  328. </script>
  329. <style lang="scss" scoped>
  330. .container {
  331. padding: 20px 20rpx 180rpx;
  332. font-size: 30rpx;
  333. >.header{
  334. .user-box{
  335. .head-img{
  336. .avatar{
  337. background: transparent;
  338. }
  339. image{
  340. width: 100rpx;
  341. height: 100rpx;
  342. border-radius: 50%;
  343. }
  344. }
  345. .base{
  346. margin-left: 30rpx;
  347. .nickname{
  348. color: $primary-color;
  349. font-size: 32rpx;
  350. }
  351. .vip{
  352. font-size: 22rpx;
  353. color: #fff;
  354. margin-top: 6rpx;
  355. }
  356. }
  357. }
  358. .refresh{
  359. background: $primary-color;
  360. color: #fff;
  361. border-radius: 30rpx;
  362. padding: 10rpx 0;
  363. width: 140rpx;
  364. text-align: center;
  365. letter-spacing: .1rem;
  366. line-height: initial;
  367. }
  368. }
  369. .recharge{
  370. background: url("/static/image/my-recharge-bg.png") no-repeat center;
  371. background-size: 100%;
  372. height: 200rpx;
  373. margin-top: 20rpx;
  374. margin-bottom: 10rpx;
  375. .static-box{
  376. margin-left: 30rpx;
  377. .icon{
  378. border-radius: 20rpx;
  379. image{
  380. width: 100rpx;
  381. height: 100rpx;
  382. }
  383. }
  384. .overage{
  385. font-weight: 800;
  386. margin-left: 20rpx;
  387. font-size: 32rpx;
  388. }
  389. }
  390. .recharge-btn{
  391. border: 1rpx solid #fff;
  392. padding: 10rpx 0 ;
  393. text-align: center;
  394. width: 140rpx;
  395. margin-right: 20rpx;
  396. border-radius: 30rpx;
  397. color: #ffffff;
  398. }
  399. }
  400. .history{
  401. background: $bg-op-color;
  402. height: 300rpx;
  403. border-radius: 15rpx;
  404. padding: 20rpx 30rpx;
  405. margin-bottom: 40rpx;
  406. >.header{
  407. text{
  408. color: $info-color;
  409. font-weight: 600;
  410. }
  411. }
  412. .content{
  413. margin-top: 20rpx;
  414. .episode{
  415. width: calc(100%/4);
  416. margin-left: 20rpx;
  417. &:first-child{
  418. margin-left: 0;
  419. }
  420. .cover-image{
  421. image{
  422. width: 100%;
  423. height: 200rpx;
  424. border-radius: 16rpx;
  425. }
  426. }
  427. }
  428. }
  429. }
  430. .menu-group{
  431. .menu-item{
  432. padding: 20rpx 0;
  433. background: transparent;
  434. border: none;
  435. text-align: unset;
  436. width: 100%;
  437. line-height: initial;
  438. font-size: initial;
  439. justify-content: space-between;
  440. &:after{
  441. content: unset;
  442. }
  443. .left-box{
  444. flex: 1;
  445. .icon{
  446. transform: translateY(4rpx);
  447. image{
  448. width: 55rpx;
  449. height: 55rpx;
  450. }
  451. }
  452. text{
  453. color: #fff;
  454. margin-left: 10rpx;
  455. }
  456. }
  457. }
  458. }
  459. /* 微信获取用户信息 弹窗*/
  460. .slot-content{
  461. width: 100%;
  462. .avatar{
  463. background: transparent;
  464. &:after{
  465. content: unset;
  466. }
  467. image{
  468. width: 140rpx;
  469. height: 140rpx;
  470. border-radius: 50%;
  471. }
  472. }
  473. .nickname{
  474. width: 100%;
  475. margin: 30rpx 0 50rpx;
  476. border: 1rpx solid #ccc;
  477. padding: 0 20rpx;
  478. height: 76rpx;
  479. line-height: 76rpx;
  480. border-radius: 10rpx;
  481. }
  482. .confirm{
  483. background: $primary-color;
  484. color: #fff;
  485. border-radius: 30rpx;
  486. padding: 12rpx 0;
  487. width: 100%;
  488. text-align: center;
  489. letter-spacing: .1rem;
  490. line-height: initial;
  491. }
  492. }
  493. }
  494. </style>