pay.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <template>
  2. <view class="app">
  3. <view class="price-box">
  4. <text>支付金额</text>
  5. <text class="price">{{ money }}</text>
  6. </view>
  7. <view class="pay-type-list">
  8. <!-- <view class="type-item b-b" @click="changePayType(1)">
  9. <text class="icon iconfont iconweixin"></text>
  10. <view class="con">
  11. <text class="tit">微信支付</text>
  12. <text>推荐使用微信支付</text>
  13. </view>
  14. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 1"></radio></label>
  15. </view> -->
  16. <!-- #ifdef APP-PLUS -->
  17. <view class="type-item b-b" @click="changePayType(2)">
  18. <text class="icon iconfont iconzhifubao"></text>
  19. <view class="con"><text class="tit">支付宝支付</text></view>
  20. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 2"></radio></label>
  21. </view>
  22. <!-- #endif -->
  23. <view class="type-item" @click="changePayType(3)">
  24. <text class="icon iconfont iconyue"></text>
  25. <view class="con">
  26. <text class="tit">佣金支付</text>
  27. <text>可用佣金 ¥{{ now_money }}</text>
  28. </view>
  29. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 3"></radio></label>
  30. </view>
  31. <view class="type-item" @click="changePayType(4)">
  32. <text class="icon iconfont iconyue iconyuee"></text>
  33. <view class="con">
  34. <text class="tit">余额支付</text>
  35. <text>可用余额 ¥{{ yuemoney || '0.0' }}</text>
  36. </view>
  37. <label class="radio"><radio value="" color="#5dbc7c" :checked="payType == 4"></radio></label>
  38. </view>
  39. </view>
  40. <text class="mix-btn" :class="{ clickbg: payLoding }" @click="!payLoding ? confirm() : ''">确认支付</text>
  41. </view>
  42. </template>
  43. <script>
  44. import { balance } from '@/api/wallet.js';
  45. import { createOrderkey,computedOrderkey,orderPay } from '@/api/order.js';
  46. import { mapState } from 'vuex';
  47. // #ifdef H5
  48. import weichatObj from '@/plugin/jweixin-module/index.js';
  49. // #endif
  50. export default {
  51. data() {
  52. return {
  53. payType: 3, //支付类型
  54. // #ifdef H5
  55. payName: 'yue',
  56. // #endif
  57. // #ifdef MP-WEIXIN
  58. payName: 'yue',
  59. // #endif
  60. orderInfo: {},
  61. money: 0.0, //订单金额
  62. now_money: 0.0, //佣金
  63. orderKey: '',
  64. orderId: '', //保存订单id
  65. payLoding: false, //判断是否支付中
  66. type: '', //判断是否从订单中进入
  67. // #ifdef H5
  68. froms: '', //保存h5中数据来源对象
  69. // #endif
  70. pinkid: '' //保存拼团商品id
  71. };
  72. },
  73. computed: {
  74. // #ifdef H5
  75. ...mapState(['weichatObj']),
  76. // #endif
  77. ...mapState('user',['userInfo']),
  78. yuemoney() {
  79. return this.userInfo.money*1
  80. }
  81. },
  82. onLoad(options) {
  83. if (options.type == 1) {
  84. this.type = 1;
  85. this.orderId = options.ordid;
  86. this.money = options.money;
  87. } else {
  88. this.orderKey = options.key;
  89. let prepage = this.$api.prePage();
  90. computedOrderkey({
  91. orderkey: this.orderKey,
  92. couponId: prepage.couponChecked.id, //优惠券编号
  93. addressId: prepage.addressData.id, //地址编号
  94. useIntegral: prepage.checkedPoints ? 1 : 0
  95. }).then(({ data }) => {
  96. // 获取支付金额
  97. this.money = data.result.pay_price;
  98. });
  99. }
  100. // 保存pinkid
  101. if (options.pinkid) {
  102. this.pinkid = options.pinkid;
  103. }
  104. // 载入余额
  105. balance({}).then(({ data }) => {
  106. // 获取余额
  107. this.now_money = data.now_money;
  108. });
  109. },
  110. methods: {
  111. //选择支付方式
  112. changePayType(type) {
  113. this.payType = type;
  114. if (this.payType == 1) {
  115. this.payName = 'weixin';
  116. }
  117. if (this.payType == 2) {
  118. this.payName = 'ali';
  119. }
  120. if (this.payType == 3) {
  121. this.payName = 'yue';
  122. }
  123. if (this.payType == 4) {
  124. this.payName = 'money';
  125. }
  126. },
  127. // 支付金额
  128. orderMoneyPay() {
  129. let obj = this;
  130. orderPay({
  131. uni: obj.orderId,
  132. // #ifdef H5
  133. from: obj.froms ? 'weixin' : 'H5', //来源
  134. // #endif
  135. // #ifdef MP-WEIXIN
  136. from: 'routine', //来源
  137. // #endif
  138. // #ifdef APP-PLUS
  139. from: 'app', //来源
  140. // #endif
  141. paytype: obj.payName //支付类型 weixin-微信 yue-余额
  142. })
  143. .then(e => {
  144. // 判断是否为余额支付
  145. if (obj.payName == 'yue' && e.data.status == 'SUCCESS') {
  146. if (e.status == 200) {
  147. obj.paySuccessTo();
  148. } else {
  149. obj.$api.msg(msg);
  150. }
  151. }
  152. if (obj.payName == 'money' && e.data.status == 'SUCCESS') {
  153. if (e.status == 200) {
  154. obj.paySuccessTo();
  155. } else {
  156. obj.$api.msg(msg);
  157. }
  158. }
  159. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  160. let da = e.data.result.jsConfig;
  161. let data = {
  162. // #ifdef H5
  163. timestamp: da.timestamp,
  164. // #endif
  165. // #ifdef MP
  166. timeStamp: da.timestamp,
  167. // #endif
  168. nonceStr: da.nonceStr,
  169. package: da.package,
  170. signType: da.signType,
  171. paySign: da.paySign,
  172. success: function(res) {
  173. obj.paySuccessTo();
  174. },
  175. fail: () => {
  176. uni.navigateTo({
  177. url: '/pages/order/order?state=0'
  178. });
  179. }
  180. };
  181. // #ifdef H5
  182. if(obj.payName == 'weixin'){
  183. weichatObj.chooseWXPay(data);
  184. }
  185. // #endif
  186. // #ifdef MP-WEIXIN
  187. if(obj.payName == 'weixin' || obj.payName == 'routine' ){
  188. console.log('开始支付')
  189. wx.requestPayment(data);
  190. }
  191. // #endif
  192. }
  193. uni.hideLoading();
  194. obj.payLoding = false;
  195. })
  196. .catch(e => {
  197. // 支付完成
  198. uni.hideLoading();
  199. obj.payLoding = false;
  200. console.log(e);
  201. });
  202. },
  203. // 支付成功跳转
  204. paySuccessTo() {
  205. let obj = this
  206. uni.hideLoading();
  207. uni.redirectTo({
  208. url: '/pages/money/paySuccess?orderid=' + this.orderId
  209. });
  210. },
  211. //确认支付
  212. confirm: async function() {
  213. let obj = this;
  214. // 判断是否余额不足
  215. if (obj.payName == 'yue' && +obj.now_money*1 < obj.money*1) {
  216. uni.showModal({
  217. title: '提示',
  218. content: '账户佣金不足!',
  219. showCancel: false,
  220. success: res => {},
  221. fail: () => {},
  222. complete: () => {}
  223. });
  224. return;
  225. }
  226. if (obj.payName == 'money' && +obj.yuemoney*1 < obj.money*1) {
  227. uni.showModal({
  228. title: '提示',
  229. content: '账户余额不足!',
  230. showCancel: false,
  231. success: res => {},
  232. fail: () => {},
  233. complete: () => {}
  234. });
  235. return;
  236. }
  237. uni.showLoading({
  238. title: '支付中',
  239. mask: true
  240. })
  241. // 支付中
  242. obj.payLoding = true;
  243. // #ifdef H5
  244. // 获取当前是否为微信浏览器
  245. obj.froms = uni.getStorageSync('weichatBrowser') || '';
  246. // #endif
  247. // 判断是否为未支付订单中跳转进入
  248. if (obj.type != 1) {
  249. // 初次生成订单
  250. obj.firstCreateOrder();
  251. } else {
  252. // 已经生成订单未支付
  253. obj.orderMoneyPay();
  254. }
  255. },
  256. // 初次订单创建
  257. firstCreateOrder() {
  258. let obj = this;
  259. // 获取下单页面数据
  260. let prepage = obj.$api.prePage();
  261. let data = {
  262. real_name: prepage.addressData.real_name, //联系人名称
  263. phone: prepage.addressData.phone, //联系人号码
  264. couponId: prepage.couponChecked.id, //优惠券编号
  265. addressId: prepage.addressData.id, //支付地址id
  266. useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
  267. payType: obj.payName, //支付类型 weixin-微信 yue-余额
  268. mark: prepage.desc, //备注
  269. // #ifdef H5
  270. from: obj.froms ? 'weixin' : 'H5', //来源
  271. // #endif
  272. // #ifdef MP-WEIXIN
  273. from: 'routine', //来源
  274. // #endif
  275. // #ifdef APP-PLUS
  276. from: 'app', //来源
  277. // #endif
  278. shipping_type: prepage.tabCurrentIndex+1 //提货方式 1 快递 2自提
  279. };
  280. if(data.shipping_type == 2) {
  281. data.store_id = prepage.checkedPoint.id
  282. }
  283. // 判断是否拼团商品
  284. if (obj.pinkid) {
  285. data.pinkId = obj.pinkid;
  286. }
  287. // 生成订单
  288. createOrderkey(data, obj.orderKey)
  289. .then(({ data, status, msg }) => {
  290. console.log('生成订单')
  291. // 判断是否支付失败
  292. if (data.status == 'ORDER_EXIST') {
  293. uni.showModal({
  294. title: '提示',
  295. content: msg,
  296. showCancel: false
  297. });
  298. uni.hideLoading();
  299. obj.payLoding = false;
  300. return;
  301. }
  302. // 保存订单号
  303. obj.orderId = data.result.orderId;
  304. // 判断是否为余额支付
  305. if (obj.payName == 'yue') {
  306. if (status == 200 && data.status == 'SUCCESS') {
  307. obj.paySuccessTo();
  308. } else {
  309. obj.$api.msg(msg);
  310. }
  311. } else {
  312. // 立即支付
  313. obj.orderMoneyPay();
  314. }
  315. })
  316. .catch(e => {
  317. uni.hideLoading();
  318. obj.payLoding = false;
  319. console.log(e);
  320. });
  321. }
  322. }
  323. };
  324. </script>
  325. <style lang="scss">
  326. .app {
  327. width: 100%;
  328. }
  329. .price-box {
  330. background-color: #fff;
  331. height: 265upx;
  332. display: flex;
  333. flex-direction: column;
  334. justify-content: center;
  335. align-items: center;
  336. font-size: 28upx;
  337. color: #909399;
  338. .price {
  339. font-size: 50upx;
  340. color: #303133;
  341. margin-top: 12upx;
  342. &:before {
  343. content: '¥';
  344. font-size: 40upx;
  345. }
  346. }
  347. }
  348. .pay-type-list {
  349. margin-top: 20upx;
  350. background-color: #fff;
  351. padding-left: 60upx;
  352. .type-item {
  353. height: 120upx;
  354. padding: 20upx 0;
  355. display: flex;
  356. justify-content: space-between;
  357. align-items: center;
  358. padding-right: 60upx;
  359. font-size: 30upx;
  360. position: relative;
  361. }
  362. .icon {
  363. width: 100upx;
  364. font-size: 52upx;
  365. }
  366. .iconyue {
  367. color: #fe8e2e;
  368. }
  369. .iconyuee {
  370. color: #36cb59;
  371. }
  372. .iconweixin {
  373. color: #36cb59;
  374. }
  375. .iconzhifubao {
  376. color: #01aaef;
  377. }
  378. .tit {
  379. font-size: $font-lg;
  380. color: $font-color-dark;
  381. margin-bottom: 4upx;
  382. }
  383. .con {
  384. flex: 1;
  385. display: flex;
  386. flex-direction: column;
  387. font-size: $font-sm;
  388. color: $font-color-light;
  389. }
  390. }
  391. .mix-btn {
  392. display: flex;
  393. align-items: center;
  394. justify-content: center;
  395. width: 630upx;
  396. height: 80upx;
  397. margin: 80upx auto 30upx;
  398. font-size: $font-lg;
  399. color: #fff;
  400. background-color: $base-color;
  401. border-radius: 10upx;
  402. /* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
  403. }
  404. .clickbg {
  405. background-color: $color-gray !important;
  406. }
  407. </style>