AccountDetail.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. *------Create thems Model------
  4. *------SCWPHP Version 1.0.0------
  5. *------Dev Model Jions------
  6. *------Create Time 2017-06-12 05:08:18------
  7. */
  8. namespace App\Api\Model;
  9. use \System\Model;
  10. use Biz\Account\AccountManager;
  11. class AccountDetail extends Model {
  12. protected $table = 'account_detailed';
  13. /**
  14. * 修改用户基本信息
  15. *
  16. * @access public
  17. * @return String
  18. */
  19. public function updateUserInfo()
  20. {
  21. // 获取用户信息
  22. $accountManagerClass = new AccountManager;
  23. $userInfo = $accountManagerClass -> getCurrentUser();
  24. if (empty($userInfo['identity'])) {
  25. Render([], '2001', lang('Common','Api') -> get('user does login'));
  26. }
  27. $updateWhere['account_identity'] = $userInfo['identity'];
  28. $updateData = [];
  29. // 提取修改数据
  30. if (!empty($_POST['email'])) {
  31. $updateData['email'] = $_POST['email'];
  32. }
  33. if (!empty($_POST['phone'])) {
  34. $updateData['phone'] = $_POST['phone'];
  35. }
  36. if (!empty($_POST['img_id'])) {
  37. $updateData['img_id'] = $_POST['img_id'];
  38. $updateData['img_url'] = '';
  39. $oldImg = lm('Account_detailed', 'commons') -> accountDetailed(['img_url'], $updateWhere);
  40. // 删除以前的文件
  41. unlink(ROOT_PATH . $oldImg -> img_url);
  42. }
  43. if (!empty($_POST['name'])) {
  44. $updateData['name'] = $_POST['name'];
  45. }
  46. if (empty($updateData)) {
  47. Render([], '4027', lang('Common','Api') -> get('please enter the modification'));
  48. }
  49. return lm('Account_detailed', 'commons') -> updateDetailed($updateWhere, $updateData);
  50. }
  51. /**
  52. * 上传用户基本信息头像
  53. *
  54. * @access public
  55. * @return String
  56. */
  57. public function updateHeader()
  58. {
  59. // 获取用户信息
  60. $accountManagerClass = new AccountManager;
  61. $userInfo = $accountManagerClass -> getCurrentUser();
  62. if (empty($userInfo['identity'])) {
  63. Render([], '2001', lang('Common','Api') -> get('user does login'));
  64. }
  65. // 规定上传格式
  66. $allowedType = ["png", "jpg", "gif"];
  67. $allowedExtensions = ["image/jpg","image/jpeg","image/png","image/pjpeg","image/gif","image/bmp","image/x-png"];
  68. // 最大为 100 Kb
  69. $maxSize = 102400;
  70. // 获取上传文件
  71. $img = $_FILES['img'];
  72. // 获取上传文件格式并转换为小写
  73. $imgType = strtolower(substr($img['name'],strrpos($img['name'],".") + 1));
  74. // 判断格式是否符合要求
  75. if (!in_array($imgType, $allowedType) || !in_array(strtolower($img['type']), $allowedExtensions)) {
  76. Render([], '50040', lang('Common','Api') -> get('Picture files are not supported'));
  77. }
  78. if ($img['size'] > $maxSize) {
  79. Render([], '50041', lang('Common','Api') -> get('Picture too large'));
  80. }
  81. // 修改文件名
  82. $imgName = strtotime('now') + rand(10000, 99999);
  83. $filePath = ROOT_PATH . '/Public/img/header/';
  84. $imgPath = '/Public/img/header/' . $imgName . ".png";
  85. if (move_uploaded_file($img['tmp_name'], $filePath . $imgName . ".png")) {
  86. $updateWhere['account_identity'] = $userInfo['identity'];
  87. $oldImg = lm('Account_detailed', 'commons') -> accountDetailed(['img_url'], $updateWhere);
  88. // 删除以前的文件
  89. unlink(ROOT_PATH . $oldImg -> img_url);
  90. lm('Account_detailed', 'commons') -> updateDetailed($updateWhere, ['img_url' => $imgPath]);
  91. } else {
  92. Render([], '50042', lang('Common','Api') -> get('Upload failure'));
  93. }
  94. }
  95. }