Пример #1
0
 /**
  * migrate account channel(channel-migration)
  */
 public function actionChannelMigration()
 {
     $accounts = Account::findAll([]);
     $channels = [];
     foreach ($accounts as $account) {
         $channelIds = $account->channels;
         $testWechat = empty($channelIds['testWechat']) ? [] : $channelIds['testWechat'];
         $channelIds = array_merge($channelIds['wechat'], $channelIds['weibo'], $testWechat);
         if (empty($channelIds)) {
             continue;
         }
         try {
             $weChannels = Yii::$app->weConnect->getAccounts($channelIds);
         } catch (\Exception $e) {
             echo 'error channel' . json_encode($channelIds) . PHP_EOL;
             continue;
         }
         foreach ($weChannels as $weChannel) {
             $channelOrigin = $weChannel['channel'];
             switch ($channelOrigin) {
                 case 'WEIXIN':
                     $origin = Channel::WECHAT;
                     break;
                 case 'WEIBO':
                     $origin = Channel::WEIBO;
                     break;
                 case 'ALIPAY':
                     $origin = Channel::ALIPAY;
                     break;
                 default:
                     $origin = strtolower($weChannel['channel']);
                     echo $weChannel['id'] . ' ' . $origin . PHP_EOL;
                     break;
             }
             $type = empty($weChannel['accountType']) ? '' : $weChannel['accountType'];
             $channel = Channel::getByAccountAndChannelId($account->_id, $weChannel['id']);
             $isTest = $origin == Channel::WECHAT && !empty($weChannel['appSecret']) ? true : false;
             if (!empty($channel)) {
                 $channel->origin = $origin;
                 $channel->name = $weChannel['name'];
                 $channel->type = $type;
                 $channel->status = strtolower($weChannel['status']);
                 $channel->isTest = $isTest;
                 if (!$channel->save()) {
                     echo 'update-' . $weChannel['id'] . ' ' . $origin . PHP_EOL;
                 }
             } else {
                 $channels[] = ['channelId' => $weChannel['id'], 'origin' => $origin, 'name' => $weChannel['name'], 'type' => $type, 'status' => strtolower($weChannel['status']), 'isTest' => $isTest, 'accountId' => $account->_id];
             }
         }
     }
     if (Channel::batchInsert($channels)) {
         echo 'success' . PHP_EOL;
     } else {
         echo 'fail' . PHP_EOL;
     }
 }