pay.vue 8.4 KB

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