| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\constants\OptionSetting;
- use app\models\Option;
- use app\models\Share;
- use app\models\StoreMini;
- use app\models\User;
- use app\models\SaasUser;
- use app\models\UserAuditLog;
- use app\models\WechatConfig;
- use app\modules\client\controllers\BaseController;
- use app\modules\client\models\ApiModel;
- use app\modules\client\models\v1\LoginForm;
- use app\utils\Wechat\WechatMini;
- use EasyWeChat\Factory;
- use Exception;
- use yii\base\BaseObject;
- use app\utils\Douyin\Douyin;
- class AuthLoginForm extends ApiModel
- {
- // 商城id
- public $store_id;
- //绑定店铺来源 TODO 好像没用到
- public $store_id_first;
- // 供应链系统,将要切换的store_id
- public $switch_store_id;
- public $password; // 密码
- public $re_password; // 密码
- // 微信小程序
- public $code;
- public $encryptedData;
- public $iv;
- public $user_id;
- public $app_user_code;
-
- public $nickname;
- public $avatar_url;
- public $mobile;
- // 手机注册
- public $phone;
- public $verify_code;
- public $path;
- // 分销商user_id
- public $share_user_id;
- // 微信小程序
- const PLATFORM_WX = 'wx';
- // 微信公众号
- const PLATFORM_MP = 'mp';
- // app端微信
- const PLATFORM_APP = 'app';
- // 手机号注册登录
- const PLATFORM_PHONE = 'phone';
- public $_platform;
- public $platform; // wechat(微信), alipay(支付宝), douyin(抖音)
- /**
- * @var \EasyWeChat\MiniProgram\Application
- */
- public $wechat;
- public $form_value = 'h5';
- public function rules()
- {
- return [
- ['_platform', 'in', 'range' => [self::PLATFORM_WX, self::PLATFORM_MP, self::PLATFORM_APP, self::PLATFORM_PHONE]],
- ['_platform', 'required'],
- [['share_user_id', 'store_id', 'user_id', 'store_id_first', 'switch_store_id'], 'integer'],
- [['code', 'encryptedData', 'iv', 'platform', 'path', 'password', 're_password', 'form_value'], 'string'],
- [['code', 'encryptedData', 'iv', 'password', 're_password'], 'trim'],
- [['nickname', 'avatar_url', 'mobile', 'app_user_code', 'phone', 'verify_code'], 'safe'],
- ];
- }
- public function editPassword()
- {
- $user = get_user();
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '用户不存在',
- ];
- }
- if (isset($this->re_password)) {
- if (empty($this->password) || empty($this->re_password)) {
- return [
- 'code' => 1,
- 'msg' => '密码不能为空',
- ];
- }
- if ($this->password != $this->re_password) {
- return [
- 'code' => 1,
- 'msg' => '两次密码输入不一致',
- ];
- }
- }
- $user->password = \Yii::$app->security->generatePasswordHash($this->password);
- if ($user->save()) {
- return [
- 'code' => 0,
- 'msg' => '修改成功',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '修改失败',
- ];
- }
- }
- //忘记密码
- public function forgetPassword() {
- try {
- $phone = $this->phone;
- $verify_code = $this->verify_code;
- $password = trim($this->password);
- $re_password = trim($this->re_password);
- $loginForm = new LoginForm([
- 'store_id' => $this->store_id,
- 'phone' => $phone,
- 'verify_code' => $verify_code,
- ]);
- $user = User::findOne(['binding' => $phone, 'is_delete' => User::USER_NOT_DELETE, 'store_id' => $this->store_id]);
- if (!$user) {
- throw new \Exception('用户信息不存在');
- }
- $result = $loginForm->verifySmsCode(LoginForm::CACHE_KEY_SMS_FORGET_PASSWORD);
- if ($result['code']) {
- return $result;
- }
- if (!empty($password) || !empty($re_password)) {
- if (empty($password) || empty($re_password)) {
- throw new \Exception('密码不能为空');
- }
- if ($password != $re_password) {
- throw new \Exception('两次密码输入不一致');
- }
- $user->password = \Yii::$app->security->generatePasswordHash($password);
- if (!$user->save()) {
- throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
- }
- return [
- 'code' => 0,
- 'msg' => '修改密码成功'
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '验证通过'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //用户密码注册
- public function userPasswordRegedit() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $phone = $this->phone;
- $verify_code = $this->verify_code;
- $password = trim($this->password);
- $re_password = trim($this->re_password);
- $store_id = $this->store_id;
- // //是否开启密码登录注册接口
- // $is_password_login = Option::get(OptionSetting::IS_PASSWORD_LOGIN, $store_id, '', 0)['value'];
- // if ($is_password_login) {
- // throw new \Exception('商城未开启密码注册方式');
- // }
- $user = User::findOne(['binding' => $phone, 'is_delete' => User::USER_NOT_DELETE, 'store_id' => $store_id]);
- if ($user) {
- throw new \Exception('账户已存在');
- }
- //商城是否开启密码登录验证手机号+验证码
- $is_password_login_phone_verify = Option::get(OptionSetting::IS_PASSWORD_LOGIN_PHONE_VERIFY, $store_id, 'display', 0)['value'];
- $is_password_login_phone_verify = Option::get(OptionSetting::IS_PASSWORD_LOGIN_PHONE_VERIFY, $store_id, 'reg', $is_password_login_phone_verify)['value'];
- if (intval($is_password_login_phone_verify)) {
- $loginForm = new LoginForm([
- 'store_id' => $store_id,
- 'phone' => $phone,
- 'verify_code' => $verify_code,
- ]);
- $result = $loginForm->verifySmsCode(LoginForm::CACHE_KEY_SMS_LOGIN);
- if ($result['code']) {
- $t->rollBack();
- return $result;
- }
- }
- if (empty($password) || empty($re_password)) {
- throw new \Exception('密码不能为空');
- }
- if ($password != $re_password) {
- throw new \Exception('两次密码输入不一致');
- }
- $saas_user = SaasUser::findOne(['mobile' => $phone, 'is_delete' => 0]);
- if (!$saas_user) {
- $saas_user = new SaasUser();
- $saas_user->access_token = \Yii::$app->security->generateRandomString();
- $saas_user->name = substr_replace($phone, '******', 3, 6);
- $saas_user->mobile = $phone ?? '';
- $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- $saas_user->store_id = $store_id;
- } else {
- if (empty($saas_user->name)) {
- $saas_user->name = substr_replace($phone, '****', 3, 4);
- }
- if (empty($saas_user->avatar)) {
- $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- }
- if (empty($saas_user->access_token)) {
- $saas_user->access_token = \Yii::$app->security->generateRandomString();
- }
- }
- if (!$saas_user->save()) {
- throw new \Exception(json_encode($saas_user->errors, JSON_UNESCAPED_UNICODE));
- }
- $isNew = 0;
- $user = User::findOne(['binding' => $phone, 'store_id' => $store_id, 'is_delete' => 0]);
- if (!$user) {
- $isNew = 1;
- $user = new User();
- $user->type = User::USER_TYPE_NORMAL;
- $user->binding = $phone ?? '';
- $user->nickname = $saas_user->name;
- $user->avatar_url = $saas_user->avatar;
- $user->username = \Yii::$app->security->generateRandomString();
- $user->password = \Yii::$app->security->generatePasswordHash($password);
- $user->auth_key = \Yii::$app->security->generateRandomString();
- $user->access_token = \Yii::$app->security->generateRandomString();
- $user->is_delete = User::USER_NOT_DELETE;
- $user->store_id = $store_id;
- $user->parent_id = 0;
- $user->old_parent_id = 0;
- if (!$user->save()) {
- throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- if ($this->share_user_id && $this->share_user_id > 0) {
- try {
- $bind = new BindForm();
- $bind->user_id = $user->id;
- $bind->store_id = $store_id;
- $bind->parent_id = $this->share_user_id;
- $bind->condition = 0;
- $bind->isNew = $isNew;
- $bind->save();
- } catch (\Throwable $e){}
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '创建账户成功'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- // public function phoneAuth() {
- // try {
- // $this->wechat = self::getWechat();
- // $session = $this->wechat->auth->session($this->code);
- // $decryptedData = $this->wechat->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
- // $user = User::findOne(['binding' => $decryptedData['phoneNumber'], 'store_id' => get_store_id()]);
- // // 是否开启强制绑定手机号
- // $is_open_bind = Option::get(OptionSetting::STORE_LOGIN_FORCIBLY_BIND_MOBILE, $this->store_id, 'store')['value'];
- // $share = $share_user = null;
-
- // if ($this->share_user_id > 0) {
- // // 绑定上下级
- // $bindForm = new BindForm();
- // $bindForm->store_id = $this->store_id;
- // $bindForm->user_id = $user->id;
- // $bindForm->parent_id = $this->share_user_id;
- // $bindForm->condition = 0;
- // $bindForm->save();
- // }
- // if ($user) {
- // if ($user->parent_id > 0) {
- // $share = Share::findOne(['user_id' => $user->parent_id]);
- // $share_user = User::findOne(['id' => $share->user_id]);
- // }
- // $data = [
- // 'access_token' => $user->access_token,
- // 'nickname' => $user->nickname,
- // 'avatar_url' => $user->avatar_url,
- // 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
- // 'errCode' => 0,
- // 'parent' => $share ? ($share->name ? $share->name : $share_user->nickname) : '总店',
- // 'id' => $user->id,
- // 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- // 'integral' => $user->integral === null ? 0 : $user->integral,
- // 'money' => $user->money === null ? 0 : $user->money,
- // 'binding' => $user->binding,
- // 'level' => $user->level,
- // 'blacklist' => $user->blacklist,
- // 'is_open_bind' => $is_open_bind ? $is_open_bind : 0
- // ];
- // return [
- // 'code' => 0,
- // 'data' => $data,
- // 'msg' => '登录成功'
- // ];
- // } else {
- // return [
- // 'code' => 1,
- // 'data' => ['info' => $decryptedData['phoneNumber']],
- // 'msg' => ''
- // ];
- // }
- // } catch (\Exception $e) {
- // throw new \Exception($e->getMessage(), 1);
- // }
- // }
- public function getUserInfoByMobile()
- {
- $data = [
- 'avatar_url' => '',
- 'nickname' => '',
- 'mobile' => $this->mobile,
- 'id' => 0,
- ];
- $user = User::findOne(['binding' => $this->mobile, 'is_delete' => 0, 'store_id' => $this->store_id]);
- if ($user) {
- $data['id'] = $user->id;
- $saasUser = SaasUser::find()->where(['mobile' => $user->binding])->one();
- if ($saasUser) {
- $data['avatar_url'] = $saasUser->avatar;
- $data['nickname'] = $saasUser->name;
- $data['mobile'] = $saasUser->mobile;
- }
- }
- return [
- 'code' => 0,
- 'data' => $data,
- ];
- }
- public function getUserInfoById()
- {
- $data = [
- 'avatar_url' => '',
- 'nickname' => '',
- 'mobile' => '',
- 'id' => $this->user_id,
- ];
- $user = User::findOne(['id' => $this->user_id, 'is_delete' => 0, 'store_id' => $this->store_id]);
- if ($user) {
- $saasUser = SaasUser::find()->where(['mobile' => $user->binding])->one();
- if ($saasUser) {
- $data['avatar_url'] = $saasUser->avatar;
- $data['nickname'] = $saasUser->name;
- $data['mobile'] = $saasUser->mobile;
- }
- }
- return [
- 'code' => 0,
- 'data' => $data,
- ];
- }
- public function getOptionByH5()
- {
- $data = [
- 'is_exist_user' => 0,
- 'parent_avatar_url' => '',
- 'parent_nickname' => '',
- 'parent_mobile' => '',
- 'parent_id' => 0,
- ];
- // 获取是否强制绑定手机号配置
- $enforce_bind_parent = 0;
- $shareSetting = Option::get(OptionSetting::SHARE_BASIC_SETTING, $this->store_id, OptionSetting::SHARE_GROUP_NAME, null);
- if ($shareSetting = \json_decode($shareSetting['value'], true)) {
- $enforce_bind_parent = (int)$shareSetting['enforce_bind_parent']['value'];
- }
- $data['enforce_bind_parent'] = $enforce_bind_parent;
- $user = User::findOne(['binding' => $this->mobile, 'is_delete' => 0, 'store_id' => $this->store_id]);
- if ($user) {
- $data['is_exist_user'] = 1;
- }
- if ($this->share_user_id) {
- $parent = User::findOne(['id' => $this->share_user_id, 'is_delete' => 0]);
- if ($parent) {
- $parentSaasUser = SaasUser::find()->where(['mobile' => $parent->binding])->one();
- if ($parentSaasUser) {
- $data['parent_avatar_url'] = $parentSaasUser->avatar;
- $data['parent_nickname'] = $parentSaasUser->name;
- $data['parent_mobile'] = $parentSaasUser->mobile;
- $data['parent_id'] = $parent->id;
- }
- }
- }
- return [
- 'code' => 0,
- 'data' => $data,
- ];
- }
-
- public function code() {
- $login_use_mobile = Option::get(OptionSetting::STORE_LOGIN_USE_MOBILE, get_store_id(), 'store', 1)['value'];
- $login_use_mobile = Option::get(OptionSetting::STORE_LOGIN_USE_MOBILE, get_store_id(), 'reg', $login_use_mobile);
- $default_user_info_arr = OptionSetting::default_user_info_arr();
- $data = [
- 'access_token' => '',
- 'avatar_url' => $default_user_info_arr['default_user_pic'] ?? '',
- 'nickname' => $default_user_info_arr['default_user_nickname'] ?? '',
- 'login_use_mobile' => (int)$login_use_mobile['value'],
- 'is_exist_user' => 0,
- ];
- if (!intval($default_user_info_arr['isOpenDefault'])) {
- $data['avatar_url'] = '';
- $data['nickname'] = '';
- }
- try {
- if(is_isv()){
- $config = [
- 'app_id' => Option::get("platform_third_appid",0,'saas')['value'],
- 'secret' => Option::get("platform_third_secret",0,'saas')['value'],
- 'token' => Option::get("platform_token",0,'saas')['value'],
- 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value']
- ];
- $openPlatform = Factory::openPlatform($config);
- if(!empty(get_mini_id())){
- $mini = StoreMini::findOne(get_mini_id());
- $app = $openPlatform->miniProgram($mini->appid,$mini->authorizer_refresh_token);
- $session = $app->auth->session($this->code);
- }else{
- throw new \Exception('code异常1');
- }
- }elseif ($this->platform == 'bytedance') {
- /**
- * @var \ByteDance\MiniProgram\Application $byteDance
- */
- $byteDance = \Yii::$app->controller->byteDance;
- if (!$byteDance) {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- // $this->code = '121231231231';
- $session = $byteDance->auth->session($this->code);
- \Yii::warning($session);
- } else {
- if (is_h5()) {
- $app = WechatMini::getWechatConfig(get_store_id(), 0, WechatMini::TYPE_OFFICIAL);
- $session = $app->oauth->userFromCode($this->code)->getTokenResponse();
- } else {
- $this->wechat = self::getWechat();
- if (!$this->wechat) {
- throw new \Exception('code异常2');
- }
- $session = $this->wechat->auth->session($this->code);
- }
- }
- //获取session
- // $session = self::getWechat()->auth->session($this->code);
- if (!$session || empty($session['openid'])) {
- throw new \Exception('获取openid失败.');
- }
- $openid = $session['openid'];
- $store_user = User::find()->where(['or', ['wechat_open_id' => $openid], ['wechat_union_id' => $openid], ['bytedance_open_id' => $openid], ['wechat_platform_open_id' => $openid]])->andWhere(['store_id' => $this->store_id])->orderBy('id DESC')->one();
- if ($store_user) {
- $saas_user = SaasUser::find()->where(['mobile' => $store_user->binding])->one();
- // if ($saas_user) {
- // $data['avatar_url'] = $saas_user->avatar;
- // $data['nickname'] = $saas_user->name;
- // $data['id'] = $store_user->id;
- // $data['access_token'] = $saas_user['access_token'];
- // } else {
- $data['avatar_url'] = $store_user->avatar_url;
- $data['nickname'] = $store_user->nickname;
- $data['id'] = $store_user->id;
- // }
- $data['is_exist_user'] = 1;
- } else {
- // $user = User::find()->where(['or', ['wechat_open_id' => $openid], ['wechat_union_id' => $openid]])->orderBy('id DESC')->one();
- // if ($user) {
- // $data['avatar_url'] = $user->avatar_url;
- // $data['nickname'] = $user->nickname;
- // $data['id'] = $user->id;
- // }else{
- $saasuser = SaasUser::find()->where(['or', ['platform_open_id' => $openid], ['platform_open_id_merchant' => $openid], ['bytedance_open_id' => $openid]])->orderBy('id DESC')->one();
- if ($saasuser) {
- $data['id'] = $saasuser->id;
- $data['access_token'] = $saasuser->access_token;
- }
- // }
- }
- // 获取是否强制绑定手机号配置
- $enforce_bind_parent = 0;
- $shareSetting = Option::get(OptionSetting::SHARE_BASIC_SETTING, $this->store_id, OptionSetting::SHARE_GROUP_NAME, null);
- if ($shareSetting = \json_decode($shareSetting['value'], true)) {
- $enforce_bind_parent = (int)$shareSetting['enforce_bind_parent']['value'];
- }
- $data['enforce_bind_parent'] = $enforce_bind_parent;
- $data['openid'] = $openid;
- if($this->app_user_code){
- $user_id = cache()->get($this->app_user_code);
- cache()->delete($this->app_user_code);
- if(empty($user_id)){
- return [
- 'code' => 0,
- 'msg' => 'error',
- 'data' => $data,
- ];
- }
- $user = User::findOne($user_id);
- $user->wechat_open_id = $session['openid'];
- $user->save();
- $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
- $saas_user->platform_open_id = $session['openid'];
- $saas_user->save();
- $data['access_token'] = $saas_user['access_token'];
- }
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => 'ok'
- ];
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, $e]);
- return [
- 'code' => 0,
- 'msg' => 'error1' . $e->getMessage(),
- 'data' => $data,
- ];
- }
- }
- public function phoneAuth()
- {
- try {
- $decryptedData = [];
- if($this->phone && $this->verify_code){
- // 验证码验证
- $loginForm = new LoginForm([
- 'store_id' => $this->store_id,
- 'phone' => $this->phone,
- 'verify_code' => $this->verify_code,
- ]);
- $result = $loginForm->verifySmsCode(LoginForm::CACHE_KEY_SMS_LOGIN);
- $result2 = $loginForm->verifySmsCode(LoginForm::CACHE_KEY_BIND_PHONE);
- if ($result['code'] != 0 && $result2['code'] != 0) {
- return $result;
- }
- $decryptedData['phoneNumber'] = $this->phone;
- }
- if(is_isv()){
- if ($this->platform == 'bytedance') {
- $session = Douyin::code2session(get_mini_id(), $this->code);
- if($session['code'] != 0){
- return $session;
- }else{
- $session = $session['data']['data'];
- }
- }else{
- $config = [
- 'app_id' => Option::get("platform_third_appid",0,'saas')['value'],
- 'secret' => Option::get("platform_third_secret",0,'saas')['value'],
- 'token' => Option::get("platform_token",0,'saas')['value'],
- 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value']
- ];
- $openPlatform = Factory::openPlatform($config);
- if(!empty(get_mini_id())){
- $mini = StoreMini::findOne(get_mini_id());
- $app = $openPlatform->miniProgram($mini->appid,$mini->authorizer_refresh_token);
- $session = $app->auth->session($this->code);
- \Yii::error("今天是个好日子");
- \Yii::error($session);
- if (!empty($this->iv) && !empty($this->encryptedData)) {
- $decryptedData = $app->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
- \Yii::error($decryptedData);
- }
- }else{
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- }
- }elseif ($this->platform == 'bytedance') {
- /**
- * @var \ByteDance\MiniProgram\Application $byteDance
- */
- $byteDance = \Yii::$app->controller->byteDance;
- if (!$byteDance) {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- // $this->code = '121231231231';
- $session = $byteDance->auth->session($this->code);
- \Yii::warning($session);
- if (!empty($this->iv) && !empty($this->encryptedData)) {
- $decryptedData = $byteDance->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
- \Yii::error($decryptedData);
- }
- } else {
- $this->wechat = self::getWechat();
- if (!$this->wechat) {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- $session = $this->wechat->auth->session($this->code);
- \Yii::warning($session);
- if (!empty($this->iv) && !empty($this->encryptedData)) {
- $decryptedData = $this->wechat->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
- \Yii::error($decryptedData);
- }
- }
- $saas_user = null;
- $login_use_mobile = Option::get(OptionSetting::STORE_LOGIN_USE_MOBILE, get_store_id(), 'store', 1)['value'];
- $login_use_mobile = Option::get(OptionSetting::STORE_LOGIN_USE_MOBILE, get_store_id(), 'reg', $login_use_mobile);
- if (intval($login_use_mobile['value']) === 1 && empty($decryptedData['phoneNumber'])) {
- return [
- 'code' => 1,
- 'msg' => '手机号获取失败'
- ];
- }
- if (intval($login_use_mobile['value']) === 0 && empty($decryptedData['phoneNumber'])) {
- $decryptedData['phoneNumber'] = $this->generateRandomString(10) . 'X';
- }
- if (isset($decryptedData['phoneNumber']) && !empty($decryptedData['phoneNumber'])) {
- // 创建平台会员saas_user
- $saas_user = SaasUser::findOne(['mobile' => $decryptedData['phoneNumber'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE]);
- }
- if (!$saas_user && $session['openid']) {
- // 针对同一个微信号使用不同的手机号问题,直接更新最新的手机号
- $w = [
- 'and',
- ['or', ['platform_open_id' => $session['openid']], ['bytedance_open_id' => $session['openid']]],
- ['is_delete' => SaasUser::DELETE_STATUS_FALSE],
- ];
- $saas_user = SaasUser::find()->where($w)->limit(1)->one();
- }
- if (!$saas_user) {
- // 针对同一个微信号使用不同的手机号问题,直接更新最新的手机号
- // if ($session['openid']) {
- // $saas_user = SaasUser::find()->where(['platform_open_id' => $session['openid'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
- // }
- $saas_user = new SaasUser();
- $saas_user->access_token = \Yii::$app->security->generateRandomString();
- $saas_user->name = substr_replace($decryptedData['phoneNumber'], '******', 3, 6);
- $saas_user->mobile = $decryptedData['phoneNumber'] ?? '';
- // TODO: 独立小程序登录是不是无法获得平台小程序的openid? 暂时注释
- if ($this->platform == 'bytedance') {
- $saas_user->bytedance_open_id = $session['openid'];
- } else {
- $saas_user->platform_open_id = $session['openid'];
- }
- $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- $saas_user->store_id = $this->store_id;
- if($this->store_id_first > 0){
- $saas_user->store_id = $this->store_id_first;
- }
- if ($session['openid']) {
- $saas_user->platform_open_id = $session['openid'];
- }
- $saas_user->save();
- } else {
- if (empty($saas_user->name)) {
- $saas_user->name = substr_replace($decryptedData['phoneNumber'], '****', 3, 4);
- }
- if (empty($saas_user->avatar)) {
- $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- }
- if (empty($saas_user->access_token)) {
- $saas_user->access_token = \Yii::$app->security->generateRandomString();
- }
- if ($decryptedData['phoneNumber']) {
- $saas_user->mobile = $decryptedData['phoneNumber'];
- }
- if (is_platform()) {
- if ($this->platform == 'bytedance') {
- $saas_user->bytedance_open_id = $session['openid'];
- } else {
- $saas_user->platform_open_id = $session['openid'];
- }
- }
- if ($session['openid']) {
- if ($this->platform == 'bytedance') {
- $saas_user->bytedance_open_id = $session['openid'];
- }
- }
- $saas_user->save();
- }
- if($this->nickname){
- $saas_user->name = $this->nickname;
- }
- if($this->avatar_url){
- $saas_user->avatar = $this->avatar_url;
- }
- $saas_user->save();
- // 平台登录 todo: 后续补充其他数据
- if ($this->store_id == 0) {
- return [
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $saas_user->access_token,
- 'session_key' => $session['session_key'],
- 'nickname' => $saas_user->name,
- 'avatar_url' => $saas_user->avatar,
- 'id' => $saas_user->id,
- 'money' => $saas_user->share_profit,
- 'integral' => $saas_user->integral,
- 'mobile' => $saas_user->mobile,
- 'binding' => $saas_user->mobile,
- ]
- ];
- }
- // else {
- // \Yii::error(['登录失败', $decryptedData, $session]);
- // return [
- // 'code' => 1,
- // 'msg' => '登录失败',
- // ];
- // }
- $user = User::findOne(['binding' => $decryptedData['phoneNumber'], 'store_id' => $this->store_id, 'is_delete' => 0]);
- if (!$user) {
- $user = User::findOne(['wechat_open_id' => $session['openid'], 'store_id' => $this->store_id, 'is_delete' => 0]);
- }
- $share = $share_user = null;
- $isNew = 0;
- if ($user) {
- if ($user->blacklist == '1') {
- return [
- 'code' => 1,
- 'msg' => '您的账号已被限制登录!',
- ];
- }
- $data = [
- 'access_token' => $saas_user->access_token,
- 'nickname' => $saas_user->name,
- 'avatar_url' => $saas_user->avatar,
- 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
- 'errCode' => 0,
- 'id' => $user->id,
- 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- 'integral' => $user->integral === null ? 0 : $user->integral,
- 'money' => $user->money === null ? 0 : $user->money,
- 'binding' => $user->binding,
- 'level' => $user->level,
- 'blacklist' => $user->blacklist,
- 'is_saas_clerk' => (int)$user->is_saas_clerk,
- 'store_id' => $saas_user->store_id,
- ];
- if ($this->platform == 'bytedance') {
- $user->bytedance_open_id = $session['openid'];
- } else {
- $user->wechat_open_id = $session['openid'];
- if($session['unionid']){
- $user->wechat_union_id = $session['unionid'];
- }
- }
- if($this->nickname){
- $user->nickname = $this->nickname;
- }
- if($this->avatar_url){
- $user->avatar_url = $this->avatar_url;
- }
- $user->save();
- } else {
- $isNew = 1;
- $data = [
- 'nickName' => substr_replace($decryptedData['phoneNumber'], '******', 3, 6),
- 'avatarUrl' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png',
- ];
- $user = new User();
- $user->type = User::USER_TYPE_NORMAL;
- $user->binding = $decryptedData['phoneNumber'] ?? '';
- $user->nickname = $this->nickname ? $this->nickname : $data['nickName'];
- $user->avatar_url = $this->avatar_url ? $this->avatar_url : $data['avatarUrl'];
- $user->username = \Yii::$app->security->generateRandomString();
- $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
- $user->auth_key = \Yii::$app->security->generateRandomString();
- $user->access_token = \Yii::$app->security->generateRandomString();
- $user->is_delete = User::USER_NOT_DELETE;
- $user->store_id = $this->store_id;
- $user->parent_id = 0;
- $user->old_parent_id = 0;
- if ($this->platform == 'bytedance') {
- $user->bytedance_open_id = $session['openid'];
- $user->platform = User::USER_FROM_BYTEDANCE; // 字节
- } else {
- $user->platform = User::USER_FROM_WECHAT; // 微信
- $user->wechat_open_id = $session['openid'];
- if($session['unionid']){
- $user->wechat_union_id = $session['unionid'];
- }
- }
- if (!$user->save()) {
- \Yii::error(['登录失败', $user]);
- return [
- 'code' => 1,
- 'msg' => '登陆失败',
- 'data' => $user->getErrorSummary(false)[0]
- ];
- }
- $data = [
- 'access_token' => $saas_user->access_token,
- 'nickname' => $saas_user->name,
- 'gender' => $saas_user->gender,
- 'avatar_url' => $saas_user->avatar,
- 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
- 'errCode' => 0,
- 'id' => $user->id,
- 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- 'integral' => $user->integral === null ? 0 : $user->integral,
- 'money' => $user->money === null ? 0 : $user->money,
- 'binding' => $user->binding,
- 'level' => $user->level,
- 'blacklist' => $user->blacklist,
- 'is_saas_clerk' => 0,
- 'store_id' => $saas_user->store_id,
- ];
- }
- $userAuditLog = UserAuditLog::findOne(['user_id' => $user->id, 'is_delete' => 0]);
- if (!$userAuditLog) {
- $userAuditLog = UserAuditLog::findOne(['wechat_open_id' => $user->wechat_open_id, 'is_delete' => 0]);
- if (!empty($userAuditLog)) {
- $userAuditLog->user_id = $user->id;
- $userAuditLog->save();
- }
- }
- if (isset($data['nickname'])) {
- $data['is_can_wechat'] = 0;
- if (strpos($data['nickname'], '****') !== false) {
- $data['is_can_wechat'] = 1;
- }
- }
- if ($this->share_user_id && $this->share_user_id > 0) {
- try {
- $bind = new BindForm();
- $bind->user_id = $data['id'];
- $bind->store_id = $this->store_id;
- $bind->parent_id = $this->share_user_id;
- $bind->condition = 0;
- $bind->isNew = $isNew;
- $bind->save();
- } catch (\Throwable $e){}
- }
- // 处理 供应链openid问题
- if (\Yii::$app->prod_is_dandianpu()) {
- if (self_mini()) {
- // 独立小程序openid保存在user表
- $user->wechat_open_id = $session['openid'];
- if($session['unionid']){
- $user->wechat_union_id = $session['unionid'];
- }
- $user->save();
- } else {
- // 平台小程序openid保存在saas_user表
- $saas_user->platform_open_id = $session['openid'];
- $saas_user->save();
- }
- }
- $data['session_key'] = $session['session_key'];
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => '登录成功'
- ];
- } catch (\Throwable $e) {
- \Yii::error(['登录失败', $e]);
- \Yii::error($e->getMessage() . ' file => ' . $e->getFile() . ' line => ' . $e->getLine());
- return [
- 'code' => 1,
- 'msg' => '登录失败',
- ];
- }
- }
- public function generateRandomString($length = 10) {
- $characters = '1234567890';
- $charactersLength = strlen($characters);
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $characters[rand(0, $charactersLength - 1)];
- }
- $randomString = (string)$randomString;
- $user = User::findOne(['binding' => $randomString . 'X']);
- if ($user) {
- return $this->generateRandomString($length);
- }
- return $randomString;
- }
- public function platformPhoneAuth()
- {
- try {
- if(is_isv()){
- $config = [
- 'app_id' => Option::get("platform_third_appid",0,'saas')['value'],
- 'secret' => Option::get("platform_third_secret",0,'saas')['value'],
- 'token' => Option::get("platform_token",0,'saas')['value'],
- 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value']
- ];
- $openPlatform = Factory::openPlatform($config);
- if(!empty(get_mini_id())){
- $mini = StoreMini::findOne(get_mini_id());
- $app = $openPlatform->miniProgram($mini->appid,$mini->authorizer_refresh_token);
- $session = $app->auth->session($this->code);
- \Yii::error("今天是个好日子");
- \Yii::error($session);
- $decryptedData = $app->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
- \Yii::error($decryptedData);
- }else{
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- }elseif ($this->platform == 'bytedance') {
- /**
- * @var \ByteDance\MiniProgram\Application $byteDance
- */
- $byteDance = \Yii::$app->controller->byteDance;
- if (!$byteDance) {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- $session = $byteDance->auth->session($this->code);
- $decryptedData = $byteDance->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
- } else {
- $wechat = Option::getSaasPlatformWechat();
- $config = [
- 'app_id' => $wechat['appid'],
- 'secret' => $wechat['key'],
- 'response_type' => 'array'
- ];
- $this->wechat = Factory::miniProgram($config);
- if (!$this->wechat) {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- $session = $this->wechat->auth->session($this->code);
- $decryptedData = $this->wechat->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
- }
- if (isset($decryptedData['phoneNumber']) && !empty($decryptedData['phoneNumber'])) {
- // 创建平台会员saas_user
- $saas_user = SaasUser::find()->where(['mobile' => $decryptedData['phoneNumber'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
- if (!$saas_user) {
- $saas_user = new SaasUser();
- $saas_user->access_token = \Yii::$app->security->generateRandomString();
- $saas_user->name = substr_replace($decryptedData['phoneNumber'], '******', 3, 6);
- $saas_user->mobile = $decryptedData['phoneNumber'];
- if ($this->platform == 'bytedance') {
- $saas_user->bytedance_open_id = $session['openid'];
- } else {
- $saas_user->platform_open_id = $session['openid'];
- }
- $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- $saas_user->store_id = $this->store_id;
- $saas_user->save();
- } else {
- if (empty($saas_user->name)) {
- $saas_user->name = substr_replace($decryptedData['phoneNumber'], '****', 3, 4);
- }
- if (empty($saas_user->avatar)) {
- $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- }
- if (empty($saas_user->access_token)) {
- $saas_user->access_token = \Yii::$app->security->generateRandomString();
- }
- if ($this->platform == 'bytedance') {
- $saas_user->bytedance_open_id = $session['openid'];
- } else {
- $saas_user->platform_open_id = $session['openid'];
- }
- $saas_user->save();
- }
- // 平台登录 todo: 后续补充其他数据
- if ($this->store_id == 0) {
- return [
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $saas_user->access_token,
- 'session_key' => $session['session_key'],
- 'nickname' => $saas_user->name,
- 'avatar_url' => $saas_user->avatar,
- 'id' => $saas_user->id,
- 'money' => $saas_user->share_profit,
- 'integral' => $saas_user->integral
- ]
- ];
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => '登录失败',
- ];
- }
- if($this->nickname){
- $saas_user->name = $this->nickname;
- }
- if($this->avatar_url){
- $saas_user->avatar = $this->avatar_url;
- }
- $saas_user->save();
- $user = User::findOne(['binding' => $decryptedData['phoneNumber'], 'store_id' => $this->store_id, 'is_delete' => 0]);
- $share = $share_user = null;
- if ($user) {
- if ($user->blacklist == '1') {
- return [
- 'code' => 1,
- 'msg' => '您的账号已被限制登录!',
- ];
- }
- $data = [
- 'access_token' => $saas_user->access_token,
- 'nickname' => $saas_user->name,
- 'avatar_url' => $saas_user->avatar,
- 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
- 'errCode' => 0,
- 'id' => $user->id,
- 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- 'integral' => $user->integral === null ? 0 : $user->integral,
- 'money' => $user->money === null ? 0 : $user->money,
- 'binding' => $user->binding,
- 'level' => $user->level,
- 'blacklist' => $user->blacklist,
- 'is_saas_clerk' => (int)$user->is_saas_clerk,
- ];
- } else {
- $data = [
- 'nickName' => substr_replace($decryptedData['phoneNumber'], '******', 3, 6),
- 'avatarUrl' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png',
- ];
- $user = new User();
- $user->type = User::USER_TYPE_NORMAL;
- $user->binding = $decryptedData['phoneNumber'];
- $user->nickname = $data['nickName'];
- $user->avatar_url = $data['avatarUrl'];
- $user->username = \Yii::$app->security->generateRandomString();
- $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
- $user->auth_key = \Yii::$app->security->generateRandomString();
- $user->access_token = \Yii::$app->security->generateRandomString();
- $user->is_delete = User::USER_NOT_DELETE;
- $user->store_id = $this->store_id;
- if ($this->platform == 'bytedance') {
- $user->platform = User::USER_FROM_BYTEDANCE; // 字节
- } else {
- $user->platform = User::USER_FROM_WECHAT; // 微信
- }
- if (!$user->save()) {
- return [
- 'code' => 1,
- 'msg' => '登陆失败',
- 'data' => $user->getErrorSummary(false)[0]
- ];
- }
- $data = [
- 'access_token' => $saas_user->access_token,
- 'nickname' => $saas_user->name,
- 'avatar_url' => $saas_user->avatar,
- 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
- 'errCode' => 0,
- 'id' => $user->id,
- 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- 'integral' => $user->integral === null ? 0 : $user->integral,
- 'money' => $user->money === null ? 0 : $user->money,
- 'binding' => $user->binding,
- 'level' => $user->level,
- 'blacklist' => $user->blacklist,
- 'is_saas_clerk' => 0,
- ];
- }
- if($this->nickname){
- $user->nickname = $this->nickname;
- }
- if($this->avatar_url){
- $user->avatar_url = $this->avatar_url;
- }
- $user->save();
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => '登录成功'
- ];
- } catch (\Exception $e) {
- \Yii::error($e->getMessage() . ' file => ' . $e->getFile() . ' line => ' . $e->getLine());
-
- return [
- 'code' => 1,
- 'msg' => '登录失败',
- ];
- }
- }
- // 使用openid登录
- public function loginByOpenid()
- {
- try {
- $this->wechat = self::getWechat();
- if (!$this->wechat) {
- return [
- 'code' => 1,
- 'msg' => ''
- ];
- }
- $session = $this->wechat->auth->session($this->code);
- $openid = $session['openid'];
- // $openid = 'or1pO5a0oYCKlPm_wsfhdi6Ti4VQ';
- $user = User::findOne(['wechat_open_id' => $openid, 'is_delete' => 0]);
- if (!$user) {
- throw new \Exception('');
- }
- $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
- if (!$saas_user) {
- throw new \Exception('');
- }
- $data = [
- 'access_token' => $saas_user->access_token,
- 'nickname' => $saas_user->name,
- 'gender' => $saas_user->gender,
- 'avatar_url' => $saas_user->avatar,
- 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
- 'errCode' => 0,
- 'id' => $user->id,
- 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- 'integral' => $user->integral === null ? 0 : $user->integral,
- 'money' => $user->money === null ? 0 : $user->money,
- 'binding' => $user->binding,
- 'level' => $user->level,
- 'blacklist' => $user->blacklist,
- 'is_saas_clerk' => 0,
- 'store_id' => $saas_user->store_id,
- ];
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => '登录成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => '',
- ];
- }
- }
- public function loginByUserId()
- {
- try {
- // $user = User::findOne(['id' => $this->user_id, 'is_delete' => 0]);
- // if (!$user) {
- // throw new \Exception('');
- // }
- $saas_user = SaasUser::findOne(['id' => $this->user_id, 'is_delete' => 0]);
- if (!$saas_user) {
- throw new \Exception('');
- }
- $data = [
- 'access_token' => $saas_user->access_token,
- 'nickname' => $saas_user->name,
- 'gender' => $saas_user->gender,
- 'avatar_url' => $saas_user->avatar,
- // 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
- 'errCode' => 0,
- 'id' => $saas_user->id,
- // 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- // 'integral' => $user->integral === null ? 0 : $user->integral,
- // 'money' => $user->money === null ? 0 : $user->money,
- 'binding' => $saas_user->mobile,
- // 'level' => $user->level,
- // 'blacklist' => $user->blacklist,
- 'is_saas_clerk' => 0,
- 'store_id' => $saas_user->store_id,
- ];
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => '登录成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => '',
- ];
- }
- }
- // 用于供应链小程序切换商城后自动登录
- public function loginByAuto()
- {
- try {
- $saasUser = \get_saas_user();
- $oldUser = \get_user();
- $switchUser = User::find()->where([
- 'is_delete' => 0,
- 'binding' => $saasUser->mobile,
- 'store_id' => $this->switch_store_id,
- 'type' => User::USER_TYPE_NORMAL,
- ])->one();
- if (!$switchUser) {
- $switchUser = new User();
- $switchUser->type = User::USER_TYPE_NORMAL;
- $switchUser->binding = $saasUser->mobile;
- $switchUser->nickname = $saasUser->name;
- $switchUser->avatar_url = $saasUser->avatar;
- $switchUser->username = \Yii::$app->security->generateRandomString();
- $switchUser->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
- $switchUser->auth_key = \Yii::$app->security->generateRandomString();
- $switchUser->access_token = \Yii::$app->security->generateRandomString();
- $switchUser->is_delete = User::USER_NOT_DELETE;
- $switchUser->store_id = $this->switch_store_id;
- if ($this->platform == 'bytedance') {
- $switchUser->bytedance_open_id = $oldUser->bytedance_open_id;
- $switchUser->platform = User::USER_FROM_BYTEDANCE; // 字节
- } else {
- $switchUser->platform = User::USER_FROM_WECHAT; // 微信
- $switchUser->wechat_open_id = $oldUser->wechat_open_id;
- }
- $switchUser->save();
- }
- // 以下数据在访问用户中心时会被替换掉,所以这里不查询其他数据,只返回用户基本信息
- $user_info = [
- 'access_token' => $saasUser->access_token,
- 'avatar_url' => $saasUser->avatar,
- 'binding' => $switchUser->binding,
- 'blacklist' => $switchUser->blacklist,
- 'id' => $switchUser->id,
- 'is_admin' => 0,
- 'is_clerk' => '0',
- 'is_saas_clerk' => '0',
- 'is_delivery' => '0',
- 'is_distributor' => '0',
- 'is_live' => 0,
- 'is_real' => '0',
- 'level' => $switchUser->level,
- 'level_name' => '普通用户',
- 'nickname' => $saasUser->name,
- 'parent' => '总店',
- 'integral' => 0,
- 'money' => "0.00",
- 'price' => '0.00',
- 'gender' => $saasUser->gender,
- 'saas_money' => $saasUser->share_profit,
- 'is_salesman' => $saasUser->is_salesman,
- 'is_md_staff' => 0,
- 'is_md_manager' => 0,
- 'user_id' => $switchUser->id,
- 'is_show_toggle_store' => 0,
- 'has_store' => 0,
- 'is_holder' => 0,
- 'is_video_author' => 0,
- ];
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'user_info' => $user_info,
- ],
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => '自动登录失败',
- ];
- }
- }
- public function getRedirectUri() {
- try {
- $store_id = $this->store_id;
- $wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'is_delete' => 0, 'type' => 2]);
- $one_store_wechat_official_appid = Option::get('one_store_wechat_official_appid', 0, 'saas', '')['value'];
- if ((!empty($wechat_config) && $wechat_config->app_id) || !empty($one_store_wechat_official_appid)) {
- $appid = $wechat_config->app_id;
- $redirect_uri = \Yii::$app->request->hostInfo . '/'. $this->form_value .'/#' . $this->path ?: '/';
- $state = 'store_id=' . $store_id;
- // $redirect_uri = urlencode($redirect_uri);
- $uri = getAuthLink($store_id, $redirect_uri, 0);
- // $uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base#wechat_redirect";
- //https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'uri' => $uri
- ]
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '失败',
- 'data' => [
- 'uri' => ''
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 0,
- 'msg' => $e->getMessage(),
- 'data' => [
- 'uri' => ''
- ]
- ];
- }
- }
- public function setH5UserInfo() {
- try {
- $code = $this->code;
- $saas_user = SaasUser::findOne(get_saas_user_id());
- $user = User::findOne(get_user_id());
- $app = WechatMini::getWechatConfig(get_store_id(), 0, WechatMini::TYPE_OFFICIAL);
- if (!$app) {
- return [
- 'code' => 1,
- 'msg' => '没有对应的配置项'
- ];
- }
- $session_result = $app->oauth->userFromCode($code);
- $session = $session_result->getTokenResponse();
- if ($session['scope'] === 'snsapi_userinfo') {
- $avatar = $session_result->getAvatar();
- $nickname = $session_result->getNickname();
- if ($saas_user) {
- if (!empty($avatar)) {
- $saas_user->avatar = $avatar;
- }
- if (!empty($nickname)) {
- $saas_user->name = $nickname;
- }
- if (!$saas_user->save()) {
- throw new \Exception(json_encode($saas_user->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- if ($user) {
- if (!empty($avatar)) {
- $user->avatar_url = $avatar;
- }
- if (!empty($nickname)) {
- $user->nickname = $nickname;
- }
- if (!empty($session['openid'])) {
- // $user->wechat_open_id = $session['openid'];
- $user->wechat_platform_open_id = $session['openid'];
- }
- if (!$user->save()) {
- throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => '设置成功',
- 'data' => [
- 'user_info' => [
- 'avatar' => $saas_user->avatar,
- 'name' => $saas_user->name
- ]
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 0,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|