index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import $http from '@/common/request/index'
  2. // #ifdef H5
  3. import wxsdk from '@/common/wechat/sdk'
  4. // #endif
  5. import wechat from '@/common/wechat/wechat'
  6. import {
  7. router as $Router
  8. } from '@/common/router/index.js'
  9. import store from '@/common/store';
  10. import $platform from '@/common/platform';
  11. /**
  12. * 支付
  13. *
  14. * @param {String} payment = ['wechat','alipay','wallet'] - 支付方式
  15. * @param {Object} order = {} - 订单详情
  16. * @param {String} orderType = ['goods','recharge'] - 订单类型
  17. */
  18. export default class ShoproPay {
  19. // wxOfficialAccount wxMiniProgram App H5
  20. // wechat 公众号JSSDK支付 小程序支付 微信开放平台支付 H5网页支付
  21. // alipay 复制网址 复制网址 支付宝开放平台支付 直接跳转链接
  22. // wallet v v v v
  23. constructor(payment, order, orderType) {
  24. this.payment = payment;
  25. this.order = order;
  26. this.orderType = orderType;
  27. this.platform = $platform.get();
  28. let payMehod = this.getPayMethod();
  29. payMehod();
  30. }
  31. getPayMethod() {
  32. var payMethod = {
  33. 'wxOfficialAccount': {
  34. 'wechat': () => {
  35. this.wxOfficialAccountPay()
  36. },
  37. 'alipay': () => {
  38. this.copyPayLink()
  39. },
  40. 'wallet': () => {
  41. this.walletPay()
  42. }
  43. },
  44. 'wxMiniProgram': {
  45. 'wechat': () => {
  46. this.wxMiniProgramPay()
  47. },
  48. 'alipay': () => {
  49. this.copyPayLink()
  50. },
  51. 'wallet': () => {
  52. this.walletPay()
  53. }
  54. },
  55. 'App': {
  56. 'wechat': () => {
  57. this.wechatPay()
  58. },
  59. 'alipay': () => {
  60. this.aliPay()
  61. },
  62. 'wallet': () => {
  63. this.walletPay()
  64. },
  65. },
  66. 'H5': {
  67. 'wechat': () => {
  68. this.wechatWapPay()
  69. },
  70. 'alipay': () => {
  71. this.goToPayLink()
  72. },
  73. 'wallet': () => {
  74. this.walletPay()
  75. },
  76. },
  77. }
  78. return payMethod[this.platform][this.payment];
  79. }
  80. // 预支付
  81. prepay() {
  82. let that = this;
  83. // console.log(this.platform);
  84. return new Promise((resolve, reject) => {
  85. let that = this;
  86. let params = {
  87. order_number: that.order.order_number,
  88. payment: that.payment,
  89. platform:that.platform,
  90. is_commission:that.orderType
  91. }
  92. if (uni.getStorageSync('openid')) {
  93. params.openid = uni.getStorageSync('openid');
  94. }
  95. $http('pay.prepay', params, '支付中').then(res => {
  96. // console.log(res);
  97. if (res.code === 0) {
  98. res.data === 'no_openid' ?
  99. uni.showModal({
  100. title: '微信支付',
  101. content: '请先绑定微信再使用微信支付',
  102. confirmColor:'#00CA88',
  103. success: function(res) {
  104. if (res.confirm) {
  105. wechat.bind();
  106. }
  107. },
  108. }) :
  109. resolve(res);
  110. }
  111. })
  112. });
  113. }
  114. // 微信H5支付
  115. async wxOfficialAccountPay() {
  116. let that = this;
  117. let result = await this.prepay();
  118. wxsdk.wxpay(result.data, (res) => {
  119. res.errMsg == "chooseWXPay:ok" ? that.payResult('success') : that.payResult('fail')
  120. });
  121. }
  122. //浏览器微信支付
  123. async wechatWapPay() {
  124. let that = this;
  125. let result = await this.prepay();
  126. if (result.code === 1) {
  127. var url = result.data.pay_data.match(/url\=\'(\S*)\'/);
  128. let reg = new RegExp('&', 'g') //g代表全部
  129. let newUrl = url[1].replace(reg, '&');
  130. let domain = store.getters.initShop.domain; //域名需要https
  131. let params = encodeURIComponent(
  132. `${domain}pages/order/payment/result?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
  133. )
  134. // let params = encodeURIComponent(
  135. // `${domain}pages/user/public/paymentdetail?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
  136. // )
  137. window.location.href = newUrl + '&redirect_url=' + params;
  138. }
  139. }
  140. // 微信小程序支付
  141. async wxMiniProgramPay() {
  142. let that = this;
  143. let result = await this.prepay();
  144. uni.requestPayment({
  145. provider: 'wxpay',
  146. ...result.data,
  147. success: res => {
  148. console.log(res);
  149. that.payResult('success')
  150. },
  151. fail: err => {
  152. console.log('支付取消或者失败:', err);
  153. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  154. uni.showModal({
  155. title: '失败',
  156. content: '支付取消或者失败',
  157. confirmText: '重新支付',
  158. confirmColor:'#00CA88',
  159. success: (res) => {
  160. if (res.confirm) {
  161. that.wxMiniProgramPay()
  162. }
  163. }
  164. })
  165. }
  166. });
  167. }
  168. // 余额支付
  169. async walletPay() {
  170. let that = this;
  171. let result = await this.prepay();
  172. result.code === 1 && that.payResult('success')
  173. }
  174. // 支付宝复制链接支付
  175. async copyPayLink() {
  176. let that = this;
  177. let result = await this.prepay();
  178. if (result.code === 1) {
  179. //引入showModal 点击确认 复制链接;
  180. uni.showModal({
  181. title: '支付宝支付',
  182. content: '复制链接到外部浏览器',
  183. confirmText: '复制链接',
  184. confirmColor:'#00CA88',
  185. success: (res) => {
  186. if (res.confirm) {
  187. uni.setClipboardData({
  188. data: result.data.pay_data,
  189. success: function(data) {
  190. that.$u.toast('已复制到剪切板');
  191. }
  192. });
  193. }
  194. }
  195. })
  196. }
  197. }
  198. // 支付链接
  199. async goToPayLink() {
  200. let that = this;
  201. let result = await this.prepay();
  202. if (result.code === 1) {
  203. window.location = result.data.pay_data;
  204. }
  205. }
  206. // 支付宝支付
  207. async aliPay() {
  208. let that = this;
  209. let result = await this.prepay();
  210. if (result.code === 1) {
  211. uni.requestPayment({
  212. provider: 'alipay',
  213. orderInfo: result.data.pay_data, //支付宝订单数据
  214. success: res => {
  215. that.payResult('success')
  216. },
  217. fail: err => {
  218. console.log('支付取消或者失败:', err);
  219. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  220. }
  221. });
  222. }
  223. }
  224. // 微信支付
  225. async wechatPay() {
  226. let that = this;
  227. let result = await this.prepay();
  228. if (result.code === 1) {
  229. uni.requestPayment({
  230. provider: 'wxpay',
  231. orderInfo: JSON.parse(result.data.pay_data), //微信订单数据(官方说是string。实测为object)
  232. success: res => {
  233. that.payResult('success')
  234. },
  235. fail: err => {
  236. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  237. console.log('支付取消或者失败:', err);
  238. }
  239. });
  240. }
  241. }
  242. // 支付结果跳转,success:成功,fail:失败
  243. payResult(resultType) {
  244. const that = this;
  245. if(resultType == 'success'){
  246. store.dispatch('getUserInfo')
  247. $Router.replace({
  248. path: '/pages/user/public/paymentdetail',
  249. query: {
  250. orderId: that.order.id,
  251. type: that.payment, //重新支付的时候使用
  252. payState: resultType,
  253. orderType: that.orderType
  254. }
  255. });
  256. store.commit("subscribeMessage", 'unread_msg');
  257. }else{
  258. uni.showToast({
  259. title:'支付失败',
  260. icon:'none'
  261. })
  262. }
  263. }
  264. }