payment.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* eslint-disable */
  2. // #ifdef H5
  3. import { wechatConfig } from '@/api/basic';
  4. import $mConfig from '@/config/index.config';
  5. import jweixin from '@/common/jweixin';
  6. // #endif
  7. import { isBindingCheck } from '@/api/login';
  8. import { payCreate } from '@/api/basic';
  9. import { http } from '@/utils/request';
  10. import mHelper from '@/utils/helper';
  11. import mRouter from '@/utils/router';
  12. import { orderPay } from '@/api/product';
  13. export default {
  14. // 判断是否公众号(微信H5)
  15. isWechat() {
  16. // #ifdef H5
  17. const ua = window.navigator.userAgent.toLowerCase();
  18. console.log("window.navigator.userAgent.toLowerCase();"+window.navigator.userAgent.toLowerCase())
  19. if (ua.match(/micromessenger/i) == 'micromessenger') {
  20. return true;
  21. } else {
  22. return false;
  23. }
  24. // #endif
  25. },
  26. // wxjssdk
  27. async wxConfigH5(url) {
  28. if (this.isWechat()) {
  29. const jsApiList = JSON.stringify([
  30. 'chooseWXPay',
  31. 'scanQRCode',
  32. 'updateAppMessageShareData',
  33. 'updateTimelineShareData'
  34. ]);
  35. await http
  36. .post(`${wechatConfig}`, {
  37. url: url,
  38. jsApiList, // 需要调用微信的原生方法
  39. debug: false // 是否打开调试
  40. })
  41. .then(r => {
  42. jweixin.config({
  43. ...r.data
  44. });
  45. });
  46. }
  47. },
  48. /*
  49. *@des 微信支付
  50. *
  51. *@param order_group 订单:order;充值:recharge;
  52. *@param data 订单 {“order_id”:199} 充值 {“money”:100};
  53. */
  54. async weixinPay(order_group, data, route = '/pages/user/money/success', code) {
  55. // #ifdef H5
  56. if (!this.isWechat()) {
  57. mHelper.toast('仅支持微信H5、微信小程序、APP支付');
  58. return;
  59. }
  60. // #endif
  61. // #ifdef MP-QQ
  62. mHelper.toast('QQ小程序暂不支持充值');
  63. return;
  64. // #endif
  65. // #ifdef APP-PLUS
  66. await http
  67. .post(`${payCreate}`, {
  68. order_group,
  69. pay_type: 1,
  70. trade_type: 'app',
  71. data
  72. })
  73. .then(r => {
  74. uni.requestPayment({
  75. provider: 'wxpay', // 微信支付
  76. orderInfo: JSON.stringify(r.data.config), //微信订单数据 r.data.config
  77. success: function() {
  78. mHelper.toast('支付成功');
  79. mRouter.redirectTo({ route });
  80. },
  81. fail: function(err) {
  82. console.log('err', err);
  83. mHelper.toast('支付已取消');
  84. }
  85. });
  86. });
  87. // #endif
  88. // #ifndef APP-PLUS
  89. await http
  90. .post(`${isBindingCheck}`, {
  91. // #ifdef H5
  92. oauth_client: 'wechat',
  93. // #endif
  94. // #ifdef MP-WEIXIN
  95. oauth_client: 'wechatMp'
  96. // #endif
  97. })
  98. .then(async res => {
  99. if (res.data.openid) {
  100. await http
  101. .post(`${payCreate}`, {
  102. order_group,
  103. pay_type: 1,
  104. // #ifdef H5
  105. trade_type: 'js',
  106. // #endif
  107. // #ifdef MP-WEIXIN
  108. trade_type: 'mini_program',
  109. // #endif
  110. data,
  111. openid: res.data.openid
  112. })
  113. .then(r => {
  114. // #ifdef H5
  115. jweixin.ready(() => {
  116. jweixin.chooseWXPay({
  117. ...r.data.config,
  118. success() {
  119. mHelper.toast('支付成功');
  120. mRouter.redirectTo({ route });
  121. },
  122. fail(res) {
  123. // 支付成功后的回调函数
  124. mHelper.toast('支付已取消');
  125. }
  126. });
  127. });
  128. // #endif
  129. // #ifdef MP-WEIXIN
  130. uni.requestPayment({
  131. ...r.data.config,
  132. timeStamp: r.data.config.timestamp,
  133. success: () => {
  134. mHelper.toast('支付成功');
  135. mRouter.redirectTo({ route });
  136. },
  137. fail: res => {
  138. mHelper.toast('支付已取消');
  139. },
  140. complete: () => {}
  141. });
  142. // #endif
  143. });
  144. } else {
  145. // #ifdef MP-WEIXIN
  146. uni.login({
  147. provider: 'weixin',
  148. success: function(loginRes) {
  149. http
  150. .post(`${payCreate}?code=${loginRes.code}`, {
  151. order_group,
  152. pay_type: 1,
  153. trade_type: 'mini_program',
  154. data
  155. })
  156. .then(r => {
  157. uni.requestPayment({
  158. ...r.data.config,
  159. timeStamp: r.data.config.timestamp,
  160. success: () => {
  161. mHelper.toast('支付成功');
  162. mRouter.redirectTo({ route });
  163. },
  164. fail: res => {
  165. mHelper.toast('支付已取消');
  166. },
  167. complete: () => {}
  168. });
  169. });
  170. },
  171. fail: function(err) {
  172. mHelper.log(err);
  173. }
  174. });
  175. // #endif
  176. // #ifdef H5
  177. await http
  178. .post(`${payCreate}?code=${code}`, {
  179. order_group,
  180. pay_type: 1,
  181. trade_type: 'js',
  182. data
  183. })
  184. .then(r => {
  185. jweixin.ready(() => {
  186. jweixin.chooseWXPay({
  187. ...r.data.config,
  188. success() {
  189. mHelper.toast('支付成功');
  190. mRouter.redirectTo({ route });
  191. },
  192. fail(res) {
  193. // 支付成功后的回调函数
  194. mHelper.toast('支付已取消');
  195. }
  196. });
  197. });
  198. });
  199. // #endif
  200. }
  201. });
  202. // #endif
  203. },
  204. /*
  205. *@des 支付宝支付
  206. *
  207. *@param order_group 订单:order;充值:recharge;
  208. *@param data 订单 {“order_id”:199} 充值 {“money”:100};
  209. */
  210. async aliPay(order_group, data, route = '/pages/user/money/success') {
  211. // #ifdef MP-QQ
  212. mHelper.toast('QQ小程序不支持支付宝充值');
  213. return;
  214. // #endif
  215. // #ifdef MP-WEIXIN
  216. mHelper.toast('微信小程序不支持支付宝充值');
  217. return;
  218. // #endif
  219. await http
  220. .post(`${payCreate}`, {
  221. order_group,
  222. pay_type: 2,
  223. // #ifdef H5
  224. trade_type: 'wap',
  225. // #endif
  226. // #ifdef APP-PLUS
  227. trade_type: 'app',
  228. // #endif
  229. data
  230. })
  231. .then(r => {
  232. // #ifdef H5
  233. window.location.href = r.data.config.config;
  234. // #endif
  235. // #ifdef APP-PLUS
  236. uni.requestPayment({
  237. provider: 'alipay',
  238. orderInfo: r.data.config.config, //微信、支付宝订单数据
  239. success: function() {
  240. mHelper.toast('支付成功');
  241. mRouter.redirectTo({ route });
  242. },
  243. fail: function(err) {
  244. mHelper.toast('支付已取消');
  245. }
  246. });
  247. // #endif
  248. });
  249. },
  250. /*
  251. *@des 余额支付
  252. *
  253. *@param data 支付参数
  254. */
  255. async balancePay(data, route = '/pages/user/money/success') {
  256. await http
  257. .post(`${orderPay}`, {
  258. order_group: 'order',
  259. trade_type: 'js',
  260. pay_type: 5,
  261. data
  262. })
  263. .then(() => {
  264. mRouter.redirectTo({ route });
  265. });
  266. }
  267. };