pay.vue 8.6 KB

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