index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. SettingModel.data()
  252. .then(setting => {
  253. app.setting = setting
  254. resolve(setting)
  255. }).catch(reject)
  256. })
  257. },
  258. // 获取当前用户信息
  259. getUserInfo() {
  260. const app = this
  261. return new Promise((resolve, reject) => {
  262. !app.isLogin ? resolve(null) : UserApi.info({}, { load: app.isFirstload })
  263. .then(result => {
  264. app.userInfo = result.data.userInfo
  265. resolve(app.userInfo)
  266. })
  267. .catch(err => {
  268. if (err.result && err.result.status == 401) {
  269. app.isLogin = false
  270. resolve(null)
  271. } else {
  272. reject(err)
  273. }
  274. })
  275. })
  276. },
  277. // 获取账户资产
  278. getUserAssets() {
  279. const app = this
  280. return new Promise((resolve, reject) => {
  281. !app.isLogin ? resolve(null) : UserApi.assets({}, { load: app.isFirstload })
  282. .then(result => {
  283. app.assets = result.data.assets
  284. resolve(app.assets)
  285. })
  286. .catch(err => {
  287. if (err.result && err.result.status == 401) {
  288. app.isLogin = false
  289. resolve(null)
  290. } else {
  291. reject(err)
  292. }
  293. })
  294. })
  295. },
  296. // 获取当前用户待处理的订单数量
  297. getTodoCounts() {
  298. const app = this
  299. return new Promise((resolve, reject) => {
  300. !app.isLogin ? resolve(null) : OrderApi.todoCounts({}, { load: app.isFirstload })
  301. .then(result => {
  302. app.todoCounts = result.data.counts
  303. resolve(app.todoCounts)
  304. })
  305. .catch(err => {
  306. if (err.result && err.result.status == 401) {
  307. app.isLogin = false
  308. resolve(null)
  309. } else {
  310. reject(err)
  311. }
  312. })
  313. })
  314. },
  315. // 跳转到登录页
  316. handleLogin() {
  317. !this.isLogin && this.$navTo('pages/login/index')
  318. },
  319. // 退出登录
  320. handleLogout() {
  321. const app = this
  322. uni.showModal({
  323. title: '友情提示',
  324. content: '您确定要退出登录吗?',
  325. success(res) {
  326. if (res.confirm) {
  327. store.dispatch('Logout', {})
  328. .then(result => app.onRefreshPage())
  329. }
  330. }
  331. })
  332. },
  333. // 跳转到钱包页面
  334. onTargetWallet() {
  335. this.$navTo('pages/wallet/index')
  336. },
  337. // 跳转到订单页
  338. onTargetOrder(item) {
  339. this.$navTo('pages/order/index', { dataType: item.id })
  340. },
  341. // 跳转到我的积分页面
  342. onTargetPoints() {
  343. this.$navTo('pages/points/log')
  344. },
  345. // 跳转到我的优惠券页
  346. onTargetMyCoupon() {
  347. this.$navTo('pages/my-coupon/index')
  348. },
  349. // 跳转到服务页面
  350. handleService({ url }) {
  351. this.$navTo(url)
  352. },
  353. },
  354. /**
  355. * 下拉刷新
  356. */
  357. onPullDownRefresh() {
  358. // 获取首页数据
  359. this.getPageData(() => {
  360. uni.stopPullDownRefresh()
  361. })
  362. },
  363. }
  364. </script>
  365. <style lang="scss" scoped>
  366. // 页面头部
  367. .main-header {
  368. background-color: #fff;
  369. // background-image: url('/static/background/user-header.png');
  370. position: relative;
  371. width: 100%;
  372. height: 280rpx;
  373. background-size: 100% 100%;
  374. overflow: hidden;
  375. display: flex;
  376. align-items: center;
  377. // padding-top: 40rpx;
  378. padding-left: 30rpx;
  379. .bg-image {
  380. position: absolute;
  381. top: 0;
  382. left: 0;
  383. width: 100%;
  384. height: 100%;
  385. z-index: 0;
  386. }
  387. .user-info {
  388. display: flex;
  389. height: 100rpx;
  390. z-index: 1;
  391. .user-content {
  392. display: flex;
  393. flex-direction: column;
  394. justify-content: center;
  395. margin-left: 30rpx;
  396. color: #c59a46;
  397. .nick-name {
  398. font-size: 32rpx;
  399. font-weight: bold;
  400. max-width: 270rpx;
  401. }
  402. .mobile {
  403. margin-top: 15rpx;
  404. font-size: 26rpx;
  405. }
  406. .user-grade {
  407. align-self: baseline;
  408. display: flex;
  409. align-items: center;
  410. background: #3c3c3c;
  411. margin-top: 12rpx;
  412. border-radius: 10rpx;
  413. padding: 4rpx 12rpx;
  414. .user-grade_icon .image {
  415. display: block;
  416. width: 32rpx;
  417. height: 32rpx;
  418. }
  419. .user-grade_name {
  420. margin-left: 5rpx;
  421. font-size: 24rpx;
  422. color: #EEE0C3;
  423. }
  424. }
  425. .login-tips {
  426. margin-top: 12rpx;
  427. font-size: 28rpx;
  428. }
  429. }
  430. }
  431. }
  432. // 角标组件
  433. .item-badge {
  434. position: absolute;
  435. top: 0;
  436. right: 55rpx;
  437. background: #fa2209;
  438. color: #fff;
  439. border-radius: 100%;
  440. min-width: 38rpx;
  441. height: 38rpx;
  442. display: flex;
  443. justify-content: center;
  444. align-items: center;
  445. padding: 1rpx;
  446. font-size: 22rpx;
  447. }
  448. // 我的钱包
  449. .my-asset {
  450. display: flex;
  451. background: #fff;
  452. padding: 40rpx 0;
  453. .asset-right {
  454. width: 200rpx;
  455. border-left: 1rpx solid #eee;
  456. }
  457. .asset-right-item {
  458. text-align: center;
  459. color: #545454;
  460. .item-icon {
  461. font-size: 40rpx;
  462. }
  463. .item-name {
  464. margin-top: 10rpx;
  465. }
  466. .item-name text {
  467. font-size: 25rpx;
  468. }
  469. }
  470. .asset-left-item {
  471. text-align: center;
  472. color: #666;
  473. padding: 0 42rpx;
  474. .item-value {
  475. font-size: 32rpx;
  476. color: red;
  477. }
  478. .item-name {
  479. margin-top: 6rpx;
  480. }
  481. .item-name {
  482. font-size: 25rpx;
  483. }
  484. }
  485. }
  486. // 订单操作
  487. .order-navbar {
  488. display: flex;
  489. margin: 20rpx auto 20rpx auto;
  490. padding: 20rpx 0;
  491. width: 94%;
  492. box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  493. font-size: 30rpx;
  494. border-radius: 5rpx;
  495. background: #fff;
  496. &-item {
  497. position: relative;
  498. width: 25%;
  499. .item-icon {
  500. text-align: center;
  501. margin: 0 auto;
  502. padding: 10rpx 0;
  503. color: #545454;
  504. font-size: 40rpx;
  505. }
  506. .item-name {
  507. font-size: 26rpx;
  508. color: #545454;
  509. text-align: center;
  510. margin-right: 10rpx;
  511. }
  512. }
  513. }
  514. // 我的服务
  515. .my-service {
  516. margin: 22rpx auto 22rpx auto;
  517. padding: 20rpx 0;
  518. width: 94%;
  519. box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  520. border-radius: 5rpx;
  521. background: #fff;
  522. .service-title {
  523. padding-left: 20rpx;
  524. margin-bottom: 30rpx;
  525. font-size: 28rpx;
  526. }
  527. .service-content {
  528. // margin-bottom: -30rpx;
  529. .service-item {
  530. position: relative;
  531. width: 25%;
  532. float: left;
  533. margin-bottom: 30rpx;
  534. .item-icon {
  535. text-align: center;
  536. margin: 0 auto;
  537. padding: 10rpx 0;
  538. color: #ff3800;
  539. font-size: 40rpx;
  540. }
  541. .item-name {
  542. font-size: 26rpx;
  543. color: #545454;
  544. text-align: center;
  545. margin-right: 10rpx;
  546. }
  547. }
  548. }
  549. }
  550. // 退出登录
  551. .my-logout {
  552. display: flex;
  553. justify-content: center;
  554. margin-top: 50rpx;
  555. .logout-btn {
  556. width: 50%;
  557. margin: 0 auto;
  558. font-size: 26rpx;
  559. color: #616161;
  560. border-radius: 20rpx;
  561. border: 1px solid #dcdcdc;
  562. padding: 14rpx 0;
  563. text-align: center;
  564. }
  565. }
  566. </style>