pay.vue 9.9 KB

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