| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace api\modules\v1\controllers\userwages;
- header('Access-Control-Allow-Origin:*');
- use addons\Salary\common\logic\UserLogic;
- use addons\Salary\common\services\WyaServices;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- use common\helpers\sms\Sms;
- use common\models\common\ServiceAgreement;
- use common\models\mall\Mall;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use Yii;
- use Exception;
- class AuthController extends OnAuthController
- {
- /**
- * @var string
- */
- public $modelClass = '';
- /**
- * @var array
- */
- protected $authOptional = ['login', 'logout', 'sms-code', 'service-agreement-detail', 'up-password'];
- /**
- * @var string|null
- */
- public $withdraw_income = null;
- public function actionLogin()
- {
- try {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['username'], 'required'],
- [['captcha', 'password'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = ['mobile' => $params['username']];
- $mall_user = User::getUser($where, '*', [], 0);
- if (!empty($mall_user)) $user = $mall_user->toArray();
- if (empty($user)) {
- // 用户不存在,检查是否启用对外部接口用户数据获取
- if (MallAddons::isInstall($this->mall_id, 'Salary') && Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.is_external')) {
- $user = UserLogic::register($this->mall_id, $params['username']);
- } else {
- throw new \yii\db\Exception('账号不存在');
- }
- } else {
- // 用户已存在,更新用户最新的账号金额信息
- if (MallAddons::isInstall($this->mall_id, 'Salary') && Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.is_external')) {
- UserLogic::updateUser($user);
- }
- }
- if (empty($params['password']) && empty($params['captcha'])) throw new \yii\db\Exception('必须选择一种登录方式');
- if (!empty($params['password'])) {
- if (!\Yii::$app->getSecurity()->validatePassword(trim($params['password']), $user['password'])) throw new Exception('密码错误');
- } else {
- if (YII_ENV == 'prod') {
- if (!Sms::checkValidateCode($params['username'], $params['captcha'], Sms::SMS_TYPE_USER_LOGIN)) throw new Exception('验证码不正确');
- }
- }
- $token = Yii::$app->services->apiAccessToken->getAccessToken($mall_user, 'api');
- $token = $token['access_token'];
- return $this->success('登录成功', ['userwagesToken' => $token]);
- }
- if (Yii::$app->request->isGet) {
- $service_agreement_list = [];
- $agreements = Yii::$app->services->setting->addons->get($this->mall_id, 'Salary', 'basic.agreements');
- if (!empty($agreements)) {
- $service_agreement_ids = json_decode($agreements, true);
- $service_agreement_list = ServiceAgreement::lists([
- 'where' => [
- ['id' => $service_agreement_ids],
- ['mall_id' => $this->mall_id],
- ['status' => StatusEnum::ENABLED]
- ],
- 'select' => 'id,name',
- 'order' => 'sort asc,id desc'
- ]);
- }
- return $this->success('登录成功', ['service_agreement_list' => $service_agreement_list]);
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- public function actionSmsCode()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile'], 'required', 'message' => '请输入手机号码'],
- [['mobile', 'area_code'], 'integer'],
- [['sms_type'], 'string'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $area_code = '';// 初始化区号
- $gateways = 'domestic';// 默认国内短信
- // 检测是登录并且区号不为空则使用国际短信
- if (!empty($this->user) && !empty($this->user->area_code)) {
- $gateways = 'international';
- $area_code = $this->user->area_code;
- }
- // 判断是否有区号
- if (!empty($params['area_code']) && $params['area_code'] != 86) {
- $gateways = 'international';
- $area_code = $params['area_code'];
- }
- $params['sms_type'] = $params['sms_type'] ?: '';
- $sms = new Sms($this->mall_id, $gateways);
- try {
- if ($sms->sendCaptcha($params['mobile'], $area_code, Sms::SMS_TYPE_USER_LOGIN) === false) return $this->error('发送失败');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success('发送成功');
- }
- public function actionLogout()
- {
- if (Yii::$app->services->apiAccessToken->disableByAccessToken(Yii::$app->user->identity->access_token)) return $this->success('退出成功');
- return $this->error('退出失败');
- }
- public function actionServiceAgreementDetail()
- {
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required', 'message' => '请输入id'],
- [['id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $service_agreement = ServiceAgreement::getOne([
- ['id' => $params['id']],
- ['mall_id' => $this->mall_id],
- ['status' => StatusEnum::ENABLED]
- ], true, [], false, 'id,name,content');
- return $this->success('成功', $service_agreement ?? []);
- }
- public function actionUpPassword()
- {
- try {
- if (\Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile', 'captcha', 'password'], 'required'],
- ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
- [['mobile'], 'integer'],
- ['password', 'string', 'length' => [6, 11]],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user = User::find()->where(['mobile' => $params['mobile'], 'mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->one();
- //短信验证码
- if (YII_ENV == 'prod') {
- if (!Sms::checkValidateCode($params['mobile'], $params['captcha'])) return $this->error('验证码不正确');
- }
- $user->password = Yii::$app->getSecurity()->generatePasswordHash(trim($params['password']));
- if ($user->save() === false) return $this->error(User::getStaticError());
- return $this->success('设置成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|