You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

payment.vue 10KB

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