payment.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <view>
  3. <view
  4. style="width: 700upx;height: 180upx;background: #FFFFFF;margin: 30upx; padding: 20upx;border-radius: 20upx;">
  5. <view style="display: flex;margin: 15upx 0 0 20upx;">
  6. <image :src="image" style="width: 20%; height: 120upx;"></image>
  7. <view style="margin-left: 20upx;width: 70%;">
  8. <view style="font-size: 28upx;color: #05C25C;font-weight: bold;margin-top: 10upx;">¥ {{money}}
  9. </view>
  10. <view
  11. style="width: 100%;margin-top: 20upx;font-size: 28upx;color: #333333;font-weight: bold;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">
  12. {{title}}
  13. </view>
  14. </view>
  15. </view>
  16. </view>
  17. <view style="width: 700upx;background: #FFFFFF;margin: 30upx; padding: 20upx;border-radius: 20upx;"
  18. v-if="payList.length > 0">
  19. <view style="display: flex;height: 100upx;margin: 0upx 0 0 30upx;align-items: center;padding-bottom: 20upx;"
  20. v-for="(item,index) in payList" :key='index'>
  21. <image :src="item.image" style="width: 50upx;height: 50upx;border-radius: 50upx;"></image>
  22. <view style="font-size: 28upx;color: #333333;margin-left: 20upx;width: 70%;" v-if="item.name == '钱包零钱'">
  23. {{item.name}}({{lingqian}})
  24. </view>
  25. <view style="font-size: 28upx;color: #333333;margin-left: 20upx;width: 70%;" v-else>{{item.name}}</view>
  26. <radio-group name="openWay" style="margin-left: 40upx;" @tap='selectWay(item.id)'>
  27. <label class="tui-radio">
  28. <radio color="#05C25C" :checked="openWay === item.id ? true : false" />
  29. </label>
  30. </radio-group>
  31. </view>
  32. </view>
  33. <button v-if="payList.length > 0"
  34. style="background-color: #05C25C;width: 680upx;height: 80upx;font-size: 28upx;color: #FFFFFF;text-align: center;margin: 30upx;border-radius: 50upx;padding-top: 5upx;"
  35. @tap='goPay'>确定支付</button>
  36. </view>
  37. </template>
  38. <script>
  39. export default {
  40. data() {
  41. return {
  42. openWay: 0,
  43. money: 0,
  44. lingqian: 0.00,
  45. title: '',
  46. image: '',
  47. id: '',
  48. providerList: [],
  49. checkWeixin: true,
  50. checkZhifubao: true,
  51. payList: [{
  52. id: 0,
  53. image: '/static/image/pay_weixin.png',
  54. name: '微信'
  55. }, {
  56. id: 1,
  57. image: '/static/image/pay_zhifubao.png',
  58. name: '支付宝'
  59. },
  60. {
  61. id: 2,
  62. image: '/static/image/pay_lingqian.png',
  63. name: '钱包零钱'
  64. }
  65. ]
  66. }
  67. },
  68. onBackPress(event) {
  69. if (event.from == 'backbutton') {
  70. uni.showModal({
  71. title: '温馨提示',
  72. content: '订单尚未完成支付,确认退出吗?',
  73. showCancel: true,
  74. cancelText: '继续支付',
  75. confirmText: '确定',
  76. success: res => {
  77. if (res.confirm) {
  78. uni.redirectTo({
  79. url: '../member/orderdetail?id=' + this.id
  80. });
  81. }
  82. }
  83. });
  84. return true; //阻止默认返回行为
  85. }
  86. },
  87. onLoad(d) {
  88. this.getlingqian();
  89. // #ifdef MP-WEIXIN
  90. this.payList = [];
  91. this.openWay = 0;
  92. let data = [];
  93. let data1 = [{
  94. id: 0,
  95. image: '/static/image/pay_weixin.png',
  96. name: '微信'
  97. }, {
  98. id: 2,
  99. image: '/static/image/pay_lingqian.png',
  100. name: '钱包零钱'
  101. }]
  102. this.payList = data1;
  103. // #endif
  104. // #ifdef APP-PLUS
  105. this.payList = [];
  106. this.openWay = 0;
  107. let data1 = [{
  108. id: 0,
  109. image: '/static/image/pay_weixin.png',
  110. name: '微信'
  111. }, {
  112. id: 1,
  113. image: '/static/image/pay_zhifubao.png',
  114. name: '支付宝'
  115. }, {
  116. id: 2,
  117. image: '/static/image/pay_lingqian.png',
  118. name: '钱包零钱'
  119. }]
  120. this.payList = data1;
  121. // #endif
  122. // #ifdef H5
  123. let target = navigator.userAgent.toLowerCase();
  124. let isWeixin = (/micromessenger/.test(target)) ? true : false;
  125. this.payList = [];
  126. if (isWeixin) {
  127. this.openWay = 0;
  128. let data1 = [{
  129. id: 0,
  130. image: '/static/image/pay_weixin.png',
  131. name: '微信'
  132. }, {
  133. id: 2,
  134. image: '/static/image/pay_lingqian.png',
  135. name: '钱包零钱'
  136. }]
  137. this.payList = data1;
  138. } else {
  139. this.openWay = 1;
  140. let data1 = [{
  141. id: 1,
  142. image: '/static/image/pay_zhifubao.png',
  143. name: '支付宝'
  144. }, {
  145. id: 2,
  146. image: '/static/image/pay_lingqian.png',
  147. name: '钱包零钱'
  148. }]
  149. this.payList = data1;
  150. }
  151. // #endif
  152. if (d) {
  153. this.id = d.id;
  154. this.getCheckPay(d.id);
  155. this.getCommondityList(d.id);
  156. }
  157. uni.getProvider({
  158. service: 'payment',
  159. success: e => {
  160. let providerList = [];
  161. e.provider.map(value => {
  162. switch (value) {
  163. case 'alipay':
  164. providerList.push({
  165. name: '支付宝',
  166. id: value,
  167. loading: false
  168. });
  169. break;
  170. case 'wxpay':
  171. providerList.push({
  172. name: '微信',
  173. id: value,
  174. loading: false
  175. });
  176. break;
  177. default:
  178. break;
  179. }
  180. });
  181. this.providerList = providerList;
  182. },
  183. fail: e => {
  184. console.log('获取支付通道失败:', e);
  185. }
  186. });
  187. },
  188. methods: {
  189. getlingqian() {
  190. let userId = this.$queue.getData('userId');
  191. this.$Request.get("/app/artificer/selectArtificerMoney").then(res => {
  192. if (res.code == 0) {
  193. this.lingqian = res.data.money ? res.data.money : 0; //累计余额
  194. }
  195. });
  196. },
  197. getCheckPay(id) {
  198. this.$Request.getT('/app/orders/wxPay?ordersId=' + id).then(res => {
  199. if (res.status === 0) {
  200. uni.redirectTo({
  201. url: './myList'
  202. });
  203. }
  204. });
  205. },
  206. getCommondityList(id) {
  207. uni.showLoading({
  208. title: '加载中...'
  209. });
  210. this.$Request.getT('/app/orders/find?id=' + id).then(res => {
  211. if (res.status === 0) {
  212. this.image = res.data.img;
  213. this.money = res.data.payMoney;
  214. this.title = res.data.title;
  215. }
  216. uni.hideLoading();
  217. });
  218. },
  219. selectWay: function(id) {
  220. this.openWay = id;
  221. },
  222. goPay() {
  223. let userId = this.$queue.getData('userId');
  224. uni.showLoading({
  225. title: '支付中'
  226. });
  227. if (this.openWay === 0) { //0:微信支付
  228. // #ifdef H5
  229. this.$Request.postJson('/pay/wxPayWeb?ordersId=' + this.id + '&userId=' + userId).then(res => {
  230. this.callPay(res);
  231. });
  232. // #endif
  233. // #ifdef APP-PLUS
  234. this.$Request.postJson('/pay/wxPayApp?ordersId=' + this.id + '&userId=' + userId).then(res => {
  235. this.setPayment('wxpay', JSON.stringify(res));
  236. });
  237. // #endif
  238. // #ifdef MP-WEIXIN
  239. this.$Request.postJson('/pay/wxPayJS?ordersId=' + this.id + '&userId=' + userId).then(res => {
  240. uni.requestPayment({
  241. provider: 'wxpay',
  242. timeStamp: res.timestamp,
  243. nonceStr: res.noncestr,
  244. package: res.package,
  245. signType: res.signType,
  246. paySign: res.sign,
  247. success: function(res) {
  248. uni.showLoading({
  249. title: '支付成功'
  250. });
  251. uni.hideLoading();
  252. setTimeout(() => {
  253. uni.redirectTo({
  254. url: '../my/myList'
  255. });
  256. }, 1000);
  257. },
  258. fail: function(err) {
  259. uni.hideLoading();
  260. this.$queue.showToast('支付失败');
  261. }
  262. });
  263. });
  264. // #endif
  265. } else if (this.openWay === 1) { //1:支付宝支付
  266. // #ifdef H5
  267. this.$Request.getT('/aliPay/payH5?ordersId=' + this.id).then(res => {
  268. const div = document.createElement('div')
  269. div.innerHTML = res.data //此处form就是后台返回接收到的数据
  270. document.body.appendChild(div)
  271. document.forms[0].submit()
  272. });
  273. // #endif
  274. // #ifdef APP-PLUS
  275. this.$Request.getT('/aliPay/payApp?ordersId=' + this.id).then(res => {
  276. this.setPayment('alipay', res.data);
  277. });
  278. // #endif
  279. } else if (this.openWay === 2) { //2:零钱支付
  280. let orderIds = this.id;
  281. this.$Request.getT('/app/orders/changePay?ordersId=' + this.id).then(res => {
  282. if (res.status === 0) {
  283. uni.hideLoading();
  284. this.$queue.showToast('支付成功');
  285. setTimeout(function() {
  286. console.log("id:" + orderIds)
  287. uni.hideLoading();
  288. uni.redirectTo({
  289. url: '/package/pages/zysc/member/orderdetail?id=' + orderIds
  290. });
  291. }, 1000);
  292. } else {
  293. uni.hideLoading();
  294. this.$queue.showToast(res.msg);
  295. }
  296. });
  297. }
  298. },
  299. callPay: function(response) {
  300. if (typeof WeixinJSBridge === "undefined") {
  301. if (document.addEventListener) {
  302. document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady(response), false);
  303. } else if (document.attachEvent) {
  304. document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(response));
  305. document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(response));
  306. }
  307. } else {
  308. this.onBridgeReady(response);
  309. }
  310. },
  311. onBridgeReady: function(response) {
  312. let that = this;
  313. if (!response.package) {
  314. return;
  315. }
  316. WeixinJSBridge.invoke(
  317. 'getBrandWCPayRequest', {
  318. "appId": response.appid, //公众号名称,由商户传入
  319. "timeStamp": response.timestamp, //时间戳,自1970年以来的秒数
  320. "nonceStr": response.noncestr, //随机串
  321. "package": response.package,
  322. "signType": response.signType, //微信签名方式:
  323. "paySign": response.sign //微信签名
  324. },
  325. function(res) {
  326. if (res.err_msg === "get_brand_wcpay_request:ok") {
  327. // 使用以上方式判断前端返回,微信团队郑重提示:
  328. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  329. uni.showLoading({
  330. title: '支付成功'
  331. });
  332. setTimeout(function() {
  333. uni.hideLoading();
  334. uni.redirectTo({
  335. url: '../member/orderdetail?id=' + that.id
  336. });
  337. }, 1000);
  338. } else {
  339. uni.hideLoading();
  340. }
  341. WeixinJSBridge.log(response.err_msg);
  342. }
  343. );
  344. },
  345. setPayment(name, order) {
  346. let that = this;
  347. uni.requestPayment({
  348. provider: name,
  349. orderInfo: order, //微信、支付宝订单数据
  350. success: function(res) {
  351. uni.hideLoading();
  352. uni.showLoading({
  353. title: '支付成功'
  354. });
  355. setTimeout(function() {
  356. uni.hideLoading();
  357. uni.redirectTo({
  358. url: '../member/orderdetail?id=' + that.id
  359. });
  360. }, 1000);
  361. },
  362. fail: function(err) {
  363. uni.hideLoading();
  364. console.log(12)
  365. }
  366. });
  367. }
  368. }
  369. }
  370. </script>
  371. <style>
  372. page {
  373. background: #F2F2F2;
  374. }
  375. </style>