<?php namespace wallet\controllers; use Yii; use common\models\Admin; use common\models\LoginForm; use wallet\base\BaseController; use common\service\trusteeship\TrusteeShipService; class UserController extends BaseController { /** * landing * @return array * @throws \yii\base\Exception * @throws \yii\base\InvalidConfigException */ public function actionLogin() { $msg = 'ok'; $code = 0; $model = new LoginForm(); $model->setScenario(LoginForm::SCENARIOS_LOGIN); $model->load(Yii::$app->request->post(), ''); if (!$model->login()) { $msg = implode(", ", \yii\helpers\ArrayHelper::getColumn($model->errors, 0, false)); // Model's Errors string $data = null; $code = -1; goto doEnd; } $data = ['access_token' => $model->login()]; doEnd : return ['code' => $code, 'msg' => $msg, 'data' => $data]; } public function actionUserInfo() { $msg = 'ok'; $code = 0; $token_string = Yii::$app->request->headers->get('Token'); $user = Admin::findIdentityByAccessToken($token_string); $data = [ 'username' => $user->username, 'uid' => isset($user->bind_uid) ? $user->bind_uid : $user->uid, 'type' => isset($user->bind_uid) ? 2 : 1, 'platform_id' => $user->platform_id ]; return ['code' => $code, 'msg' => $msg, 'data' => $data]; } /** * 用户同步 */ public function actionUserSync() { $items = Yii::$app->request->post(); if (count($items['items']) > 10) { return ['code' => -1, 'data' => [], 'msg' => '一次最多同步20条数据']; } $duplicate = 0; foreach ($items['items'] as $key => $item) { $model = Admin::find()->where(['username' => $item['username']])->andWhere(['platform_id' => (int)$item['platform']])->one(); if ($model) { $duplicate++; continue; } $datas[] = [ $item['bind_uid'], $item['username'], Yii::$app->security->generateRandomString(), Yii::$app->security->generatePasswordHash('123456'), time(), ip2long('127.0.0.1'), 0, ip2long('127.0.0.1'), 0, 1, $item['platform'] ]; } if (!empty($datas)) { Admin::loadArray($datas); } return ['code' => 1, 'data' => [], 'msg' => '数据更新成功,共有 ' . $duplicate . ' 条重复']; $header = Yii::$app->request->headers; $platform_id = $header['platform_id'] ?? 17; $post = Yii::$app->request->post(); $data = [ 'bind_uid' => $post['bind_uid'], 'username' => $post['username'], 'salt' => Yii::$app->security->generateRandomString(), 'password' => Yii::$app->security->generatePasswordHash('123456'), 'reg_time' => time(), 'reg_ip' => ip2long('127.0.0.1'), 'last_login_time' => 0, 'last_login_ip' => ip2long('127.0.0.1'), 'update_time' => 0, 'status' => 1, 'platform_id' => $platform_id ]; $role = Yii::$app->request->post('role', 'GHPwallet'); $model = new Admin(); if ($model->load($data, '') && $model->save()) { $auth = Yii::$app->authManager; $role = $auth->getRole($role); $auth->assign($role, $model->uid); exit; } else { var_dump($model->errors); exit; } } /** * 用户列表 */ public function actionUserList() { $current_platform_id = Yii::$app->request->getPlatformId(); if(1 === $current_platform_id) { $platform_id = Yii::$app->request->get('platform_id', 1); $platform_id = empty($platform_id) ? 1 : $platform_id; } else { $platform_id = Yii::$app->request->getPlatformId(); } if(!isset(Yii::$app->params['trusteeship']['node_'. $platform_id])){ return ['code' => -1, 'data' => [], 'msg' => '此钱包节点尚未开通']; } $node_params = Yii::$app->params['trusteeship']['node_'. $platform_id]; $page = Yii::$app->request->get('page', 1); $size = Yii::$app->request->get('size', 15); $real_type = Yii::$app->request->get('real_type', ''); $search_type = Yii::$app->request->get('search_type', 'user'); $search = Yii::$app->request->get('search', ''); $start_time = Yii::$app->request->get('start_time', ''); $end_time = Yii::$app->request->get('end_time', ''); $params = [ 'page' => $page, 'size' => $size, 'real_type' => $real_type, 'search_type' => $search_type, 'search' => $search, 'start_time' => $start_time, 'end_time' => $end_time ]; $time = time(); $appKey = isset($node_params['appKey']) ? $node_params['appKey'] : null; $appSecret = isset($node_params['appSecret']) ? $node_params['appSecret'] : null; $signature = self::getSign($params, $appKey, $appSecret, $time); $headers = [ 'FZM-Wallet-Signature' => $signature, 'FZM-Wallet-Timestamp' => $time, 'FZM-Wallet-AppKey' => $appKey, 'FZM-Wallet-AppIp' => Yii::$app->request->userIP ]; $service = new TrusteeShipService($node_params, $headers); $result = $service->getUserList($params); if (200 !== $result['code']) { return ['code' => $result['code'], 'data' => [], 'msg' => $result['msg']]; } return ['code' => 1, 'data' => $result['msg'], 'msg' => 'success']; } }