pay.vue 8.6 KB

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