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