index.vue 18 KB

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