WorkOrderController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 芸众网
  5. * Date: 2019/7/1
  6. * Time: 13:43
  7. */
  8. namespace app\backend\modules\setting\controllers;
  9. use app\common\components\BaseController;
  10. use app\common\facades\Setting;
  11. use app\common\helpers\ImageHelper;
  12. use app\common\helpers\Url;
  13. use app\common\models\WebSiteInfo;
  14. use Illuminate\Support\Facades\Storage;
  15. use Ixudra\Curl\Facades\Curl;
  16. class WorkOrderController extends BaseController
  17. {
  18. // 工单管理
  19. public function index()
  20. {
  21. $key = Setting::get('shop.key')['key'];
  22. $secret = Setting::get('shop.key')['secret'];
  23. $post_data = [
  24. 'page' => 1,
  25. 'post_data' => [
  26. 'category_id' => 0,
  27. 'status' => 0,
  28. 'work_order_sn' => 0,
  29. 'has_time_limit' => 0,
  30. 'start_time' => 0,
  31. 'end_time' => 0,
  32. 'show_all' => 0,
  33. 'domain' => $_SERVER['HTTP_HOST'],
  34. ],
  35. ];
  36. $url = config('auto-update.workOrderUrl') . '/api/work-order/get-work-order-list/';
  37. $data = Curl::to($url)
  38. ->withHeader("Authorization: Basic " . base64_encode("{$key}:{$secret}"))
  39. ->withData($post_data)
  40. ->asJsonResponse(true)
  41. ->post();
  42. if ($data && $data['result'] == 1) {
  43. return view('setting.work-order.list', [
  44. 'data' => json_encode($data['data']),
  45. 'category_list' => json_encode($this->category()),
  46. 'status_list' => json_encode($this->status()),
  47. ])->render();
  48. } else {
  49. return view('setting.work-order.list', [
  50. 'data' => json_encode([]),
  51. 'category_list' => json_encode($this->category()),
  52. 'status_list' => json_encode($this->status()),
  53. ])->render();
  54. }
  55. }
  56. /**
  57. * 工单检索
  58. * @return \Illuminate\Http\JsonResponse
  59. */
  60. public function search()
  61. {
  62. $data = request()->input('data');
  63. $post_data = [
  64. 'page' => $data['page'],
  65. 'post_data' => [
  66. 'category_id' => $data['category_id'],
  67. 'status' => $data['status'],
  68. 'work_order_sn' => $data['work_order_sn'],
  69. 'has_time_limit' => $data['has_time_limit'],
  70. 'start_time' => $data['start_time'] / 1000,
  71. 'end_time' => $data['end_time'] / 1000,
  72. 'show_all' => $data['show_all'],
  73. 'domain' => $_SERVER['HTTP_HOST'],
  74. ],
  75. ];
  76. $key = Setting::get('shop.key')['key'];
  77. $secret = Setting::get('shop.key')['secret'];
  78. $url = config('auto-update.workOrderUrl') . '/api/work-order/get-work-order-list/';
  79. $data = Curl::to($url)
  80. ->withHeader("Authorization: Basic " . base64_encode("{$key}:{$secret}"))
  81. ->withData($post_data)
  82. ->asJsonResponse(true)
  83. ->post();
  84. if ($data && $data['result'] == 1) {
  85. return $this->successJson('成功', $data['data']);
  86. } else {
  87. return $this->errorJson('失败');
  88. }
  89. }
  90. private function getFileType()
  91. {
  92. return ['docx','ai','avi','txt','jpg','png','jpeg','bmp','cdr','doc','eps','gif','html','mp3','mp4','pdf','ppt','pr','psd','rar','svg','gif','xlsx','zip'];
  93. }
  94. /**
  95. * 上传
  96. * @return \Illuminate\Http\JsonResponse
  97. */
  98. public function uploadFile()
  99. {
  100. $file = request()->file('file');
  101. if (!$file) {
  102. return $this->errorJson('请传入正确参数.');
  103. }
  104. if ($file->isValid()) {
  105. $ext = $file->getClientOriginalExtension();
  106. if(!in_array($ext,$this->getFileType())){
  107. return $this->errorJson('请检查文件后缀是否支持');exit();
  108. }
  109. // 获取文件相关信息
  110. $originalName = $file->getClientOriginalName(); // 文件原名
  111. $realPath = $file->getRealPath(); //临时文件的绝对路径
  112. $newOriginalName = md5($originalName . str_random(6)) . '.' . $ext;
  113. Storage::disk('image')->put($newOriginalName, file_get_contents($realPath));
  114. if (config('APP_Framework') == 'platform') {
  115. $attachment = 'static/upload/';
  116. } else {
  117. $attachment = 'attachment/';
  118. }
  119. return $this->successJson('上传成功', [
  120. 'thumb' => \Storage::disk('image')->url($newOriginalName),
  121. 'thumb_url' => ImageHelper::getImageUrl($attachment . substr(\Storage::disk('image')->url($newOriginalName), strripos(\Storage::disk('image')->url($newOriginalName), "image"))),
  122. ]);
  123. }
  124. return $this->errorJson($file->getErrorMessage());
  125. }
  126. /**
  127. * base64图片上传
  128. * @return \Illuminate\Http\JsonResponse
  129. */
  130. public function base64Upload()
  131. {
  132. $base_img = request()->input('file');
  133. $base_img = str_replace('data:image/jpg;base64,', '', $base_img);
  134. $newOriginalName = time() . rand(100, 999) . '.png';
  135. Storage::disk('image')->put($newOriginalName, file_get_contents($base_img));
  136. if (config('APP_Framework') == 'platform') {
  137. $attachment = 'static/upload/';
  138. } else {
  139. $attachment = 'attachment/';
  140. }
  141. return $this->successJson('上传成功', [
  142. 'thumb' => \Storage::disk('image')->url($newOriginalName),
  143. 'thumb_url' => ImageHelper::getImageUrl($attachment . substr(\Storage::disk('image')->url($newOriginalName), strripos(\Storage::disk('image')->url($newOriginalName), "image"))),
  144. ]);
  145. }
  146. /**
  147. * 详情
  148. * @return mixed|string
  149. */
  150. public function details()
  151. {
  152. $id = request()->input('id');
  153. $key = Setting::get('shop.key')['key'];
  154. $secret = Setting::get('shop.key')['secret'];
  155. $data = Curl::to(config('auto-update.workOrderUrl') . '/work-order/details/' . $id)
  156. ->withHeader("Authorization: Basic " . base64_encode("{$key}:{$secret}"))
  157. ->asJsonResponse(true)
  158. ->get();
  159. if ($data && $data['result'] == 1) {
  160. foreach ($data['data']['has_many_comment'] as $key => $value) {
  161. $data['data']['has_many_comment'][$key]['thumb_url'] = json_decode($value['thumb_url']);
  162. }
  163. $data['data']['thumb_url'] = json_decode($data['data']['thumb_url']);
  164. return view('setting.work-order.details', ['data' => json_encode($data['data'])])->render();
  165. } else {
  166. return $this->message('ID不存在', Url::absoluteWeb('setting.work-order.index'));
  167. }
  168. }
  169. /**
  170. * 评论接口
  171. * @return \Illuminate\Http\JsonResponse
  172. */
  173. public function comment()
  174. {
  175. $postData = request()->input();
  176. $this->validateCommentParam();
  177. $data = [
  178. 'work_order_id' => $postData['work_order_id'],
  179. 'content' => $postData['content'],
  180. 'thumb_url' => json_encode($postData['thumb_url']),
  181. 'work_order' => 1,
  182. 'domain' => $_SERVER['HTTP_HOST'],
  183. ];
  184. $key = Setting::get('shop.key')['key'];
  185. $secret = Setting::get('shop.key')['secret'];
  186. $data = Curl::to(config('auto-update.workOrderUrl') . '/work-order/comment')
  187. ->withHeader("Authorization: Basic " . base64_encode("{$key}:{$secret}"))
  188. ->withData($data)
  189. ->asJsonResponse(true)
  190. ->post();
  191. if (!$data) {
  192. $this->errorJson('远端服务器异常');
  193. }
  194. if ($data['result'] == 0) {
  195. return $this->errorJson('提交失败');
  196. } else {
  197. return $this->successJson('提交成功');
  198. }
  199. }
  200. /**
  201. * 页面
  202. * @return string
  203. */
  204. public function storePage()
  205. {
  206. return view('setting.work-order.store-page', [
  207. 'site_url' => json_encode($_SERVER['HTTP_HOST']),
  208. 'category_list' => json_encode($this->category()),
  209. 'first_list' => json_encode($this->firstList()),
  210. ])->render();
  211. }
  212. /**
  213. * 获取秘钥
  214. */
  215. private function getKey()
  216. {
  217. $key = Setting::get('shop.key')['key'];
  218. $secret = Setting::get('shop.key')['secret'];
  219. $data = Curl::to(config('auto-update.workOrderUrl') . '/work-order/get-user-key')
  220. ->withHeader("Authorization: Basic " . base64_encode("{$key}:{$secret}"))
  221. ->withData(['domain' => $_SERVER['HTTP_HOST'], 'work_order' => 1])
  222. ->asJsonResponse(true)
  223. ->post();
  224. if ($data['result'] == 1 && $data) {
  225. return $data['data'];
  226. } else {
  227. return false;
  228. }
  229. }
  230. /**
  231. * 第一次提交数据
  232. * @return array
  233. */
  234. private function firstList()
  235. {
  236. $webSiteInfo = WebSiteInfo::where('website_url', $_SERVER['HTTP_HOST'])->first();
  237. $key = $this->getKey();
  238. if ($key == false) {
  239. return [
  240. "website_url" => $_SERVER['HTTP_HOST'],
  241. ];
  242. }
  243. return [
  244. "id" => $webSiteInfo->id,
  245. "uniacid" => $webSiteInfo->uniacid,
  246. "website_url" => $webSiteInfo->website_url,
  247. "founder_account" => $this->decryption($webSiteInfo->founder_account, $key),
  248. "founder_password" => $this->decryption($webSiteInfo->founder_password, $key),
  249. "server_ip" => $this->decryption($webSiteInfo->server_ip, $key),
  250. "root_password" => $this->decryption($webSiteInfo->root_password, $key),
  251. "root_username" => $this->decryption($webSiteInfo->root_username, $key),
  252. "ssh_port" => $this->decryption($webSiteInfo->ssh_port, $key),
  253. "database_address" => $this->decryption($webSiteInfo->database_address, $key),
  254. "database_username" => $this->decryption($webSiteInfo->database_username, $key),
  255. "database_password" => $this->decryption($webSiteInfo->database_password, $key),
  256. "root_directory" => $this->decryption($webSiteInfo->root_directory, $key),
  257. "qq" => $this->decryption($webSiteInfo->qq, $key),
  258. "mobile" => $this->decryption($webSiteInfo->mobile, $key),
  259. ];
  260. }
  261. /**
  262. * 解密
  263. * @param $string
  264. * @param $key
  265. * @return string
  266. */
  267. private function encipherment($string, $key)
  268. {
  269. return openssl_encrypt($string, 'DES-ECB', $key, 0);
  270. }
  271. /**
  272. * 加密
  273. * @param $string
  274. * @param $key
  275. * @return string
  276. */
  277. private function decryption($string, $key)
  278. {
  279. return openssl_decrypt($string, 'DES-ECB', $key, 0);
  280. }
  281. /**
  282. * 生成获取秘钥
  283. * @return bool
  284. */
  285. private function getEncryptionKey()
  286. {
  287. $key = Setting::get('shop.key')['key'];
  288. $secret = Setting::get('shop.key')['secret'];
  289. $data = Curl::to(config('auto-update.workOrderUrl') . '/work-order/get-key')
  290. ->withHeader("Authorization: Basic " . base64_encode("{$key}:{$secret}"))
  291. ->withData(['domain' => $_SERVER['HTTP_HOST'], 'work_order' => 1])
  292. ->asJsonResponse(true)
  293. ->post();
  294. if ($data['result'] == 1 && $data) {
  295. return $data['data'];
  296. } else {
  297. return false;
  298. }
  299. }
  300. /**
  301. * 工单提交接口
  302. * @return \Illuminate\Http\JsonResponse
  303. */
  304. public function store()
  305. {
  306. $encryptionKey = $this->getEncryptionKey();
  307. if ($encryptionKey == false) {
  308. $this->errorJson('获取秘钥失败');
  309. }
  310. $postData = request()->input('data');
  311. $this->validateParam();
  312. $data = [
  313. 'work_order' => '1',
  314. 'domain' => $_SERVER['HTTP_HOST'],
  315. 'category_id' => $postData['category_id'],
  316. 'question_title' => $postData['question_title'],
  317. 'question_describe' => $postData['question_describe'],
  318. 'thumb_url' => json_encode($postData['thumb_url']),
  319. 'user_data' => [
  320. 'website_url' => $_SERVER['HTTP_HOST'],
  321. 'founder_account' => $this->encipherment($postData['first_list']['founder_account'], $encryptionKey),
  322. 'founder_password' => $this->encipherment($postData['first_list']['founder_password'], $encryptionKey),
  323. 'server_ip' => $this->encipherment($postData['first_list']['server_ip'], $encryptionKey),
  324. 'root_username' => $this->encipherment($postData['first_list']['root_username'], $encryptionKey),
  325. 'root_password' => $this->encipherment($postData['first_list']['root_password'], $encryptionKey),
  326. 'ssh_port' => $this->encipherment($postData['first_list']['ssh_port'], $encryptionKey),
  327. 'database_address' => $this->encipherment($postData['first_list']['database_address'], $encryptionKey),
  328. 'database_username' => $this->encipherment($postData['first_list']['database_username'], $encryptionKey),
  329. 'database_password' => $this->encipherment($postData['first_list']['database_password'], $encryptionKey),
  330. 'root_directory' => $this->encipherment($postData['first_list']['root_directory'], $encryptionKey),
  331. 'qq' => $this->encipherment($postData['first_list']['qq'], $encryptionKey),
  332. 'mobile' => $this->encipherment($postData['first_list']['mobile'], $encryptionKey),
  333. ],
  334. ];
  335. $userData = $data['user_data'];
  336. $userData['uniacid'] = \YunShop::app()->uniacid;
  337. WebSiteInfo::updateOrCreate(['website_url' => $userData['website_url']], $userData);
  338. $key = Setting::get('shop.key')['key'];
  339. $secret = Setting::get('shop.key')['secret'];
  340. $data = Curl::to(config('auto-update.workOrderUrl') . '/work-order/index')
  341. ->withHeader("Authorization: Basic " . base64_encode("{$key}:{$secret}"))
  342. ->withData($data)
  343. ->asJsonResponse(true)
  344. ->post();
  345. if (!$data) {
  346. $this->errorJson('远端服务器异常');
  347. }
  348. if ($data['result'] == 0) {
  349. return $this->errorJson($data['msg']);
  350. } else {
  351. return $this->successJson('提交成功');
  352. }
  353. }
  354. /**
  355. * 状态
  356. * @return array
  357. */
  358. private function status()
  359. {
  360. return [
  361. 0 => [
  362. 'id' => 0,
  363. 'name' => '全部',
  364. ],
  365. 1 => [
  366. 'id' => 1,
  367. 'name' => '未处理',
  368. ],
  369. 2 => [
  370. 'id' => 2,
  371. 'name' => '处理中',
  372. ],
  373. 3 => [
  374. 'id' => 3,
  375. 'name' => '已处理',
  376. ],
  377. ];
  378. }
  379. /**
  380. * 验证
  381. */
  382. protected function validateParam()
  383. {
  384. $this->validate([
  385. 'data.category_id' => 'required | integer',
  386. 'data.question_title' => 'required',
  387. 'data.question_describe' => 'required | max:80',
  388. 'data.first_list.founder_account' => 'required | max:80',
  389. 'data.first_list.founder_password' => 'required | max:80',
  390. 'data.first_list.server_ip' => 'required | max:80',
  391. 'data.first_list.ssh_port' => 'required | max:80',
  392. 'data.first_list.database_address' => 'required | max:80',
  393. 'data.first_list.database_username' => 'required | max:80',
  394. 'data.first_list.root_password' => 'required | max:80',
  395. 'data.first_list.qq' => 'required | max:80',
  396. 'data.first_list.mobile' => 'required | max:80',
  397. ]);
  398. }
  399. /**
  400. * 验证评论参数
  401. */
  402. protected function validateCommentParam()
  403. {
  404. $this->validate([
  405. 'content' => 'required',
  406. 'work_order_id' => 'required | integer',
  407. ]);
  408. }
  409. /**
  410. * 分类
  411. * @return array
  412. */
  413. private function category()
  414. {
  415. return [
  416. 0 => [
  417. 'id' => 0,
  418. 'name' => '全部',
  419. ],
  420. 1 => [
  421. 'id' => 1,
  422. 'name' => 'bug提交',
  423. ],
  424. 2 => [
  425. 'id' => 2,
  426. 'name' => '优化建议',
  427. ],
  428. 3 => [
  429. 'id' => '3',
  430. 'name' => '开发需求',
  431. ],
  432. 4 => [
  433. 'id' => 4,
  434. 'name' => '其他',
  435. ],
  436. ];
  437. }
  438. }