pay.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. // #ifdef H5
  40. import weichatObj from '@/plugin/jweixin-module/index.js';
  41. // #endif
  42. export default {
  43. data() {
  44. return {
  45. payType: 1, //支付类型
  46. // #ifdef H5
  47. payName: 'weixin',
  48. // #endif
  49. // #ifdef MP-WEIXIN
  50. payName: 'weixin',
  51. // #endif
  52. orderInfo: {},
  53. money: 0.0, //订单金额
  54. now_money: 0.0, //余额
  55. orderKey: '',
  56. orderId: '', //保存订单id
  57. payLoding: false, //判断是否支付中
  58. type: '', //判断是否从订单中进入
  59. // #ifdef H5
  60. froms: '', //保存h5中数据来源对象
  61. // #endif
  62. pinkid: '' //保存拼团商品id
  63. };
  64. },
  65. computed: {
  66. // #ifdef H5
  67. ...mapState(['weichatObj']),
  68. // #endif
  69. ...mapState('user',['userInfo'])
  70. },
  71. onLoad(options) {
  72. if (options.type == 1) {
  73. this.type = 1;
  74. this.orderId = options.ordid;
  75. this.money = options.money;
  76. } else {
  77. this.orderKey = options.key;
  78. let prepage = this.$api.prePage();
  79. computedOrderkey({
  80. orderkey: this.orderKey,
  81. couponId: prepage.couponChecked.id, //优惠券编号
  82. addressId: prepage.addressData.id, //地址编号
  83. useIntegral: prepage.checkedPoints ? 1 : 0
  84. }).then(({ data }) => {
  85. // 获取支付金额
  86. this.money = data.result.pay_price;
  87. });
  88. }
  89. // 保存pinkid
  90. if (options.pinkid) {
  91. this.pinkid = options.pinkid;
  92. }
  93. // 载入余额
  94. balance({}).then(({ data }) => {
  95. // 获取余额
  96. this.now_money = data.now_money;
  97. });
  98. },
  99. methods: {
  100. //选择支付方式
  101. changePayType(type) {
  102. this.payType = type;
  103. if (this.payType == 1) {
  104. this.payName = 'weixin';
  105. }
  106. if (this.payType == 2) {
  107. this.payName = 'ali';
  108. }
  109. if (this.payType == 3) {
  110. this.payName = 'yue';
  111. }
  112. },
  113. // 支付金额
  114. orderMoneyPay() {
  115. let obj = this;
  116. console.log('支付金额')
  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. console.log(obj.payName,'支付类型')
  132. console.log(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. weichatObj.chooseWXPay(data);
  166. }
  167. // #endif
  168. // #ifdef MP-WEIXIN
  169. if(obj.payName == 'weixin' || obj.payName == 'routine' ){
  170. console.log('开始支付')
  171. wx.requestPayment(data);
  172. }
  173. // #endif
  174. }
  175. uni.hideLoading();
  176. obj.payLoding = false;
  177. })
  178. .catch(e => {
  179. // 支付完成
  180. uni.hideLoading();
  181. obj.payLoding = false;
  182. console.log(e);
  183. });
  184. },
  185. // 支付成功跳转
  186. paySuccessTo() {
  187. let obj = this
  188. uni.hideLoading();
  189. uni.redirectTo({
  190. url: '/pages/money/paySuccess?orderid=' + this.orderId
  191. });
  192. },
  193. //确认支付
  194. confirm: async function() {
  195. let obj = this;
  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. uni.showLoading({
  209. title: '支付中',
  210. mask: true
  211. })
  212. // 支付中
  213. obj.payLoding = true;
  214. // #ifdef H5
  215. // 获取当前是否为微信浏览器
  216. obj.froms = uni.getStorageSync('weichatBrowser') || '';
  217. // #endif
  218. // 判断是否为未支付订单中跳转进入
  219. if (obj.type != 1) {
  220. // 初次生成订单
  221. obj.firstCreateOrder();
  222. } else {
  223. // 已经生成订单未支付
  224. obj.orderMoneyPay();
  225. }
  226. },
  227. // 初次订单创建
  228. firstCreateOrder() {
  229. let obj = this;
  230. // 获取下单页面数据
  231. let prepage = obj.$api.prePage();
  232. let data = {
  233. real_name: prepage.addressData.real_name, //联系人名称
  234. phone: prepage.addressData.phone, //联系人号码
  235. couponId: prepage.couponChecked.id, //优惠券编号
  236. addressId: prepage.addressData.id, //支付地址id
  237. useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
  238. payType: obj.payName, //支付类型 weixin-微信 yue-余额
  239. mark: prepage.desc, //备注
  240. // #ifdef H5
  241. from: obj.froms ? 'weixin' : 'H5', //来源
  242. // #endif
  243. // #ifdef MP-WEIXIN
  244. from: 'routine', //来源
  245. // #endif
  246. // #ifdef APP-PLUS
  247. from: 'app', //来源
  248. // #endif
  249. shipping_type: 1 //提货方式 1 快递 2自提
  250. };
  251. // 判断是否拼团商品
  252. if (obj.pinkid) {
  253. data.pinkId = obj.pinkid;
  254. }
  255. // 生成订单
  256. createOrderkey(data, obj.orderKey)
  257. .then(({ data, status, msg }) => {
  258. console.log('生成订单')
  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>