RealAuthDetail.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace addons\RealAuth\common\models;
  3. use common\models\base\BaseActiveRecord;
  4. use common\enums\AddonsEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\common\Region;
  7. use common\models\user\Town;
  8. use Yii;
  9. /**
  10. * This is the model class for table "{{%addons_real_auth_detail}}".
  11. *
  12. * @property int $id
  13. * @property int $mall_id
  14. * @property int $user_id 用户ID
  15. * @property int $auth_type 实名认证类型:0未知,1 个人 2 企业
  16. * @property int $created_at 添加时间戳
  17. * @property int $updated_at 更新时间戳
  18. * @property string $detail 实名认证资料,json格式
  19. */
  20. class RealAuthDetail extends BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return "{{%addons_real_auth_detail}}";
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [["mall_id", "user_id","auth_id", "auth_type", "created_at", "updated_at","check_at","check_status"], "integer"],
  36. [["detail"], "safe"],
  37. [["remark"], "string"],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. "id" => "ID",
  47. "mall_id" => "Mall ID",
  48. "user_id" => "User ID",
  49. "auth_type" => "Auth Type",
  50. "created_at" => "Created At",
  51. "updated_at" => "Updated At",
  52. "detail" => "Detail",
  53. "auth_id"=> "Auth Id",
  54. "check_at" => "Check At",
  55. "check_status" => "Check Status",
  56. "remark" => 'Remark',
  57. ];
  58. }
  59. public function getAuth()
  60. {
  61. return $this->hasOne(RealAuthUser::class, ['id' => 'auth_id']);
  62. }
  63. /**
  64. * @param array $data
  65. * @return int
  66. * @throws \Exception
  67. */
  68. public static function setData(array $data)
  69. {
  70. $data = self::paramsValidate($data);
  71. if(isset($data["id"]) && $data["id"] > 0 ){
  72. $model = self::findOne([ "id" => $data["id"]]);
  73. if(!$model){
  74. throw new \Exception("记录不存在");
  75. }
  76. }else{
  77. //认证详情每个用户只有一条记录,当个人认证升级为企业认证,个人认证记录不存在
  78. $model = self::findOne(["mall_id" => $data["mall_id"], "user_id" => $data["user_id"],"status"=>StatusEnum::ENABLED,'auth_type'=>$data['auth_type']]);
  79. if(!$model){
  80. $model = new self();
  81. }
  82. }
  83. $model->attributes = $data;
  84. if (!$model->save()) {
  85. throw new \Exception($model->getErrorMessage());
  86. }
  87. return $model->id;
  88. }
  89. public static function paramsValidate($data){
  90. $mall_id = $data["mall_id"];
  91. $params = $data["detail"];
  92. $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_REAL_AUTH, "basic");
  93. if(!empty($config["setting_status"])){
  94. if(empty($params["province_id"])&&!empty($params["province"])){
  95. $region = Region::findOne(["name"=>$params["province"],"level"=>1]);
  96. if(empty($region)) throw new \Exception("查无该省份");
  97. $params["province_id"] = $region["id"];
  98. }
  99. if(empty($params["province_id"]) || empty($params["province"])){
  100. throw new \Exception("省份不能为空");
  101. }
  102. if(empty($params["city_id"])){
  103. if(empty($params["city"])) throw new \Exception("请填写城市");
  104. $region = Region::findOne(["name"=>$params["city"],"level"=>2]);
  105. if(empty($region)) throw new \Exception("查无该城市");
  106. $parent_region = Region::findOne(["id"=> $region["parent_id"]]);
  107. $params["province"] = $parent_region["name"];
  108. $params["province_id"] = $region["parent_id"];
  109. $params["city_id"] = $region["id"];
  110. }
  111. if(empty($params["city_id"])){
  112. throw new \Exception("城市不能为空");
  113. }
  114. if(empty($params["district_id"])&&!empty($params["district"])){
  115. $region = Region::findOne(["name"=>$params["district"],"level"=>3,"parent_id"=>$params["city_id"]]);
  116. if(empty($region)) throw new \Exception("查无该区");
  117. $params["district_id"] = $region["id"];
  118. }
  119. if(empty($params["district_id"]) || empty($params["district"])){
  120. throw new \Exception("区不能为空");
  121. }
  122. $town_exist = Town::findOne(['district_id'=>$params["district_id"]]);
  123. if(!empty($town_exist)){
  124. if(empty($params["town_id"]) || empty($params["town"])){
  125. throw new \Exception("乡/镇/街道不能为空");
  126. }
  127. }
  128. //企业认证
  129. if($data["auth_type"] == 2){
  130. if(empty($params["mobile"])){
  131. throw new \Exception("联系电话不能为空");
  132. }
  133. if(empty($params["business_name"])){
  134. throw new \Exception("企业名称不能为空");
  135. }
  136. if(empty($params["business_code"])){
  137. throw new \Exception("社会信用代码不能为空");
  138. }
  139. if(empty($params["id_card_front"])){
  140. throw new \Exception("请上传法人身份证人像面");
  141. }
  142. if(empty($params["id_card_back"])){
  143. throw new \Exception("请上传法人身份证国徽面");
  144. }
  145. if(empty($params["business_license_url"])){
  146. throw new \Exception("请上传营业执照正面");
  147. }
  148. if(empty($params["business_license_held"])){
  149. throw new \Exception("请上传上传手持营业执照照片");
  150. }
  151. }
  152. }
  153. // $data["detail"] = json_encode($params,JSON_UNESCAPED_UNICODE);
  154. return $data;
  155. }
  156. }