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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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,requestNew } from "@/utils/network/request.js";
  84. import {
  85. checkOrderStatus,
  86. orderPay,
  87. wechatAppID,
  88. wechatPayConfigId,
  89. aliPayConfigId,
  90. obtainUserId,
  91. wechatSecret,
  92. getOpenId
  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. requestNew(obtainUserId, optionsUser).then((res) => {
  189. const data = res;
  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. },
  231. });
  232. };
  233. const getOpenid = (code) => {
  234. const options = {
  235. type: 2,
  236. data: {
  237. "jsCode": code
  238. },
  239. method: "POST",
  240. showLoading: true,
  241. };
  242. // #ifdef MP-WEIXIN
  243. requestNew(getOpenId, options).then((res) => {
  244. const result = res;
  245. console.log("获取微信小程序openid", result);
  246. const openidData = stringToJson(result.data);
  247. state.openid = openidData.openid;
  248. });
  249. // #endif
  250. }
  251. //支付成功改变订单状态
  252. const checkOrder = () => {
  253. let configId = ""
  254. if (state.methodSelected == "wx") {
  255. configId = wechatPayConfigId;
  256. } else if (state.methodSelected == "ali") {
  257. configId = aliPayConfigId
  258. }
  259. const options = {
  260. type: 2,
  261. data: {
  262. orderId: state.orderData.orderId,
  263. payConfigId: configId,
  264. },
  265. method: "POST",
  266. showLoading: true,
  267. };
  268. request(checkOrderStatus, options).then((res) => {
  269. payOrderSuccess();
  270. });
  271. };
  272. const payOrderSuccess = () => {
  273. confirm(
  274. "您申办的ETC订单支付成功,请等待业务员审核信息,确认无误后发货!",
  275. () => {
  276. uni.$emit("refreshOrder");
  277. uni.navigateBack();
  278. },
  279. "支付成功",
  280. false
  281. );
  282. };
  283. onLoad((options) => {
  284. //需要参数参数json形式:{orderId, productId,amount}
  285. state.orderData = JSON.parse(options.data);
  286. console.log(state.orderData);
  287. getOpenID();
  288. });
  289. </script>
  290. <style lang="scss" scoped>
  291. .bg-color {
  292. position: fixed;
  293. top: 0;
  294. left: 0;
  295. right: 0;
  296. bottom: 0;
  297. background: #f3f3f3;
  298. z-index: -1;
  299. }
  300. .wrapper {
  301. padding-bottom: 20rpx;
  302. }
  303. .money {
  304. background: #ffffff;
  305. display: flex;
  306. flex-direction: column;
  307. height: 266rpx;
  308. justify-content: center;
  309. align-items: center;
  310. .money-title {
  311. color: #717171;
  312. font-size: 29rpx;
  313. }
  314. .money-value {
  315. font-size: 40rpx;
  316. color: #2a2a2a;
  317. font-weight: 500;
  318. margin-top: 32rpx;
  319. }
  320. }
  321. .bankcard,
  322. .pay-method {
  323. background: #ffffff;
  324. margin-top: 20rpx;
  325. padding: 0 30rpx;
  326. }
  327. .method-item {
  328. display: flex;
  329. justify-content: space-between;
  330. align-items: center;
  331. padding: 20rpx 0;
  332. .text-space {
  333. margin-left: 69rpx;
  334. }
  335. .img-arrow {
  336. width: 14rpx;
  337. height: 25rpx;
  338. margin-right: 12rpx;
  339. }
  340. .method-name {
  341. .name-img {
  342. width: 50rpx;
  343. height: 50rpx;
  344. vertical-align: middle;
  345. }
  346. .name-text {
  347. color: #333333;
  348. font-size: 28rpx;
  349. margin-left: 19rpx;
  350. line-height: 30rpx;
  351. }
  352. }
  353. .select-circle {
  354. width: 37rpx;
  355. height: 37rpx;
  356. background: #ffffff;
  357. border: 2px solid #999999;
  358. border-radius: 50%;
  359. }
  360. .name-img-selected {
  361. width: 37rpx;
  362. height: 37rpx;
  363. }
  364. .text-icon {
  365. font-size: 25rpx;
  366. }
  367. }
  368. .border-line {
  369. border-bottom: 1rpx solid #dcdcdc;
  370. }
  371. .btn {
  372. margin: 233rpx 40rpx 40rpx 40rpx;
  373. height: 80rpx;
  374. background: linear-gradient(-90deg, #43a1e0 0%, #13e7c1 100%);
  375. border-radius: 40rpx;
  376. font-size: 32rpx;
  377. font-weight: 400;
  378. color: #fff;
  379. text-align: center;
  380. line-height: 80rpx;
  381. }
  382. .list-header {
  383. height: 81rpx;
  384. background: #ffffff;
  385. border: 1px solid #dcdcdc;
  386. line-height: 81rpx;
  387. text-align: center;
  388. color: #333333;
  389. font-size: 36rpx;
  390. }
  391. .p-list-item {
  392. padding: 30rpx 46rpx;
  393. border-bottom: 1px solid #dcdcdc;
  394. font-size: 28rpx;
  395. color: #333333;
  396. }
  397. .list-box {
  398. padding: 0 30rpx;
  399. }
  400. </style>