pay.vue 8.6 KB

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