|
@@ -0,0 +1,92 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+const assert = require('assert');
|
|
|
+const Service = require('egg').Service;
|
|
|
+const moment = require('moment');
|
|
|
+class VipService extends Service {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx);
|
|
|
+ this.model = this.ctx.model.Vip;
|
|
|
+ }
|
|
|
+ async create({ name, thumbnail, phone, company, tab, openid, vipTab }) {
|
|
|
+ assert(name, '昵称不存在');
|
|
|
+ assert(phone, '电话不存在');
|
|
|
+ assert(openid, 'openid不存在');
|
|
|
+ assert(vipTab, '会员标识不存在');
|
|
|
+ const filter = {};
|
|
|
+ const arr = { phone, openid };
|
|
|
+ for (const e in arr) {
|
|
|
+ const data = `{ "${e}": "${arr[e]}" }`;
|
|
|
+ if (arr[e]) {
|
|
|
+ filter.$or = [];
|
|
|
+ filter.$or.push(JSON.parse(data));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const total = await this.model.find({ ...filter });
|
|
|
+ if (total.length > 0) {
|
|
|
+ return { errcode: -1001, errmsg: '用户手机号或微信已存在' };
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ const startTime = moment().valueOf();
|
|
|
+ const endTime = vipTab === 'svip' ? moment().add(1, 'Y').valueOf() : 0;
|
|
|
+ const res = await this.model.create({ name, thumbnail, phone, company, tab, openid, status: '0', startTime, endTime, vipTab });
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: res };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async update({ id, name, thumbnail, phone, company, tab, status }) {
|
|
|
+ assert(id, 'id不存在');
|
|
|
+ try {
|
|
|
+ await this.model.updateOne({ _id: id }, { name, thumbnail, phone, company, tab, status });
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: '' };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async proceed({ id }) {
|
|
|
+ assert(id, 'id不存在');
|
|
|
+ try {
|
|
|
+ const endTime = moment().add(1, 'Y').valueOf();
|
|
|
+ await this.model.updateOne({ _id: id }, { endTime });
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: '' };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async delete({ id }) {
|
|
|
+ assert(id, 'id不存在');
|
|
|
+ try {
|
|
|
+ await this.model.deleteOne({ _id: id });
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: '' };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ async query({ skip, limit, name, phone, company, tab, openid, status, startTime, endTime, vipTab }) {
|
|
|
+ const filter = {};
|
|
|
+ const arr = { name, phone, company, tab, openid, status, startTime, endTime, vipTab };
|
|
|
+ for (const e in arr) {
|
|
|
+ const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
|
|
|
+ if (arr[e]) {
|
|
|
+ filter.$or = [];
|
|
|
+ filter.$or.push(JSON.parse(data));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ const total = await this.model.find({ ...filter });
|
|
|
+ let res;
|
|
|
+ if (skip && limit) {
|
|
|
+ res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
|
|
|
+ } else {
|
|
|
+ res = await this.model.find({ ...filter });
|
|
|
+ }
|
|
|
+ return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
|
|
|
+ } catch (error) {
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = VipService;
|