index.vue 17 KB

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