index.vue 8.3 KB

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