index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <!-- 收银台 -->
  2. <template>
  3. <s-layout title="收银台">
  4. <view class="bg-white ss-modal-box ss-flex-col">
  5. <!-- 订单信息 -->
  6. <view class="modal-header ss-flex-col ss-col-center ss-row-center">
  7. <view class="money-box ss-m-b-20">
  8. <text class="money-text">{{ fen2yuan(state.orderInfo.price) }}</text>
  9. </view>
  10. <view class="time-text">
  11. <text>{{ payDescText }}</text>
  12. </view>
  13. </view>
  14. <!-- 支付方式 -->
  15. <view class="modal-content ss-flex-1">
  16. <view class="pay-title ss-p-l-30 ss-m-y-30">选择支付方式</view>
  17. <radio-group @change="onTapPay">
  18. <label class="pay-type-item" v-for="item in state.payMethods" :key="item.title">
  19. <view
  20. class="pay-item ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom"
  21. :class="{ 'disabled-pay-item': item.disabled }"
  22. >
  23. <view class="ss-flex ss-col-center">
  24. <image
  25. class="pay-icon"
  26. v-if="item.disabled"
  27. :src="sheep.$url.static('/static/img/shop/pay/cod_disabled.png')"
  28. mode="aspectFit"
  29. />
  30. <image
  31. class="pay-icon"
  32. v-else
  33. :src="sheep.$url.static(item.icon)"
  34. mode="aspectFit"
  35. />
  36. <text class="pay-title">{{ item.title }}</text>
  37. </view>
  38. <view class="check-box ss-flex ss-col-center ss-p-l-10">
  39. <view class="userInfo-money ss-m-r-10" v-if="item.value === 'wallet'">
  40. 余额: {{ fen2yuan(userWallet.balance) }}元
  41. </view>
  42. <radio
  43. :value="item.value"
  44. color="var(--ui-BG-Main)"
  45. style="transform: scale(0.8)"
  46. :disabled="item.disabled"
  47. :checked="state.payment === item.value"
  48. />
  49. </view>
  50. </view>
  51. </label>
  52. </radio-group>
  53. </view>
  54. <!-- 工具 -->
  55. <view class="modal-footer ss-flex ss-row-center ss-col-center ss-m-t-80 ss-m-b-40">
  56. <button v-if="state.payStatus === 0" class="ss-reset-button past-due-btn">
  57. 检测支付环境中
  58. </button>
  59. <button v-else-if="state.payStatus === -1" class="ss-reset-button past-due-btn" disabled>
  60. 支付已过期
  61. </button>
  62. <button
  63. v-else
  64. class="ss-reset-button save-btn"
  65. @tap="onPay"
  66. :disabled="state.payStatus !== 1"
  67. :class="{ 'disabled-btn': state.payStatus !== 1 }"
  68. >
  69. 立即支付
  70. </button>
  71. </view>
  72. </view>
  73. </s-layout>
  74. </template>
  75. <script setup>
  76. import { computed, reactive } from 'vue';
  77. import { onLoad } from '@dcloudio/uni-app';
  78. import sheep from '@/sheep';
  79. import { fen2yuan, useDurationTime } from '@/sheep/hooks/useGoods';
  80. import PayOrderApi from '@/sheep/api/pay/order';
  81. import PayChannelApi from '@/sheep/api/pay/channel';
  82. import { getPayMethods } from '@/sheep/platform/pay';
  83. const userWallet = computed(() => sheep.$store('user').userWallet);
  84. // 检测支付环境
  85. const state = reactive({
  86. orderType: 'goods', // 订单类型; goods - 商品订单, recharge - 充值订单
  87. orderInfo: {}, // 支付单信息
  88. payStatus: 0, // 0=检测支付环境, -2=未查询到支付单信息, -1=支付已过期, 1=待支付,2=订单已支付
  89. payMethods: [], // 可选的支付方式
  90. payment: '', // 选中的支付方式
  91. });
  92. const onPay = () => {
  93. if (state.payment === '') {
  94. sheep.$helper.toast('请选择支付方式');
  95. return;
  96. }
  97. if (state.payment === 'wallet') {
  98. uni.showModal({
  99. title: '提示',
  100. content: '确定要支付吗?',
  101. success: function (res) {
  102. if (res.confirm) {
  103. sheep.$platform.pay(state.payment, state.orderType, state.orderInfo.id);
  104. }
  105. },
  106. });
  107. } else {
  108. sheep.$platform.pay(state.payment, state.orderType, state.orderInfo.id);
  109. }
  110. };
  111. // 支付文案提示
  112. const payDescText = computed(() => {
  113. if (state.payStatus === 2) {
  114. return '该订单已支付';
  115. }
  116. if (state.payStatus === 1) {
  117. const time = useDurationTime(state.orderInfo.expireTime);
  118. if (time.ms <= 0) {
  119. state.payStatus = -1;
  120. return '';
  121. }
  122. return `剩余支付时间 ${time.h}:${time.m}:${time.s} `;
  123. }
  124. if (state.payStatus === -2) {
  125. return '未查询到支付单信息';
  126. }
  127. return '';
  128. });
  129. // 状态转换:payOrder.status => payStatus
  130. function checkPayStatus() {
  131. if (state.orderInfo.status === 10 || state.orderInfo.status === 20) {
  132. // 支付成功
  133. state.payStatus = 2;
  134. return;
  135. }
  136. if (state.orderInfo.status === 30) {
  137. // 支付关闭
  138. state.payStatus = -1;
  139. return;
  140. }
  141. state.payStatus = 1; // 待支付
  142. }
  143. // 切换支付方式
  144. function onTapPay(e) {
  145. state.payment = e.detail.value;
  146. }
  147. // 设置支付订单信息
  148. async function setOrder(id) {
  149. // 获得支付订单信息
  150. const { data, code } = await PayOrderApi.getOrder(id);
  151. if (code !== 0 || !data) {
  152. state.payStatus = -2;
  153. return;
  154. }
  155. state.orderInfo = data;
  156. // 获得支付方式
  157. await setPayMethods();
  158. // 设置支付状态
  159. checkPayStatus();
  160. }
  161. // 获得支付方式
  162. async function setPayMethods() {
  163. const { data, code } = await PayChannelApi.getEnableChannelCodeList(state.orderInfo.appId);
  164. if (code !== 0) {
  165. return;
  166. }
  167. state.payMethods = getPayMethods(data)
  168. state.payMethods.find(item => {
  169. if (item.value && !item.disabled) {
  170. state.payment = item.value;
  171. return true;
  172. }
  173. });
  174. }
  175. onLoad((options) => {
  176. if (
  177. sheep.$platform.name === 'WechatOfficialAccount' &&
  178. sheep.$platform.os === 'ios' &&
  179. !sheep.$platform.landingPage.includes('pages/pay/index')
  180. ) {
  181. location.reload();
  182. return;
  183. }
  184. // 获得支付订单信息
  185. let id = options.id;
  186. if (options.orderType) {
  187. state.orderType = options.orderType;
  188. }
  189. setOrder(id);
  190. // 刷新钱包的缓存
  191. sheep.$store('user').getWallet();
  192. });
  193. </script>
  194. <style lang="scss" scoped>
  195. .pay-icon {
  196. width: 36rpx;
  197. height: 36rpx;
  198. margin-right: 26rpx;
  199. }
  200. .ss-modal-box {
  201. // max-height: 1000rpx;
  202. .modal-header {
  203. position: relative;
  204. padding: 60rpx 20rpx 40rpx;
  205. .money-text {
  206. color: $red;
  207. font-size: 46rpx;
  208. font-weight: bold;
  209. font-family: OPPOSANS;
  210. &::before {
  211. content: '¥';
  212. font-size: 30rpx;
  213. }
  214. }
  215. .time-text {
  216. font-size: 26rpx;
  217. color: $gray-b;
  218. }
  219. .close-icon {
  220. position: absolute;
  221. top: 10rpx;
  222. right: 20rpx;
  223. font-size: 46rpx;
  224. opacity: 0.2;
  225. }
  226. }
  227. .modal-content {
  228. overflow-y: auto;
  229. .pay-title {
  230. font-size: 26rpx;
  231. font-weight: 500;
  232. color: #333333;
  233. }
  234. .pay-tip {
  235. font-size: 26rpx;
  236. color: #bbbbbb;
  237. }
  238. .pay-item {
  239. height: 86rpx;
  240. }
  241. .disabled-pay-item {
  242. .pay-title {
  243. color: #999999;
  244. }
  245. }
  246. .userInfo-money {
  247. font-size: 26rpx;
  248. color: #bbbbbb;
  249. line-height: normal;
  250. }
  251. }
  252. .save-btn {
  253. width: 710rpx;
  254. height: 80rpx;
  255. border-radius: 40rpx;
  256. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  257. color: $white;
  258. }
  259. .disabled-btn {
  260. background: #e5e5e5;
  261. color: #999999;
  262. }
  263. .past-due-btn {
  264. width: 710rpx;
  265. height: 80rpx;
  266. border-radius: 40rpx;
  267. background-color: #999;
  268. color: #fff;
  269. }
  270. }
  271. </style>