CustomerRegister.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\models\mysqldb\customer;
  10. use fecshop\models\mysqldb\Customer;
  11. /**
  12. * @author Terry Zhao <2358269014@qq.com>
  13. * @since 1.0
  14. */
  15. class CustomerRegister extends Customer
  16. {
  17. private $_admin_user;
  18. private $_rules;
  19. public function setCustomerRules($rules)
  20. {
  21. $this->_rules = $rules;
  22. }
  23. public function rules()
  24. {
  25. $parent_rules = parent::rules();
  26. $current_rules = [
  27. ['id', 'filter', 'filter' => 'trim'],
  28. ['email', 'filter', 'filter' => 'trim'],
  29. ['email', 'email'],
  30. ['email', 'validateEmail'],
  31. ['password', 'filter', 'filter' => 'trim'],
  32. ['password', 'string', 'length' => [6, 30]],
  33. // 注册账户,名字不作为必填选项
  34. ['firstname', 'filter', 'filter' => 'trim'],
  35. // ['firstname', 'string', 'length' => [1, 50]],
  36. // 注册账户,名字不作为必填选项
  37. ['lastname', 'filter', 'filter' => 'trim'],
  38. // ['lastname', 'string', 'length' => [1, 50]],
  39. ['is_subscribed', 'validateIsSubscribed'],
  40. ];
  41. $rules = array_merge($parent_rules, $current_rules);
  42. if (is_array($this->_rules)) {
  43. $rules = array_merge($rules, $this->_rules);
  44. }
  45. return $rules;
  46. }
  47. public function validateIsSubscribed($attribute, $params)
  48. {
  49. if ($this->is_subscribed != 1) {
  50. $this->is_subscribed = 2;
  51. }
  52. }
  53. public function validateEmail($attribute, $params)
  54. {
  55. if ($this->id) {
  56. $one = Customer::find()
  57. ->where(' id != :id AND email = :email ', [':id'=>$this->id, ':email'=>$this->email])
  58. ->one();
  59. if ($one['id']) {
  60. $this->addError($attribute, 'this email is exist!');
  61. }
  62. } else {
  63. $one = Customer::find()
  64. ->where('email = :email', [':email' => $this->email])
  65. ->one();
  66. if ($one['id']) {
  67. $this->addError($attribute, 'this email is exist!');
  68. }
  69. }
  70. }
  71. public function setPassword($password)
  72. {
  73. if ($this->password) {
  74. $this->password_hash = \Yii::$app->security->generatePasswordHash($password, 6);
  75. $this->password = '';
  76. }
  77. }
  78. // 重写保存方法
  79. public function save($runValidation = true, $attributeNames = null)
  80. {
  81. // 如果password为空,则return
  82. if (!$this->password) {
  83. return false;
  84. }
  85. // 如果auth_key为空,则重置
  86. if (!$this->auth_key) {
  87. $this->generateAuthKey();
  88. }
  89. // 如果access_token为空,则重置
  90. if (!$this->access_token) {
  91. $this->generateAccessToken();
  92. }
  93. // 设置password
  94. $this->setPassword($this->password);
  95. return parent::save($runValidation, $attributeNames);
  96. }
  97. }