index.vue 17 KB

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