金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
8.6 KiB

  1. <template>
  2. <view>
  3. <lf-nav title="支付收银台" :showIcon="true" bgColor="#fff"></lf-nav>
  4. <view class="content">
  5. <view class="card lf-flex-column lf-row-center">
  6. <view class="lf-font-28 lf-color-222">需要支付</view>
  7. <view class="lf-m-t-10 lf-m-b-10">
  8. <text class="symbol"></text>
  9. <text class="price">{{ amount }}</text>
  10. </view>
  11. <view class="tips">
  12. <view>剩余支付时间</view>
  13. <view>
  14. <countdown-timer :time="time" :autoStart="true" @finish="dateFinish">
  15. <template v-slot="{minute, second}">
  16. <!-- <view>{{minute}}:{{second}}</view> -->
  17. <view class="lf-flex">
  18. <view>{{ minute >= 10 ? minute : "0" + minute }}</view>
  19. <view>:</view>
  20. <view>{{ second >= 10 ? second : "0" + second }}</view>
  21. </view>
  22. </template>
  23. </countdown-timer>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="card lf-row-between" v-for="(item, index) in pay_list" :key="index">
  28. <view>
  29. <text class="lf-iconfont" :class="item.icon"></text>
  30. <text class="lf-m-l-10 lf-font-28 lf-color-222">{{ item.name }}</text>
  31. </view>
  32. <radio-group @change="checkChange($event, index)">
  33. <radio :checked="item.checked"></radio>
  34. </radio-group>
  35. </view>
  36. </view>
  37. <view class="fixed-btn" hover-class="lf-opacity" @click="confirm">立即支付</view>
  38. <view class="fixed-agreement">购买须知</view>
  39. </view>
  40. </template>
  41. <script>
  42. import countdownTimer from '@/components/countdown-timer/countdown-timer';
  43. export default {
  44. components: {
  45. countdownTimer
  46. },
  47. data(){
  48. return {
  49. pay_list: [{
  50. name: '余额支付',
  51. icon: 'icon--',
  52. type: 'balance',
  53. checked: false
  54. },{
  55. name: '微信支付',
  56. icon: 'icon--1',
  57. type: 'wx_lite',
  58. checked: true
  59. }],
  60. time: new Date('2021/09/8 14:15:00').getTime() - new Date('2021/09/8 14:10:00').getTime(),
  61. is_date_finish: false,
  62. amount: '',
  63. order_no: '',
  64. token: '',
  65. balance_sum: 0
  66. }
  67. },
  68. onLoad(options){
  69. this.token = this.$cookieStorage.get('user_token');
  70. this.amount = options.amount;
  71. this.order_no = options.order_no;
  72. this.getBalanceSum();
  73. },
  74. methods: {
  75. // 获取余额
  76. getBalanceSum(){
  77. this.$http.get({
  78. api: 'api/users/balance/sum',
  79. header: {
  80. Authorization: this.token
  81. }
  82. }).then(res => {
  83. this.balance_sum = res.data.data.sum;
  84. })
  85. },
  86. // 改变支付方式
  87. checkChange(event, current){
  88. let list = this.pay_list.map((item, index) => {
  89. if(index == current){
  90. item.checked = true;
  91. }else{
  92. item.checked = false;
  93. }
  94. return item;
  95. })
  96. this.pay_list = list;
  97. },
  98. // 倒计时结束
  99. dateFinish(){
  100. console.log("倒计时结束");
  101. this.is_date_finish = true;
  102. this.$msg('订单超时', {icon: 'error'}).then(() => {
  103. this.$toBack();
  104. })
  105. },
  106. // 支付
  107. confirm(){
  108. if(this.is_date_finish) return this.$msg('订单超时未支付');
  109. this.getOpenid().then(res => {
  110. let channel = '';
  111. this.pay_list.map(item => {
  112. if(item.checked){
  113. channel = item.type;
  114. }
  115. })
  116. var data = {
  117. channel: channel == 'balance' ? 'wx_lite' : channel,
  118. openid: res,
  119. order_no: this.order_no,
  120. balance: channel == 'balance' ? Number(this.amount) : 0
  121. };
  122. this.$http.post({
  123. api: `api/shopping/order/charge`,
  124. data: data,
  125. header: {
  126. Authorization: this.token
  127. }
  128. }).then(res => {
  129. res = res.data;
  130. if (res.status) {
  131. // this.formId = e.detail.formId || '';
  132. if (res.data.name == 'balance') {
  133. this.balanceCharge();
  134. } else {
  135. this.newcharge(true, res.data.charge);
  136. }
  137. } else {
  138. this.newcharge(false, res.message);
  139. }
  140. })
  141. }).catch(() => {
  142. this.setData({
  143. loading: false
  144. });
  145. wx.showModal({
  146. content: '支付失败',
  147. showCancel: false
  148. });
  149. });
  150. },
  151. // 获取openid
  152. getOpenid() {
  153. return new Promise((resolve, reject) => {
  154. wx.login({
  155. success: res => {
  156. this.$http.get({
  157. api: `api/oauth/miniprogram/openid`,
  158. data: {
  159. code: res.code
  160. }
  161. }).then(res => {
  162. res = res.data;
  163. resolve(res.data.openid);
  164. }).catch(() => {
  165. reject('获取openid失败');
  166. });
  167. },
  168. fail: () => {
  169. wx.showModal({
  170. content: "接口请求失败",
  171. showCancel: false
  172. });
  173. }
  174. });
  175. });
  176. },
  177. // 新版支付
  178. newcharge(success, charge) {
  179. if (success) {
  180. var that = this;
  181. if (charge.pay_scene == 'test') {
  182. wx.showModal({
  183. content: '不支持模拟支付',
  184. showCancel: false
  185. });
  186. this.setData({
  187. loading: false
  188. });
  189. } else {
  190. wx.requestPayment({
  191. "timeStamp": charge.credential.wechat.timeStamp,
  192. "nonceStr": charge.credential.wechat.nonceStr,
  193. "package": charge.credential.wechat.package,
  194. "signType": charge.credential.wechat.signType,
  195. "paySign": charge.credential.wechat.paySign,
  196. success: res => {
  197. if (res.errMsg == 'requestPayment:ok') {
  198. this.setData({
  199. loading: false
  200. });
  201. // wx.redirectTo({
  202. // url: `/pages/store/success/success?order_no=${that.order_no}&amount=${that.amount}&channel=${that.channel}&formId=${this.formId}` //&charge_id=${charge.charge_id} charge_id 支付测试使用
  203. // });
  204. uni.redirectTo({
  205. url: '/pages/aboutpay/paystate?payState=1&amount='+ that.amount
  206. })
  207. } else {
  208. wx.showModal({
  209. content: '调用支付失败!',
  210. showCancel: false
  211. });
  212. }
  213. },
  214. fail: err => {
  215. this.setData({
  216. loading: false
  217. });
  218. if (err.errMsg == 'requestPayment:fail cancel') {
  219. // wx.redirectTo({
  220. // url: `/pages/order/detail/detail?no=${that.order_no}`
  221. // });
  222. uni.redirectTo({
  223. url: '/pages/order/index/onlineorder'
  224. })
  225. } else {
  226. wx.showModal({
  227. content: '调用支付失败!',
  228. showCancel: false
  229. });
  230. }
  231. }
  232. });
  233. }
  234. } else {
  235. this.setData({
  236. loading: false
  237. });
  238. wx.showModal({
  239. content: charge || '请求支付失败,请重试!',
  240. showCancel: false
  241. });
  242. }
  243. },
  244. // 纯余额支付
  245. balanceCharge() {
  246. this.setData({
  247. loading: false
  248. });
  249. // wx.redirectTo({
  250. // url: `/pages/store/success/success?order_no=${this.order_no}&amount=${this.amount}&channel=${this.channel}&formId=${this.formId}`
  251. // });
  252. uni.redirectTo({
  253. url: '/pages/aboutpay/paystate?payState=1&amount='+ this.amount
  254. })
  255. },
  256. setData: function (obj) {
  257. let that = this;
  258. let keys = [];
  259. let val, data;
  260. Object.keys(obj).forEach(function (key) {
  261. keys = key.split('.');
  262. val = obj[key];
  263. data = that.$data;
  264. keys.forEach(function (key2, index) {
  265. if (index + 1 == keys.length) {
  266. that.$set(data, key2, val);
  267. } else {
  268. if (!data[key2]) {
  269. that.$set(data, key2, {});
  270. }
  271. }
  272. data = data[key2];
  273. });
  274. });
  275. }
  276. }
  277. }
  278. </script>
  279. <style>
  280. page{
  281. background-color: #F8F8F8;
  282. }
  283. </style>
  284. <style lang="scss" scoped="scoped">
  285. .content{
  286. padding: 30rpx 32rpx;
  287. width: 750rpx;
  288. height: max-content;
  289. box-sizing: border-box;
  290. .card{
  291. width: 100%;
  292. height: max-content;
  293. padding: 30rpx 40rpx;
  294. background-color: #FFFFFF;
  295. border-radius: 20rpx;
  296. &:nth-child(n+2){
  297. margin-top: 30rpx;
  298. }
  299. .symbol{
  300. color: #15716E;
  301. font-size: 32rpx;
  302. }
  303. .price{
  304. font-size: 72rpx;
  305. color: #15716E;
  306. font-weight: bold;
  307. }
  308. .tips{
  309. font-size: 24rpx;
  310. color: #FF9D9D;
  311. display: flex;
  312. }
  313. }
  314. }
  315. .fixed-agreement {
  316. position: fixed;
  317. bottom: 5vh;
  318. font-size: 28rpx;
  319. color: #15716E;
  320. left: calc(50% - 84rpx);
  321. width: 168rpx;
  322. }
  323. .fixed-btn{
  324. position: fixed;
  325. bottom: 10vh;
  326. left: calc(50% - 275rpx);
  327. width: 550rpx;
  328. height: 100rpx;
  329. background-color: #15716E;
  330. color: #FFFFFF;
  331. text-align: center;
  332. line-height: 100rpx;
  333. font-size: 32rpx;
  334. border-radius: 50rpx;
  335. }
  336. </style>