RealAuthUser.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace addons\RealAuth\common\models;
  3. use common\components\export\CsvTool;
  4. use common\enums\DownloadEnum;
  5. use common\models\base\User;
  6. use common\models\common\Download;
  7. use Yii;
  8. use yii\db\ActiveRecord;
  9. /**
  10. * This is the model class for table "{{%addons_real_auth_user}}".
  11. * @property int $id
  12. * @property int $mall_id
  13. * @property int $user_id
  14. * @property int $id_type
  15. * @property string $id_no
  16. * @property int $auth
  17. * @property int $created_at
  18. * @property int $updated_at
  19. * @property string $real_name
  20. * @property string $auth_result
  21. */
  22. class RealAuthUser extends \common\models\base\BaseActiveRecord
  23. {
  24. //认证类型 1 个人 2 企业
  25. const USER = 1;
  26. const BUSINESS = 2;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%addons_real_auth_user}}';
  33. }
  34. public static function fieldsList($field = null)
  35. {
  36. $fields = [
  37. 'user_id' => '会员ID',
  38. 'nickname' => '昵称',
  39. 'username' => '用户名',
  40. 'mobile' => '手机号',
  41. 'real_name' => '姓名',
  42. 'id_no' => '证件号',
  43. 'created_at' => '认证时间',
  44. ];
  45. if ($field === null) return $fields;
  46. $temp = [];
  47. foreach ($field as $val) {
  48. if (isset($fields[$val])) $temp[$val] = $fields[$val];
  49. }
  50. return $temp;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function rules()
  56. {
  57. return [
  58. [['mall_id', 'user_id', 'id_type', 'auth', 'created_at','updated_at','auth_type'], 'integer'],
  59. [['real_name','id_no'], 'string', 'max' => 64],
  60. [['auth_result','business_code'], 'string'],
  61. ];
  62. }
  63. public static function getUser($mall_id,$user_id,$auth_type=0)
  64. {
  65. $query = self::find()->where(['mall_id' => $mall_id,'user_id' =>$user_id]);
  66. if($auth_type){
  67. $query->andWhere(['=','auth_type',$auth_type]);
  68. }
  69. return $query->one();
  70. }
  71. public static function getUserByInfo($mall_id, $user_id, $real_name, $id_no, $auth_type=0)
  72. {
  73. $query = self::find()->where(['mall_id' => $mall_id,'user_id' =>$user_id, 'real_name' => trim($real_name), 'id_no' => trim($id_no)]);
  74. if($auth_type){
  75. $query->andWhere(['=','auth_type',$auth_type]);
  76. }
  77. return $query->one();
  78. }
  79. /**
  80. * @param array $data
  81. * @return int
  82. * @throws \Exception
  83. */
  84. public static function setData(array $data)
  85. {
  86. if(isset($data['id']) && $data['id'] > 0 ){
  87. $model = self::findOne([ 'id' => $data['id']]);
  88. if(!$model){
  89. throw new \Exception('记录不存在');
  90. }
  91. }else{
  92. $model = self::findOne(['mall_id' => $data['mall_id'], 'user_id' => $data['user_id']]);
  93. if(!$model){
  94. $model = new self();
  95. }
  96. }
  97. $model->attributes = $data;
  98. if (!$model->save()) {
  99. throw new \Exception($model->getErrorMessage());
  100. }
  101. return $model->id;
  102. }
  103. public static function exportHandle(array $data)
  104. {
  105. date_default_timezone_set('Asia/Shanghai');
  106. $download_id = $data['download_id'] ?? 0;
  107. $download = Download::findOne(['id' => $download_id]);
  108. if ($download) {
  109. try {
  110. $params = json_decode($download->params, true);
  111. $filename = $download->name;
  112. $type = $download->type;
  113. $model = RealAuthUser::find();
  114. $model->where(['mall_id' => $params['mall_id']]);
  115. if (!empty($params['user_id'])) $model->andWhere(['=', 'user_id', $params['user_id']]);
  116. if (!empty($params['real_name'])) $model->andWhere(['=', 'real_name', $params['real_name']]);
  117. if (!empty($params['id_no'])) $model->andWhere(['=', 'id_no', $params['id_no']]);
  118. if (!empty($params['data'])) {
  119. list($start, $end) = $params['data'];
  120. $end .= ' 23:59:59';
  121. $model->andWhere(['between', 'created_at', strtotime($start), strtotime($end)]);
  122. }
  123. $fields = $params['fields'];
  124. $fields_arr = self::fieldsList();
  125. $have_select_field_arr = [];
  126. foreach ($fields as $value) {
  127. if (isset($fields_arr[$value])) $have_select_field_arr[] = $fields_arr[$value];
  128. }
  129. $csv = new CsvTool();
  130. $csv->filename = $filename;
  131. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  132. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  133. $csv->header = $have_select_field_arr;
  134. $i = 1;
  135. $page_size = 1000;
  136. while ($list = $model
  137. ->offset(($i - 1) * $page_size)->limit($page_size)->orderBy('id asc')->all()) {
  138. $user_ids = array_column($list, 'user_id');
  139. $users = User::lists(['where' => [['id' => $user_ids]], 'index' => 'id']);
  140. $csv->data = array_map(function ($real_auth_user) use ($fields, $users) {
  141. $row = [];
  142. foreach ($fields as $field) {
  143. switch (true) {
  144. case (in_array($field, ['created_at'])):
  145. $row[] = !empty($real_auth_user[$field]) ? date('Y-m-d H:i:s', $real_auth_user[$field]) : '';
  146. break;
  147. case (in_array($field, ['id_no'])):
  148. $row[] = !empty($real_auth_user[$field]) ? $real_auth_user[$field] . "\t" : '';
  149. break;
  150. case (in_array($field, ['nickname', 'username', 'mobile'])):
  151. if (isset($users[$real_auth_user['user_id']])) {
  152. $row[] = $users[$real_auth_user['user_id']][$field] . "\t";
  153. } else {
  154. $row[] = '';
  155. }
  156. break;
  157. default:
  158. $row[] = !empty($real_auth_user[$field]) ? $real_auth_user[$field] : '';
  159. }
  160. }
  161. return $row;
  162. }, $list);
  163. $csv->export();
  164. $i++;
  165. }
  166. $csv->updateDown($download, $params['mall_id']);
  167. return true;
  168. } catch (\Exception $e) {
  169. $download->status = 3;
  170. $res = $download->save();
  171. \Yii::error($e->getMessage() . $e->getFile() . $e->getLine());
  172. self::setStaticError($e->getMessage());
  173. return $e->getMessage();
  174. }
  175. }
  176. }
  177. }