index.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. orderType:0
  128. },
  129. rules: {
  130. "smsCodeUseReqDTO.code": {
  131. rules: [{
  132. required: true,
  133. errorMessage: '验证码不能为空',
  134. }, ],
  135. },
  136. }
  137. });
  138. const onPay = () => {
  139. if (state.payment === '') {
  140. sheep.$helper.toast('请选择支付方式');
  141. return;
  142. }
  143. sheep.$platform.pay(state.payment, state.orderType, state.orderInfo.id , state.model.type);
  144. };
  145. const FormRef = ref(null);
  146. const onZeroPay = async () => {
  147. const validate = await unref(FormRef)
  148. .validate()
  149. .catch((error) => {
  150. console.log('error: ', error);
  151. });
  152. if (!validate) {
  153. return;
  154. }
  155. const { code , data } = await PayOrderApi.ZeroPurchaseSubmit(state.model);
  156. if (data) {
  157. sheep.$router.redirect('/pages/pay/resultYuan', {
  158. id: state.model.id,
  159. orderType: state.orderType,
  160. payState: 'success',
  161. payRes: JSON.stringify(data),
  162. });
  163. }
  164. }
  165. // 支付文案提示
  166. const payDescText = computed(() => {
  167. if (state.payStatus === 2) {
  168. return '该订单已支付';
  169. }
  170. if (state.payStatus === 1) {
  171. const time = useDurationTime(state.orderInfo.expireTime);
  172. if (time.ms <= 0) {
  173. state.payStatus = -1;
  174. return '';
  175. }
  176. return `剩余支付时间 ${time.h}:${time.m}:${time.s} `;
  177. }
  178. if (state.payStatus === -2) {
  179. return '未查询到支付单信息';
  180. }
  181. return '';
  182. });
  183. // 状态转换:payOrder.status => payStatus
  184. function checkPayStatus() {
  185. if (state.orderInfo.status === 10 ||
  186. state.orderInfo.status === 20) { // 支付成功
  187. state.payStatus = 2;
  188. return;
  189. }
  190. if (state.orderInfo.status === 30) { // 支付关闭
  191. state.payStatus = -1;
  192. return;
  193. }
  194. state.payStatus = 1; // 待支付
  195. }
  196. // 切换支付方式
  197. function onTapPay(e) {
  198. state.payment = e.detail.value;
  199. }
  200. // 设置支付订单信息
  201. async function setOrder(id) {
  202. // 获得支付订单信息
  203. const {
  204. data,
  205. code
  206. } = await PayOrderApi.getOrder(id);
  207. if (code !== 0 || !data) {
  208. state.payStatus = -2;
  209. return;
  210. }
  211. state.orderInfo = data;
  212. // 获得支付方式
  213. await setPayMethods();
  214. // 设置支付状态
  215. checkPayStatus();
  216. }
  217. // 设置支付订单信息
  218. async function setPayOrder(id) {
  219. // 获得支付订单信息
  220. const {
  221. data,
  222. code
  223. } = await PayOrderApi.getPayOrder(id);
  224. if (code !== 0 || !data) {
  225. state.payStatus = -2;
  226. return;
  227. }
  228. state.orderInfo = data;
  229. // 获得支付方式
  230. await setPayMethods();
  231. // 设置支付状态
  232. checkPayStatus();
  233. }
  234. // 获得支付方式
  235. async function setPayMethods() {
  236. const {
  237. data,
  238. code
  239. } = await PayChannelApi.getEnableChannelCodeList(state.orderInfo.appId)
  240. if (code !== 0) {
  241. return
  242. }
  243. state.payMethods = getPayMethods(data)
  244. }
  245. onLoad((options) => {
  246. if (sheep.$platform.name === 'WechatOfficialAccount' &&
  247. sheep.$platform.os === 'ios' &&
  248. !sheep.$platform.landingPage.includes('pages/pay/index')) {
  249. location.reload();
  250. return;
  251. }
  252. let id = options.id;
  253. let type = options.type;
  254. state.model.id = options.id;
  255. state.model.type = options.type;
  256. if (options.orderType) {
  257. state.orderType = options.orderType;
  258. }
  259. setOrder(id);
  260. // if (options.openType == 2) {
  261. // setPayOrder(id)
  262. // } else {
  263. // setOrder(id);
  264. // }
  265. // 刷新钱包的缓存
  266. sheep.$store('user').getWallet();
  267. // 拿到用户的手机号
  268. state.model.smsCodeUseReqDTO.mobile = userInfo.value.mobile
  269. });
  270. </script>
  271. <style lang="scss" scoped>
  272. .code-btn-start {
  273. width: 158rpx;
  274. height: 56rpx;
  275. line-height: normal;
  276. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  277. border-radius: 28rpx;
  278. font-size: 26rpx;
  279. font-weight: 500;
  280. color: #fff;
  281. }
  282. :deep() {
  283. .is-disabled {
  284. color: #333333;
  285. }
  286. }
  287. .pay-icon {
  288. width: 36rpx;
  289. height: 36rpx;
  290. margin-right: 26rpx;
  291. }
  292. .ss-modal-box {
  293. // max-height: 1000rpx;
  294. .modal-header {
  295. position: relative;
  296. padding: 60rpx 20rpx 40rpx;
  297. .money-text {
  298. color: $red;
  299. font-size: 46rpx;
  300. font-weight: bold;
  301. font-family: OPPOSANS;
  302. &::before {
  303. content: '¥';
  304. font-size: 30rpx;
  305. }
  306. }
  307. .time-text {
  308. font-size: 26rpx;
  309. color: $gray-b;
  310. }
  311. .close-icon {
  312. position: absolute;
  313. top: 10rpx;
  314. right: 20rpx;
  315. font-size: 46rpx;
  316. opacity: 0.2;
  317. }
  318. }
  319. .modal-content {
  320. overflow-y: auto;
  321. .pay-title {
  322. font-size: 26rpx;
  323. font-weight: 500;
  324. color: #333333;
  325. }
  326. .pay-tip {
  327. font-size: 26rpx;
  328. color: #bbbbbb;
  329. }
  330. .pay-item {
  331. height: 86rpx;
  332. }
  333. .disabled-pay-item {
  334. .pay-title {
  335. color: #999999;
  336. }
  337. }
  338. .userInfo-money {
  339. font-size: 26rpx;
  340. color: #bbbbbb;
  341. line-height: normal;
  342. }
  343. }
  344. .save-btn {
  345. width: 710rpx;
  346. height: 80rpx;
  347. border-radius: 40rpx;
  348. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  349. color: $white;
  350. }
  351. .disabled-btn {
  352. background: #e5e5e5;
  353. color: #999999;
  354. }
  355. .past-due-btn {
  356. width: 710rpx;
  357. height: 80rpx;
  358. border-radius: 40rpx;
  359. background-color: #999;
  360. color: #fff;
  361. }
  362. }
  363. </style>