pay.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import { T_PAY_ORDER } from '../constant'
  2. // import { mapState, mapGetters } from 'vuex'
  3. import store from '../store'
  4. // import { payOrderGoodsApi, payOrderGoodsAPPApi } from '../api/xxx'
  5. // import { payBOrderH5, payOtherPlatformApi } from '../api/xxx'
  6. import { getUserId } from './index'
  7. // export const payH5Method = (res, type) => {
  8. // uni.removeStorageSync(T_PAY_ORDER)
  9. // uni.setStorageSync(T_PAY_ORDER, {
  10. // type: type || 'DEFAULT',
  11. // TL_ORDER_NO: res.orderNo
  12. // })
  13. // if (res.errno === -1) {
  14. // uni.showToast({
  15. // title: res.errmsg,
  16. // duration: 2000,
  17. // icon: 'error'
  18. // })
  19. // }
  20. // if (res.data.isZeroOrder === '1') { // 零元支付情况
  21. // uni.redirectTo({
  22. // url: '/user/otherServe/payment-completed/index'
  23. // })
  24. // } else {
  25. // const payData = JSON.parse(res.data.h5PayUrl)
  26. // const form = document.createElement('form')
  27. // form.setAttribute('action', payData.url)
  28. // form.setAttribute('method', 'POST')
  29. // const data = JSON.parse(payData.data)
  30. // let input
  31. // for (const key in data) {
  32. // input = document.createElement('input')
  33. // input.name = key
  34. // input.value = data[key]
  35. // form.appendChild(input)
  36. // }
  37. // document.body.appendChild(form)
  38. // form.submit()
  39. // document.body.removeChild(form)
  40. // }
  41. // }
  42. export const payFn = (data, payType, type = 'DEFAULT', otherArgs = {}) => {
  43. // console.log({ ...mapState('app', [ 'isInMiniProgram' ]) })
  44. // console.log(mapState('app', [ 'isInMiniProgram' ]).isInMiniProgram)
  45. // console.log({ ...mapState({ 'isInMiniProgram': (state) => state.isInMiniProgram }) })
  46. console.log(store.state.app.isInMiniProgram)
  47. if (store.state.app.isInMiniProgram) { // || getApp().globalData.isInMiniprogram
  48. payOrderGoodsAPPApi({
  49. userId: getUserId(),
  50. orderNo: data.orderSn,
  51. payType,
  52. ...otherArgs
  53. }).then((res) => {
  54. if (res.errno === 0) {
  55. if (type) {
  56. uni.removeStorageSync(T_PAY_ORDER)
  57. uni.setStorageSync(T_PAY_ORDER, {
  58. type,
  59. TL_ORDER_NO: data.orderSn
  60. })
  61. }
  62. if (res.data.isZeroOrder === '1') { // 零元支付情况
  63. uni.redirectTo({
  64. url: '/user/otherServe/payment-completed/index'
  65. })
  66. } else {
  67. delete res.data.isZeroOrder
  68. let query = ''
  69. for (const key in res.data) {
  70. query += key + '=' + res.data[key] + '&'
  71. }
  72. wx.miniProgram.navigateTo({
  73. url: '/pages/loading/loading?' + query + 'orderNo=' + data.orderSn + '&userId=' + getUserId(),
  74. fail: () => {
  75. uni.switchTab({
  76. url: '/pages/order/order?type=shop&status=1'
  77. })
  78. }
  79. })
  80. }
  81. }
  82. })
  83. } else {
  84. // #ifdef H5
  85. payOrderGoodsApi({
  86. orderNo: data.orderSn,
  87. userId: getUserId(),
  88. payType,
  89. ...otherArgs
  90. }).then((res) => {
  91. if (type) {
  92. uni.removeStorageSync(T_PAY_ORDER)
  93. uni.setStorageSync(T_PAY_ORDER, {
  94. type,
  95. TL_ORDER_NO: data.orderSn
  96. })
  97. }
  98. if (res.data.isZeroOrder === '1') { // 零元支付情况
  99. uni.redirectTo({
  100. url: '/user/otherServe/payment-completed/index'
  101. })
  102. } else {
  103. const payData = JSON.parse(res.data.h5PayUrl)
  104. const form = document.createElement('form')
  105. form.setAttribute('action', payData.url)
  106. form.setAttribute('method', 'POST')
  107. const data = JSON.parse(payData.data)
  108. let input
  109. for (const key in data) {
  110. input = document.createElement('input')
  111. input.name = key
  112. input.value = data[key]
  113. form.appendChild(input)
  114. }
  115. document.body.appendChild(form)
  116. form.submit()
  117. document.body.removeChild(form)
  118. }
  119. })
  120. // #endif
  121. // #ifdef APP
  122. payOrderGoodsAPPApi({
  123. userId: getUserId(),
  124. orderNo: data.orderSn,
  125. payType,
  126. ...otherArgs
  127. }).then((res) => {
  128. if (res.errno === 0) {
  129. if (type) {
  130. uni.removeStorageSync(T_PAY_ORDER)
  131. uni.setStorageSync(T_PAY_ORDER, {
  132. type,
  133. TL_ORDER_NO: data.orderSn
  134. })
  135. }
  136. if (res.data.isZeroOrder === '1') { // 零元支付情况
  137. uni.redirectTo({
  138. url: '/user/otherServe/payment-completed/index'
  139. })
  140. } else {
  141. delete res.data.isZeroOrder
  142. let query = ''
  143. for (const key in res.data) {
  144. query += key + '=' + res.data[key] + '&'
  145. }
  146. plus.share.getServices(function (result) {
  147. let sweixin = null
  148. for (const i in result) {
  149. if (result[i].id == 'weixin') {
  150. sweixin = result[i]
  151. }
  152. }
  153. if (sweixin) {
  154. sweixin.launchMiniProgram({
  155. id: 'gh_e64a1a89a0ad', // 微信小程序的原始ID("g_"开头的字符串)
  156. type: 0,
  157. path: 'pages/orderDetail/orderDetail?' + query
  158. })
  159. }
  160. }, function (e) {
  161. console.log('获取分享服务列表失败:' + e.message)
  162. })
  163. }
  164. }
  165. })
  166. // #endif
  167. }
  168. }
  169. export const payShopFn = (data, payType, type = 'DEFAULT', otherArgs = {}) => {
  170. console.log(store.state.app.isInMiniProgram)
  171. if (store.state.app.isInMiniProgram) {
  172. payOtherPlatformApi({
  173. userId: getUserId(),
  174. orderNo: data.orderNo,
  175. payType,
  176. ...otherArgs
  177. }).then((resStr) => {
  178. const res = JSON.parse(resStr)
  179. if (type) {
  180. uni.removeStorageSync(T_PAY_ORDER)
  181. uni.setStorageSync(T_PAY_ORDER, {
  182. type,
  183. TL_ORDER_NO: data.orderNo
  184. })
  185. }
  186. if (res.isZeroOrder === '1') { // 零元支付情况
  187. uni.redirectTo({
  188. url: '/user/otherServe/payment-completed/index'
  189. })
  190. } else {
  191. let query = ''
  192. for (const key in res.data) {
  193. query += key + '=' + res.data[key] + '&'
  194. }
  195. wx.miniProgram.navigateTo({
  196. url: '/pages/loading/loading?' + query + 'orderNo=' + data.orderNo + '&userId=' + getUserId(),
  197. fail: () => {
  198. uni.switchTab({
  199. url: '/pages/order/order?type=shop&status=1'
  200. })
  201. }
  202. })
  203. }
  204. })
  205. } else {
  206. // #ifdef H5
  207. payBOrderH5({
  208. orderNo: data.orderNo,
  209. userId: getUserId(),
  210. payType,
  211. ...otherArgs
  212. }).then((resStr) => {
  213. const res = JSON.parse(resStr)
  214. // console.log(resStr)
  215. // console.log(res)
  216. // console.log(JSON.parse(res.data))
  217. if (type) {
  218. uni.removeStorageSync(T_PAY_ORDER)
  219. uni.setStorageSync(T_PAY_ORDER, {
  220. type,
  221. TL_ORDER_NO: data.orderNo
  222. })
  223. }
  224. if (res.isZeroOrder === '1') { // 零元支付情况
  225. uni.redirectTo({
  226. url: '/user/otherServe/payment-completed/index'
  227. })
  228. } else {
  229. const payData = res
  230. const form = document.createElement('form')
  231. form.setAttribute('action', payData.url)
  232. form.setAttribute('method', 'POST')
  233. const data = JSON.parse(payData.data)
  234. let input
  235. for (const key in data) {
  236. input = document.createElement('input')
  237. input.name = key
  238. input.value = data[key]
  239. form.appendChild(input)
  240. }
  241. document.body.appendChild(form)
  242. form.submit()
  243. document.body.removeChild(form)
  244. }
  245. })
  246. // #endif
  247. // #ifdef APP
  248. payOtherPlatformApi({
  249. userId: getUserId(),
  250. orderNo: data.orderNo,
  251. payType,
  252. ...otherArgs
  253. }).then((resStr) => {
  254. const res = JSON.parse(resStr)
  255. if (type) {
  256. uni.removeStorageSync(T_PAY_ORDER)
  257. uni.setStorageSync(T_PAY_ORDER, {
  258. type,
  259. TL_ORDER_NO: data.orderNo
  260. })
  261. }
  262. if (res.isZeroOrder === '1') { // 零元支付情况
  263. uni.redirectTo({
  264. url: '/user/otherServe/payment-completed/index'
  265. })
  266. } else {
  267. let query = ''
  268. for (const key in res.data) {
  269. query += key + '=' + res.data[key] + '&'
  270. }
  271. plus.share.getServices(function (result) {
  272. let sweixin = null
  273. for (const i in result) {
  274. if (result[i].id == 'weixin') {
  275. sweixin = result[i]
  276. }
  277. }
  278. if (sweixin) {
  279. sweixin.launchMiniProgram({
  280. id: 'gh_e64a1a89a0ad', // 微信小程序的原始ID("g_"开头的字符串)
  281. type: 0,
  282. path: 'pages/orderDetail/orderDetail?' + query
  283. })
  284. }
  285. }, function (e) {
  286. console.log('获取分享服务列表失败:' + e.message)
  287. })
  288. }
  289. })
  290. // #endif
  291. }
  292. }