pay.vue 8.7 KB

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