index2.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. }
  91. if (uni.getStorageSync('openid')) {
  92. params.openid = uni.getStorageSync('openid');
  93. }
  94. $http('pay.prepay', params, '支付中').then(res => {
  95. // console.log(res);
  96. if (res.code === 0) {
  97. res.data === 'no_openid' ?
  98. uni.showModal({
  99. title: '微信支付',
  100. content: '请先绑定微信再使用微信支付',
  101. confirmColor:'#00CA88',
  102. success: function(res) {
  103. if (res.confirm) {
  104. wechat.bind();
  105. }
  106. },
  107. }) :
  108. resolve(res);
  109. }
  110. })
  111. });
  112. }
  113. // 微信H5支付
  114. async wxOfficialAccountPay() {
  115. let that = this;
  116. let result = await this.prepay();
  117. wxsdk.wxpay(result.data, (res) => {
  118. res.errMsg == "chooseWXPay:ok" ? that.payResult('success') : that.payResult('fail')
  119. });
  120. }
  121. //浏览器微信支付
  122. async wechatWapPay() {
  123. let that = this;
  124. let result = await this.prepay();
  125. if (result.code === 1) {
  126. var url = result.data.pay_data.match(/url\=\'(\S*)\'/);
  127. let reg = new RegExp('&', 'g') //g代表全部
  128. let newUrl = url[1].replace(reg, '&');
  129. let domain = store.getters.initShop.domain; //域名需要https
  130. let params = encodeURIComponent(
  131. `${domain}pages/order/payment/result?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
  132. )
  133. // let params = encodeURIComponent(
  134. // `${domain}pages/user/public/paymentdetail?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
  135. // )
  136. window.location.href = newUrl + '&redirect_url=' + params;
  137. }
  138. }
  139. // 微信小程序支付
  140. async wxMiniProgramPay() {
  141. let that = this;
  142. let result = await this.prepay();
  143. uni.requestPayment({
  144. provider: 'wxpay',
  145. ...result.data,
  146. success: res => {
  147. console.log(res);
  148. that.payResult('success')
  149. },
  150. fail: err => {
  151. console.log('支付取消或者失败:', err);
  152. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  153. uni.showModal({
  154. title: '失败',
  155. content: '支付取消或者失败',
  156. confirmText: '重新支付',
  157. confirmColor:'#00CA88',
  158. success: (res) => {
  159. if (res.confirm) {
  160. that.wxMiniProgramPay()
  161. }
  162. }
  163. })
  164. }
  165. });
  166. }
  167. // 余额支付
  168. async walletPay() {
  169. let that = this;
  170. let result = await this.prepay();
  171. result.code === 1 && that.payResult('success')
  172. }
  173. // 支付宝复制链接支付
  174. async copyPayLink() {
  175. let that = this;
  176. let result = await this.prepay();
  177. if (result.code === 1) {
  178. //引入showModal 点击确认 复制链接;
  179. uni.showModal({
  180. title: '支付宝支付',
  181. content: '复制链接到外部浏览器',
  182. confirmText: '复制链接',
  183. confirmColor:'#00CA88',
  184. success: (res) => {
  185. if (res.confirm) {
  186. uni.setClipboardData({
  187. data: result.data.pay_data,
  188. success: function(data) {
  189. that.$u.toast('已复制到剪切板');
  190. }
  191. });
  192. }
  193. }
  194. })
  195. }
  196. }
  197. // 支付链接
  198. async goToPayLink() {
  199. let that = this;
  200. let result = await this.prepay();
  201. if (result.code === 1) {
  202. window.location = result.data.pay_data;
  203. }
  204. }
  205. // 支付宝支付
  206. async aliPay() {
  207. let that = this;
  208. let result = await this.prepay();
  209. if (result.code === 1) {
  210. uni.requestPayment({
  211. provider: 'alipay',
  212. orderInfo: result.data.pay_data, //支付宝订单数据
  213. success: res => {
  214. that.payResult('success')
  215. },
  216. fail: err => {
  217. console.log('支付取消或者失败:', err);
  218. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  219. }
  220. });
  221. }
  222. }
  223. // 微信支付
  224. async wechatPay() {
  225. let that = this;
  226. let result = await this.prepay();
  227. if (result.code === 1) {
  228. uni.requestPayment({
  229. provider: 'wxpay',
  230. orderInfo: JSON.parse(result.data.pay_data), //微信订单数据(官方说是string。实测为object)
  231. success: res => {
  232. that.payResult('success')
  233. },
  234. fail: err => {
  235. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  236. console.log('支付取消或者失败:', err);
  237. }
  238. });
  239. }
  240. }
  241. // 支付结果跳转,success:成功,fail:失败
  242. payResult(resultType) {
  243. const that = this;
  244. if(resultType == 'success'){
  245. store.dispatch('getUserInfo')
  246. $Router.replace({
  247. path: '/pages/user/public/paymentdetail',
  248. query: {
  249. orderId: that.order.id,
  250. type: that.payment, //重新支付的时候使用
  251. payState: resultType,
  252. orderType: that.orderType
  253. }
  254. });
  255. store.commit("subscribeMessage", 'unread_msg');
  256. }else{
  257. uni.showToast({
  258. title:'支付失败',
  259. icon:'none'
  260. })
  261. }
  262. }
  263. }