index.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { browser, getUrlCode } from '@/helpers/utils'
  2. import { Button, Cell, CellGroup, Icon } from 'vant'
  3. import { defineComponent, onMounted, reactive } from 'vue'
  4. import styles from './index.module.less'
  5. export default defineComponent({
  6. name: 'pay-define',
  7. setup() {
  8. const state = reactive({
  9. amount: 0,
  10. browserStatus: false,
  11. errorText: '',
  12. payType: '',
  13. code: null as any,
  14. wxAppId: ''
  15. })
  16. const getPayment = () => {}
  17. // getPayment: throttle(function() {
  18. // try {
  19. // if (!(parseFloat(this.amount) > 0)) {
  20. // this.$toast("支付金额异常");
  21. // return;
  22. // }
  23. // removeAuthToken();
  24. // const returnUrl = this.returnUrl.replace(/\^\^/gi, "&");
  25. // let that = this,
  26. // payMap = {
  27. // orderNo: this.orderNo,
  28. // sign: this.sign,
  29. // amount: numeral(this.amount).format("0.00"),
  30. // payChannel: this.payType, // 支付渠道
  31. // orderBody: this.orderBody,
  32. // orderSubject: this.orderSubject,
  33. // tenantId: this.tenantId,
  34. // returnUrl: returnUrl,
  35. // notifyUrl: this.notifyUrl,
  36. // platform: this.platform,
  37. // };
  38. // // 判断是否是微信公众号支付
  39. // if (this.payType == "wx_pub") {
  40. // payMap.code = this.code;
  41. // }
  42. // this.$toast.loading({
  43. // duration: 0,
  44. // message: "加载中...",
  45. // forbidClick: true,
  46. // });
  47. // console.log(payMap);
  48. // executePayment(payMap)
  49. // .then((res) => {
  50. // this.$toast.clear();
  51. // let result = res.data;
  52. // if (result.code == 200) {
  53. // let payment = result.data;
  54. // if (payment.status === "succeeded") {
  55. // this.scanCodePay(payment);
  56. // } else if (payment.status == "failed") {
  57. // this.browserStatus = false;
  58. // this.errorText = payment.error_msg;
  59. // document.title = "ERROR";
  60. // } else if (payment.status == "pending") {
  61. // this.$dialog.alert({
  62. // title: "提示",
  63. // message: "订单处理中...",
  64. // confirmButtonColor: "#269a93",
  65. // });
  66. // }
  67. // } else {
  68. // this.$dialog.alert({
  69. // title: "提示",
  70. // message: result.msg,
  71. // confirmButtonColor: "#269a93",
  72. // });
  73. // }
  74. // })
  75. // .catch((err) => {
  76. // this.$toast.clear();
  77. // this.$dialog.alert({
  78. // title: "提示",
  79. // message: JSON.stringify(err),
  80. // confirmButtonColor: "#269a93",
  81. // });
  82. // });
  83. // } catch (error) {
  84. // this.$dialog.alert({
  85. // title: "提示",
  86. // message: "网络异常,请检查网络连接",
  87. // confirmButtonColor: "#269a93",
  88. // });
  89. // }
  90. // }, 500),
  91. // scanCodePay(data) {
  92. // // 判断支付方式 如果是 test 模式 支付用测试url 否则用生产url
  93. // if (this.payType == "alipay_qr") {
  94. // let url =
  95. // data.prod_mode === "false"
  96. // ? data.expend.qrcode_url +
  97. // "?payment_id=" +
  98. // data.id +
  99. // "&pay_channel=" +
  100. // data.pay_channel
  101. // : data.expend.qrcode_url;
  102. // window.location.href = url;
  103. // } else if (this.payType == "wx_pub") {
  104. // let tempPayInfo = JSON.parse(data.expend.pay_info);
  105. // this.payInfo = tempPayInfo;
  106. // if (typeof WeixinJSBridge == "undefined") {
  107. // if (document.addEventListener) {
  108. // document.addEventListener(
  109. // "WeixinJSBridgeReady",
  110. // this.onBridgeReady,
  111. // false
  112. // );
  113. // } else if (document.attachEvent) {
  114. // document.attachEvent("WeixinJSBridgeReady", this.onBridgeReady);
  115. // document.attachEvent("onWeixinJSBridgeReady", this.onBridgeReady);
  116. // }
  117. // } else {
  118. // this.onBridgeReady();
  119. // }
  120. // }
  121. // },
  122. // onBridgeReady() {
  123. // let payInfo = this.payInfo;
  124. // let orderNo = this.orderNo;
  125. // WeixinJSBridge.invoke(
  126. // "getBrandWCPayRequest",
  127. // {
  128. // appId: payInfo.appId, //公众号名称,由商户传入
  129. // timeStamp: payInfo.timeStamp, //时间戳,自1970年以来的秒数
  130. // nonceStr: payInfo.nonceStr, //随机串
  131. // package: payInfo.package,
  132. // signType: payInfo.signType, //微信签名方式:
  133. // paySign: payInfo.paySign, //微信签名
  134. // },
  135. // (res) => {
  136. // // if(res.err_msg == "get_brand_wcpay_request:ok" ){
  137. // // 使用以上方式判断前端返回,微信团队郑重提示:
  138. // //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  139. // // } else
  140. // if (
  141. // res.err_msg == "get_brand_wcpay_request:cancel" ||
  142. // res.err_msg == "get_brand_wcpay_request:fail"
  143. // ) {
  144. // // 用户取消支付
  145. // if (this.platform == "manager") {
  146. // // 管理端
  147. // // window.location.replace(validManageUrl() + '/#/paymentResult?orderNo=' + orderNo)
  148. // this.$router.replace({
  149. // path: "/paymentResult",
  150. // query: {
  151. // type: "error",
  152. // groupType: "REPAIR",
  153. // isBack: "off",
  154. // },
  155. // });
  156. // } else if (this.platform == "teacher") {
  157. // // 老师端
  158. // window.location.replace(
  159. // validTeacherUrl() +
  160. // "/#/paymentResult?orderNo=" +
  161. // orderNo +
  162. // "&isBack=off"
  163. // );
  164. // } else {
  165. // this.$router.replace({
  166. // path: "/paymentResult",
  167. // query: {
  168. // type: "error",
  169. // orderNo: orderNo,
  170. // isBack: "off",
  171. // },
  172. // });
  173. // }
  174. // } else {
  175. // // 使用以上方式判断前端返回,微信团队郑重提示:
  176. // //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  177. // if (this.platform == "manager") {
  178. // // 管理端
  179. // // window.location.replace(validManageUrl() + '/#/paymentResult?orderNo=' + orderNo)
  180. // this.$router.replace({
  181. // path: "/paymentResult",
  182. // query: {
  183. // orderNo: this.orderNo,
  184. // isBack: "off",
  185. // },
  186. // });
  187. // } else if (this.platform == "teacher") {
  188. // // 老师端
  189. // window.location.replace(
  190. // validTeacherUrl() +
  191. // "/#/paymentResult?orderNo=" +
  192. // orderNo +
  193. // "&isBack=off"
  194. // );
  195. // } else {
  196. // this.$router.replace({
  197. // path: "/paymentResult",
  198. // query: {
  199. // orderNo: this.orderNo,
  200. // isBack: "off",
  201. // },
  202. // });
  203. // }
  204. // }
  205. // }
  206. // );
  207. // },
  208. const goAuth = () => {
  209. // 用户授权
  210. const urlNow = encodeURIComponent(window.location.href)
  211. const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知
  212. const appid = state.wxAppId || 'wx751141096e75a4ee'
  213. const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`
  214. window.location.replace(url)
  215. }
  216. onMounted(() => {
  217. if (window.location.href.indexOf('?#') < 0) {
  218. window.location.href = window.location.href.replace('#', '?#')
  219. }
  220. // 判断当前浏览器
  221. const payType = state.payType
  222. if (browser().weixin) {
  223. if (payType === 'wx_pub') {
  224. //授权
  225. const code = getUrlCode('code')
  226. if (code) {
  227. state.code = code // 赋值code码
  228. }
  229. state.browserStatus = true
  230. document.title = '微信支付'
  231. } else if (payType == 'alipay_qr') {
  232. state.errorText = '请使用支付宝扫码'
  233. }
  234. } else if (browser().alipay) {
  235. if (payType === 'wx_pub') {
  236. state.errorText = '请使用微信扫码'
  237. } else if (payType == 'alipay_qr') {
  238. // 支付宝支付
  239. state.browserStatus = true
  240. document.title = '支付宝支付'
  241. }
  242. } else {
  243. state.errorText = '请在微信或支付宝客户端打开'
  244. }
  245. state.errorText && (document.title = 'ERROR')
  246. })
  247. return () => (
  248. <div class={styles.paydefine}>
  249. {state.browserStatus && (
  250. <div class={styles.container}>
  251. <div class={styles.amount}>¥{state.amount}</div>
  252. <CellGroup>
  253. <Cell title={'支付方式'} value={''}></Cell>
  254. <Cell title={'实付金额'} value={'¥0元'}></Cell>
  255. </CellGroup>
  256. <Button type="primary" block size="large" onClick={getPayment} round color="#01C1B5">
  257. 立即支付
  258. </Button>
  259. </div>
  260. )}
  261. {!state.browserStatus && (
  262. <div class={styles.container}>
  263. <div class={styles['error-text']}>
  264. {state.errorText && <Icon class={styles['error-icon']} name="warning-o" />}
  265. {state.errorText}
  266. </div>
  267. </div>
  268. )}
  269. </div>
  270. )
  271. }
  272. })