pay.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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_gold: obj.is_gold,
  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: 1 //提货方式 1 快递 2自提
  248. };
  249. // 判断是否拼团商品
  250. if (obj.pinkid) {
  251. data.pinkId = obj.pinkid;
  252. }
  253. // 生成订单
  254. createOrderkey(data, obj.orderKey)
  255. .then(({ data, status, msg }) => {
  256. // 判断是否支付失败
  257. if (data.status == 'ORDER_EXIST') {
  258. uni.showModal({
  259. title: '提示',
  260. content: msg,
  261. showCancel: false
  262. });
  263. uni.hideLoading();
  264. obj.payLoding = false;
  265. return;
  266. }
  267. // 保存订单号
  268. obj.orderId = data.result.orderId;
  269. // 判断是否为余额支付
  270. if (obj.payName == 'yue') {
  271. if (status == 200 && data.status == 'SUCCESS') {
  272. obj.paySuccessTo();
  273. } else {
  274. obj.$api.msg(msg);
  275. }
  276. } else {
  277. // 立即支付
  278. obj.orderMoneyPay();
  279. }
  280. })
  281. .catch(e => {
  282. uni.hideLoading();
  283. obj.payLoding = false;
  284. console.log(e);
  285. });
  286. }
  287. }
  288. };
  289. </script>
  290. <style lang="scss">
  291. .app {
  292. width: 100%;
  293. }
  294. .price-box {
  295. background-color: #fff;
  296. height: 265upx;
  297. display: flex;
  298. flex-direction: column;
  299. justify-content: center;
  300. align-items: center;
  301. font-size: 28upx;
  302. color: #909399;
  303. .price {
  304. font-size: 50upx;
  305. color: #303133;
  306. margin-top: 12upx;
  307. &:before {
  308. content: '¥';
  309. font-size: 40upx;
  310. }
  311. }
  312. }
  313. .pay-type-list {
  314. margin-top: 20upx;
  315. background-color: #fff;
  316. padding-left: 60upx;
  317. .type-item {
  318. height: 120upx;
  319. padding: 20upx 0;
  320. display: flex;
  321. justify-content: space-between;
  322. align-items: center;
  323. padding-right: 60upx;
  324. font-size: 30upx;
  325. position: relative;
  326. }
  327. .icon {
  328. width: 100upx;
  329. font-size: 52upx;
  330. }
  331. .iconyue {
  332. color: #fe8e2e;
  333. }
  334. .iconweixin {
  335. color: #36cb59;
  336. }
  337. .iconzhifubao {
  338. color: #01aaef;
  339. }
  340. .tit {
  341. font-size: $font-lg;
  342. color: $font-color-dark;
  343. margin-bottom: 4upx;
  344. }
  345. .con {
  346. flex: 1;
  347. display: flex;
  348. flex-direction: column;
  349. font-size: $font-sm;
  350. color: $font-color-light;
  351. }
  352. }
  353. .mix-btn {
  354. display: flex;
  355. align-items: center;
  356. justify-content: center;
  357. width: 630upx;
  358. height: 80upx;
  359. margin: 80upx auto 30upx;
  360. font-size: $font-lg;
  361. color: #fff;
  362. background-color: $base-color;
  363. border-radius: 10upx;
  364. /* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
  365. }
  366. .clickbg {
  367. background-color: $color-gray !important;
  368. }
  369. </style>