OrderPay.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import {
  11. svipOrderCreateApi
  12. } from '@/api/activity.js';
  13. import {
  14. orderPayApi
  15. } from '@/api/order.js';
  16. import {
  17. ProductTypeEnum,
  18. } from "@/enums/productEnums";
  19. import {openPaySubscribe} from "../utils/SubscribeMessage";
  20. import {getAddressDetail} from "../api/user";
  21. import util from "../utils/util";
  22. export default {
  23. data() {
  24. return {
  25. ProductTypeEnum: ProductTypeEnum,
  26. isBuy: false, //是否可以点击购买
  27. addressId: 0,
  28. addressInfo: {},
  29. };
  30. },
  31. methods: {
  32. /**
  33. * 调支付订单接口后的操作
  34. * @param {Object} res 接口返回的值
  35. * @param {Object} orderNo 订单号
  36. * @param {Object} productType 商品类型
  37. * @param {Object} fromType 页面来源
  38. * @param {Object} payType 支付方式
  39. * @param {Object} payPrice 支付金额
  40. */
  41. handleOrderPay(res,orderNo,productType,fromType,payType,payPrice){
  42. let jsConfig = res.data.jsConfig;
  43. let goPages = ''
  44. if(fromType==='svip'){
  45. goPages = `/pages/goods/order_pay_status/index?order_id=${orderNo}&payType=${payType}&payPrice=${payPrice}&fromType=${fromType}`;
  46. }else{
  47. goPages = '/pages/goods/order_pay_status/index?order_id=' + orderNo;
  48. }
  49. switch (res.data.payType) {
  50. case 'weixin':
  51. uni.hideLoading();
  52. this.weixinPay(jsConfig, orderNo, goPages,productType,fromType);
  53. break;
  54. case 'yue':
  55. uni.hideLoading();
  56. uni.reLaunch({
  57. url: goPages + '&status=1'
  58. });
  59. break;
  60. case 'h5':
  61. uni.hideLoading();
  62. setTimeout(() => {
  63. location.href = jsConfig.mwebUrl + '&redirect_url=' + window
  64. .location
  65. .protocol + '//' + window.location.host + goPages +
  66. '&status=1';
  67. }, 500)
  68. break;
  69. case 'alipay':
  70. //#ifdef H5
  71. //h5支付
  72. uni.hideLoading();
  73. this.formContent = res.data.alipayRequest;
  74. this.$nextTick(() => {
  75. document.forms['punchout_form'].submit();
  76. })
  77. uni.setStorage({
  78. key: 'orderNo',
  79. data: orderNo
  80. });
  81. uni.setStorage({
  82. key: 'payResultfromType',
  83. data: fromType
  84. });
  85. //#endif
  86. // #ifdef APP-PLUS
  87. let alipayRequest = res.data.alipayRequest;
  88. uni.requestPayment({
  89. provider: 'alipay',
  90. orderInfo: alipayRequest,
  91. success: (e) => {
  92. setTimeout(res => {
  93. uni.hideLoading();
  94. uni.navigateTo({
  95. url: `/pages/goods/alipay_return/alipay_return?out_trade_no=${orderNo}&payChannel=appAlipay&payPrice=${payPrice}&fromType=${fromType}`
  96. })
  97. }, 500)
  98. },
  99. fail: (e) => {
  100. uni.hideLoading();
  101. uni.showModal({
  102. content: "支付失败",
  103. showCancel: false,
  104. confirmColor: '#f55850',
  105. success: function(res) {
  106. if (res.confirm) {
  107. //点击确认的操作
  108. uni.navigateTo({
  109. url: '/pages/goods/alipay_return/alipay_return?out_trade_no=' +
  110. orderNo +
  111. '&payChannel=' +
  112. 'appAlipay'
  113. })
  114. }
  115. }
  116. })
  117. },
  118. complete: () => {
  119. uni.hideLoading();
  120. },
  121. });
  122. // #endif
  123. break;
  124. }
  125. },
  126. //微信支付
  127. weixinPay(jsConfig, orderNo, goPages,productType,fromType) {
  128. // #ifdef MP
  129. if (productType === 'video') {
  130. uni.requestOrderPayment({
  131. timeStamp: jsConfig.timeStamp,
  132. nonceStr: jsConfig.nonceStr,
  133. package: jsConfig.packages,
  134. signType: jsConfig.signType,
  135. paySign: jsConfig.paySign,
  136. ticket: productType === 'normal' ? null : jsConfig.ticket,
  137. success: function(ress) {
  138. openPaySubscribe().then(() => {
  139. setTimeout(res => {
  140. uni.hideLoading();
  141. uni.reLaunch({
  142. url: goPages
  143. })
  144. }, 100);
  145. })
  146. },
  147. fail: function(e) {
  148. uni.hideLoading();
  149. this.isBuy = false;
  150. return util.Tips({
  151. title: '取消支付'
  152. }, {
  153. tab: 5,
  154. url: goPages + '&status=2'
  155. });
  156. },
  157. complete: function(e) {
  158. uni.hideLoading();
  159. this.isBuy = false;
  160. //关闭当前页面跳转至订单状态
  161. if (e.errMsg == 'requestPayment:cancel') return util.Tips({
  162. title: '取消支付'
  163. }, {
  164. tab: 5,
  165. url: goPages + '&status=2'
  166. });
  167. },
  168. })
  169. } else {
  170. uni.requestPayment({
  171. timeStamp: jsConfig.timeStamp,
  172. nonceStr: jsConfig.nonceStr,
  173. package: jsConfig.packages,
  174. signType: jsConfig.signType,
  175. paySign: jsConfig.paySign,
  176. ticket: productType === 'normal' ? null : jsConfig.ticket,
  177. success: function(ress) {
  178. // 弹起小程序订阅消息模板
  179. openPaySubscribe().then(() => {
  180. setTimeout(res => {
  181. uni.hideLoading();
  182. uni.reLaunch({
  183. url: goPages
  184. })
  185. }, 100);
  186. })
  187. },
  188. fail: function(e) {
  189. uni.hideLoading();
  190. this.isBuy = false;
  191. return util.Tips({
  192. title: '取消支付'
  193. }, {
  194. tab: 5,
  195. url: goPages + '&status=2'
  196. });
  197. },
  198. complete: function(e) {
  199. uni.hideLoading();
  200. this.isBuy = false;
  201. //关闭当前页面跳转至订单状态
  202. if (e.errMsg == 'requestPayment:cancel') return util.Tips({
  203. title: '取消支付'
  204. }, {
  205. tab: 5,
  206. url: goPages + '&status=2'
  207. });
  208. },
  209. })
  210. }
  211. // #endif
  212. // #ifdef H5
  213. let data = {
  214. timestamp: jsConfig.timeStamp,
  215. nonceStr: jsConfig.nonceStr,
  216. package: jsConfig.packages,
  217. signType: jsConfig.signType,
  218. paySign: jsConfig.paySign
  219. };
  220. if (this.$wechat.isWeixin()) {
  221. this.$wechat.pay(data).then(res => {
  222. setTimeout(res => {
  223. uni.hideLoading();
  224. uni.redirectTo({
  225. url: goPages
  226. })
  227. }, 500);
  228. }).catch(res => {
  229. uni.hideLoading();
  230. this.isBuy = false;
  231. return util.Tips({
  232. title: '取消支付'
  233. }, {
  234. tab: 5,
  235. url: goPages + '&status=2'
  236. });
  237. });
  238. } else {
  239. setTimeout(() => {
  240. location.href = jsConfig.mwebUrl + '&redirect_url=' + window.location.protocol + '//' + window.location.host + goPages;
  241. }, 100)
  242. uni.hideLoading();
  243. }
  244. // #endif
  245. // #ifdef APP-PLUS
  246. uni.requestPayment({
  247. provider: 'wxpay',
  248. orderInfo: {
  249. "appid": jsConfig.appId, // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
  250. "noncestr": jsConfig.nonceStr, // 随机字符串
  251. "package": "Sign=WXPay", // 固定值
  252. "partnerid": jsConfig.partnerid, // 微信支付商户号
  253. "prepayid": jsConfig.prepayid, // 统一下单订单号
  254. "timestamp": Number(jsConfig.timeStamp), // 时间戳(单位:秒)
  255. "sign": jsConfig.paySign // 签名
  256. }, //微信、支付宝订单数据 【注意微信的订单信息,键值应该全部是小写,不能采用驼峰命名】
  257. success: function(res) {
  258. uni.hideLoading();
  259. setTimeout(res => {
  260. uni.redirectTo({
  261. url: goPages
  262. })
  263. }, 500)
  264. },
  265. fail: function(err) {
  266. this.isBuy = false;
  267. uni.hideLoading();
  268. uni.showModal({
  269. content: "支付失败",
  270. showCancel: false,
  271. confirmColor: '#f55850',
  272. success: function(res) {
  273. if (res.confirm) {
  274. uni.redirectTo({
  275. url: goPages + '&status=2'
  276. })
  277. }
  278. }
  279. })
  280. },
  281. complete: (err) => {
  282. this.isBuy = false;
  283. uni.hideLoading();
  284. }
  285. });
  286. // #endif
  287. },
  288. //购买svip
  289. svipOnCreateOrder(data, orderNo,payType,payPrice,fromType) {
  290. svipOrderCreateApi(data).then(res => {
  291. let jsConfig = res.data.jsConfig;
  292. let goPages = `/pages/goods/order_pay_status/index?order_id=${res.data.orderNo}&payType=${payType}&payPrice=${payPrice}&fromType=${fromType}`;
  293. this.weixinPay(jsConfig, res.data.orderNo, goPages,'normal',fromType)
  294. }).catch(err => {
  295. uni.hideLoading();
  296. this.isBuy = false;
  297. return util.Tips({
  298. title: err
  299. });
  300. });
  301. },
  302. /**
  303. * 跳入支付收银台页面
  304. * @param {Object} secondType 二级订单类型
  305. * @param {data} res 订单对象
  306. */
  307. getToPayment(secondType,data) {
  308. let url = `/pages/goods/order_payment/index?orderNo=${data.orderNo}&payPrice=${data.payPrice}&totalPriceReat=${data.totalPriceReat}`
  309. uni.redirectTo({
  310. url: url
  311. });
  312. },
  313. /**
  314. * 订单支付
  315. * @param {Object} orderNo 订单号
  316. * @param {Object} payChannel 支付渠道
  317. * @param {Object} payType 支付方式
  318. * @param {Object} productType 商品类型
  319. * @param {Object} fromType 页面来源
  320. * @param {Object} payPrice 支付金额
  321. */
  322. changeOrderPay(orderNo, payChannel, payType ,productType, fromType, payPrice,totalPriceReat) {
  323. orderPayApi({
  324. totalPriceReat: totalPriceReat,
  325. orderNo: orderNo,
  326. payChannel: payChannel,
  327. payType: payType,
  328. scene: productType === 'normal' ? 0 : 1177
  329. }).then(res => {
  330. this.handleOrderPay(res, orderNo, productType, fromType, payType, payPrice)
  331. }).catch(err => {
  332. uni.hideLoading();
  333. this.isBuy = false;
  334. return util.Tips({
  335. title: err
  336. });
  337. });
  338. },
  339. /*
  340. * 获取默认收货地址或者获取某条地址信息
  341. */
  342. getAddressInfo: function() {
  343. if (this.addressId) {
  344. getAddressDetail(this.addressId).then(res => {
  345. if (res.data) {
  346. res.data.isDefault = res.data.isDefault;
  347. this.addressInfo = res.data || {};
  348. }
  349. })
  350. }
  351. },
  352. }
  353. };