UserModel.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\web\Response;
  6. use app\models\entity\Projecttypes;
  7. use app\models\entity\AccountsJobtypes;
  8. use app\models\entity\Accounts;
  9. /**
  10. * ContactForm is the model behind the contact form.
  11. */
  12. class UserModel extends Model
  13. {
  14. public $employeesArray = [];
  15. public $company_id = 3;
  16. public function getListUserToAdd() {
  17. $url = '62.141.88.61:8085/Thingworx/Things/2050RepairsLibrary/Services/users';
  18. $curl = curl_init();
  19. curl_setopt($curl, CURLOPT_URL, $url);
  20. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json","AppKey: 6a7bdbaa-6bc1-4159-a2f4-07a13c8907d1"));
  21. curl_setopt($curl, CURLOPT_TIMEOUT, 20);
  22. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  23. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  24. curl_setopt($curl, CURLOPT_POST, 1);
  25. curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonEncoded);
  26. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  27. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  28. $result = curl_exec($curl);
  29. //$info = curl_getinfo($curl, CURLINFO_HEADER_OUT);
  30. $res = json_decode($result,true);
  31. $listEmployee = json_decode($res['rows'][0]["result"],true);
  32. $i = 0;
  33. $employeesArray = [];
  34. foreach($listEmployee['data'] as $employee){
  35. if( 2 == $i){
  36. break;
  37. }
  38. $employeesArray[] = $employee;
  39. //echo "<pre>";var_dump($employee); echo "</pre>";
  40. $i++;
  41. }
  42. curl_close($curl);
  43. $this->employeesArray = $employeesArray;
  44. return true;
  45. }
  46. public function createAccount() {
  47. foreach ($this->employeesArray as $user) {
  48. $accountsEntity = new Accounts();
  49. $accountsEntity->id = $user['id'];
  50. $accountsEntity->name = $user['name'];
  51. $accountsEntity->cmdlevel = 1;
  52. $accountsEntity->phone = '+7(916)9254713';
  53. $accountsEntity->password = md5('123');
  54. $accountsEntity->login = 'login'.rand(0,999);
  55. $accountsEntity->company_id = $this->company_id;
  56. if($accountsEntity->save()){
  57. $accountsJobtypesEntity = new AccountsJobtypes();
  58. $accountsJobtypesEntity->accounts_id = $accountsEntity->id;
  59. $accountsJobtypesEntity->jobtypes_id = 64;
  60. $accountsJobtypesEntity->save();
  61. } else {
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. }