index.vue 7.3 KB

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