index.vue 6.8 KB

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