index.vue 10 KB

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