pay.vue 8.7 KB

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