Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

payment.vue 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <template>
  2. <view class="wrapper">
  3. <!-- 背景颜色充满屏 -->
  4. <view class="bg-color"></view>
  5. <navBar title="收银"></navBar>
  6. <view class="money">
  7. <text class="money-title">需支付金额(元)</text>
  8. <text class="money-value">¥{{ state.orderData.amount / 100 ?state.orderData.amount / 100: "0.00" }}</text>
  9. </view>
  10. <view class="bankcard">
  11. <view class="method-item border-line">
  12. <view class="method-name">
  13. <image class="name-img" :src="`${$imgUrl}order/bank.png`"></image>
  14. <text class="name-text">{{ state.bankInfo.name }}</text>
  15. </view>
  16. <image class="name-img-selected" :src="`${$imgUrl}order/selected.png`"
  17. v-if="state.methodSelected == state.bankInfo.id"></image>
  18. <view class="select-circle" @click="methodHandle(state.bankInfo.id)" v-else></view>
  19. </view>
  20. <view class="method-item other-method" @click="showList">
  21. <view class="method-name">
  22. <text class="name-text text-space">其他银行卡</text>
  23. </view>
  24. <image class="img-arrow" :src="`${$imgUrl}user/icon-arrow.png`"></image>
  25. </view>
  26. </view>
  27. <view class="pay-method">
  28. <view class="method-item border-line" @click="methodHandle('wx')">
  29. <view class="method-name">
  30. <image class="name-img" :src="`${$imgUrl}order/uni.png`"></image>
  31. <text class="name-text">微信支付</text>
  32. </view>
  33. <image class="name-img-selected" :src="`${$imgUrl}order/selected.png`"
  34. v-if="state.methodSelected == 'wx'"></image>
  35. <view class="select-circle" v-else></view>
  36. </view>
  37. <view class="method-item border-line" @click="methodHandle('ali')">
  38. <view class="method-name">
  39. <image class="name-img" :src="`${$imgUrl}order/uni.png`"></image>
  40. <text class="name-text">支付宝支付</text>
  41. </view>
  42. <image class="name-img-selected" :src="`${$imgUrl}order/selected.png`"
  43. v-if="state.methodSelected == 'ali'"></image>
  44. <view class="select-circle" v-else></view>
  45. </view>
  46. <view class="method-item border-line" @click="methodHandle('ysf')">
  47. <view class="method-name">
  48. <image class="name-img" :src="`${$imgUrl}order/ysf.png`"></image>
  49. <text class="name-text">云闪付</text>
  50. </view>
  51. <image class="name-img-selected" :src="`${$imgUrl}order/selected.png`"
  52. v-if="state.methodSelected == 'ysf'"></image>
  53. <view class="select-circle" v-else></view>
  54. </view>
  55. <view class="method-item" @click="methodHandle('wxhy')">
  56. <view class="method-name">
  57. <image class="name-img" :src="`${$imgUrl}order/wx1.png`"></image>
  58. <text class="name-text">微信好友支付</text>
  59. </view>
  60. <image class="name-img-selected" :src="`${$imgUrl}order/selected.png`"
  61. v-if="state.methodSelected == 'wxhy'"></image>
  62. <view class="select-circle" v-else></view>
  63. </view>
  64. </view>
  65. <view class="btn" @click="toPayOrder">支付</view>
  66. <!-- 银行list弹窗 -->
  67. <u-popup v-model="state.listInfo.show" mode="bottom" :mask-close-able="false">
  68. <view class="list-header">请选择</view>
  69. <scroll-view scroll-y="true" style="height: 530rpx; background: #f4f4f4">
  70. <view class="list-box">
  71. <view v-for="item in state.listInfo.list" :key="item.id" class="p-list-item"
  72. @click="listSelected(item)">
  73. {{ item.name }}
  74. </view>
  75. </view>
  76. </scroll-view>
  77. </u-popup>
  78. </view>
  79. </template>
  80. <script setup lang="ts">
  81. import navBar from "@/components/nav-bar/nav-bar.vue";
  82. import { reactive } from "vue";
  83. import { request } from "@/utils/network/request.js";
  84. import {
  85. checkOrderStatus,
  86. orderPay,
  87. wechatAppID,
  88. wechatPayConfigId,
  89. aliPayConfigId,
  90. obtainUserId,
  91. wechatSecret,
  92. getOpenidApi
  93. } from "@/utils/network/api";
  94. import { stringToJson } from "@/utils/network/encryption";
  95. import { onLoad } from "@dcloudio/uni-app";
  96. import { confirm, msg } from "@/utils/utils";
  97. const state = reactive({
  98. methodSelected: "wx",
  99. bankInfo: { id: 1, name: "招商银行储蓄卡(0843)" },
  100. listInfo: {
  101. show: false,
  102. list: [
  103. { name: "工商银行", id: 2 },
  104. { name: "建设银行", id: 3 },
  105. ],
  106. },
  107. orderData: {} as any, //订单数据
  108. openid: "",
  109. });
  110. const methodHandle = (val) => {
  111. state.methodSelected = val;
  112. console.log(state.methodSelected);
  113. };
  114. // 展开其他银行卡
  115. const showList = () => {
  116. state.listInfo.show = true;
  117. };
  118. //其他银行卡选择
  119. const listSelected = (item) => {
  120. state.listInfo.show = false;
  121. };
  122. //订单支付
  123. const toPayOrder = () => {
  124. if (state.methodSelected == "wx") {
  125. //微信支付
  126. wxPayment();
  127. } else if (state.methodSelected == "ali") {
  128. aliPayment()
  129. } else {
  130. msg("目前只支持微信支付!");
  131. }
  132. };
  133. //掉起微信支付
  134. const wxPayment = () => {
  135. // #ifdef MP-WEIXIN
  136. if (!state.openid) {
  137. getOpenID();
  138. msg("请求失败,请稍后重试!");
  139. return;
  140. }
  141. const options = {
  142. type: 2,
  143. data: {
  144. openid: state.openid,
  145. productId: state.orderData.productId, //产品id
  146. orderId: state.orderData.orderId,
  147. payConfigId: wechatPayConfigId,
  148. },
  149. method: "POST",
  150. showLoading: true,
  151. };
  152. request(orderPay, options).then((res) => {
  153. const data = stringToJson(res.bizContent);
  154. uni.requestPayment({
  155. provider: "wxpay",
  156. orderInfo: "",
  157. timeStamp: data.timestamp,
  158. nonceStr: data.noncestr,
  159. package: data.wxPackage ? data.wxPackage : "",
  160. signType: data.signType,
  161. paySign: data.sign,
  162. success: function (e) {
  163. console.log("支付成功", e);
  164. checkOrder();
  165. },
  166. fail: function (err) {
  167. confirm(err, () => { }, "支付失败", false);
  168. },
  169. });
  170. });
  171. // #endif
  172. };
  173. //掉起支付宝支付
  174. const aliPayment = () => {
  175. // #ifdef MP-ALIPAY
  176. my.getAuthCode({
  177. scopes: 'auth_user',
  178. success: res => {
  179. const optionsUser = {
  180. type: 2,
  181. data: {
  182. payConfigId: aliPayConfigId,
  183. code: res.authCode
  184. },
  185. method: "POST",
  186. showLoading: true,
  187. };
  188. request(obtainUserId, optionsUser).then((res) => {
  189. const data = stringToJson(res.bizContent);
  190. const optionsali = {
  191. type: 2,
  192. data: {
  193. openid: data.openId,
  194. productId: state.orderData.productId, //产品id
  195. orderId: state.orderData.orderId,
  196. payConfigId: aliPayConfigId,
  197. },
  198. method: "POST",
  199. showLoading: true,
  200. };
  201. request(orderPay, optionsali).then((res) => {
  202. const data = stringToJson(res.bizContent);
  203. state.orderData.orderId = data.orderId
  204. my.tradePay({
  205. // 调用统一收单交易创建接口(alipay.trade.create),获得返回字段支付宝交易号 trade_no
  206. tradeNO: data.prepayId,
  207. success: res => {
  208. console.log("支付成功", res);
  209. checkOrder();
  210. },
  211. fail: res => {
  212. confirm(res, () => { }, "支付失败", false);
  213. },
  214. });
  215. });
  216. });
  217. },
  218. fail: err => {
  219. console.log('my.getAuthCode 调用失败', err)
  220. }
  221. });
  222. // #endif
  223. };
  224. //获取微信小程序openid
  225. const getOpenID = () => {
  226. uni.login({
  227. provider: "weixin",
  228. success: function (e) {
  229. getOpenid(e.code);
  230. // uni.request({
  231. // url: `https://api.weixin.qq.com/sns/jscode2session?appid=${wechatAppID}&secret=${wechatSecret}&js_code=${e.code}&grant_type=authorization_code`,
  232. // success: (res: any) => {
  233. // state.openid = res.data.openid;
  234. // },
  235. // });
  236. },
  237. });
  238. };
  239. const getOpenid = (code) => {
  240. const options = {
  241. type: 2,
  242. data: {
  243. "jsCode": code
  244. },
  245. method: "POST",
  246. showLoading: true,
  247. };
  248. // #ifdef MP-WEIXIN
  249. request(getOpenidApi, options).then((res) => {
  250. const result = stringToJson(res.bizContent);
  251. console.log("获取微信小程序openid", result);
  252. const openidData = stringToJson(result.data);
  253. state.openid = openidData.openid;
  254. });
  255. // #endif
  256. }
  257. //支付成功改变订单状态
  258. const checkOrder = () => {
  259. let configId = ""
  260. if (state.methodSelected == "wx") {
  261. configId = wechatPayConfigId;
  262. } else if (state.methodSelected == "ali") {
  263. configId = aliPayConfigId
  264. }
  265. const options = {
  266. type: 2,
  267. data: {
  268. orderId: state.orderData.orderId,
  269. payConfigId: configId,
  270. },
  271. method: "POST",
  272. showLoading: true,
  273. };
  274. request(checkOrderStatus, options).then((res) => {
  275. payOrderSuccess();
  276. });
  277. };
  278. const payOrderSuccess = () => {
  279. confirm(
  280. "您申办的ETC订单支付成功,请等待业务员审核信息,确认无误后发货!",
  281. () => {
  282. uni.$emit("refreshOrder");
  283. uni.navigateBack();
  284. },
  285. "支付成功",
  286. false
  287. );
  288. };
  289. onLoad((options) => {
  290. //需要参数参数json形式:{orderId, productId,amount}
  291. state.orderData = JSON.parse(options.data);
  292. console.log(state.orderData);
  293. getOpenID();
  294. });
  295. </script>
  296. <style lang="scss" scoped>
  297. .bg-color {
  298. position: fixed;
  299. top: 0;
  300. left: 0;
  301. right: 0;
  302. bottom: 0;
  303. background: #f3f3f3;
  304. z-index: -1;
  305. }
  306. .wrapper {
  307. padding-bottom: 20rpx;
  308. }
  309. .money {
  310. background: #ffffff;
  311. display: flex;
  312. flex-direction: column;
  313. height: 266rpx;
  314. justify-content: center;
  315. align-items: center;
  316. .money-title {
  317. color: #717171;
  318. font-size: 29rpx;
  319. }
  320. .money-value {
  321. font-size: 40rpx;
  322. color: #2a2a2a;
  323. font-weight: 500;
  324. margin-top: 32rpx;
  325. }
  326. }
  327. .bankcard,
  328. .pay-method {
  329. background: #ffffff;
  330. margin-top: 20rpx;
  331. padding: 0 30rpx;
  332. }
  333. .method-item {
  334. display: flex;
  335. justify-content: space-between;
  336. align-items: center;
  337. padding: 20rpx 0;
  338. .text-space {
  339. margin-left: 69rpx;
  340. }
  341. .img-arrow {
  342. width: 14rpx;
  343. height: 25rpx;
  344. margin-right: 12rpx;
  345. }
  346. .method-name {
  347. .name-img {
  348. width: 50rpx;
  349. height: 50rpx;
  350. vertical-align: middle;
  351. }
  352. .name-text {
  353. color: #333333;
  354. font-size: 28rpx;
  355. margin-left: 19rpx;
  356. line-height: 30rpx;
  357. }
  358. }
  359. .select-circle {
  360. width: 37rpx;
  361. height: 37rpx;
  362. background: #ffffff;
  363. border: 2px solid #999999;
  364. border-radius: 50%;
  365. }
  366. .name-img-selected {
  367. width: 37rpx;
  368. height: 37rpx;
  369. }
  370. .text-icon {
  371. font-size: 25rpx;
  372. }
  373. }
  374. .border-line {
  375. border-bottom: 1rpx solid #dcdcdc;
  376. }
  377. .btn {
  378. margin: 233rpx 40rpx 40rpx 40rpx;
  379. height: 80rpx;
  380. background: linear-gradient(-90deg, #43a1e0 0%, #13e7c1 100%);
  381. border-radius: 40rpx;
  382. font-size: 32rpx;
  383. font-weight: 400;
  384. color: #fff;
  385. text-align: center;
  386. line-height: 80rpx;
  387. }
  388. .list-header {
  389. height: 81rpx;
  390. background: #ffffff;
  391. border: 1px solid #dcdcdc;
  392. line-height: 81rpx;
  393. text-align: center;
  394. color: #333333;
  395. font-size: 36rpx;
  396. }
  397. .p-list-item {
  398. padding: 30rpx 46rpx;
  399. border-bottom: 1px solid #dcdcdc;
  400. font-size: 28rpx;
  401. color: #333333;
  402. }
  403. .list-box {
  404. padding: 0 30rpx;
  405. }
  406. </style>