pay-desk.uvue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <!-- 自定义收银台页面模式 -->
  3. <view class="uni-pay">
  4. <view class="mobile-pay-popup">
  5. <view class="mobile-pay-popup-amount-box">
  6. <view><text class="text">待支付金额:</text></view>
  7. <view class="mobile-pay-popup-amount"><text class="text">{{ amountFormat(myOptions.getNumber('total_fee')) }}</text></view>
  8. </view>
  9. <view class="mobile-pay-popup-provider-list">
  10. <view class="uni-list">
  11. <!-- #ifdef MP-WEIXIN || H5 || APP -->
  12. <view class="uni-list-item" v-if="checkProvider('wxpay')" @click="createOrderByProvider('wxpay')">
  13. <view class="uni-list-item__container container--right">
  14. <view class="uni-list-item__header">
  15. <image :src="insideData.getJSON('images')!['wxpay']" class="image"></image>
  16. </view>
  17. <view class="uni-list-item__content uni-list-item__content--center">
  18. <text class="text">微信支付</text>
  19. </view>
  20. </view>
  21. <view class="arrowright"></view>
  22. </view>
  23. <!-- #endif -->
  24. <!-- #ifdef MP-ALIPAY || H5 || APP -->
  25. <view class="uni-list-item" v-if="checkProvider('alipay')" @click="createOrderByProvider('alipay')">
  26. <view class="uni-list-item__container container--right">
  27. <view class="uni-list-item__header">
  28. <image :src="insideData.getJSON('images')!['alipay']" class="image"></image>
  29. </view>
  30. <view class="uni-list-item__content uni-list-item__content--center">
  31. <text class="text">支付宝</text>
  32. </view>
  33. </view>
  34. <view class="arrowright"></view>
  35. </view>
  36. <!-- #endif -->
  37. </view>
  38. </view>
  39. </view>
  40. <!-- 挂载支付组件 -->
  41. <uni-pay ref="payRef" height="450px" :to-success-page="false" @mounted="onMounted" @success="onSuccess" @fail="onFail" @cancel="onCancel"></uni-pay>
  42. </view>
  43. </template>
  44. <script>
  45. import { objectAssign } from "../../js_sdk/js_sdk"
  46. export default {
  47. data() {
  48. return {
  49. myOptions: {
  50. total_fee: "",
  51. } as UTSJSONObject,
  52. insideData: {
  53. currentProviders: [] as Array<string>,
  54. images: {
  55. alipay: "",
  56. wxpay: ""
  57. } as UTSJSONObject
  58. } as UTSJSONObject, // uni-pay组件mounted事件获得的数据
  59. adpid: "", // 广告id
  60. return_url: "", // 支付成功后点击查看订单跳转的订单详情页面地址
  61. main_color: "", // 支付成功页面的主色调
  62. }
  63. },
  64. // 监听 - 页面每次【加载时】执行(如:前进)
  65. onLoad(options) {
  66. let optionsStr = options['options'] as string | null;
  67. if (optionsStr != null && optionsStr != "") {
  68. let newOptions = JSON.parse(decodeURI(optionsStr)!) as UTSJSONObject;
  69. this.myOptions = newOptions as UTSJSONObject;
  70. }
  71. console.log('myOptions: ', this.myOptions)
  72. },
  73. // 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快,会在页面进入动画完成前触发
  74. onReady() { },
  75. // 监听 - 页面每次【显示时】执行(如:前进和返回) (页面每次出现在屏幕上都触发,包括从下级页面点返回露出当前页面)
  76. onShow() { },
  77. // 监听 - 页面每次【隐藏时】执行(如:返回)
  78. onHide() { },
  79. // 函数
  80. methods: {
  81. // 监听 - 支付组件加载完毕事件
  82. onMounted(insideData : UTSJSONObject) {
  83. this.insideData['currentProviders'] = insideData.getArray('currentProviders') as Array<string>;
  84. this.insideData['images'] = insideData.getJSON('images') as UTSJSONObject;
  85. },
  86. // 发起支付
  87. createOrder(data : UTSJSONObject) {
  88. this.myOptions = objectAssign(this.myOptions, data);
  89. const payInstance = this.$refs["payRef"] as UniPayComponentPublicInstance;
  90. payInstance.createOrder(this.myOptions);
  91. },
  92. // 根据provider发起支付
  93. createOrderByProvider(provider : string) {
  94. this.createOrder({
  95. provider
  96. })
  97. },
  98. // 监听事件 - 支付成功
  99. onSuccess(res : UTSJSONObject) {
  100. let out_trade_no = res['out_trade_no'] as string;
  101. let pay_order = res.getJSON('pay_order') as UTSJSONObject;
  102. let order_no = pay_order['order_no'] as string;
  103. //let pay_date = pay_order['pay_date'] as string;
  104. let total_fee = pay_order.getNumber('total_fee');
  105. if (total_fee == null) {
  106. total_fee = 0;
  107. }
  108. let return_url = this.return_url as string;
  109. let adpid = this.adpid as string;
  110. let main_color = this.main_color as string;
  111. console.log('success: ', res);
  112. let user_order_success = res.getBoolean('user_order_success');
  113. if (user_order_success != null && user_order_success != true) {
  114. // 代表用户已付款,且你自己写的回调成功并正确执行了
  115. uni.redirectTo({
  116. url: `/uni_modules/uni-pay-x/pages/success/success?out_trade_no=${out_trade_no}&order_no=${order_no}&total_fee=${total_fee}&adpid=${adpid}&return_url=${return_url}&main_color=${main_color}`
  117. });
  118. } else {
  119. // 代表用户已付款,但你自己写的回调执行失败(通常是因为你的回调代码有问题)
  120. }
  121. },
  122. // 监听事件 - 支付失败
  123. onFail(err : RequestPaymentFail) {
  124. console.log('fail: ', err)
  125. },
  126. // 监听事件 - 取消支付
  127. onCancel(err : RequestPaymentFail) {
  128. console.log('cancel: ', err)
  129. },
  130. amountFormat(totalFee : number | null) :string{
  131. if (totalFee == null) {
  132. return "0";
  133. } else {
  134. return (totalFee / 100).toFixed(2)
  135. }
  136. },
  137. // 检测provider
  138. checkProvider(provider : string) : boolean {
  139. let currentProviders = this.insideData.getArray<string>('currentProviders');
  140. return currentProviders != null && currentProviders.indexOf(provider) > -1 ? true : false;
  141. }
  142. },
  143. // 监听器
  144. watch: {
  145. },
  146. // 计算属性
  147. computed: {
  148. }
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. .uni-pay{
  153. flex: 1;
  154. display: flex;
  155. flex-direction: column;
  156. background-color: #f3f3f3;
  157. }
  158. .mobile-pay-popup {
  159. flex: 1;
  160. .mobile-pay-popup-amount-box {
  161. background-color: #ffffff;
  162. padding: 15px;
  163. .mobile-pay-popup-amount {
  164. margin-top: 10px;
  165. .text {
  166. color: #e43d33;
  167. font-size: 30px;
  168. }
  169. }
  170. }
  171. .mobile-pay-popup-provider-list {
  172. background-color: #ffffff;
  173. margin-top: 10px;
  174. .uni-list {
  175. display: flex;
  176. background-color: #fff;
  177. position: relative;
  178. flex-direction: column;
  179. .uni-list-item {
  180. display: flex;
  181. position: relative;
  182. justify-content: space-between;
  183. align-items: center;
  184. background-color: #fff;
  185. flex-direction: row;
  186. border-bottom: 1px solid #f8f8f8;
  187. /* #ifdef H5 */
  188. cursor: pointer;
  189. /* #endif */
  190. &:hover {
  191. background-color: #f1f1f1;
  192. }
  193. .uni-list-item__container {
  194. position: relative;
  195. display: flex;
  196. flex-direction: row;
  197. padding: 12px 15px;
  198. padding-left: 15px;
  199. flex: 1;
  200. overflow: hidden;
  201. .uni-list-item__header {
  202. display: flex;
  203. flex-direction: row;
  204. align-items: center;
  205. .image {
  206. width: 26px;
  207. height: 26px;
  208. margin-right: 9px;
  209. }
  210. }
  211. }
  212. .container--right {
  213. padding-right: 0;
  214. }
  215. .uni-list-item__content {
  216. display: flex;
  217. padding-right: 8px;
  218. flex: 1;
  219. flex-direction: column;
  220. justify-content: space-between;
  221. overflow: hidden;
  222. .text {
  223. color: #3b4144;
  224. font-size: 14px;
  225. }
  226. }
  227. .uni-list-item__content--center {
  228. justify-content: center;
  229. }
  230. .arrowright {
  231. border-top: 1px solid #bbbbbb;
  232. border-right: 1px solid #bbbbbb;
  233. width: 8px;
  234. height: 8px;
  235. margin-right: 15px;
  236. transform: rotate(45deg);
  237. }
  238. }
  239. }
  240. }
  241. }
  242. </style>