MemberAddress.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/2
  6. * Time: 下午8:34
  7. */
  8. namespace app\frontend\modules\member\models;
  9. class MemberAddress extends \app\common\models\MemberAddress
  10. {
  11. protected $guarded = [''];
  12. public static function getStaticModel()
  13. {
  14. return new static();
  15. }
  16. /*
  17. * Get a list of members receiving addresses
  18. *
  19. * @param int $memberId
  20. *
  21. * @return array
  22. * */
  23. public static function getAddressList($memberId)
  24. {
  25. return static::select('id', 'username', 'mobile', 'zipcode', 'province', 'city', 'district', 'address', 'isdefault', 'latitude', 'longitude', 'country_code')
  26. ->uniacid()->where('uid', $memberId)->get()->toArray();
  27. }
  28. /*
  29. * Get the receiving address information through the receiving address ID
  30. *
  31. * @param int $addressId
  32. *
  33. * @return array
  34. * */
  35. public static function getAddressById($addressId)
  36. {
  37. return static::uniacid()->where('id', $addressId)->first();
  38. }
  39. /*
  40. * Delete the receiving address by receiving address ID
  41. *
  42. * @param int $addressId
  43. *
  44. * @return int 0 or 1
  45. * */
  46. public static function destroyAddress($addressId)
  47. {
  48. return static::where('id', $addressId)->delete();
  49. }
  50. /*
  51. * Cancel the default address
  52. *
  53. * @param int $memberId
  54. *
  55. * @return int 0or 1
  56. * */
  57. public static function cancelDefaultAddress($memberId)
  58. {
  59. return static::uniacid()->where('uid', $memberId)->where('isdefault', '1')->update(['isdefault' => '0']);
  60. }
  61. /**
  62. * 定义字段名
  63. *
  64. * @return array
  65. */
  66. public function atributeNames()
  67. {
  68. return [
  69. 'mobile' => 'mobile_phone',
  70. 'username' => '收货人',
  71. 'province' => '省份',
  72. 'city' => '城市',
  73. 'district' => '区域',
  74. ];
  75. }
  76. /**
  77. * 字段规则
  78. *
  79. * @return array
  80. */
  81. public function rules()
  82. {
  83. return [
  84. 'mobile' => 'regex:/^[0-9]*$/',
  85. 'username' => 'required|max:45|min:2',
  86. //'province' => 'required',
  87. //'city' => 'required',
  88. //'district' => 'required',
  89. ];
  90. }
  91. /**
  92. * 获取会员收货地址
  93. *
  94. * @param $addressId
  95. * @param $userId
  96. * @return MemberAddress
  97. */
  98. public static function getAddressAndUserById($addressId, $userId)
  99. {
  100. return static::uniacid()->where('id', $addressId)->where('uid', $userId)->first();
  101. }
  102. }