index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <template>
  2. <view v-if="!isLoading" 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-center">
  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. isLogin: false,
  163. // 系统设置
  164. setting: {},
  165. // 当前用户信息
  166. userInfo: {},
  167. // 账户资产
  168. assets: { balance: '--', points: '--', coupon: '--' },
  169. // 我的服务
  170. service,
  171. // 订单操作
  172. orderNavbar,
  173. // 当前用户待处理的订单数量
  174. todoCounts: { payment: 0, deliver: 0, received: 0 }
  175. }
  176. },
  177. /**
  178. * 生命周期函数--监听页面显示
  179. */
  180. onShow(options) {
  181. this.onRefreshPage()
  182. },
  183. methods: {
  184. // 刷新页面
  185. onRefreshPage() {
  186. // 更新购物车角标
  187. setCartTabBadge()
  188. // 判断是否已登录
  189. this.isLogin = checkLogin()
  190. // 获取页面数据
  191. this.getPageData()
  192. },
  193. // 获取页面数据
  194. getPageData(callback) {
  195. const app = this
  196. app.isLoading = true
  197. Promise.all([app.getSetting(), app.getUserInfo(), app.getUserAssets(), app.getTodoCounts()])
  198. .then(result => {
  199. app.isLoading = false
  200. // 初始化我的服务数据
  201. app.initService()
  202. // 初始化订单操作数据
  203. app.initOrderTabbar()
  204. // 执行回调函数
  205. callback && callback()
  206. })
  207. .catch(err => {
  208. console.log('catch', err)
  209. })
  210. },
  211. // 初始化我的服务数据
  212. initService() {
  213. const app = this
  214. const newService = []
  215. service.forEach(item => {
  216. if (item.id === 'points') {
  217. item.name = '我的' + app.setting[SettingKeyEnum.POINTS.value].points_name
  218. }
  219. newService.push(item)
  220. })
  221. app.service = newService
  222. },
  223. // 初始化订单操作数据
  224. initOrderTabbar() {
  225. const app = this
  226. const newOrderNavbar = []
  227. orderNavbar.forEach(item => {
  228. if (item.hasOwnProperty('count')) {
  229. item.count = app.todoCounts[item.id]
  230. }
  231. newOrderNavbar.push(item)
  232. })
  233. app.orderNavbar = newOrderNavbar
  234. },
  235. // 获取商城设置
  236. getSetting() {
  237. const app = this
  238. return new Promise((resolve, reject) => {
  239. // 优化建议:data()方法传参true,启用缓存
  240. SettingModel.data().then(setting => {
  241. app.setting = setting
  242. resolve(setting)
  243. }).catch(reject)
  244. })
  245. },
  246. // 获取当前用户信息
  247. getUserInfo() {
  248. const app = this
  249. return new Promise((resolve, reject) => {
  250. !app.isLogin ? resolve(null) : UserApi.info()
  251. .then(result => {
  252. app.userInfo = result.data.userInfo
  253. resolve(app.userInfo)
  254. })
  255. .catch(err => {
  256. if (err.result && err.result.status == 401) {
  257. app.isLogin = false
  258. resolve(null)
  259. } else {
  260. reject(err)
  261. }
  262. })
  263. })
  264. },
  265. // 获取账户资产
  266. getUserAssets() {
  267. const app = this
  268. return new Promise((resolve, reject) => {
  269. !app.isLogin ? resolve(null) : UserApi.assets()
  270. .then(result => {
  271. app.assets = result.data.assets
  272. resolve(app.assets)
  273. })
  274. .catch(err => {
  275. if (err.result && err.result.status == 401) {
  276. app.isLogin = false
  277. resolve(null)
  278. } else {
  279. reject(err)
  280. }
  281. })
  282. })
  283. },
  284. // 获取当前用户待处理的订单数量
  285. getTodoCounts() {
  286. const app = this
  287. return new Promise((resolve, reject) => {
  288. !app.isLogin ? resolve(null) : OrderApi.todoCounts()
  289. .then(result => {
  290. app.todoCounts = result.data.counts
  291. resolve(app.todoCounts)
  292. })
  293. .catch(err => {
  294. if (err.result && err.result.status == 401) {
  295. app.isLogin = false
  296. resolve(null)
  297. } else {
  298. reject(err)
  299. }
  300. })
  301. })
  302. },
  303. // 跳转到登录页
  304. handleLogin() {
  305. !this.isLogin && this.$navTo('pages/login/index')
  306. },
  307. // 退出登录
  308. handleLogout() {
  309. const app = this
  310. uni.showModal({
  311. title: '友情提示',
  312. content: '您确定要退出登录吗?',
  313. success(res) {
  314. if (res.confirm) {
  315. store.dispatch('Logout', {})
  316. .then(result => app.onRefreshPage())
  317. }
  318. }
  319. })
  320. },
  321. // 跳转到钱包页面
  322. onTargetWallet() {
  323. this.$navTo('pages/wallet/index')
  324. },
  325. // 跳转到订单页
  326. onTargetOrder(item) {
  327. this.$navTo('pages/order/index', { dataType: item.id })
  328. },
  329. // 跳转到我的积分页面
  330. onTargetPoints() {
  331. this.$navTo('pages/points/log')
  332. },
  333. // 跳转到我的优惠券页
  334. onTargetMyCoupon() {
  335. this.$navTo('pages/my-coupon/index')
  336. },
  337. // 跳转到服务页面
  338. handleService({ url }) {
  339. this.$navTo(url)
  340. },
  341. },
  342. /**
  343. * 下拉刷新
  344. */
  345. onPullDownRefresh() {
  346. // 获取首页数据
  347. this.getPageData(() => {
  348. uni.stopPullDownRefresh()
  349. })
  350. },
  351. }
  352. </script>
  353. <style lang="scss" scoped>
  354. // 页面头部
  355. .main-header {
  356. background-color: #fff;
  357. // background-image: url('/static/background/user-header.png');
  358. position: relative;
  359. width: 100%;
  360. height: 280rpx;
  361. background-size: 100% 100%;
  362. overflow: hidden;
  363. display: flex;
  364. align-items: center;
  365. // padding-top: 40rpx;
  366. padding-left: 30rpx;
  367. .bg-image {
  368. position: absolute;
  369. top: 0;
  370. left: 0;
  371. width: 100%;
  372. height: 100%;
  373. z-index: 0;
  374. }
  375. .user-info {
  376. display: flex;
  377. height: 100rpx;
  378. z-index: 1;
  379. .user-content {
  380. display: flex;
  381. flex-direction: column;
  382. justify-content: center;
  383. margin-left: 30rpx;
  384. color: #c59a46;
  385. .nick-name {
  386. font-size: 32rpx;
  387. font-weight: bold;
  388. max-width: 270rpx;
  389. }
  390. .mobile {
  391. margin-top: 15rpx;
  392. font-size: 26rpx;
  393. }
  394. .user-grade {
  395. align-self: baseline;
  396. display: flex;
  397. align-items: center;
  398. background: #3c3c3c;
  399. margin-top: 12rpx;
  400. border-radius: 10rpx;
  401. padding: 4rpx 12rpx;
  402. .user-grade_icon .image {
  403. display: block;
  404. width: 32rpx;
  405. height: 32rpx;
  406. }
  407. .user-grade_name {
  408. margin-left: 5rpx;
  409. font-size: 24rpx;
  410. color: #EEE0C3;
  411. }
  412. }
  413. .login-tips {
  414. margin-top: 12rpx;
  415. font-size: 28rpx;
  416. }
  417. }
  418. }
  419. }
  420. // 我的钱包
  421. .my-asset {
  422. display: flex;
  423. background: #fff;
  424. padding: 40rpx 0;
  425. .asset-right {
  426. width: 200rpx;
  427. border-left: 1rpx solid #eee;
  428. }
  429. .asset-right-item {
  430. text-align: center;
  431. color: #545454;
  432. .item-icon {
  433. font-size: 40rpx;
  434. }
  435. .item-name {
  436. margin-top: 10rpx;
  437. }
  438. .item-name text {
  439. font-size: 25rpx;
  440. }
  441. }
  442. .asset-left-item {
  443. text-align: center;
  444. color: #666;
  445. padding: 0 42rpx;
  446. .item-value {
  447. font-size: 32rpx;
  448. color: red;
  449. }
  450. .item-name {
  451. margin-top: 6rpx;
  452. }
  453. .item-name {
  454. font-size: 25rpx;
  455. }
  456. }
  457. }
  458. // 订单操作
  459. .order-navbar {
  460. display: flex;
  461. margin: 20rpx auto 20rpx auto;
  462. padding: 20rpx 0;
  463. width: 94%;
  464. box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  465. font-size: 30rpx;
  466. border-radius: 5rpx;
  467. background: #fff;
  468. &-item {
  469. position: relative;
  470. width: 25%;
  471. .item-icon {
  472. text-align: center;
  473. margin: 0 auto;
  474. padding: 10rpx 0;
  475. color: #545454;
  476. font-size: 40rpx;
  477. }
  478. .item-name {
  479. font-size: 26rpx;
  480. color: #545454;
  481. text-align: center;
  482. margin-right: 10rpx;
  483. }
  484. .order-badge {
  485. position: absolute;
  486. top: 0;
  487. right: 55rpx;
  488. font-size: 22rpx;
  489. background: #fa2209;
  490. text-align: center;
  491. line-height: 28rpx;
  492. color: #fff;
  493. border-radius: 100%;
  494. min-height: 30rpx;
  495. min-width: 30rpx;
  496. padding: 1rpx;
  497. }
  498. }
  499. }
  500. // 我的服务
  501. .my-service {
  502. margin: 22rpx auto 22rpx auto;
  503. padding: 20rpx 0;
  504. width: 94%;
  505. box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  506. border-radius: 5rpx;
  507. background: #fff;
  508. .service-title {
  509. padding-left: 20rpx;
  510. margin-bottom: 30rpx;
  511. font-size: 28rpx;
  512. }
  513. .service-content {
  514. // margin-bottom: -30rpx;
  515. .service-item {
  516. position: relative;
  517. width: 25%;
  518. float: left;
  519. margin-bottom: 30rpx;
  520. .item-icon {
  521. text-align: center;
  522. margin: 0 auto;
  523. padding: 10rpx 0;
  524. color: #ff3800;
  525. font-size: 40rpx;
  526. }
  527. .item-name {
  528. font-size: 26rpx;
  529. color: #545454;
  530. text-align: center;
  531. margin-right: 10rpx;
  532. }
  533. }
  534. }
  535. }
  536. // 退出登录
  537. .my-logout {
  538. display: flex;
  539. justify-content: center;
  540. margin-top: 30rpx;
  541. .logout-btn {
  542. margin: 0 auto;
  543. display: inline-block;
  544. font-size: 26rpx;
  545. color: #616161;
  546. border-radius: 20rpx;
  547. border: 1px solid #dcdcdc;
  548. padding: 14rpx 150rpx;
  549. }
  550. }
  551. </style>