pay.vue 8.9 KB

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