index.vue 17 KB

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