fa-payment.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <template>
  2. <view>
  3. <u-popup v-model="isShow" mode="center" border-radius="5" z-index="1000">
  4. <view class="payment" :style="[payStyle]">
  5. <view class="header u-flex u-row-between u-p-l-30 u-p-r-30 u-p-t-20 u-p-b-20">
  6. <view class="">{{ title }}</view>
  7. <u-icon name="close" @click="close"></u-icon>
  8. </view>
  9. <view class="u-p-30">
  10. <view class="alert-warning u-p-20 u-m-b-10">
  11. <view class="">{{ articleTitle }}</view>
  12. <view class="price u-m-t-10">付费金额:¥{{ money }}</view>
  13. </view>
  14. <view class="u-border-top u-m-t-20">
  15. <u-radio-group v-model="paytype" class="group">
  16. <view class="group" v-for="(item, index) in paytypelist" :key="index">
  17. <u-cell-item :icon="item.icon" :icon-style="item.iconColor" :arrow="false" :title="item.name" @click="selectType(item.value)">
  18. <u-radio slot="right-icon" :active-color="theme.bgColor" label-size="30" :name="item.value">
  19. <u-icon size="30" name="arrow-right"></u-icon>
  20. </u-radio>
  21. </u-cell-item>
  22. </view>
  23. </u-radio-group>
  24. </view>
  25. </view>
  26. <view class="u-text-center u-p-30">
  27. <view class="">
  28. <u-button
  29. type="primary"
  30. hover-class="none"
  31. :custom-style="{ width: '80%', height: '60rpx', backgroundColor: theme.bgColor, color: theme.color }"
  32. size="mini"
  33. @click="submit"
  34. >
  35. 立即支付
  36. </u-button>
  37. </view>
  38. <view class="u-m-t-30" v-if="vip > 0">
  39. <u-button
  40. type="primary"
  41. hover-class="none"
  42. size="mini"
  43. :custom-style="{ width: '80%', height: '60rpx', backgroundColor: '#18b566', color: theme.color }"
  44. @click="$Router.push('/pages/my/member?vip='+vip)"
  45. >
  46. 升级VIP{{ vip }},免费阅读
  47. </u-button>
  48. </view>
  49. </view>
  50. </view>
  51. </u-popup>
  52. </view>
  53. </template>
  54. <script>
  55. import {loginfunc} from '@/common/fa.mixin.js'
  56. export default {
  57. mixins:[loginfunc],
  58. props:{
  59. title:{
  60. type:String,
  61. default:'付费阅读'
  62. },
  63. articleTitle:{
  64. type:String,
  65. default:''
  66. },
  67. articleId:{
  68. type:[String,Number],
  69. default:0
  70. },
  71. money:{
  72. type:[String,Number],
  73. default:0
  74. },
  75. vip:{
  76. type:[String,Number],
  77. default:0
  78. }
  79. },
  80. computed:{
  81. defaultpaytype(){
  82. if(this.vuex_config.config){
  83. return this.vuex_config.config.defaultpaytype;
  84. }
  85. return 'balance'
  86. },
  87. paytypelist(){
  88. if(this.vuex_config.config){
  89. this.paytype = this.vuex_config.config.defaultpaytype;
  90. let paytypelist = this.vuex_config.config.paytypelist.split(',');
  91. let list = [];
  92. paytypelist.map(item=>{
  93. switch(item){
  94. // #ifdef APP-PLUS || MP-WEIXIN || H5
  95. case 'wechat':
  96. list.push({
  97. name:'微信支付',
  98. value:'wechat',
  99. icon:'weixin-circle-fill',
  100. iconColor: { color:'#40BA49' }
  101. })
  102. break;
  103. // #endif
  104. // #ifdef APP-PLUS || H5 || MP-ALIPAY
  105. case 'alipay':
  106. if(!this.isWeixin){
  107. list.push({
  108. name:'支付宝支付',
  109. value:'alipay',
  110. icon:'zhifubao-circle-fill',
  111. iconColor: { color: '#00AAEE' }
  112. })
  113. }
  114. break;
  115. // #endif
  116. case 'balance':
  117. list.push({
  118. name:'余额支付',
  119. value:'balance',
  120. icon:'rmb-circle-fill',
  121. iconColor: { color: '#f39c12' }
  122. })
  123. break;
  124. }
  125. })
  126. return list;
  127. }
  128. return [];
  129. },
  130. isWeixin(){
  131. // #ifndef MP-WEIXIN
  132. return this.$util.isWeiXinBrowser()
  133. // #endif
  134. // #ifdef MP-WEIXIN
  135. return true;
  136. // #endif
  137. },
  138. payStyle(){
  139. let style = {};
  140. if(this.vip>0){
  141. style.height = this.paytypelist.length == 3 ? '870rpx' : this.paytypelist.length == 2 ? '770rpx' : '670rpx';
  142. }else{
  143. style.height = this.paytypelist.length == 3 ? '770rpx' : this.paytypelist.length == 2 ? '670rpx' : '570rpx'
  144. }
  145. return style;
  146. }
  147. },
  148. data() {
  149. return {
  150. isShow:false,
  151. paytype:''
  152. };
  153. },
  154. methods:{
  155. show(){
  156. this.isShow = true;
  157. },
  158. close(){
  159. this.isShow = false;
  160. },
  161. selectType(type){
  162. this.paytype = type;
  163. },
  164. // #ifdef MP-BAIDU
  165. submit: async function() {
  166. let res = await this.$api.getArchivesOrder({
  167. id: this.articleId,
  168. paytype: 'balance',
  169. method: 'miniapp'
  170. });
  171. if(!res.code){
  172. this.$u.toast(res.msg);
  173. return;
  174. }
  175. if(this.paytype=='balance'){
  176. this.close();
  177. this.$emit('success');
  178. this.close();
  179. return;
  180. }
  181. uni.requestPayment({
  182. provider: 'wxpay',
  183. timeStamp: res.data.timeStamp,
  184. nonceStr: res.data.nonceStr,
  185. package: res.data.package,
  186. signType: res.data.signType,
  187. paySign: res.data.paySign,
  188. success: rest => {
  189. this.$u.toast('支付成功!');
  190. this.$emit('success');
  191. this.close()
  192. },
  193. fail: err => {
  194. this.$u.toast('fail:' + JSON.stringify(err));
  195. }
  196. });
  197. }
  198. // #endif
  199. // #ifdef MP-WEIXIN
  200. submit: async function() {
  201. let res = await this.$api.getArchivesOrder({
  202. id: this.articleId,
  203. paytype: this.paytype,
  204. method: 'miniapp'
  205. });
  206. if(res.data=='bind'){
  207. this.$Router.push('/pages/login/wxlogin');
  208. return;
  209. }
  210. if(!res.code){
  211. this.$u.toast(res.msg);
  212. return;
  213. }
  214. if(this.paytype=='balance'){
  215. this.close();
  216. this.$emit('success');
  217. this.close();
  218. return;
  219. }
  220. uni.requestPayment({
  221. provider: 'wxpay',
  222. timeStamp: res.data.timeStamp,
  223. nonceStr: res.data.nonceStr,
  224. package: res.data.package,
  225. signType: res.data.signType,
  226. paySign: res.data.paySign,
  227. success: rest => {
  228. this.$u.toast('支付成功!');
  229. this.$emit('success');
  230. this.close()
  231. },
  232. fail: err => {
  233. this.$u.toast('fail:' + JSON.stringify(err));
  234. }
  235. });
  236. },
  237. // #endif
  238. // #ifdef H5
  239. submit: async function() {
  240. let data = {
  241. id: this.articleId,
  242. paytype: this.paytype,
  243. method: 'wap'
  244. };
  245. //在微信环境,且为微信支付
  246. if (this.isWeixin && this.paytype == 'wechat') {
  247. data.method = 'mp';
  248. let res = await this.$api.getArchivesOrder(data);
  249. if (res.data == 'bind') {
  250. this.goAuth();
  251. return;
  252. };
  253. if(!res.code){
  254. this.$u.toast(res.msg);
  255. return;
  256. }
  257. window.WeixinJSBridge.invoke(
  258. 'getBrandWCPayRequest',
  259. {
  260. appId: res.data.appId, // 公众号名称,由商户传入
  261. timeStamp: res.data.timeStamp, // 时间戳,自1970年以来的秒数
  262. nonceStr: res.data.nonceStr, // 随机串
  263. package: res.data.package,
  264. signType: res.data.signType, // 微信签名方式:
  265. paySign: res.data.paySign // 微信签名
  266. },
  267. rest => {
  268. if (rest.err_msg === 'get_brand_wcpay_request:ok') {
  269. this.$u.toast('支付成功!');
  270. this.$emit('success')
  271. this.close()
  272. } else if (rest.err_msg === 'get_brand_wcpay_request:cancel') {
  273. this.$u.toast('取消支付');
  274. } else {
  275. this.$u.toast('支付失败');
  276. }
  277. }
  278. );
  279. } else {
  280. //非微信环境的wap 支付方法,会返回orderid,再模拟表单提交
  281. data.returnurl = window.location.href;
  282. let res = await this.$api.getArchivesOrder(data);
  283. if(!res.code){
  284. this.$u.toast(res.msg);
  285. return;
  286. }
  287. if(this.paytype=='balance'){
  288. this.close();
  289. this.$emit('success');
  290. this.close();
  291. return;
  292. }
  293. //URL地址
  294. if(res.data.toString().match(/^((?:[a-z]+:)?\/\/)(.*)/i)){
  295. location.href = res.data;
  296. return;
  297. }
  298. //Form表单
  299. document.getElementsByTagName("body")[0].innerHTML = res.data;
  300. let form = document.querySelector("form");
  301. if(form && form.length>0){
  302. form.submit();
  303. return;
  304. }
  305. //Meta跳转
  306. let meta = document.querySelector('meta[http-equiv="refresh"]');
  307. if(meta && meta.length>0){
  308. setTimeout(function(){
  309. location.href = meta.content.split(/;/)[1];
  310. }, 300);
  311. return;
  312. }
  313. }
  314. }
  315. // #endif
  316. // #ifdef APP-PLUS
  317. submit: async function() {
  318. let appid = plus.runtime.appid;
  319. let res = await this.$api.getArchivesOrder({
  320. id: this.articleId,
  321. paytype: this.paytype,
  322. method: 'app',
  323. appid:appid
  324. });
  325. if(!res.code){
  326. this.$u.toast(res.msg);
  327. return;
  328. }
  329. if(this.paytype=='balance'){
  330. this.close();
  331. this.$emit('success');
  332. this.close();
  333. return;
  334. }
  335. uni.requestPayment({
  336. provider: this.paytype=='alipay'?'alipay':'wxpay',
  337. orderInfo: res.data, //微信、支付宝订单数据
  338. success: function (rest) {
  339. this.$u.toast('支付成功!');
  340. this.$emit('success');
  341. this.close()
  342. },
  343. fail: function (err) {
  344. console.log('fail:' + JSON.stringify(err));
  345. }
  346. });
  347. }
  348. // #endif
  349. }
  350. }
  351. </script>
  352. <style lang="scss">
  353. .payment {
  354. width: 600rpx;
  355. .header {
  356. border-bottom: 1px solid #eee;
  357. background-color: #f8f8f8;
  358. }
  359. .alert-info {
  360. background-color: #d9edf7;
  361. border-color: #bce8f1;
  362. color: #3498db;
  363. border-radius: 6rpx;
  364. }
  365. .alert-warning {
  366. background-color: #fcf8e3;
  367. border-color: #faebcc;
  368. color: #f39c12;
  369. border-radius: 6rpx;
  370. }
  371. .price {
  372. color: red;
  373. }
  374. .group {
  375. width: 100%;
  376. }
  377. }
  378. </style>