UserActionLogic.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\user\logic;
  12. use think\Db;
  13. class UserActionLogic
  14. {
  15. /**
  16. * 导入应用用户行为
  17. * @param $app
  18. * @return array
  19. * @throws \ReflectionException
  20. * @throws \think\Exception
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. * @throws \think\exception\DbException
  24. * @throws \think\exception\PDOException
  25. */
  26. public static function importUserActions($app)
  27. {
  28. $userActionConfigFile = cmf_get_app_config_file($app, 'user_action');
  29. if (file_exists($userActionConfigFile)) {
  30. $userActionsInFile = include $userActionConfigFile;
  31. foreach ($userActionsInFile as $userActionKey => $userAction) {
  32. $userAction['cycle_type'] = empty($userAction['cycle_type']) ? 0 : $userAction['cycle_type'];
  33. if (!in_array($userAction['cycle_type'], [0, 1, 2, 3])) {
  34. $userAction['cycle_type'] = 0;
  35. }
  36. if (!empty($userAction['url']) && is_array($userAction['url']) && !empty($userAction['url']['action'])) {
  37. $userAction['url'] = json_encode($userAction['url']);
  38. } else {
  39. $userAction['url'] = '';
  40. }
  41. $findUserAction = Db::name('user_action')->where('action', $userActionKey)->count();
  42. $userAction['app'] = $app;
  43. if ($findUserAction > 0) {
  44. Db::name('user_action')->where('action', $userActionKey)
  45. ->strict(false)->field(true)
  46. ->update([
  47. 'name' => $userAction['name'],
  48. 'url' => $userAction['url']
  49. ]);
  50. } else {
  51. $userAction['action'] = $userActionKey;
  52. Db::name('user_action')->strict(false)
  53. ->field(true)
  54. ->insert($userAction);
  55. }
  56. }
  57. }
  58. }
  59. }