index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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" v-if="state.orderInfo.price">
  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 class="pay-item ss-flex ss-col-center ss-row-between ss-p-x-30 border-bottom"
  20. :class="{ 'disabled-pay-item': item.disabled }">
  21. <view class="ss-flex ss-col-center">
  22. <image class="pay-icon" v-if="item.disabled"
  23. :src="sheep.$url.static('/static/img/shop/pay/cod_disabled.png')"
  24. mode="aspectFit" />
  25. <!-- @/static/icon/cart.png -->
  26. <image class="pay-icon" v-else :src="item.icon" mode="aspectFit" />
  27. <text class="pay-title">{{ item.title }}</text>
  28. </view>
  29. <view class="check-box ss-flex ss-col-center ss-p-l-10">
  30. <radio :value="item.value" color="var(--ui-BG-Main)" style="transform: scale(0.8)"
  31. :disabled="item.disabled" :checked="state.payment === item.value" />
  32. </view>
  33. </view>
  34. </label>
  35. </radio-group>
  36. </view>
  37. <!-- 如果是0元,被佣金或者消费分抵扣完了 走验证码 -->
  38. <view class="" v-else>
  39. <uni-forms :model="state.model" :rules="state.rules" validateTrigger="bind" labelPosition="left" border
  40. class="form-box" labelWidth='200' ref="FormRef">
  41. <view class="bg-white ss-p-x-30">
  42. <uni-forms-item name="mobile" label="手机号" class="mobile loginUniFormItem ss-p-t-10">
  43. <uni-easyinput v-model="state.model.smsCodeUseReqDTO.mobile" type="smsCodeUseReqDTO"
  44. placeholder="请输入手机号" :inputBorder="false" :clearable="false" :disabled="true">
  45. <template v-slot:right>
  46. <button class="ss-reset-button code-btn code-btn-start"
  47. :disabled="state.payStatus !== 1"
  48. @tap="getSmsCode('zeroBuy', state.model.smsCodeUseReqDTO.mobile)">
  49. {{ getSmsTimer('zeroBuy') }}
  50. </button>
  51. </template>
  52. </uni-easyinput>
  53. </uni-forms-item>
  54. <uni-forms-item name="smsCodeUseReqDTO.code" label="验证码" :required="true">
  55. <uni-easyinput v-model="state.model.smsCodeUseReqDTO.code" type="number"
  56. placeholder="请输入验证码" :inputBorder="false" :clearable="false">
  57. </uni-easyinput>
  58. </uni-forms-item>
  59. </view>
  60. </uni-forms>
  61. </view>
  62. <!-- 工具 -->
  63. <view class="modal-footer ss-flex ss-row-center ss-col-center ss-m-t-80 ss-m-b-40">
  64. <button v-if="state.payStatus === 0" class="ss-reset-button past-due-btn">
  65. 检测支付环境中
  66. </button>
  67. <button v-else-if="state.payStatus === -1" class="ss-reset-button past-due-btn" disabled>
  68. 支付已过期
  69. </button>
  70. <!-- 支付状态没过期,并且支付金额不为0 -->
  71. <button v-else-if="state.payStatus === 1 && state.orderInfo.price" class="ss-reset-button save-btn"
  72. @tap="onPay" :disabled="state.payStatus !== 1" :class="{ 'disabled-btn': state.payStatus !== 1 }">
  73. 立即支付
  74. </button>
  75. <!-- 支付状态没过期,并且支付金额为0 -->
  76. <button v-else class="ss-reset-button save-btn" @tap="onZeroPay" :disabled="state.payStatus !== 1"
  77. :class="{ 'disabled-btn': state.payStatus !== 1 }">
  78. 立即支付
  79. </button>
  80. </view>
  81. </view>
  82. </s-layout>
  83. </template>
  84. <script setup>
  85. import {
  86. computed,
  87. reactive,
  88. ref,
  89. unref
  90. } from 'vue';
  91. import {
  92. onLoad
  93. } from '@dcloudio/uni-app';
  94. import sheep from '@/sheep';
  95. import {
  96. fen2yuan,
  97. useDurationTime
  98. } from '@/sheep/hooks/useGoods';
  99. import PayOrderApi from '@/sheep/api/pay/order';
  100. import PayChannelApi from '@/sheep/api/pay/channel';
  101. import {
  102. getPayMethods
  103. } from '@/sheep/platform/pay';
  104. import md5 from 'blueimp-md5';
  105. import {
  106. showAuthModal,
  107. closeAuthModal,
  108. getSmsCode,
  109. getSmsTimer
  110. } from '@/sheep/hooks/useModal';
  111. const userWallet = computed(() => sheep.$store('user').userWallet);
  112. const userInfo = computed(() => sheep.$store('user').userInfo);
  113. // 检测支付环境
  114. const state = reactive({
  115. orderType: 'goods', // 订单类型; goods - 商品订单, recharge - 充值订单
  116. orderInfo: {}, // 支付单信息
  117. payStatus: 0, // 0=检测支付环境, -2=未查询到支付单信息, -1=支付已过期, 1=待支付,2=订单已支付
  118. payMethods: [], // 可选的支付方式
  119. payment: '', // 选中的支付方式
  120. model: {
  121. id: '',
  122. smsCodeUseReqDTO: {
  123. mobile: '',
  124. code: '',
  125. scene: 12,
  126. }
  127. },
  128. rules: {
  129. "smsCodeUseReqDTO.code": {
  130. rules: [{
  131. required: true,
  132. errorMessage: '验证码不能为空',
  133. }, ],
  134. },
  135. }
  136. });
  137. const onPay = () => {
  138. if (state.payment === '') {
  139. sheep.$helper.toast('请选择支付方式');
  140. return;
  141. }
  142. if (state.payment === 'wallet') {
  143. uni.showModal({
  144. title: '提示',
  145. content: '确定要支付吗?',
  146. success: function(res) {
  147. if (res.confirm) {
  148. sheep.$platform.pay(state.payment, state.orderType, state.orderInfo.id);
  149. }
  150. },
  151. });
  152. } else {
  153. sheep.$platform.pay(state.payment, state.orderType, state.orderInfo.id);
  154. }
  155. };
  156. const FormRef = ref(null);
  157. const onZeroPay = async () => {
  158. const validate = await unref(FormRef)
  159. .validate()
  160. .catch((error) => {
  161. console.log('error: ', error);
  162. });
  163. if (!validate) {
  164. return;
  165. }
  166. const { code , data } = await PayOrderApi.ZeroPurchaseSubmit(state.model);
  167. if (data) {
  168. sheep.$router.redirect('/pages/pay/resultYuan', {
  169. id: state.model.id,
  170. orderType: state.orderType,
  171. payState: 'success',
  172. payRes: JSON.stringify(data),
  173. });
  174. }
  175. }
  176. // 支付文案提示
  177. const payDescText = computed(() => {
  178. if (state.payStatus === 2) {
  179. return '该订单已支付';
  180. }
  181. if (state.payStatus === 1) {
  182. const time = useDurationTime(state.orderInfo.expireTime);
  183. if (time.ms <= 0) {
  184. state.payStatus = -1;
  185. return '';
  186. }
  187. return `剩余支付时间 ${time.h}:${time.m}:${time.s} `;
  188. }
  189. if (state.payStatus === -2) {
  190. return '未查询到支付单信息';
  191. }
  192. return '';
  193. });
  194. // 状态转换:payOrder.status => payStatus
  195. function checkPayStatus() {
  196. if (state.orderInfo.status === 10 ||
  197. state.orderInfo.status === 20) { // 支付成功
  198. state.payStatus = 2;
  199. return;
  200. }
  201. if (state.orderInfo.status === 30) { // 支付关闭
  202. state.payStatus = -1;
  203. return;
  204. }
  205. state.payStatus = 1; // 待支付
  206. }
  207. // 切换支付方式
  208. function onTapPay(e) {
  209. state.payment = e.detail.value;
  210. }
  211. // 设置支付订单信息
  212. async function setOrder(id) {
  213. // 获得支付订单信息
  214. const {
  215. data,
  216. code
  217. } = await PayOrderApi.getOrder(id);
  218. if (code !== 0 || !data) {
  219. state.payStatus = -2;
  220. return;
  221. }
  222. state.orderInfo = data;
  223. // 获得支付方式
  224. await setPayMethods();
  225. // 设置支付状态
  226. checkPayStatus();
  227. }
  228. // 设置支付订单信息
  229. async function setPayOrder(id) {
  230. // 获得支付订单信息
  231. const {
  232. data,
  233. code
  234. } = await PayOrderApi.getPayOrder(id);
  235. if (code !== 0 || !data) {
  236. state.payStatus = -2;
  237. return;
  238. }
  239. state.orderInfo = data;
  240. // 获得支付方式
  241. await setPayMethods();
  242. // 设置支付状态
  243. checkPayStatus();
  244. }
  245. // 获得支付方式
  246. async function setPayMethods() {
  247. const {
  248. data,
  249. code
  250. } = await PayChannelApi.getEnableChannelCodeList(state.orderInfo.appId)
  251. if (code !== 0) {
  252. return
  253. }
  254. state.payMethods = getPayMethods(data)
  255. }
  256. onLoad((options) => {
  257. if (sheep.$platform.name === 'WechatOfficialAccount' &&
  258. sheep.$platform.os === 'ios' &&
  259. !sheep.$platform.landingPage.includes('pages/pay/index')) {
  260. location.reload();
  261. return;
  262. }
  263. let id = options.id;
  264. state.model.id = options.id;
  265. if (options.orderType) {
  266. state.orderType = options.orderType;
  267. }
  268. if (options.openType == 2) {
  269. setPayOrder(id)
  270. } else {
  271. setOrder(id);
  272. }
  273. // 刷新钱包的缓存
  274. sheep.$store('user').getWallet();
  275. // 拿到用户的手机号
  276. state.model.smsCodeUseReqDTO.mobile = userInfo.value.mobile
  277. });
  278. </script>
  279. <style lang="scss" scoped>
  280. .code-btn-start {
  281. width: 158rpx;
  282. height: 56rpx;
  283. line-height: normal;
  284. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  285. border-radius: 28rpx;
  286. font-size: 26rpx;
  287. font-weight: 500;
  288. color: #fff;
  289. }
  290. :deep() {
  291. .is-disabled {
  292. color: #333333;
  293. }
  294. }
  295. .pay-icon {
  296. width: 36rpx;
  297. height: 36rpx;
  298. margin-right: 26rpx;
  299. }
  300. .ss-modal-box {
  301. // max-height: 1000rpx;
  302. .modal-header {
  303. position: relative;
  304. padding: 60rpx 20rpx 40rpx;
  305. .money-text {
  306. color: $red;
  307. font-size: 46rpx;
  308. font-weight: bold;
  309. font-family: OPPOSANS;
  310. &::before {
  311. content: '¥';
  312. font-size: 30rpx;
  313. }
  314. }
  315. .time-text {
  316. font-size: 26rpx;
  317. color: $gray-b;
  318. }
  319. .close-icon {
  320. position: absolute;
  321. top: 10rpx;
  322. right: 20rpx;
  323. font-size: 46rpx;
  324. opacity: 0.2;
  325. }
  326. }
  327. .modal-content {
  328. overflow-y: auto;
  329. .pay-title {
  330. font-size: 26rpx;
  331. font-weight: 500;
  332. color: #333333;
  333. }
  334. .pay-tip {
  335. font-size: 26rpx;
  336. color: #bbbbbb;
  337. }
  338. .pay-item {
  339. height: 86rpx;
  340. }
  341. .disabled-pay-item {
  342. .pay-title {
  343. color: #999999;
  344. }
  345. }
  346. .userInfo-money {
  347. font-size: 26rpx;
  348. color: #bbbbbb;
  349. line-height: normal;
  350. }
  351. }
  352. .save-btn {
  353. width: 710rpx;
  354. height: 80rpx;
  355. border-radius: 40rpx;
  356. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  357. color: $white;
  358. }
  359. .disabled-btn {
  360. background: #e5e5e5;
  361. color: #999999;
  362. }
  363. .past-due-btn {
  364. width: 710rpx;
  365. height: 80rpx;
  366. border-radius: 40rpx;
  367. background-color: #999;
  368. color: #fff;
  369. }
  370. }
  371. </style>