lrf 2 anos atrás
pai
commit
2f53887cf9

+ 3 - 0
app/controller/user/config/.address.js

@@ -38,4 +38,7 @@ module.exports = {
       count: true,
     },
   },
+  toDefault: {
+    params: ['!id'],
+  },
 };

+ 10 - 1
app/service/user/address.js

@@ -4,12 +4,21 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
 
-// 
+//
 class AddressService extends CrudService {
   constructor(ctx) {
     super(ctx, 'address');
     this.model = this.ctx.model.User.Address;
   }
+
+  async toDefault({ id }) {
+    const data = await this.model.findById(id);
+    if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
+    const { customer } = data;
+    await this.model.updateMany({ customer }, { is_default: '1' });
+    data.is_default = '0';
+    await data.save();
+  }
 }
 
 module.exports = AddressService;

+ 3 - 2
app/z_router/user/address.js

@@ -7,10 +7,11 @@ const rkey = 'address';
 const ckey = 'user.address';
 const keyZh = '收货地址';
 const routes = [
+  { method: 'post', path: `${rkey}/toDefault/:id`, controller: `${ckey}.toDefault`, name: `${ckey}toDefault`, zh: '默认收货地址' },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
-  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, middleware: [ 'password' ], name: `${ckey}Create`, zh: `创建${keyZh}` },
-  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, middleware: [ 'password' ], name: `${ckey}Update`, zh: `修改${keyZh}` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },
+  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, name: `${ckey}Update`, zh: `修改${keyZh}` },
   { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
 ];