| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Module\UrsPromotion\AdminControllers\Actions;
- use App\Module\UrsPromotion\Models\UrsUserMapping;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use Illuminate\Http\Request;
- use UCore\DcatAdmin\RowActionHandler;
- /**
- * 同步用户信息操作
- *
- * 从URS系统同步用户信息到本地
- */
- class SyncUserInfoAction extends RowActionHandler
- {
- /**
- * 操作按钮标题
- *
- * @var string
- */
- public $title = '同步信息';
- /**
- * 检查是否允许显示此操作
- *
- * @return bool
- */
- public function allowed()
- {
- $row = $this->getRow();
- // 只有有效的映射关系才允许同步
- return $row->status === UrsUserMapping::STATUS_VALID;
- }
- /**
- * 处理请求
- *
- * @param Request $request
- * @return mixed
- */
- public function handle(Request $request)
- {
- $id = $this->getKey();
- $mapping = UrsUserMapping::find($id);
-
- if (!$mapping) {
- return $this->response()->error('用户映射记录不存在');
- }
- if ($mapping->status !== UrsUserMapping::STATUS_VALID) {
- return $this->response()->error('只能同步有效状态的映射关系');
- }
- try {
- // 同步用户信息
- $result = UrsUserMappingService::syncUserInfo($mapping);
-
- if ($result['success']) {
- $message = "用户信息同步成功!";
-
- // 添加同步详情
- if (!empty($result['updated_fields'])) {
- $message .= "\n更新字段:" . implode(', ', $result['updated_fields']);
- }
-
- if (!empty($result['sync_time'])) {
- $message .= "\n同步时间:" . $result['sync_time'];
- }
-
- return $this->response()
- ->success($message)
- ->refresh();
- } else {
- $message = "用户信息同步失败!";
-
- // 添加失败原因
- if (!empty($result['error'])) {
- $message .= "\n错误信息:" . $result['error'];
- }
-
- return $this->response()->error($message);
- }
- } catch (\Exception $e) {
- return $this->response()->error('同步失败:' . $e->getMessage());
- }
- }
- /**
- * 确认对话框
- *
- * @return string
- */
- public function confirm()
- {
- return '确定要从URS系统同步此用户的信息吗?这将更新本地的用户数据。';
- }
- }
|