index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. import req from '@/common/request/apis.js'
  12. import http from '@/common/request/index'
  13. // console.log('12213', req.userInfo.info);
  14. /**
  15. * 支付
  16. *
  17. * @param {String} payment = ['wechat','alipay','wallet'] - 支付方式
  18. * @param {Object} order = {} - 订单详情
  19. * @param {String} orderType = ['goods','recharge'] - 订单类型
  20. */
  21. export default class ShoproPay {
  22. // wxOfficialAccount wxMiniProgram App H5
  23. // wechat 公众号JSSDK支付 小程序支付 微信开放平台支付 H5网页支付
  24. // alipay 复制网址 复制网址 支付宝开放平台支付 直接跳转链接
  25. // wallet v v v v
  26. constructor(payment, order, orderType) {
  27. this.payment = payment;
  28. this.order = order;
  29. this.orderType = orderType;
  30. this.platform = $platform.get();
  31. let payMehod = this.getPayMethod();
  32. payMehod();
  33. }
  34. getPayMethod() {
  35. var payMethod = {
  36. 'wxOfficialAccount': {
  37. 'wechat': () => {
  38. this.wxOfficialAccountPay()
  39. },
  40. 'alipay': () => {
  41. this.copyPayLink()
  42. },
  43. 'wallet': () => {
  44. this.walletPay()
  45. }
  46. },
  47. 'wxMiniProgram': {
  48. 'wechat': () => {
  49. this.wxMiniProgramPay()
  50. },
  51. 'alipay': () => {
  52. this.copyPayLink()
  53. },
  54. 'wallet': () => {
  55. this.walletPay()
  56. }
  57. },
  58. 'App': {
  59. 'wechat': () => {
  60. this.wechatPay()
  61. },
  62. 'alipay': () => {
  63. this.aliPay()
  64. },
  65. 'wallet': () => {
  66. this.walletPay()
  67. },
  68. },
  69. 'H5': {
  70. 'wechat': () => {
  71. this.wechatWapPay()
  72. },
  73. 'alipay': () => {
  74. this.goToPayLink()
  75. },
  76. 'wallet': () => {
  77. this.walletPay()
  78. },
  79. },
  80. }
  81. return payMethod[this.platform][this.payment];
  82. }
  83. // 预支付
  84. prepay() {
  85. let that = this;
  86. // console.log(this.platform);
  87. return new Promise((resolve, reject) => {
  88. let that = this;
  89. let params = {
  90. order_number: that.order.order_number,
  91. payment: that.payment,
  92. platform: that.platform,
  93. is_commission: that.orderType
  94. }
  95. if (uni.getStorageSync('openid')) {
  96. params.openid = uni.getStorageSync('openid');
  97. }
  98. $http('pay.prepay', params, '支付中').then(res => {
  99. // console.log(res);
  100. if (res.code === 0) {
  101. res.data === 'no_openid' ?
  102. uni.showModal({
  103. title: '微信支付',
  104. content: '请先绑定微信再使用微信支付',
  105. confirmColor: '#00CA88',
  106. success: function(res) {
  107. if (res.confirm) {
  108. wechat.bind();
  109. }
  110. },
  111. }) :
  112. resolve(res);
  113. }
  114. })
  115. });
  116. }
  117. // 微信H5支付
  118. async wxOfficialAccountPay() {
  119. let that = this;
  120. let result = await this.prepay();
  121. wxsdk.wxpay(result.data, (res) => {
  122. res.errMsg == "chooseWXPay:ok" ? that.payResult('success') : that.payResult('fail')
  123. });
  124. }
  125. //浏览器微信支付
  126. async wechatWapPay() {
  127. let that = this;
  128. let result = await this.prepay();
  129. if (result.code === 1) {
  130. var url = result.data.pay_data.match(/url\=\'(\S*)\'/);
  131. let reg = new RegExp('&', 'g') //g代表全部
  132. let newUrl = url[1].replace(reg, '&');
  133. let domain = store.getters.initShop.domain; //域名需要https
  134. let params = encodeURIComponent(
  135. `${domain}pages/order/payment/result?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
  136. )
  137. // let params = encodeURIComponent(
  138. // `${domain}pages/user/public/paymentdetail?orderId=${that.order.id}&type=${that.payment}&orderType=${that.orderType}`
  139. // )
  140. window.location.href = newUrl + '&redirect_url=' + params;
  141. }
  142. }
  143. // 微信小程序支付
  144. async wxMiniProgramPay() {
  145. let that = this;
  146. let result = await this.prepay();
  147. uni.requestPayment({
  148. provider: 'wxpay',
  149. ...result.data,
  150. success: res => {
  151. console.log(res);
  152. that.payResult('success')
  153. },
  154. fail: err => {
  155. console.log('支付取消或者失败:', err);
  156. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  157. uni.showModal({
  158. title: '失败',
  159. content: '支付取消或者失败',
  160. confirmText: '重新支付',
  161. confirmColor: '#00CA88',
  162. success: (res) => {
  163. if (res.confirm) {
  164. that.wxMiniProgramPay()
  165. }
  166. }
  167. })
  168. }
  169. });
  170. }
  171. // 余额支付
  172. async walletPay() {
  173. let that = this;
  174. let result = await this.prepay();
  175. result.code === 1 && that.payResult('success')
  176. }
  177. // 支付宝复制链接支付
  178. async copyPayLink() {
  179. let that = this;
  180. let result = await this.prepay();
  181. if (result.code === 1) {
  182. //引入showModal 点击确认 复制链接;
  183. uni.showModal({
  184. title: '支付宝支付',
  185. content: '复制链接到外部浏览器',
  186. confirmText: '复制链接',
  187. confirmColor: '#00CA88',
  188. success: (res) => {
  189. if (res.confirm) {
  190. uni.setClipboardData({
  191. data: result.data.pay_data,
  192. success: function(data) {
  193. that.$u.toast('已复制到剪切板');
  194. }
  195. });
  196. }
  197. }
  198. })
  199. }
  200. }
  201. // 支付链接
  202. async goToPayLink() {
  203. let that = this;
  204. let result = await this.prepay();
  205. if (result.code === 1) {
  206. window.location = result.data.pay_data;
  207. }
  208. }
  209. // 支付宝支付
  210. async aliPay() {
  211. let that = this;
  212. let result = await this.prepay();
  213. if (result.code === 1) {
  214. uni.requestPayment({
  215. provider: 'alipay',
  216. orderInfo: result.data.pay_data, //支付宝订单数据
  217. success: res => {
  218. that.payResult('success')
  219. },
  220. fail: err => {
  221. console.log('支付取消或者失败:', err);
  222. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  223. }
  224. });
  225. }
  226. }
  227. // 微信支付
  228. async wechatPay() {
  229. let that = this;
  230. let result = await this.prepay();
  231. if (result.code === 1) {
  232. uni.requestPayment({
  233. provider: 'wxpay',
  234. orderInfo: JSON.parse(result.data.pay_data), //微信订单数据(官方说是string。实测为object)
  235. success: res => {
  236. that.payResult('success')
  237. },
  238. fail: err => {
  239. err.errMsg !== "requestPayment:fail cancel" && that.payResult('fail')
  240. console.log('支付取消或者失败:', err);
  241. }
  242. });
  243. }
  244. }
  245. // 支付结果跳转,success:成功,fail:失败
  246. // payResult(resultType) {
  247. // const that = this;
  248. // if(resultType == 'success'){
  249. // store.dispatch('getUserInfo')
  250. // $Router.replace({
  251. // path: '/pages/user/public/paymentdetail',
  252. // query: {
  253. // orderId: that.order.id,
  254. // type: that.payment, //重新支付的时候使用
  255. // payState: resultType,
  256. // orderType: that.orderType
  257. // }
  258. // });
  259. // store.commit("subscribeMessage", 'unread_msg');
  260. // }else{
  261. // uni.showToast({
  262. // title:'支付失败',
  263. // icon:'none'
  264. // })
  265. // }
  266. // }
  267. payResult(resultType) {
  268. const that = this;
  269. if (resultType == 'success') {
  270. store.dispatch('getUserInfo')
  271. setTimeout(() => {
  272. let isNewUser = store.getters.userInfo.is_new
  273. if (isNewUser) {
  274. $Router.replace({
  275. path: '/pages/user/userinfo',
  276. query: {
  277. isNewUser: true
  278. }
  279. });
  280. store.commit("subscribeMessage", 'unread_msg');
  281. } else {
  282. $Router.replace({
  283. path: '/pages/user/public/paymentdetail',
  284. query: {
  285. orderId: that.order.id,
  286. type: that.payment, //重新支付的时候使用
  287. payState: resultType,
  288. orderType: that.orderType
  289. }
  290. });
  291. store.commit("subscribeMessage", 'unread_msg');
  292. }
  293. }, 1500)
  294. } else {
  295. uni.showToast({
  296. title: '支付失败',
  297. icon: 'none'
  298. })
  299. }
  300. }
  301. // payResult(resultType) {
  302. // const that = this;
  303. // if (resultType == 'success') {
  304. // store.dispatch('getUserInfo')
  305. // uni.request({
  306. // url: req.userInfo.info, //仅为示例,并非真实接口地址。
  307. // data: {},
  308. // header: {
  309. // 'Token': uni.getStorageSync('token') //自定义请求头信息
  310. // },
  311. // success: (res) => {
  312. // if (res.data.code == 0) {
  313. // if (res.data.data.is_new) {
  314. // $Router.replace({
  315. // path: '/pages/user/userinfo',
  316. // query: {
  317. // isNewUser: true
  318. // }
  319. // });
  320. // store.commit("subscribeMessage", 'unread_msg');
  321. // } else {
  322. // $Router.replace({
  323. // path: '/pages/user/public/paymentdetail',
  324. // query: {
  325. // orderId: that.order.id,
  326. // type: that.payment, //重新支付的时候使用
  327. // payState: resultType,
  328. // orderType: that.orderType
  329. // }
  330. // });
  331. // store.commit("subscribeMessage", 'unread_msg');
  332. // }
  333. // } else {
  334. // uni.showToast({
  335. // title: res.data.msg,
  336. // icon: 'none'
  337. // })
  338. // }
  339. // },
  340. // fail: (err) => {
  341. // uni.showToast({
  342. // title: err,
  343. // icon: 'none'
  344. // })
  345. // }
  346. // });
  347. // } else {
  348. // uni.showToast({
  349. // title: '支付失败',
  350. // icon: 'none'
  351. // })
  352. // }
  353. // }
  354. // payResult(resultType) {
  355. // const that = this;
  356. // if (resultType == 'success') {
  357. // store.dispatch('getUserInfo')
  358. // uni.request({
  359. // url: req.userInfo.info, //仅为示例,并非真实接口地址。
  360. // data: {},
  361. // header: {
  362. // 'Token': uni.getStorageSync('token') //自定义请求头信息
  363. // },
  364. // success: (res) => {
  365. // if (res.data.code == 0) {
  366. // if (res.data.data.is_new) {
  367. // $Router.replace({
  368. // path: '/pages/user/userinfo',
  369. // query: {
  370. // isNewUser: true
  371. // }
  372. // });
  373. // store.commit("subscribeMessage", 'unread_msg');
  374. // } else {
  375. // $Router.replace({
  376. // path: '/pages/user/public/paymentdetail',
  377. // query: {
  378. // orderId: that.order.id,
  379. // type: that.payment, //重新支付的时候使用
  380. // payState: resultType,
  381. // orderType: that.orderType
  382. // }
  383. // });
  384. // store.commit("subscribeMessage", 'unread_msg');
  385. // }
  386. // } else {
  387. // uni.showToast({
  388. // title: res.data.msg,
  389. // icon: 'none'
  390. // })
  391. // }
  392. // },
  393. // fail: (err) => {
  394. // uni.showToast({
  395. // title: err,
  396. // icon: 'none'
  397. // })
  398. // }
  399. // });
  400. // } else {
  401. // uni.showToast({
  402. // title: '支付失败',
  403. // icon: 'none'
  404. // })
  405. // }
  406. // }
  407. // payResult(resultType) {
  408. // const that = this;
  409. // if (resultType == 'success') {
  410. // http('user.info').then(res => {
  411. // if (res.code === 0) {
  412. // if (res.data.is_new) {
  413. // $Router.replace({
  414. // path: '/pages/user/userinfo',
  415. // query: {
  416. // isNewUser: true
  417. // }
  418. // });
  419. // store.commit("subscribeMessage", 'unread_msg');
  420. // } else {
  421. // $Router.replace({
  422. // path: '/pages/user/public/paymentdetail',
  423. // query: {
  424. // orderId: that.order.id,
  425. // type: that.payment, //重新支付的时候使用
  426. // payState: resultType,
  427. // orderType: that.orderType
  428. // }
  429. // });
  430. // store.commit("subscribeMessage", 'unread_msg');
  431. // }
  432. // } else {
  433. // uni.showToast({
  434. // title: res.msg,
  435. // icon: 'none'
  436. // })
  437. // }
  438. // })
  439. // } else {
  440. // uni.showToast({
  441. // title: '支付失败',
  442. // icon: 'none'
  443. // })
  444. // }
  445. // }
  446. }