pay.vue 9.5 KB

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