pay.vue 8.8 KB

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