createOrder_pay.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <template>
  2. <uni-popup ref="popup" class="create-order-pay" type="bottom">
  3. <view class="bcf page">
  4. <!-- 标题 -->
  5. <view class="d-f flex-c p10 title">
  6. <view class="F1 f15 fw700">选择支付方式</view>
  7. <text class="iconfont iconclose" @click="close"></text>
  8. </view>
  9. <!-- 选择支付方式 -->
  10. <view class="pay-type">
  11. <view class="d-f p10 flex-c" v-for="(l, i) in list" v-bind:key="i">
  12. <image style="height:20px;width:19px;" :src="l.img"></image>
  13. <text class="f14 c6 F1 ml8">{{ l.title }}</text>
  14. <view @click="radioClick(l, l.type)" :class="'radio ' + (l.check ? 'radio-active' : '')"></view>
  15. </view>
  16. </view>
  17. <!-- 提交按钮 -->
  18. <view class="d-f flex-c" >
  19. <view class="F1 pl10 d-f flex-c">
  20. <text class="f16">合计:</text>
  21. <text class="money-color f12 pt4">¥</text>
  22. <text class="money-color f20">{{ totalPrice }}</text>
  23. </view>
  24. <view class="button cf" :style="'background:' + (isSubmit ? '#999' : '#FC5B62')" @click="payTypeload">立即支付</view>
  25. </view>
  26. </view>
  27. </uni-popup>
  28. </template>
  29. <script>
  30. import uniPopup from '@/components/uni-popup/uni-popup.vue';
  31. const type = [
  32. {
  33. img: 'http://lxscimg.liuniu946.com/2019-11-29_5de0aad619857.png',
  34. title: '微信支付',
  35. check: false,
  36. type: 0 // wx
  37. },
  38. {
  39. img: 'http://lxscimg.liuniu946.com/2019-11-29_5de0aad9224d4.png',
  40. title: '零钱支付',
  41. check: false,
  42. type: 1 // 1
  43. },
  44. {
  45. img: 'http://lxscimg.liuniu946.com/2019-11-29_5de0aad9224d4.png',
  46. title: '钱包支付',
  47. check: false,
  48. type: 3 // 1
  49. },
  50. // #ifdef APP-PLUS
  51. {
  52. img: 'http://lxscimg.liuniu946.com/2019-12-12_5df1aa4e0f9b8.png',
  53. title: '支付宝支付',
  54. check: false,
  55. type: 2 // ali
  56. },
  57. // #endif
  58. {
  59. img: 'http://lxscimg.liuniu946.com/2019-12-26_5e0421807d558.png',
  60. title: '积分兑换',
  61. check: false,
  62. type: 4 // ali
  63. }
  64. ];
  65. export default {
  66. props: {
  67. totalPrice: [String, Number],
  68. //有几种支付方式
  69. payType: {
  70. type: Array,
  71. default: () => [0, 1, 2, 3]
  72. },
  73. //默认的支付方式
  74. defaultType: {
  75. type: Number,
  76. default: 2
  77. },
  78. // 是否需要调用外部按钮触发方法
  79. payCall: {
  80. type: Boolean,
  81. default: false
  82. },
  83. mtype: [String, Number]
  84. },
  85. data() {
  86. let list = [],
  87. _self = this;
  88. let payType = _self.payType.slice(0);
  89. // if(!_self.$store.state.app.state){
  90. // payType = [2]
  91. // }
  92. // if(_self.payType.slice(0).indexOf(4)>-1){
  93. // payType = [4]
  94. // }
  95. let ttype = null;
  96. type.forEach(e => {
  97. if (payType.indexOf(e.type) > -1) {
  98. e.check = e.type == _self.defaultType ? true : false;
  99. if (e.type == _self.defaultType) {
  100. ttype = e.type;
  101. }
  102. list.push(e);
  103. }
  104. });
  105. return {
  106. list,
  107. isSubmit: false,
  108. type: ttype, //0 微信支付 1余额支付 2支付宝支付
  109. order_num: '' //订单号
  110. };
  111. },
  112. created() {
  113. this.init();
  114. },
  115. methods: {
  116. //拼团购买逻辑
  117. assembleBuy(parent, d) {
  118. let _self = this;
  119. let data = {
  120. gg_id: _self.ggId,
  121. num: d[0].goods_info[0].num * 1,
  122. add_id: parent.addressData.id,
  123. ms_type: _self.msType
  124. };
  125. let url = '/goods/groupAdd';
  126. if (_self.gpId) {
  127. data.gp_id = _self.gpId;
  128. url = '/goods/groupUpd';
  129. }
  130. _self
  131. .request({ url, data })
  132. .then(r => {
  133. if (r.code == 200) {
  134. _self.order_num = r.data;
  135. _self.submit();
  136. } else {
  137. _self.$api.msg(r.msg);
  138. _self.isSubmit = false;
  139. }
  140. })
  141. .catch();
  142. },
  143. //立即购买生成订单
  144. buy(data) {
  145. let _self = this;
  146. _self
  147. .request({ url: '/Order/buy', data })
  148. .then(r => {
  149. console.log(JSON.stringify(r)+'777')
  150. if (r.code == 200) {
  151. _self.order_num = r.data;
  152. _self.submit();
  153. } else {
  154. _self.$api.msg(r.msg);
  155. _self.isSubmit = false;
  156. }
  157. })
  158. .catch(() => {
  159. _self.isSubmit = false;
  160. });
  161. },
  162. // 购物车购买生成订单
  163. carBuy(data) {
  164. let _self = this;
  165. _self
  166. .request({ url: '/Order/shopCarBuy', data })
  167. .then(r => {
  168. if (r.code == 200) {
  169. _self.order_num = r.data;
  170. _self.submit();
  171. } else {
  172. _self.$api.msg(r.msg);
  173. _self.isSubmit = false;
  174. }
  175. console.log(r);
  176. })
  177. .catch(() => {
  178. _self.isSubmit = false;
  179. });
  180. },
  181. // 初始化
  182. init() {
  183. let _self = this;
  184. let list = _self.list.slice(0).map(e => {
  185. if (e.type == _self.type) {
  186. e.check = true;
  187. }
  188. return e;
  189. });
  190. },
  191. // 打开弹窗
  192. open() {
  193. this.$refs.popup.open();
  194. },
  195. // 关闭弹窗
  196. close() {
  197. this.$refs.popup.close();
  198. uni.setStorageSync('pay_bottoms', 'true');
  199. },
  200. // 选择支付方式
  201. radioClick(e, index) {
  202. let _self = this;
  203. _self.list.map(e => {
  204. e.check = false;
  205. return e;
  206. });
  207. e.check = true;
  208. _self.type = index;
  209. },
  210. // 立即支付按钮
  211. submit() {
  212. let _self = this;
  213. let parent = _self.utils.getParent.call(this);
  214. let data = {
  215. order_num: this.order_num,
  216. remark: parent.$refs.info.desc,
  217. add_id: parent.addressData.id
  218. };
  219. if (!data.add_id) {
  220. _self.$api.msg('请选择收货地址');
  221. return;
  222. }
  223. _self.isSubmit = true;
  224. // 微信
  225. if (_self.type == 0) {
  226. _self.wxPay(data);
  227. }
  228. if (_self.type == 1) {
  229. _self.smallChange(data);
  230. }
  231. // 支付宝
  232. if (_self.type == 2) {
  233. _self.zfbPay(data);
  234. }
  235. //
  236. if (_self.type == 3) {
  237. _self.walletPay(data);
  238. }
  239. //积分兑换
  240. if (_self.type == 4) {
  241. _self.integralBuy(data);
  242. }
  243. },
  244. // 判断生成订单类型
  245. payTypeload() {
  246. let _self = this;
  247. //如果payCall 为true时 则表示需要调用外部支付方法,跳过执行下面的程序,开启回调
  248. if (_self.payCall) {
  249. // 触发支付方法
  250. _self.$emit('submit', _self.type);
  251. return;
  252. }
  253. //如果order_num 存在的时候 直接打开选择支付的按钮
  254. if(_self.order_num){
  255. _self.submit();
  256. }else{
  257. let parent = _self.utils.getParent.call(this);
  258. //不存在的时候 调用接口 生成订单
  259. if (_self.isSubmit) {
  260. return;
  261. }
  262. if (!parent.addressData.id) {
  263. _self.$api.msg('请选择收货地址');
  264. return;
  265. }
  266. _self.isSubmit = true;
  267. let d = parent.$refs.product.productList;
  268. //如果是拼团购买 前往拼团购买逻辑
  269. if (_self.msType * 1 == 3) {
  270. _self.assembleBuy(parent, d);
  271. return;
  272. }
  273. //秒杀 只执行立即购买下单逻辑 不从购物车过来
  274. if (parent.gid) {
  275. let data = {
  276. gid: parent.gid,
  277. num: d[0].goods_info[0].num,
  278. add_id: parent.addressData.id,
  279. ms_type: _self.msType,
  280. type: _self.msType
  281. };
  282. if (parent.couponId) {
  283. data.card_id = parent.couponId;
  284. }
  285. _self.buy(data);
  286. }
  287. //打印一下 数据
  288. if (parent.cid) {
  289. let data = {
  290. cid: parent.cid,
  291. add_id: parent.addressData.id
  292. };
  293. // if(!data.add_id){
  294. // _self.$api.msg("请选择收货地址")
  295. // return
  296. // }
  297. if (parent.couponId) {
  298. data.card_id = parent.couponId;
  299. }
  300. _self.carBuy(data);
  301. }
  302. }
  303. },
  304. // 积分购买
  305. integralBuy(data) {
  306. let _self = this;
  307. _self
  308. .request({ url: '/Order/integralPay', data })
  309. .then(r => {
  310. console.log(r);
  311. _self.$api.msg(r.msg, 2000, false, 'none', function() {
  312. if (r.code == 200) {
  313. uni.redirectTo({ url: '/pages/order/order?state=2' });
  314. }
  315. _self.isSubmit = false;
  316. });
  317. })
  318. .catch();
  319. },
  320. //支付宝支付
  321. zfbPay(data) {
  322. let _self = this;
  323. data.pay_type = 'ali';
  324. _self
  325. .request({ url: '/Order/alipay', data })
  326. .then(r => {
  327. //let rs=eval('(' + r + ')')
  328. if (r.code == 200) {
  329. uni.requestPayment({
  330. provider: 'alipay',
  331. orderInfo: r.data,
  332. success(r) {
  333. uni.redirectTo({ url: '/pages/order/order?state=2' });
  334. _self.isSubmit = false;
  335. },
  336. fail() {
  337. uni.redirectTo({ url: '/pages/order/order?state=1' });
  338. _self.isSubmit = false;
  339. }
  340. });
  341. } else {
  342. _self.isSubmit = false;
  343. }
  344. })
  345. .catch(() => {
  346. _self.isSubmit = false;
  347. });
  348. },
  349. // 获取传值并保存为对象
  350. paramToObject(url) {
  351. let o = {};
  352. url.split('&').forEach(e => {
  353. e = e.split('=');
  354. o[e[0]] = e[1];
  355. });
  356. return o;
  357. },
  358. // 将对象解析为传值
  359. objectToParam(o) {
  360. let param = '';
  361. for (let i in o) {
  362. param += i + `=` + o[i] + '&';
  363. }
  364. param = param.substr(0, param.length - 1);
  365. return param;
  366. },
  367. // 微信支付
  368. wxPay(data) {
  369. let _self = this;
  370. // data.pay_type
  371. _self
  372. .request({ url: '/order/wxpay', data })
  373. .then(r => {
  374. let rs=''
  375. console.log(uni.getSystemInfoSync().platform)
  376. if(uni.getSystemInfoSync().platform == 'ios'){
  377. rs=r
  378. }else{
  379. rs=eval('(' + r + ')')
  380. }
  381. if (rs.code == 200) {
  382. uni.requestPayment({
  383. provider: 'wxpay',
  384. orderInfo: rs.data,
  385. success(r) {
  386. console.log(r)
  387. uni.redirectTo({ url: '/pages/order/order?state=2' });
  388. _self.isSubmit = false;
  389. },
  390. fail(r) {
  391. console.log(r)
  392. uni.redirectTo({ url: '/pages/order/order?state=1' });
  393. _self.isSubmit = false;
  394. }
  395. });
  396. } else {
  397. _self.$api.msg(r.msg);
  398. _self.isSubmit = false;
  399. }
  400. })
  401. .catch(() => {
  402. _self.isSubmit = false;
  403. });
  404. },
  405. // 零钱支付
  406. smallChange(data) {
  407. let _self = this;
  408. _self
  409. .request({
  410. url: '/Order/changePay',
  411. data
  412. })
  413. .then(r => {
  414. if (r.code == 200) {
  415. _self.close();
  416. _self.$api.msg(r.msg);
  417. //支付成功跳转到待发货页面
  418. uni.redirectTo({
  419. url: '/pages/order/order?state=2'
  420. });
  421. _self.isSubmit = false;
  422. } else {
  423. _self.isSubmit = false;
  424. }
  425. })
  426. .catch(() => {
  427. _self.isSubmit = false;
  428. });
  429. },
  430. // 钱包支付
  431. walletPay(data) {
  432. let _self = this;
  433. _self
  434. .request({ url: '/Order/walletPay', data })
  435. .then(r => {
  436. if (r.code == 200) {
  437. _self.close();
  438. _self.$api.msg(r.msg);
  439. //支付成功跳转到待发货页面
  440. uni.redirectTo({
  441. url: '/pages/order/order?state=2'
  442. });
  443. _self.isSubmit = false;
  444. } else {
  445. _self.isSubmit = false;
  446. }
  447. })
  448. .catch(() => {
  449. _self.isSubmit = false;
  450. });
  451. }
  452. },
  453. components: {
  454. uniPopup
  455. }
  456. };
  457. </script>
  458. <style lang="scss">
  459. .create-order-pay {
  460. z-index: 9999;
  461. .page {
  462. border-radius: 10rpx 10rpx 0 0;
  463. }
  464. .title {
  465. border-bottom: 2rpx solid #eee;
  466. }
  467. .pay-type {
  468. border-bottom: 20rpx solid #f8f6f6;
  469. }
  470. .button {
  471. padding: 20rpx 60rpx;
  472. line-height: 40rpx;
  473. font-size: 30rpx;
  474. background-color: #dc4d46;
  475. }
  476. .money-color {
  477. color: $base-color;
  478. }
  479. .radio {
  480. width: 40rpx;
  481. height: 40rpx;
  482. border-radius: 50%;
  483. background-size: 40rpx 40rpx;
  484. background-position: center;
  485. background-image: url('http://lxscimg.liuniu946.com/2019-11-29_5de0badfc57f7.png');
  486. }
  487. .radio-active {
  488. background-image: url('http://lxscimg.liuniu946.com/2019-11-29_5de0bae3bcbd4.png');
  489. }
  490. }
  491. </style>