index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <template>
  2. <view v-if="!isFirstload" class="container">
  3. <!-- 页面头部 -->
  4. <view class="main-header"
  5. :style="{ height: $platform == 'H5' ? '240rpx' : '320rpx', paddingTop: $platform == 'H5' ? '0' : '50rpx' }">
  6. <image class="bg-image" src="/static/background/user-header.png" mode="scaleToFill"></image>
  7. <!-- 用户信息 -->
  8. <view v-if="isLogin" class="user-info">
  9. <view class="user-avatar">
  10. <avatar-image :url="userInfo.avatar_url" :width="100" />
  11. </view>
  12. <view class="user-content">
  13. <!-- 会员昵称 -->
  14. <view class="nick-name oneline-hide">{{ userInfo.nick_name }}</view>
  15. <!-- 会员等级 -->
  16. <view v-if="userInfo.grade_id > 0 && userInfo.grade" class="user-grade">
  17. <view class="user-grade_icon">
  18. <image class="image"
  19. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAA0lBMVEUAAAD/tjL/tzH/uDP/uC7/tjH/tzH/tzL/tTH+tTL+tjP/tDD/tTD+tzD/tjL/szD/uDH/tjL/tjL+tjD/tjT/szb/tzL/tTL+uTH+tjL/tjL/tjL/tTT/tjL/tjL+tjH/uTL/vDD/tjL/tjH/tzL9uS//tTL/nBr/sS7/tjH/ujL/szD/uTv+rzf/tzL+tzH+vDP+uzL+tjP+ry7+tDL9ki/7szf/sEX/tTL/tjL+tjL/tTH/tTT/tzH/tzL/tjP/sTX/uTP/wzX+rTn/vDX9vC8m8ckhAAAAOXRSTlMAlnAMB/vjxKWGMh0S6drMiVxPRkEY9PLy0ru0sKagmo5+dGtgVCMgBP716eXWyMGxqJGRe2o5KSmFNjaYAAABP0lEQVQ4y8XS13KDMBAF0AWDDe4t7r3ETu9lVxJgJ/n/X8rKAzHG5TE+Twz3zki7I/g/KXdghIbGJewrU4yzn08Ebgl6TuZzzuOC6W5es3HX6qsSz3NFShRU0MpucytDmOSpu3yULx3CA9RD1HjVedc0jSjqm6ZzhUjDsFDQhSp/OKj5GQvg0+ZCOixsbtDLAeTTOm/yGi8GyIphIVsgH737FEDV44LJa88IRKK/SetrwT9G/GUIr6vXjoy4GXn7+RboVXnghuSjaoGecwQxL2su3CwAKlO+QFoqxI4FMctHQhQd2OhxTu184jWUlI+rMTBTn1/IQcJHQ6GQdZ7pWiDaNdhTt330efISeiqYwQEzQpTlsURJLhzkEmpCPsERfeIUVyXr6MNuIyp5uziW6xURtt7hhGwzmMNJExfO4Bd9X0ZPqAxdNwAAAABJRU5ErkJggg==">
  20. </image>
  21. </view>
  22. <view class="user-grade_name">
  23. <text>{{ userInfo.grade.name }}</text>
  24. </view>
  25. </view>
  26. <!-- 会员无等级时显示手机号 -->
  27. <view v-else class="mobile">{{ userInfo.mobile }}</view>
  28. </view>
  29. </view>
  30. <!-- 未登录 -->
  31. <view v-else class="user-info" @click="handleLogin">
  32. <view class="user-avatar">
  33. <avatar-image :width="100" />
  34. </view>
  35. <view class="user-content">
  36. <view class="nick-name">未登录</view>
  37. <view class="login-tips">点击登录账号</view>
  38. </view>
  39. </view>
  40. </view>
  41. <!-- 我的钱包 -->
  42. <view class="my-asset">
  43. <view class="asset-left flex-box dis-flex flex-x-around">
  44. <view class="asset-left-item" @click="onTargetWallet">
  45. <view class="item-value dis-flex flex-x-center">
  46. <text>{{ isLogin ? assets.balance : '--' }}</text>
  47. </view>
  48. <view class="item-name dis-flex flex-x-center">
  49. <text>账户余额</text>
  50. </view>
  51. </view>
  52. <view class="asset-left-item" @click="onTargetPoints">
  53. <view class="item-value dis-flex flex-x-center">
  54. <text>{{ isLogin ? assets.points : '--' }}</text>
  55. </view>
  56. <view class="item-name dis-flex flex-x-center">
  57. <text>{{ setting[SettingKeyEnum.POINTS.value].points_name }}</text>
  58. </view>
  59. </view>
  60. <view class="asset-left-item" @click="onTargetMyCoupon">
  61. <view class="item-value dis-flex flex-x-center">
  62. <text>{{ isLogin ? assets.coupon : '--' }}</text>
  63. </view>
  64. <view class="item-name dis-flex flex-x-center">
  65. <text>优惠券</text>
  66. </view>
  67. </view>
  68. </view>
  69. <view class="asset-right">
  70. <view class="asset-right-item" @click="onTargetWallet">
  71. <view class="item-icon dis-flex flex-x-center">
  72. <text class="iconfont icon-qianbao"></text>
  73. </view>
  74. <view class="item-name dis-flex flex-x-center">
  75. <text>我的钱包</text>
  76. </view>
  77. </view>
  78. </view>
  79. </view>
  80. <!-- 订单操作 -->
  81. <view class="order-navbar">
  82. <view class="order-navbar-item" v-for="(item, index) in orderNavbar" :key="index" @click="onTargetOrder(item)">
  83. <view class="item-icon">
  84. <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
  85. </view>
  86. <view class="item-name">{{ item.name }}</view>
  87. <view class="item-badge" v-if="item.count && item.count > 0">
  88. <text v-if="item.count <= 99" class="text">{{ item.count }}</text>
  89. <text v-else class="text">99+</text>
  90. </view>
  91. </view>
  92. </view>
  93. <!-- 我的服务 -->
  94. <view class="my-service">
  95. <view class="service-title">我的服务</view>
  96. <view class="service-content clearfix">
  97. <block v-for="(item, index) in service" :key="index">
  98. <view v-if="item.type == 'link'" class="service-item" @click="handleService(item)">
  99. <view class="item-icon">
  100. <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
  101. </view>
  102. <view class="item-name">{{ item.name }}</view>
  103. <view class="item-badge" v-if="item.count && item.count > 0">
  104. <text v-if="item.count <= 99" class="text">{{ item.count }}</text>
  105. <text v-else class="text">99+</text>
  106. </view>
  107. </view>
  108. <view v-if="item.type == 'button' && $platform == 'MP-WEIXIN'" class="service-item">
  109. <button class="btn-normal" :open-type="item.openType">
  110. <view class="item-icon">
  111. <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
  112. </view>
  113. <view class="item-name">{{ item.name }}</view>
  114. </button>
  115. </view>
  116. </block>
  117. </view>
  118. </view>
  119. <!-- 退出登录 -->
  120. <view v-if="isLogin" class="my-logout">
  121. <view class="logout-btn" @click="handleLogout()">
  122. <text>退出登录</text>
  123. </view>
  124. </view>
  125. </view>
  126. </template>
  127. <script>
  128. import store from '@/store'
  129. import AvatarImage from '@/components/avatar-image'
  130. import { setCartTabBadge } from '@/core/app'
  131. import SettingKeyEnum from '@/common/enum/setting/Key'
  132. import SettingModel from '@/common/model/Setting'
  133. import * as UserApi from '@/api/user'
  134. import * as OrderApi from '@/api/order'
  135. import { checkLogin } from '@/core/app'
  136. // 订单操作
  137. const orderNavbar = [
  138. { id: 'all', name: '全部订单', icon: 'qpdingdan' },
  139. { id: 'payment', name: '待支付', icon: 'daifukuan', count: 0 },
  140. { id: 'delivery', name: '待发货', icon: 'daifahuo', count: 0 },
  141. { id: 'received', name: '待收货', icon: 'daishouhuo', count: 0 },
  142. ]
  143. /**
  144. * 我的服务
  145. * id: 标识; name: 标题名称; icon: 图标; type 类型(link和button); url: 跳转的链接
  146. */
  147. const service = [
  148. { id: 'address', name: '收货地址', icon: 'shouhuodizhi', type: 'link', url: 'pages/address/index' },
  149. { id: 'coupon', name: '领券中心', icon: 'lingquan', type: 'link', url: 'pages/coupon/index' },
  150. { id: 'myCoupon', name: '优惠券', icon: 'youhuiquan', type: 'link', url: 'pages/my-coupon/index' },
  151. { id: 'help', name: '我的帮助', icon: 'bangzhu', type: 'link', url: 'pages/help/index' },
  152. { id: 'contact', name: '在线客服', icon: 'kefu', type: 'button', openType: 'contact' },
  153. { id: 'points', name: '我的积分', icon: 'jifen', type: 'link', url: 'pages/points/log' },
  154. { id: 'refund', name: '退换/售后', icon: 'shouhou', type: 'link', url: 'pages/refund/index', count: 0 },
  155. ]
  156. export default {
  157. components: {
  158. AvatarImage
  159. },
  160. data() {
  161. return {
  162. // 枚举类
  163. SettingKeyEnum,
  164. // 当前运行的终端 (此处并不冗余,因为微信小程序端view层无法直接读取$platform)
  165. $platform: this.$platform,
  166. // 正在加载
  167. isLoading: true,
  168. // 首次加载
  169. isFirstload: true,
  170. // 是否已登录
  171. isLogin: false,
  172. // 系统设置
  173. setting: {},
  174. // 当前用户信息
  175. userInfo: {},
  176. // 账户资产
  177. assets: { balance: '--', points: '--', coupon: '--' },
  178. // 我的服务
  179. service,
  180. // 订单操作
  181. orderNavbar,
  182. // 当前用户待处理的订单数量
  183. todoCounts: { payment: 0, deliver: 0, received: 0 }
  184. }
  185. },
  186. /**
  187. * 生命周期函数--监听页面显示
  188. */
  189. onShow(options) {
  190. this.onRefreshPage()
  191. },
  192. methods: {
  193. // 刷新页面
  194. onRefreshPage() {
  195. // 更新购物车角标
  196. setCartTabBadge()
  197. // 判断是否已登录
  198. this.isLogin = checkLogin()
  199. // 获取页面数据
  200. this.getPageData()
  201. },
  202. // 获取页面数据
  203. getPageData(callback) {
  204. const app = this
  205. app.isLoading = true
  206. Promise.all([app.getSetting(), app.getUserInfo(), app.getUserAssets(), app.getTodoCounts()])
  207. .then(result => {
  208. app.isFirstload = false
  209. // 初始化我的服务数据
  210. app.initService()
  211. // 初始化订单操作数据
  212. app.initOrderTabbar()
  213. // 执行回调函数
  214. callback && callback()
  215. })
  216. .catch(err => console.log('catch', err))
  217. .finally(() => app.isLoading = false)
  218. },
  219. // 初始化我的服务数据
  220. initService() {
  221. const app = this
  222. const newService = []
  223. service.forEach(item => {
  224. if (item.id === 'points') {
  225. item.name = '我的' + app.setting[SettingKeyEnum.POINTS.value].points_name
  226. }
  227. // 数据角标
  228. if (item.count != undefined) {
  229. item.count = app.todoCounts[item.id]
  230. }
  231. newService.push(item)
  232. })
  233. app.service = newService
  234. },
  235. // 初始化订单操作数据
  236. initOrderTabbar() {
  237. const app = this
  238. const newOrderNavbar = []
  239. orderNavbar.forEach(item => {
  240. if (item.count != undefined) {
  241. item.count = app.todoCounts[item.id]
  242. }
  243. newOrderNavbar.push(item)
  244. })
  245. app.orderNavbar = newOrderNavbar
  246. },
  247. // 获取商城设置
  248. getSetting() {
  249. const app = this
  250. return new Promise((resolve, reject) => {
  251. // 优化建议:data()方法传参true,启用缓存
  252. SettingModel.data(true)
  253. .then(setting => {
  254. app.setting = setting
  255. resolve(setting)
  256. }).catch(reject)
  257. })
  258. },
  259. // 获取当前用户信息
  260. getUserInfo() {
  261. const app = this
  262. return new Promise((resolve, reject) => {
  263. !app.isLogin ? resolve(null) : UserApi.info({}, { load: app.isFirstload })
  264. .then(result => {
  265. app.userInfo = result.data.userInfo
  266. resolve(app.userInfo)
  267. })
  268. .catch(err => {
  269. if (err.result && err.result.status == 401) {
  270. app.isLogin = false
  271. resolve(null)
  272. } else {
  273. reject(err)
  274. }
  275. })
  276. })
  277. },
  278. // 获取账户资产
  279. getUserAssets() {
  280. const app = this
  281. return new Promise((resolve, reject) => {
  282. !app.isLogin ? resolve(null) : UserApi.assets({}, { load: app.isFirstload })
  283. .then(result => {
  284. app.assets = result.data.assets
  285. resolve(app.assets)
  286. })
  287. .catch(err => {
  288. if (err.result && err.result.status == 401) {
  289. app.isLogin = false
  290. resolve(null)
  291. } else {
  292. reject(err)
  293. }
  294. })
  295. })
  296. },
  297. // 获取当前用户待处理的订单数量
  298. getTodoCounts() {
  299. const app = this
  300. return new Promise((resolve, reject) => {
  301. !app.isLogin ? resolve(null) : OrderApi.todoCounts({}, { load: app.isFirstload })
  302. .then(result => {
  303. app.todoCounts = result.data.counts
  304. resolve(app.todoCounts)
  305. })
  306. .catch(err => {
  307. if (err.result && err.result.status == 401) {
  308. app.isLogin = false
  309. resolve(null)
  310. } else {
  311. reject(err)
  312. }
  313. })
  314. })
  315. },
  316. // 跳转到登录页
  317. handleLogin() {
  318. !this.isLogin && this.$navTo('pages/login/index')
  319. },
  320. // 退出登录
  321. handleLogout() {
  322. const app = this
  323. uni.showModal({
  324. title: '友情提示',
  325. content: '您确定要退出登录吗?',
  326. success(res) {
  327. if (res.confirm) {
  328. store.dispatch('Logout', {})
  329. .then(result => app.onRefreshPage())
  330. }
  331. }
  332. })
  333. },
  334. // 跳转到钱包页面
  335. onTargetWallet() {
  336. this.$navTo('pages/wallet/index')
  337. },
  338. // 跳转到订单页
  339. onTargetOrder(item) {
  340. this.$navTo('pages/order/index', { dataType: item.id })
  341. },
  342. // 跳转到我的积分页面
  343. onTargetPoints() {
  344. this.$navTo('pages/points/log')
  345. },
  346. // 跳转到我的优惠券页
  347. onTargetMyCoupon() {
  348. this.$navTo('pages/my-coupon/index')
  349. },
  350. // 跳转到服务页面
  351. handleService({ url }) {
  352. this.$navTo(url)
  353. },
  354. },
  355. /**
  356. * 下拉刷新
  357. */
  358. onPullDownRefresh() {
  359. // 获取首页数据
  360. this.getPageData(() => {
  361. uni.stopPullDownRefresh()
  362. })
  363. },
  364. }
  365. </script>
  366. <style lang="scss" scoped>
  367. // 页面头部
  368. .main-header {
  369. background-color: #fff;
  370. // background-image: url('/static/background/user-header.png');
  371. position: relative;
  372. width: 100%;
  373. height: 280rpx;
  374. background-size: 100% 100%;
  375. overflow: hidden;
  376. display: flex;
  377. align-items: center;
  378. // padding-top: 40rpx;
  379. padding-left: 30rpx;
  380. .bg-image {
  381. position: absolute;
  382. top: 0;
  383. left: 0;
  384. width: 100%;
  385. height: 100%;
  386. z-index: 0;
  387. }
  388. .user-info {
  389. display: flex;
  390. height: 100rpx;
  391. z-index: 1;
  392. .user-content {
  393. display: flex;
  394. flex-direction: column;
  395. justify-content: center;
  396. margin-left: 30rpx;
  397. color: #c59a46;
  398. .nick-name {
  399. font-size: 32rpx;
  400. font-weight: bold;
  401. max-width: 270rpx;
  402. }
  403. .mobile {
  404. margin-top: 15rpx;
  405. font-size: 26rpx;
  406. }
  407. .user-grade {
  408. align-self: baseline;
  409. display: flex;
  410. align-items: center;
  411. background: #3c3c3c;
  412. margin-top: 12rpx;
  413. border-radius: 10rpx;
  414. padding: 4rpx 12rpx;
  415. .user-grade_icon .image {
  416. display: block;
  417. width: 32rpx;
  418. height: 32rpx;
  419. }
  420. .user-grade_name {
  421. margin-left: 5rpx;
  422. font-size: 24rpx;
  423. color: #EEE0C3;
  424. }
  425. }
  426. .login-tips {
  427. margin-top: 12rpx;
  428. font-size: 28rpx;
  429. }
  430. }
  431. }
  432. }
  433. // 角标组件
  434. .item-badge {
  435. position: absolute;
  436. top: 0;
  437. right: 55rpx;
  438. background: #fa2209;
  439. color: #fff;
  440. border-radius: 100%;
  441. min-width: 38rpx;
  442. height: 38rpx;
  443. display: flex;
  444. justify-content: center;
  445. align-items: center;
  446. padding: 1rpx;
  447. font-size: 22rpx;
  448. }
  449. // 我的钱包
  450. .my-asset {
  451. display: flex;
  452. background: #fff;
  453. padding: 40rpx 0;
  454. .asset-right {
  455. width: 200rpx;
  456. border-left: 1rpx solid #eee;
  457. }
  458. .asset-right-item {
  459. text-align: center;
  460. color: #545454;
  461. .item-icon {
  462. font-size: 40rpx;
  463. }
  464. .item-name {
  465. margin-top: 10rpx;
  466. }
  467. .item-name text {
  468. font-size: 25rpx;
  469. }
  470. }
  471. .asset-left-item {
  472. text-align: center;
  473. color: #666;
  474. padding: 0 42rpx;
  475. .item-value {
  476. font-size: 32rpx;
  477. color: red;
  478. }
  479. .item-name {
  480. margin-top: 6rpx;
  481. }
  482. .item-name {
  483. font-size: 25rpx;
  484. }
  485. }
  486. }
  487. // 订单操作
  488. .order-navbar {
  489. display: flex;
  490. margin: 20rpx auto 20rpx auto;
  491. padding: 20rpx 0;
  492. width: 94%;
  493. box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  494. font-size: 30rpx;
  495. border-radius: 5rpx;
  496. background: #fff;
  497. &-item {
  498. position: relative;
  499. width: 25%;
  500. .item-icon {
  501. text-align: center;
  502. margin: 0 auto;
  503. padding: 10rpx 0;
  504. color: #545454;
  505. font-size: 40rpx;
  506. }
  507. .item-name {
  508. font-size: 26rpx;
  509. color: #545454;
  510. text-align: center;
  511. margin-right: 10rpx;
  512. }
  513. }
  514. }
  515. // 我的服务
  516. .my-service {
  517. margin: 22rpx auto 22rpx auto;
  518. padding: 20rpx 0;
  519. width: 94%;
  520. box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  521. border-radius: 5rpx;
  522. background: #fff;
  523. .service-title {
  524. padding-left: 20rpx;
  525. margin-bottom: 30rpx;
  526. font-size: 28rpx;
  527. }
  528. .service-content {
  529. // margin-bottom: -30rpx;
  530. .service-item {
  531. position: relative;
  532. width: 25%;
  533. float: left;
  534. margin-bottom: 30rpx;
  535. .item-icon {
  536. text-align: center;
  537. margin: 0 auto;
  538. padding: 10rpx 0;
  539. color: #ff3800;
  540. font-size: 40rpx;
  541. }
  542. .item-name {
  543. font-size: 26rpx;
  544. color: #545454;
  545. text-align: center;
  546. margin-right: 10rpx;
  547. }
  548. }
  549. }
  550. }
  551. // 退出登录
  552. .my-logout {
  553. display: flex;
  554. justify-content: center;
  555. margin-top: 50rpx;
  556. .logout-btn {
  557. width: 50%;
  558. margin: 0 auto;
  559. font-size: 26rpx;
  560. color: #616161;
  561. border-radius: 20rpx;
  562. border: 1px solid #dcdcdc;
  563. padding: 14rpx 0;
  564. text-align: center;
  565. }
  566. }
  567. </style>