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.

251 lines
6.4 KiB

4 years ago
  1. <template>
  2. <view class="app column">
  3. <view class="price-wrapper center column">
  4. <text>支付金额</text>
  5. <text class="price">{{ data.pay_price | price(2) }}</text>
  6. </view>
  7. <view class="cell row b-b" @click="checkType('wxpay')">
  8. <image class="icon" :src="'/static/icon/wxpay.png'"></image>
  9. <view class="column fill">
  10. <text class="tit">微信支付</text>
  11. </view>
  12. <text v-if="curType === 'wxpay'" class="mix-icon icon-xuanzhong"></text>
  13. </view>
  14. <!-- #ifndef MP-WEIXIN -->
  15. <view class="cell row b-b" @click="checkType('alipay')">
  16. <image class="icon" :src="'/static/icon/alipay.png'"></image>
  17. <view class="column fill">
  18. <text class="tit">支付宝</text>
  19. </view>
  20. <text v-if="curType === 'alipay'" class="mix-icon icon-xuanzhong"></text>
  21. </view>
  22. <!-- #endif -->
  23. <view class="cell row b-b" @click="checkType('balance')">
  24. <image class="icon" :src="'/static/icon/balance.png'"></image>
  25. <view class="column fill">
  26. <text class="tit">余额</text>
  27. <text class="tip">账户可用余额 {{ userInfo.money | price }}</text>
  28. </view>
  29. <text v-if="curType === 'balance'" class="mix-icon icon-xuanzhong"></text>
  30. </view>
  31. <mix-button ref="confirmBtn" text="确认支付" marginTop="80rpx" @onConfirm="confirm"></mix-button>
  32. <!-- 支付成功面板 -->
  33. <success-modal ref="successModal" :price="data.pay_price" @onConfirm="successCallback"></success-modal>
  34. <!-- 支付密码键盘 -->
  35. <pay-password-keyboard ref="pwdKeyboard" @onConfirm="balancePay"></pay-password-keyboard>
  36. <mix-loading v-if="isLoading" :mask="true"></mix-loading>
  37. <mix-modal ref="mixModal" text="您还没有设置支付密码" confirmText="立即设置" @onConfirm="navTo('/pages/auth/payPassword')"></mix-modal>
  38. </view>
  39. </template>
  40. <script>
  41. import successModal from './components/success-modal.vue'
  42. export default {
  43. components: {
  44. successModal
  45. },
  46. data() {
  47. return {
  48. curType: 'wxpay',
  49. data: {
  50. /* order_id: '5f1534f0bb1edd0001a93a2c',
  51. pay_price: 450,
  52. sourcePage: 'createOrder' */
  53. }
  54. }
  55. },
  56. computed: {
  57. userInfo(){
  58. return this.$store.state.userInfo;
  59. }
  60. },
  61. onLoad(options) {
  62. const data = JSON.parse(options.data);
  63. this.data = data;
  64. },
  65. methods: {
  66. confirm(){
  67. this.$util.throttle(()=>{
  68. if(this.curType === 'balance'){
  69. if(!this.userInfo.money || this.userInfo.money < this.data.pay_price){
  70. this.$util.msg('余额不足');
  71. this.$refs.confirmBtn.stop();
  72. return;
  73. }
  74. if(!this.userInfo.pay_password){
  75. this.$refs.mixModal.open();
  76. this.$refs.confirmBtn.stop();
  77. return;
  78. }
  79. //余额支付 打开支付密码键盘
  80. this.$refs.pwdKeyboard.open();
  81. this.$refs.confirmBtn.stop();
  82. }else{
  83. this.payByUnipay();
  84. }
  85. })
  86. },
  87. //余额支付 提交支付
  88. async balancePay(pwd){
  89. const res = await this.$request('order', 'payOrder', {
  90. order_id: this.data.order_id,
  91. pay_password: pwd,
  92. pay_type: this.curType
  93. }, {showLoading: true})
  94. if(res.status === 1){
  95. this.$store.dispatch('getUserInfo');
  96. this.$refs.pwdKeyboard.close();
  97. this.$refs.successModal.open();
  98. }else{
  99. this.$util.msg(res.msg);
  100. this.$refs.pwdKeyboard.pwd = '';
  101. }
  102. },
  103. //unipay
  104. async payByUnipay(){
  105. // #ifdef H5
  106. this.$util.msg('h5支付功能正在开发中,请使用小程序或app支付');
  107. this.$refs.confirmBtn.stop();
  108. return;
  109. // #endif
  110. const res = await this.$request('order', 'payOrder', {
  111. order_id: this.data.order_id,
  112. pay_type: this.curType,
  113. // #ifdef MP-WEIXIN
  114. code: await this.getMpCode('weixin'),
  115. // #endif
  116. }, {showLoading: true})
  117. if(res.status !== 1){
  118. this.$refs.confirmBtn.stop();
  119. console.log(res);
  120. this.$util.msg(res.msg || '获取支付信息失败');
  121. return;
  122. }
  123. const orderInfo = res.data.orderInfo;
  124. const {timeStamp, nonceStr, paySign} = orderInfo;
  125. const payParams = {
  126. provider: this.curType,
  127. orderInfo: res.data.orderInfo,
  128. timeStamp,
  129. nonceStr,
  130. package: orderInfo.package,
  131. signType: 'MD5',
  132. paySign,
  133. success: e=>{
  134. this.$refs.confirmBtn.death();
  135. this.$refs.successModal.open();
  136. },
  137. fail: err=>{
  138. this.$refs.confirmBtn.stop();
  139. if(err.errMsg.indexOf('取消') > -1 || err.errMsg.indexOf('cancel') >1 || err.errMsg.indexOf('-2') > -1){
  140. this.$util.msg('取消支付');
  141. }else{
  142. this.$util.msg('支付遇到错误,请稍候重试');
  143. console.log(err);
  144. }
  145. }
  146. };
  147. console.log(payParams);
  148. uni.requestPayment(payParams);
  149. },
  150. //统一支付成功回调
  151. successCallback(){
  152. switch(this.data.sourcePage){
  153. case 'createOrder':
  154. uni.redirectTo({
  155. url: '/pages/order/detail?id=' + this.data.order_id
  156. })
  157. break;
  158. case 'orderList':
  159. this.$util.prePage().refreshList(false);
  160. uni.redirectTo({
  161. url: '/pages/order/detail?id=' + this.data.order_id
  162. })
  163. break;
  164. case 'orderDetail':
  165. this.$util.prePage().loadData();
  166. uni.navigateBack();
  167. break;
  168. }
  169. },
  170. //选择支付方式
  171. checkType(type){
  172. this.curType = type;
  173. },
  174. //获取小程序code
  175. // #ifdef MP
  176. getMpCode(provider){
  177. return new Promise((resolve)=>{
  178. uni.login({
  179. provider,
  180. success(res) {
  181. resolve(res.code)
  182. }
  183. })
  184. })
  185. }
  186. // #endif
  187. }
  188. }
  189. </script>
  190. <style scoped lang="scss">
  191. .app{
  192. padding: 0 80rpx;
  193. align-items: center;
  194. /deep/{
  195. .mix-btn-content{
  196. width: 560rpx;
  197. }
  198. }
  199. }
  200. .price-wrapper {
  201. background-color: #fff;
  202. height: 260rpx;
  203. font-size: 28rpx;
  204. color: #909399;
  205. .price{
  206. font-size: 56rpx;
  207. color: #333;
  208. margin-top: 20rpx;
  209. font-weight: 600;
  210. &:before{
  211. content: '¥';
  212. font-size: 40rpx;
  213. }
  214. }
  215. }
  216. .cell{
  217. width: 100%;
  218. height: 124rpx;
  219. .icon{
  220. width: 44rpx;
  221. height: 44rpx;
  222. margin-right: 32rpx;
  223. }
  224. .tit{
  225. flex: 1;
  226. font-size: 30rpx;
  227. color: #333;
  228. font-weight: 700;
  229. }
  230. .tip{
  231. margin-top: 14rpx;
  232. font-size: 24rpx;
  233. color: #999;
  234. }
  235. .icon-xuanzhong{
  236. font-size: 36rpx;
  237. color: $base-color;
  238. }
  239. }
  240. </style>