pay.vue 9.6 KB

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