AddrLogic.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\mobile\logic;
  3. use app\model\AddrModel;
  4. use app\model\CommonModel;
  5. use think\response\Json;
  6. class AddrLogic extends BaseLogic
  7. {
  8. //列表
  9. public static function list(array $data = []): Json
  10. {
  11. $db = AddrModel::where('is_del', CommonModel::$del_normal);
  12. $count = $db->count('id');
  13. $list = $db
  14. ->field('id,addr_code,addr,contactor,mobile,"" addr_name')
  15. ->page($data['page'], $data['size'])
  16. ->withAttr('addr_code', function ($val) {
  17. return explode(',', $val);
  18. })->withAttr('addr_name', function ($val, $da) {
  19. return get_addr_name_array($da['addr_code']);
  20. })
  21. ->order('id', 'desc')
  22. ->select()
  23. ->toArray();
  24. return json_show(CommonModel::$success, '获取列表成功', ['count' => $count, 'list' => $list]);
  25. }
  26. //添加
  27. public static function add(array $data = []): Json
  28. {
  29. $rs = AddrModel::create(array_merge($data, [
  30. 'uid' => self::$aid,
  31. 'addr_code' => implode(',', $data['addr_code']),
  32. 'is_del' => CommonModel::$del_normal,
  33. 'addtime' => date('Y-m-d H:i:s'),
  34. 'updatetime' => date('Y-m-d H:i:s'),
  35. ]))->save();
  36. return $rs ? json_show(CommonModel::$success, '添加地址成功') : json_show(CommonModel::$error_param, '添加地址失败');
  37. }
  38. //详情
  39. public static function read(int $id = 0): Json
  40. {
  41. $rs = AddrModel::withoutField('uid,is_del')
  42. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  43. ->append(['addr_name'])
  44. ->withAttr('addr_code', function ($val) {
  45. return explode(',', $val);
  46. })->withAttr('addr_name', function ($val, $da) {
  47. return get_addr_name_array($da['addr_code']);
  48. })
  49. ->findOrEmpty()
  50. ->toArray();
  51. return json_show(CommonModel::$success, '获取收货地址详情成功', $rs);
  52. }
  53. //修改
  54. public static function edit(array $data = []): Json
  55. {
  56. $res = AddrModel::field('id')
  57. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  58. ->findOrEmpty()
  59. ->isEmpty();
  60. if ($res) return json_show(CommonModel::$error_param, '该地址不存在');
  61. $rs = AddrModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  62. ->save(array_merge($data, [
  63. 'addr_code' => implode(',', $data['addr_code']),
  64. 'updatetime' => date('Y-m-d H:i:s'),
  65. ]));
  66. return $rs ? json_show(CommonModel::$success, '修改地址成功') : json_show(CommonModel::$error_param, '修改地址失败');
  67. }
  68. //删除
  69. public static function delete(int $id = 0): Json
  70. {
  71. $rs = AddrModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  72. ->save([
  73. 'is_del' => CommonModel::$del_deleted,
  74. 'updatetime' => date('Y-m-d H:i:s'),
  75. ]);
  76. return $rs ? json_show(CommonModel::$success, '删除地址成功') : json_show(CommonModel::$error_param, '该地址不存在');
  77. }
  78. }