201 lines
6.9 KiB
PHP
201 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Maatwebsite\Excel\Concerns\ToModel;
|
|
use Maatwebsite\Excel\Concerns\ToArray;
|
|
use Maatwebsite\Excel\Concerns\ToCollection;
|
|
use Illuminate\Support\Collection;
|
|
use App\Models\ProfileCourtship;
|
|
use App\Models\ProfileMarriage;
|
|
use App\Models\GoodMatch;
|
|
use App\Utils\Http;
|
|
|
|
class UsersImport implements ToArray
|
|
{
|
|
/**
|
|
* @param array $row
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Model|null
|
|
*/
|
|
public function model(array $row)
|
|
{
|
|
return new User([
|
|
'name' => $row[0],
|
|
'mobile' => $row[1],
|
|
'email' => $row[1].'@ufutx.com',
|
|
'password' => Hash::make($row[2]),
|
|
]);
|
|
}
|
|
|
|
public function collection(Collection $rows)
|
|
{
|
|
foreach ($rows as $row)
|
|
{
|
|
User::create([
|
|
'name' => $row[0],
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function Array(Array $tables)
|
|
{
|
|
foreach ($tables as $key => $table) {
|
|
if ($key == 0) {
|
|
continue;
|
|
}
|
|
$old_id = $table[0];
|
|
$name = $table[1];
|
|
$nickname = $table[2];
|
|
if ($name == '未填写') {
|
|
$name = $nickname;
|
|
}
|
|
if ($table[3] == '女') {
|
|
$sex = 2;
|
|
}elseif ($table[3] == '男') {
|
|
$sex = 1;
|
|
}else{
|
|
$sex = 0;
|
|
}
|
|
if ($table[4] == '相亲') {
|
|
$type = "single";
|
|
}else{
|
|
$type = 'marriage';
|
|
}
|
|
$approve = $table[5];
|
|
if ($approve == '已实名') {
|
|
$is_approved = 1;
|
|
}else {
|
|
$is_approved = 0;
|
|
}
|
|
$mobile = $table[6];
|
|
//创建用户
|
|
$user = User::where('mobile', $mobile)->first();
|
|
if (empty($user)) {
|
|
$user = new User();
|
|
}else{
|
|
continue;
|
|
}
|
|
//推荐人
|
|
$recommend_name = $table[7];
|
|
$from_openid = '';
|
|
if ($recommend_name == '金辉弟兄') {
|
|
$from_openid = 'oVEaG5OMyK7ffbT8IhD1T4rCJVmk';
|
|
}
|
|
$resident = explode(' ', $table[8]);
|
|
$country = '';
|
|
if ($resident[0] == '中国') {
|
|
$country = isset($resident[0])?$resident[0]:'';
|
|
$resident_province = isset($resident[1])?$resident[1]:'';
|
|
$resident_city = isset($resident[2])?$resident[2]:'';
|
|
$resident_dist = isset($resident[3])?$resident[3]:'';
|
|
}elseif ($resident[0] == '未填写') {
|
|
$resident_province = '';
|
|
$resident_city = '';
|
|
$resident_dist = '';
|
|
}else{
|
|
$resident_province = isset($resident[0])?$resident[0]:'';
|
|
$resident_city = isset($resident[1])?$resident[1]:'';
|
|
$resident_dist = isset($resident[2])?$resident[2]:'';
|
|
}
|
|
|
|
$address = explode(' ', $table[9]);
|
|
if ($address[0] == '中国') {
|
|
$province = isset($address[1])?$address[1]:'';
|
|
$city = isset($address[2])?$address[2]:'';
|
|
$dist = isset($address[3])?$address[3]:'';
|
|
}elseif ($resident[0] == '未填写') {
|
|
$province = '';
|
|
$city = '';
|
|
$dist = '';
|
|
}else{
|
|
$province = isset($address[0])?$address[0]:'';
|
|
$city = isset($address[1])?$address[1]:'';
|
|
$dist = isset($address[2])?$address[2]:'';
|
|
}
|
|
$location_longitude = null;
|
|
$location_latitude = null;
|
|
$address_str = $province.$city.$dist;
|
|
if ($address_str) {
|
|
$location = $this->getLocation($address_str);
|
|
if (count($location)) {
|
|
$location_longitude = $location['lng'];
|
|
$location_latitude = $location['lat'];
|
|
}
|
|
}
|
|
$birthday = $table[10];
|
|
$user->name = $name;
|
|
$user->mobile = $mobile;
|
|
$user->email = $mobile.'@ufutx.com';
|
|
$user->password = '';
|
|
$user->type = $type;
|
|
$user->from_openid = $from_openid;
|
|
$user->is_approved = $is_approved;
|
|
if ($location_longitude && $location_latitude) {
|
|
$user->location_longitude = $location_longitude;
|
|
$user->location_latitude = $location_latitude;
|
|
}
|
|
$user->save();
|
|
|
|
if ($type == 'single') {
|
|
//创建单身信息
|
|
$courtship = ProfileCourtship::where('user_id', $user->id)->first();
|
|
if (empty($courtship)) {
|
|
$courtship = new ProfileCourtship();
|
|
$courtship->user_id = $user->id;
|
|
}
|
|
$courtship->birthday = $birthday;
|
|
$courtship->sex = $sex;
|
|
$courtship->province = $province;
|
|
$courtship->city = $city;
|
|
$courtship->dist = $dist;
|
|
$courtship->resident_province = $resident_province;
|
|
$courtship->resident_city = $resident_city;
|
|
$courtship->resident_dist = $resident_dist;
|
|
$courtship->resident_type = '';
|
|
$courtship->country = $country;
|
|
$courtship->save();
|
|
$good_match = GoodMatch::where('user_id', $user->id)->first();
|
|
if (empty($good_match)) {
|
|
//创建佳偶
|
|
$good_match = new GoodMatch();
|
|
$good_match->user_id = $user->id;
|
|
}
|
|
$good_match->sex = $sex;
|
|
$good_match->status = 0;
|
|
$good_match->save();
|
|
}else{
|
|
$marriage = ProfileMarriage::where('user_id', $user->id)->first();
|
|
if (empty($marriage)) {
|
|
//创建介绍人信息
|
|
$marriage = new ProfileMarriage();
|
|
$marriage->user_id = $user->id;
|
|
}
|
|
$marriage->birthday = $birthday;
|
|
$marriage->sex = $sex;
|
|
$marriage->save();
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* 地址转为经纬度
|
|
* @param Request $request [description]
|
|
* @param string $address [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function getLocation($address='')
|
|
{
|
|
if (empty($address)) {
|
|
return $this->failure('没有地址');
|
|
}
|
|
$url = "https://apis.map.qq.com/ws/geocoder/v1/?address=".$address.'&key=P43BZ-QSDCP-BMHDP-V3BTR-3EL45-KOFJL';
|
|
$result = json_decode(Http::http($url, [], 'GET'), true);
|
|
$location = isset($result['result']['location'])?$result['result']['location']:[];
|
|
return $location;
|
|
}
|
|
}
|