index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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, goPayResult } 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. // 跳转回支付成功页
  135. uni.showModal({
  136. title: '提示',
  137. content: '订单已支付',
  138. showCancel: false,
  139. success: function () {
  140. goPayResult(state.orderInfo.id, state.orderType);
  141. },
  142. });
  143. return;
  144. }
  145. if (state.orderInfo.status === 30) {
  146. // 支付关闭
  147. state.payStatus = -1;
  148. return;
  149. }
  150. state.payStatus = 1; // 待支付
  151. }
  152. // 切换支付方式
  153. function onTapPay(e) {
  154. state.payment = e.detail.value;
  155. }
  156. // 设置支付订单信息
  157. async function setOrder(id) {
  158. // 获得支付订单信息
  159. const { data, code } = await PayOrderApi.getOrder(id, true);
  160. if (code !== 0 || !data) {
  161. state.payStatus = -2;
  162. return;
  163. }
  164. state.orderInfo = data;
  165. // 设置支付状态
  166. checkPayStatus();
  167. // 获得支付方式
  168. await setPayMethods();
  169. }
  170. // 获得支付方式
  171. async function setPayMethods() {
  172. const { data, code } = await PayChannelApi.getEnableChannelCodeList(state.orderInfo.appId);
  173. if (code !== 0) {
  174. return;
  175. }
  176. state.payMethods = getPayMethods(data);
  177. state.payMethods.find((item) => {
  178. if (item.value && !item.disabled) {
  179. state.payment = item.value;
  180. return true;
  181. }
  182. });
  183. }
  184. onLoad((options) => {
  185. if (
  186. sheep.$platform.name === 'WechatOfficialAccount' &&
  187. sheep.$platform.os === 'ios' &&
  188. !sheep.$platform.landingPage.includes('pages/pay/index')
  189. ) {
  190. location.reload();
  191. return;
  192. }
  193. // 获得支付订单信息
  194. let id = options.id;
  195. if (options.orderType) {
  196. state.orderType = options.orderType;
  197. }
  198. setOrder(id);
  199. // 刷新钱包的缓存
  200. sheep.$store('user').getWallet();
  201. });
  202. </script>
  203. <style lang="scss" scoped>
  204. .pay-icon {
  205. width: 36rpx;
  206. height: 36rpx;
  207. margin-right: 26rpx;
  208. }
  209. .ss-modal-box {
  210. // max-height: 1000rpx;
  211. .modal-header {
  212. position: relative;
  213. padding: 60rpx 20rpx 40rpx;
  214. .money-text {
  215. color: $red;
  216. font-size: 46rpx;
  217. font-weight: bold;
  218. font-family: OPPOSANS;
  219. &::before {
  220. content: '¥';
  221. font-size: 30rpx;
  222. }
  223. }
  224. .time-text {
  225. font-size: 26rpx;
  226. color: $gray-b;
  227. }
  228. .close-icon {
  229. position: absolute;
  230. top: 10rpx;
  231. right: 20rpx;
  232. font-size: 46rpx;
  233. opacity: 0.2;
  234. }
  235. }
  236. .modal-content {
  237. overflow-y: auto;
  238. .pay-title {
  239. font-size: 26rpx;
  240. font-weight: 500;
  241. color: #333333;
  242. }
  243. .pay-tip {
  244. font-size: 26rpx;
  245. color: #bbbbbb;
  246. }
  247. .pay-item {
  248. height: 86rpx;
  249. }
  250. .disabled-pay-item {
  251. .pay-title {
  252. color: #999999;
  253. }
  254. }
  255. .userInfo-money {
  256. font-size: 26rpx;
  257. color: #bbbbbb;
  258. line-height: normal;
  259. }
  260. }
  261. .save-btn {
  262. width: 710rpx;
  263. height: 80rpx;
  264. border-radius: 40rpx;
  265. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  266. color: $white;
  267. }
  268. .disabled-btn {
  269. background: #e5e5e5;
  270. color: #999999;
  271. }
  272. .past-due-btn {
  273. width: 710rpx;
  274. height: 80rpx;
  275. border-radius: 40rpx;
  276. background-color: #999;
  277. color: #fff;
  278. }
  279. }
  280. </style>