index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <script setup lang="ts">
  2. import { showConfirm } from '@/components/Toast'
  3. import { ref, computed, onMounted } from 'vue'
  4. import { useUserStore } from '@/stores/user'
  5. import { openPay, getOrderInfo, wxLogin } from '@/api/api'
  6. import { useRoute } from 'vue-router'
  7. import { GetRequest, getWeChatCode } from '@/utils/index'
  8. const payType = ref('0')
  9. const route = useRoute()
  10. const orderDetal = ref({})
  11. const orderSn = ref(route.query.id || GetRequest('id'))
  12. const { getToken, openId, isEur, info, setUserData } = useUserStore()
  13. const is_weixn = computed(() => {
  14. var wx = window.navigator.userAgent.toLowerCase()
  15. if (wx.match(/MicroMessenger/i) == 'micromessenger') {
  16. return true // 是微信端
  17. } else {
  18. return false
  19. }
  20. })
  21. // let PAYSID = {
  22. // wechatPay: is_weixn.value ? 1 : 0,
  23. // alipay: 4,
  24. // paypal: 5
  25. // }
  26. //判断是否微信
  27. onMounted(() => {
  28. console.log('useUserStore', getToken, openId, isEur, info, is_weixn)
  29. getDetial()
  30. })
  31. async function handelPay() {
  32. let apiData = {
  33. orderSn: orderSn.value,
  34. payType: is_weixn.value ? '1' : payType.value,
  35. openId: 'dolor exercitation velit'
  36. }
  37. console.log('handelPay', apiData)
  38. const res = await openPay(apiData)
  39. // console.log('apiData', res)
  40. if (is_weixn.value) {
  41. //微信内支付
  42. onBridgeReady(res)
  43. } else if (res.form || res.h5Url) {
  44. console.log('payType', res.form || res.h5Url)
  45. window.location.href = res.form || res.h5Url
  46. } else {
  47. showConfirm({
  48. text: '支付异常',
  49. type: 'err',
  50. callback: (val) => {
  51. console.log('我的测试计划', val)
  52. }
  53. })
  54. }
  55. }
  56. // 调微信支付
  57. function onBridgeReady(obj) {
  58. WeixinJSBridge.invoke(
  59. 'getBrandWCPayRequest',
  60. {
  61. appId: obj.appid,
  62. //公众号名称,由商户传入
  63. timeStamp: obj.timeStamp + '',
  64. //时间戳,自1970年以来的秒数
  65. nonceStr: obj.nonceStr,
  66. //随机串
  67. package: `prepay_id=${obj.prepayId}`,
  68. signType: obj.signType,
  69. //微信签名方式:
  70. paySign: obj.paySign
  71. },
  72. function (res) {
  73. if (res.err_msg == 'get_brand_wcpay_request:ok') {
  74. // 使用以上方式判断前端返回,微信团队郑重提示:
  75. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  76. showConfirm({
  77. text: '成功了',
  78. type: 'warn',
  79. callback: (val) => {
  80. console.log('我的测试计划', val)
  81. }
  82. })
  83. console.log('成功了', res)
  84. } else {
  85. console.log('失败了', res)
  86. showConfirm({
  87. text: '失败了',
  88. type: 'warn',
  89. callback: (val) => {
  90. console.log('我的测试计划', val)
  91. }
  92. })
  93. }
  94. }
  95. )
  96. }
  97. function handleType(params: string) {
  98. payType.value = params
  99. }
  100. function getDetial() {
  101. getOrderInfo(orderSn.value).then((res) => {
  102. orderDetal.value = res
  103. handleWxlogin(res.wxAppId)
  104. })
  105. }
  106. async function handleWxlogin(wxAppId: string) {
  107. const code = GetRequest('code')
  108. if (!openId && !code) {
  109. //微信登录
  110. getWeChatCode(wxAppId)
  111. } else if (code) {
  112. //存在code 换取openid
  113. const res = wxLogin({ code, orderSn: orderSn.value })
  114. if (!res.errmsg) {
  115. setUserData(res.openid, 'openId')
  116. }
  117. }
  118. }
  119. async function handleOpenPay() {
  120. openPay({
  121. orderSn: GetRequest('orderSn'),
  122. payType: payType.value,
  123. openId: ''
  124. }).then((res) => {
  125. let url = res.qrCodeUrl
  126. })
  127. }
  128. </script>
  129. <template>
  130. <div class="mobilePage">
  131. <div class="pageTitle">支付中心</div>
  132. <div class="contentInfo">
  133. <div class="info">
  134. <div class="cell">
  135. <span>当前账号</span>
  136. <span>15915816041</span>
  137. </div>
  138. <div class="cell">
  139. <span>购买会员</span>
  140. <span>专业会员 1 ,有效期1年</span>
  141. </div>
  142. </div>
  143. <div class="payType">
  144. <div class="cell" @click="handleType('0')">
  145. <span>
  146. <img class="payTypeImg" src="@/assets/images/wechat56.png" alt="" />
  147. 微信支付
  148. </span>
  149. <div class="select" :class="{ active: payType == '0' }"></div>
  150. </div>
  151. <div class="cell" @click="handleType('4')" v-if="!is_weixn">
  152. <span>
  153. <img class="payTypeImg" src="@/assets/images/alipay56.png" alt="" />
  154. 支付宝支付
  155. </span>
  156. <div class="select" :class="{ active: payType == '4' }"></div>
  157. </div>
  158. <div class="cell" @click="handleType('5')" v-if="isEur">
  159. <span>
  160. <img class="payTypeImg" src="@/assets/images/paypal.png" alt="" />
  161. paypal
  162. </span>
  163. <div class="select" :class="{ active: payType == '5' }"></div>
  164. </div>
  165. </div>
  166. </div>
  167. <div class="bottomInfo">
  168. <div class="price">
  169. 应付金额<span>¥{{ orderDetal?.orderMoney || 0.0 }}</span>
  170. </div>
  171. <div class="payBut" @click="handelPay">付款</div>
  172. </div>
  173. </div>
  174. </template>
  175. <style lang="less" scoped>
  176. .mobilePage {
  177. background: #f7f7f7;
  178. min-height: calc(100vh - 50px);
  179. max-width: 100vw;
  180. display: block;
  181. .pageTitle {
  182. width: calc(100% - 40px);
  183. padding: 23px 20px;
  184. background: #ffffff;
  185. border-radius: 0px 0px 0px 0px;
  186. opacity: 1;
  187. border: 1px solid #e6e6e6;
  188. margin-bottom: 10px;
  189. }
  190. .contentInfo {
  191. .cell {
  192. display: flex;
  193. justify-content: space-between;
  194. background-color: #fff;
  195. padding: 15px 20px;
  196. border-bottom: 1px solid #ebedf0;
  197. align-items: center;
  198. &:last-child {
  199. border-bottom: none;
  200. }
  201. .select {
  202. border-radius: 50%;
  203. height: 20px;
  204. width: 20px;
  205. border: 1px solid #d5d8de;
  206. }
  207. .active {
  208. background: url('@/assets/images/active.png');
  209. background-position: center center;
  210. background-size: 22px;
  211. height: 22px;
  212. width: 22px;
  213. border: none;
  214. }
  215. .payTypeImg {
  216. height: 18px;
  217. width: 20px;
  218. margin-right: 8px;
  219. position: relative;
  220. top: 3px;
  221. }
  222. }
  223. }
  224. .bottomInfo {
  225. height: 56px;
  226. width: 100%;
  227. border-radius: 0px 0px 0px 0px;
  228. display: flex;
  229. align-items: center;
  230. background-color: #fff;
  231. justify-content: space-between;
  232. position: fixed;
  233. bottom: 0px;
  234. .price {
  235. padding: 5px 20px;
  236. span {
  237. font-size: 16px;
  238. font-family: PingFang SC-Regular, PingFang SC;
  239. font-weight: 400;
  240. color: #ff0000;
  241. line-height: 19px;
  242. letter-spacing: 4px;
  243. }
  244. }
  245. .payBut {
  246. background: @primary-color;
  247. border-radius: 0px 0px 0px 0px;
  248. opacity: 1;
  249. width: 120px;
  250. line-height: 56px;
  251. height: 100%;
  252. color: #fff;
  253. text-align: center;
  254. }
  255. }
  256. .info {
  257. margin-bottom: 10px;
  258. font-size: 16px;
  259. font-family: PingFang SC-Regular, PingFang SC;
  260. font-weight: 400;
  261. color: #202020;
  262. line-height: 19px;
  263. }
  264. }
  265. </style>