| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import { ApiTags } from '@nestjs/swagger'
- import { Public } from '../auth/public.decorator'
- import { MembershipService } from './membership.service'
- import {
- BadRequestException,
- Body,
- Controller,
- Get,
- NotImplementedException,
- Post,
- Query,
- Render,
- Req
- } from '@nestjs/common'
- import { RenewDto } from './dto/renew.dto'
- import { WeixinService } from '../weixin/weixin.service'
- @ApiTags('membership')
- @Controller('/membership')
- export class MembershipController {
- constructor(private readonly membershipService: MembershipService, private readonly weixinService: WeixinService) {}
- @Get('/get')
- async getMembership(@Req() req) {
- return this.membershipService.getMembership(req.user.id)
- }
- @Post('/renew')
- async renewMembership(@Req() req, @Body() body: RenewDto) {
- if (body.type === 'JSAPI') {
- if (!body.openid) {
- throw new BadRequestException('openid is required')
- }
- return await this.membershipService.combinedJsapi(req.user.id, body.planId, body.openid)
- } else {
- throw new NotImplementedException()
- }
- }
- @Public()
- @Get('/plans')
- async getPlans() {
- return await this.membershipService.getPlans()
- }
- @Public()
- @Get('/h5pay')
- @Render('h5pay')
- async h5pay(@Query() { code, userId, planId }) {
- // return { success: true, openid: '123', errorCode: 0, message: '', jsapiData: JSON.stringify({aaa:111,bbb:{ccc:"kdjlfksjdfkls"}}), price: `123` }
- if (!code) {
- return {
- success: false,
- message: '缺少code',
- errorCode: 1,
- jsapiData: '{}'
- }
- }
- if (!userId) {
- return {
- success: false,
- message: '缺少userId',
- errorCode: 2,
- jsapiData: '{}'
- }
- }
- if (!planId) {
- return {
- success: false,
- message: '缺少planId',
- errorCode: 3,
- jsapiData: '{}'
- }
- }
- const plans = await this.membershipService.getPlans()
- const plan = plans.find((p) => p.id == planId)
- if (!plan) {
- return {
- success: false,
- message: 'planId无效',
- errorCode: 4,
- jsapiData: '{}'
- }
- }
- const openid = await this.weixinService.code2oenId(code)
- const jsapiData = await this.membershipService.combinedJsapi(userId, planId, openid)
- return { success: true, openid, errorCode: 0, message: '', jsapiData: JSON.stringify(jsapiData), price: `${plan.price}` }
- }
- }
|