refactor: subscribe module
This commit is contained in:
+8
-15
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crm/common"
|
||||
"crm/models"
|
||||
"crm/response"
|
||||
"crm/service"
|
||||
@@ -25,8 +26,8 @@ func NewSubscribeApi() *SubscribeApi {
|
||||
func (s *SubscribeApi) Pay(context *gin.Context) {
|
||||
var param models.SubscribePayParam
|
||||
uid, _ := strconv.Atoi(context.Request.Header.Get("uid"))
|
||||
err := context.ShouldBind(¶m);
|
||||
if int64(uid) <= 0 || err != nil {
|
||||
err := context.ShouldBind(¶m)
|
||||
if int64(uid) <= 0 || err != nil {
|
||||
response.Result(response.ErrCodeParamInvalid, nil, context)
|
||||
return
|
||||
}
|
||||
@@ -36,18 +37,10 @@ func (s *SubscribeApi) Pay(context *gin.Context) {
|
||||
}
|
||||
|
||||
// 支付成功回调
|
||||
func (s *SubscribeApi) Callback(context *gin.Context) {
|
||||
context.Request.ParseForm()
|
||||
var outTradeNo = context.Request.Form.Get("out_trade_no")
|
||||
paySuccessURL, _ := s.subscribeService.Callback(outTradeNo)
|
||||
context.Redirect(http.StatusMovedPermanently, paySuccessURL)
|
||||
}
|
||||
|
||||
// 支付通知
|
||||
func (s *SubscribeApi) Notify(context *gin.Context) {
|
||||
context.Request.ParseForm()
|
||||
var outTradeNo = context.Request.Form.Get("out_trade_no")
|
||||
errCode := s.subscribeService.Notify(context.Request.Form, outTradeNo)
|
||||
func (s *SubscribeApi) PayBack(context *gin.Context) {
|
||||
notifyReq := common.GetAlipay().VerifySign(context.Request)
|
||||
errCode := s.subscribeService.PayBack(notifyReq.GetString("out_trade_no"))
|
||||
context.String(http.StatusOK, "%s", "success")
|
||||
response.Result(errCode, nil, context)
|
||||
}
|
||||
|
||||
@@ -60,4 +53,4 @@ func (s *SubscribeApi) GetInfo(context *gin.Context) {
|
||||
}
|
||||
subscribeInfo, errCode := s.subscribeService.GetInfo(int64(uid))
|
||||
response.Result(errCode, subscribeInfo, context)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,8 +56,8 @@ func (s *SubscribeDao) SetOrder(tradeNo string, param *models.SubscribePayOrder)
|
||||
return global.Rdb.Set(ctx, tradeNo, string(order), time.Minute*30).Err()
|
||||
}
|
||||
|
||||
func (s *SubscribeDao) GetOrder(orderNo string) (string, error) {
|
||||
orderJson, err := global.Rdb.Get(ctx, orderNo).Result()
|
||||
func (s *SubscribeDao) GetOrder(tradeNo string) (string, error) {
|
||||
orderJson, err := global.Rdb.Get(ctx, tradeNo).Result()
|
||||
if err != nil {
|
||||
return StringNull, err
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ package global
|
||||
import (
|
||||
"crm/config"
|
||||
|
||||
"github.com/go-pay/gopay/alipay"
|
||||
"github.com/go-redis/redis/v9"
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ func Router() {
|
||||
route.POST("/user/register", api.NewUserApi().Register)
|
||||
route.POST("/user/pass", api.NewUserApi().ForgotPass)
|
||||
route.DELETE("/user/delete", api.NewUserApi().Delete)
|
||||
route.GET("/subscribe/callback", api.NewSubscribeApi().Callback)
|
||||
route.POST("/subscribe/payback", api.NewSubscribeApi().PayBack)
|
||||
|
||||
// 初始化数据
|
||||
route.POST("/init/data", api.InitData)
|
||||
@@ -73,7 +73,6 @@ func Router() {
|
||||
// 订阅模块
|
||||
route.GET("/subscribe/info", api.NewSubscribeApi().GetInfo)
|
||||
route.POST("/subscribe/pay", api.NewSubscribeApi().Pay)
|
||||
route.POST("/subscribe/notify", api.NewSubscribeApi().Notify)
|
||||
|
||||
// 通知模块
|
||||
route.GET("/notice/list", api.NewNoticeApi().GetList)
|
||||
|
||||
@@ -23,13 +23,14 @@ type SubscribeUpdateParam struct {
|
||||
}
|
||||
|
||||
type SubscribePayParam struct {
|
||||
Uid int64 `json:"uid" binding:"-"`
|
||||
Version int `json:"version" binding:"required,oneof=2 3"`
|
||||
Uid int64 `json:"uid" binding:"-"`
|
||||
Duration int64 `json:"duration" binding:"required,oneof=30 90 180 365 730"`
|
||||
}
|
||||
|
||||
type SubscribePayOrder struct {
|
||||
Uid int64 `json:"uid"`
|
||||
Version int `json:"version"`
|
||||
Uid int64 `json:"uid"`
|
||||
TradeNo string `json:"tradeNo"`
|
||||
Duration int64 `json:"duration"`
|
||||
}
|
||||
|
||||
type SubscribePayUrl struct {
|
||||
|
||||
+24
-100
@@ -1,23 +1,12 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crm/common"
|
||||
"crm/dao"
|
||||
"crm/global"
|
||||
"crm/models"
|
||||
"crm/response"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
"github.com/smartwalle/xid"
|
||||
)
|
||||
|
||||
const (
|
||||
Paying = 1 // 支付中
|
||||
Payed = 2 // 已支付
|
||||
)
|
||||
|
||||
type SubscribeService struct {
|
||||
@@ -37,134 +26,69 @@ func NewSubscribeService() *SubscribeService {
|
||||
func (s *SubscribeService) Pay(param models.SubscribePayParam) (*models.SubscribePayUrl, int) {
|
||||
|
||||
// 构建订单支付信息
|
||||
tradeNo := fmt.Sprintf("%d", xid.Next())
|
||||
var p = alipay.TradePagePay{}
|
||||
p.NotifyURL = global.Config.Alipay.NotifyURL
|
||||
p.ReturnURL = global.Config.Alipay.ReturnURL
|
||||
p.Subject = "支付测试:" + tradeNo
|
||||
p.OutTradeNo = tradeNo
|
||||
p.ProductCode = "FAST_INSTANT_TRADE_PAY"
|
||||
switch param.Version {
|
||||
case 2:
|
||||
p.TotalAmount = "18.00"
|
||||
case 3:
|
||||
p.TotalAmount = "198.00"
|
||||
}
|
||||
|
||||
// 缓存订单信息
|
||||
order := &models.SubscribePayOrder{
|
||||
Uid: param.Uid,
|
||||
Version: param.Version,
|
||||
}
|
||||
if err := s.subscribeDao.SetOrder(tradeNo, order); err != nil {
|
||||
return nil, response.ErrCodeFailed
|
||||
}
|
||||
|
||||
// 返回支付链接
|
||||
payUrl, err := global.Alipay.TradePagePay(p)
|
||||
tradeNo := common.GetAlipay().GenTradeNo()
|
||||
totalAmount := float64(param.Duration) * 0.6
|
||||
payUrl, err := common.GetAlipay().PagePay(tradeNo, totalAmount)
|
||||
if err != nil {
|
||||
return nil, response.ErrCodeFailed
|
||||
}
|
||||
|
||||
order := models.SubscribePayOrder{
|
||||
Uid: param.Uid,
|
||||
TradeNo: tradeNo,
|
||||
Duration: param.Duration,
|
||||
}
|
||||
|
||||
if err := s.subscribeDao.SetOrder(tradeNo, &order); err != nil {
|
||||
return nil, response.ErrCodeFailed
|
||||
}
|
||||
|
||||
subscribePayUrl := models.SubscribePayUrl{
|
||||
PayUrl: payUrl.String(),
|
||||
PayUrl: payUrl,
|
||||
}
|
||||
return &subscribePayUrl, response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 支付成功回调
|
||||
func (s *SubscribeService) Callback(outTradeNo string) (string, int) {
|
||||
|
||||
// 验证订单信息
|
||||
var p = alipay.TradeQuery{}
|
||||
p.OutTradeNo = outTradeNo
|
||||
rsp, err := global.Alipay.TradeQuery(p)
|
||||
if err != nil {
|
||||
log.Printf("验证订单 %s 信息发生错误: %s", outTradeNo, err.Error())
|
||||
return StringNull, response.ErrCodeFailed
|
||||
}
|
||||
if !rsp.IsSuccess() {
|
||||
log.Printf("验证订单 %s 信息发生错误: %s-%s", outTradeNo, rsp.Content.Msg, rsp.Content.SubMsg)
|
||||
return StringNull, response.ErrCodeFailed
|
||||
}
|
||||
func (s *SubscribeService) PayBack(outTradeNo string) int {
|
||||
|
||||
// 获取订单信息
|
||||
var order models.SubscribePayOrder
|
||||
orderJson, err := s.subscribeDao.GetOrder(outTradeNo)
|
||||
if err != nil {
|
||||
return StringNull, response.ErrCodeFailed
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
if err := json.Unmarshal([]byte(orderJson), &order); err != nil {
|
||||
return StringNull, response.ErrCodeFailed
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
|
||||
// 创建订阅信息
|
||||
var expired int64
|
||||
var content string
|
||||
switch order.Version {
|
||||
case 2:
|
||||
expired = time.Now().Unix() + int64(2592000)
|
||||
content = SUBSCRIBE_NOTICE_TEMPLATE1
|
||||
case 3:
|
||||
expired = time.Now().Unix() + int64(31104000)
|
||||
content = SUBSCRIBE_NOTICE_TEMPLATE2
|
||||
}
|
||||
expired := order.Duration * 24 * 60 * 60
|
||||
if !s.subscribeDao.IsExists(order.Uid) {
|
||||
subscribe := models.SubscribeCreateParam{
|
||||
Uid: order.Uid,
|
||||
Version: order.Version,
|
||||
Version: 2,
|
||||
Expired: expired,
|
||||
}
|
||||
if err := s.subscribeDao.Create(&subscribe); err != nil {
|
||||
return StringNull, response.ErrCodeFailed
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
} else {
|
||||
subscribe := models.SubscribeUpdateParam{
|
||||
Uid: order.Uid,
|
||||
Version: order.Version,
|
||||
Expired: expired,
|
||||
}
|
||||
if err := s.subscribeDao.Update(&subscribe); err != nil {
|
||||
return StringNull, response.ErrCodeFailed
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
}
|
||||
|
||||
// 订阅通知
|
||||
s.noticeDao.Create(&models.NoticeCreateParam{
|
||||
Content: content,
|
||||
Content: SUBSCRIBE_NOTICE_TEMPLATE1,
|
||||
Creator: order.Uid,
|
||||
})
|
||||
|
||||
return global.Config.Alipay.PaySuccessURL, response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 异步验签
|
||||
func (s *SubscribeService) Notify(data url.Values, outTradeNo string) int {
|
||||
ok, err := global.Alipay.VerifySign(data)
|
||||
if err != nil {
|
||||
log.Println("异步通知验证签名发生错误", err)
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
|
||||
if !ok {
|
||||
log.Println("异步通知验证签名未通过")
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
|
||||
log.Println("异步通知验证签名通过")
|
||||
|
||||
var p = alipay.TradeQuery{}
|
||||
p.OutTradeNo = outTradeNo
|
||||
rsp, err := global.Alipay.TradeQuery(p)
|
||||
if err != nil {
|
||||
log.Printf("异步通知验证订单 %s 信息发生错误: %s \n", outTradeNo, err.Error())
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
if !rsp.IsSuccess() {
|
||||
log.Printf("异步通知验证订单 %s 信息发生错误: %s-%s \n", outTradeNo, rsp.Content.Msg, rsp.Content.SubMsg)
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
|
||||
log.Printf("订单 %s 支付成功 \n", outTradeNo)
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
|
||||
+122
-56
@@ -1,58 +1,90 @@
|
||||
<template>
|
||||
<div :style="{ padding: '20px 20px 12px 20px' }">
|
||||
<a-alert message="点击立即购买后,会跳转到支付宝支付页面(沙箱环境,账户名 emrpqt1589@sandbox.com 登录密码/支付密码 111111)" type="info"
|
||||
show-icon /><br />
|
||||
<a-row :gutter="30">
|
||||
<a-col :span="8">
|
||||
<a-col :span="12">
|
||||
<a-card :class="activedClass[0]" :bordered="false">
|
||||
<span class="member-tag" style="color: #33B9FE;">基础版</span>
|
||||
<h2 class="title">免费
|
||||
<check-circle-filled v-if="ver == 1" class="icon" />
|
||||
</h2>
|
||||
<div class="content">满足基础功能需求,永久免费</div><br />
|
||||
<a-button size="large" class="btn-free" shape="round">免费使用</a-button>
|
||||
<br />
|
||||
<div class="content">满足基础功能需求,永久免费</div>
|
||||
<a-divider />
|
||||
<div class="subscribe-list" v-for="item in ['客户管理', '合同管理', '产品管理']">
|
||||
<check-circle-filled style="color: #33B9FE;font-size: 20px;;" />
|
||||
<span style="margin-left: 10px;">{{ item }}</span>
|
||||
</div>
|
||||
<br />
|
||||
<a-button size="large" class="btn-free" shape="round">免费使用</a-button><br />
|
||||
</a-card>
|
||||
</a-col>
|
||||
<a-col :span="8">
|
||||
<a-col :span="12">
|
||||
<a-card :class="activedClass[1]" :bordered="false">
|
||||
<span class="member-tag" style="color: #3788FF;">专业版</span>
|
||||
<h2 class="title">按月付费,每月18元</h2>
|
||||
<div class="content">能力不设限,新功能优先体验</div><br />
|
||||
<a-button v-if="version == 1 || version == 3" type="primary" size="large" class="btn-buy"
|
||||
@click="onPay(2)" shape="round" :disabled="disabled">立即购买</a-button>
|
||||
<a-button v-if="version == 2" type="primary" size="large" class="btn-buy" shape="round">{{
|
||||
<h2 class="title">按订阅时长付费</h2>
|
||||
<div class="content">能力不设限,新功能优先体验</div>
|
||||
<a-divider />
|
||||
<a-row :gutter="0">
|
||||
<a-col :span="8">
|
||||
<div class="subscribe-list" v-for="item in ['客户管理', '合同管理', '产品管理']">
|
||||
<check-circle-filled style="color: #33B9FE;font-size: 20px;;" />
|
||||
<span style="margin-left: 10px;">{{ item }}</span>
|
||||
</div>
|
||||
</a-col>
|
||||
<a-col :span="8">
|
||||
<div class="subscribe-list" v-for="item in ['仪表盘功能', '邮件服务配置', '邮件发送功能']">
|
||||
<check-circle-filled style="color: #476FFF;font-size: 20px;;" />
|
||||
<span style="margin-left: 10px;">{{ item }}</span>
|
||||
</div>
|
||||
</a-col>
|
||||
</a-row>
|
||||
<br />
|
||||
<a-button v-if="version == 1" type="primary" size="large" class="btn-buy" @click="onBuy"
|
||||
shape="round" :disabled="disabled">立即购买</a-button>
|
||||
<a-button v-if="version == 2" type="primary" size="large" class="btn-buy" shape="round"
|
||||
@click="onBuy">
|
||||
立即续费
|
||||
</a-button><br />
|
||||
<a-button v-if="version == 2" type="link" size="large" class="btn-buy" shape="round">专业版于 {{
|
||||
expired
|
||||
}} 到期</a-button>
|
||||
<br />
|
||||
<div class="subscribe-list" v-for="item in ['客户管理', '合同管理', '产品管理', '仪表盘可体验30天']">
|
||||
<check-circle-filled style="color: #476FFF;font-size: 20px;;" />
|
||||
<span style="margin-left: 10px;">{{ item }}</span>
|
||||
</div>
|
||||
</a-card>
|
||||
</a-col>
|
||||
<a-col :span="8">
|
||||
<a-card :class="activedClass[2]" :bordered="false">
|
||||
<span class="member-tag" style="color: #3788FF;">高级版</span>
|
||||
<h2 class="title">按年付费,每年198元</h2>
|
||||
<div class="content">能力不设限,新功能优先体验</div><br />
|
||||
<a-button v-if="version == 1 || version == 2" type="primary" size="large" class="btn-buy"
|
||||
@click="onPay(3)" shape="round" :disabled="disabled">立即购买</a-button>
|
||||
<a-button v-if="version == 3" type="primary" size="large" class="btn-buy" shape="round">{{
|
||||
expired
|
||||
}} 到期</a-button>
|
||||
<br />
|
||||
<div class="subscribe-list" v-for="item in ['客户管理', '合同管理', '产品管理', '仪表盘可体验365天']">
|
||||
<check-circle-filled style="color: #476FFF;font-size: 20px;;" />
|
||||
<span style="margin-left: 10px;">{{ item }}</span>
|
||||
</div>
|
||||
</a-card>
|
||||
</a-col>
|
||||
</a-row>
|
||||
<a-modal v-model:visible="visible" title="选择订阅类型" @ok="onPay" @cancel="onCancel" cancelText="取消" okText="支付"
|
||||
width="600px" :centered="true">
|
||||
<div style="height: 45vh;overflow-y: scroll;padding: 0 15px;">
|
||||
<a-alert message="支付提示"
|
||||
description="点击支付后,会跳转到支付宝支付页面。您可以使用支付宝沙箱环境的账户名 emrpqt1589@sandbox.com 和 登录密码/支付密码 111111,完成支付。"
|
||||
type="info" show-icon /><br />
|
||||
<a-form ref="subscribeFormRef" :model="subscribe" name="subscribe" :rules="rules">
|
||||
<a-row :gutter="16">
|
||||
<a-col :span="24">
|
||||
<a-form-item label="订阅时长" name="duration">
|
||||
<a-select v-model:value="subscribe.duration" placeholder="请选择">
|
||||
<a-select-option :value="30">1个月</a-select-option>
|
||||
<a-select-option :value="90">3个月</a-select-option>
|
||||
<a-select-option :value="180">6个月</a-select-option>
|
||||
<a-select-option :value="365">一年</a-select-option>
|
||||
<a-select-option :value="730">两年</a-select-option>
|
||||
</a-select>
|
||||
</a-form-item>
|
||||
<a-form-item label="支付方式" name="payMode">
|
||||
<a-radio-group v-model:value="subscribe.payMode">
|
||||
<a-radio :value="1">支付宝</a-radio>
|
||||
<a-radio :value="2">微信</a-radio>
|
||||
</a-radio-group>
|
||||
</a-form-item>
|
||||
<a-form-item label="合计支付" name="payment">
|
||||
<span style="color: #ff991f;font-size: 18px;font-weight: 550;">¥{{
|
||||
subscribe.duration *
|
||||
0.6
|
||||
}}</span>
|
||||
</a-form-item>
|
||||
</a-col>
|
||||
</a-row>
|
||||
</a-form>
|
||||
</div>
|
||||
</a-modal>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -60,40 +92,74 @@
|
||||
import { ref, reactive, onBeforeMount } from 'vue';
|
||||
import { CheckCircleFilled } from '@ant-design/icons-vue';
|
||||
import { subscribePay, getSubscribeInfo } from '../api/subscribe';
|
||||
import { useRouter } from 'vue-router'
|
||||
import moment from 'moment'
|
||||
|
||||
const router = useRouter()
|
||||
import moment from 'moment';
|
||||
import { message, Modal } from 'ant-design-vue';
|
||||
|
||||
const version = ref(0)
|
||||
const expired = ref(undefined)
|
||||
const payUrl = ref()
|
||||
const visible = ref(false)
|
||||
const disabled = ref(false)
|
||||
const activedClass = reactive(['card', 'card', 'card'])
|
||||
|
||||
const payResult = ref(false)
|
||||
const title = ref('')
|
||||
const buttonText = ref(undefined)
|
||||
const subscribe = reactive({
|
||||
duration: 30,
|
||||
payMode: 1,
|
||||
payment: 0.00
|
||||
})
|
||||
|
||||
const isClick = (index) => {
|
||||
active.value = index
|
||||
}
|
||||
// 表单校验
|
||||
const rules = {
|
||||
duration: [{
|
||||
required: true,
|
||||
message: '请选择订阅时长',
|
||||
trigger: 'blur',
|
||||
}],
|
||||
payMode: [{
|
||||
required: true,
|
||||
message: '请选择支付方式',
|
||||
trigger: 'blur',
|
||||
}],
|
||||
payment: [{
|
||||
required: true,
|
||||
}],
|
||||
};
|
||||
|
||||
const payResult = ref(false)
|
||||
const subscribeFormRef = ref()
|
||||
|
||||
// 初始化数据
|
||||
onBeforeMount(() => { subscribeInfo() })
|
||||
|
||||
// 点击购买
|
||||
const onBuy = () => {
|
||||
visible.value = true
|
||||
}
|
||||
|
||||
// 点击支付
|
||||
const onPay = (ver) => {
|
||||
let param = {
|
||||
version: ver
|
||||
const onPay = () => {
|
||||
if (subscribe.payMode == 2) {
|
||||
message.error('暂不支持微信支付!')
|
||||
return
|
||||
}
|
||||
subscribePay(param).then((res) => {
|
||||
if (res.data.code == 0) {
|
||||
visible.value = false
|
||||
payResult.value = true
|
||||
window.open(res.data.data.payUrl, '_self')
|
||||
subscribeFormRef.value.validateFields().then(() => {
|
||||
let param = {
|
||||
duration: subscribe.duration
|
||||
}
|
||||
subscribePay(param).then((res) => {
|
||||
if (res.data.code == 0) {
|
||||
visible.value = false
|
||||
payResult.value = true
|
||||
Modal.info({
|
||||
title: '正在跳转到支付宝支付页面...',
|
||||
centered: true,
|
||||
okText: '返回',
|
||||
onOk() {
|
||||
window.stop()
|
||||
},
|
||||
});
|
||||
window.open(res.data.data.payUrl, '_self')
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -147,19 +213,19 @@ const subscribeInfo = () => {
|
||||
}
|
||||
|
||||
.card {
|
||||
min-height: 450px;
|
||||
min-height: 480px;
|
||||
box-shadow: 0 1px 10px 0 rgb(33 41 48 / 5%);
|
||||
}
|
||||
|
||||
.selected-free-card {
|
||||
min-height: 450px;
|
||||
min-height: 480px;
|
||||
box-shadow: 0 1px 10px 0 rgb(33 41 48 / 5%);
|
||||
border: 2px solid #ceebfa;
|
||||
background: #f0faff;
|
||||
}
|
||||
|
||||
.selected-card {
|
||||
min-height: 450px;
|
||||
min-height: 480px;
|
||||
box-shadow: 0 1px 10px 0 rgb(33 41 48 / 5%);
|
||||
border: 2px solid #d6ddf9;
|
||||
background: #f3f6fd;
|
||||
|
||||
Reference in New Issue
Block a user