| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\constants\OptionSetting;
- use app\models\Admin;
- use app\models\Option;
- use app\models\Share;
- use app\models\StoreAdmin;
- use app\models\User;
- use app\models\WechatConfig;
- use app\modules\client\models\ApiModel;
- use app\utils\Notice\NoticeSend;
- use app\utils\Wechat\WechatMini;
- use yii\helpers\Json;
- use app\models\SaasUser;
- use app\modules\admin\models\GolangOrderForm;
- use Overtrue\Socialite\Providers\WeChat;
- use Overtrue\Socialite\SocialiteManager;
- include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
- class LoginForm extends ApiModel
- {
- // 前端类型
- public $_platform;
- // 商城id
- public $store_id;
- public $login_type;
- public $password;
- // 微信小程序
- public $code;
- public $authCode;
- public $user_info;
- public $encrypted_data;
- public $iv;
- public $signature;
- // 手机注册
- public $phone;
- public $verify_code;
- public $isLogin; //直接绑定上下级
- // 分销商user_id
- public $share_user_id;
- /**
- * @var User $user
- */
- public $user;
- // 微信小程序
- const PLATFORM_WX = 'wx';
- // 微信公众号
- const PLATFORM_MP = 'mp';
- // app端微信
- const PLATFORM_APP = 'app';
- // 手机号注册登录
- const PLATFORM_PHONE = 'phone';
- // 短信登录验证码
- const CACHE_KEY_SMS_LOGIN = '_mobile_sms_login_';
- // 绑定手机号验证码
- const CACHE_KEY_BIND_PHONE = '_mobile_bind_';
- // 短信忘记密码验证码
- const CACHE_KEY_SMS_FORGET_PASSWORD = '_mobile_sms_forget_password_';
- // 验证码类型
- // 手机登录
- const TYPE_VERIFY_CODE_LOGIN = 'login';
- // 手机绑定
- const TYPE_VERIFY_CODE_BIND = 'bind';
- // 清除数据
- const TYPE_VERIFY_CODE_CLEAR = 'clear';
- // 手机绑定
- const TYPE_VERIFY_CODE_FORGET_PASSWORD = 'forget_password';
- public $parent_id;
- public $appCid;
- public function rules()
- {
- return [
- ['_platform', 'in', 'range' => [self::PLATFORM_WX, self::PLATFORM_MP, self::PLATFORM_APP, self::PLATFORM_PHONE]],
- ['_platform', 'required'],
- [['code', 'user_info', 'encrypted_data', 'iv', 'signature'], 'required', 'on' => self::PLATFORM_WX],
- [['user_info'], 'required', 'on' => self::PLATFORM_APP],
- [['phone'], 'required', 'on' => self::PLATFORM_PHONE],
- [['phone'], 'match', 'pattern' => '/^(111\d{8}|1[3-9]\d{9})$/', 'message' => '手机号错误'],
- [['verify_code', 'parent_id', 'share_user_id', 'isLogin', 'login_type'], 'integer'],
- [['code', 'user_info', 'encrypted_data', 'iv', 'signature', 'phone', 'password'], 'string'],
- [['code', 'user_info', 'encrypted_data', 'iv', 'signature', 'phone'], 'trim'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'login_type' => 'login_type',
- '_platform' => '_platform',
- 'store_id' => 'store_id',
- 'code' => 'code',
- 'user_info' => 'user_info',
- 'encrypted_data' => 'encrypted_data',
- 'iv' => 'iv',
- 'signature' => 'signature',
- 'share_user_id' => 'share_user_id',
- ];
- }
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- return $scenarios;
- }
- /**
- * 登录统一入口
- * @return array
- * @throws \yii\base\Exception
- */
- public function login($_alipay = false, $authCode = '')
- {
- if ($this->_platform == self::PLATFORM_WX) {
- // 微信小程序登录
- return $this->loginWX();
- } else if ($this->_platform == self::PLATFORM_APP) {
- // app端微信登录
- return $this->loginAppWechat();
- } else if ($this->_platform == self::PLATFORM_PHONE) {
- // 手机号登录,没有找到手机号自动注册
- // return $this->phoneLogin();
- return $this->phoneLoginCode($_alipay, $authCode);
- } else {
- return [
- 'code' => 1,
- 'msg' => '登录失败,请检查参数是否正确'
- ];
- }
- }
- public function loginnew()
- {
- if ($this->_platform == self::PLATFORM_WX) {
- // 微信小程序登录
- return $this->loginWX();
- } else if ($this->_platform == self::PLATFORM_APP) {
- // app端微信登录
- return $this->loginAppWechat();
- } else if ($this->_platform == self::PLATFORM_PHONE) {
- // 手机号登录,没有找到手机号自动注册
- // return $this->phoneLogin();
- return $this->phoneLoginCodeNew();
- } else {
- return [
- 'code' => 1,
- 'msg' => '登录失败,请检查参数是否正确'
- ];
- }
- }
- // 后台参数token
- public function getToKen($saasuserid, $mobile)
- {
- $where = [];
- // 判断 saasuserid 是否为空并添加到查询条件中
- if (!empty($saasuserid)) {
- $where['id'] = $saasuserid;
- }
- // 判断 mobile 是否为空并添加到查询条件中
- if (!empty($mobile)) {
- $where['mobile'] = $mobile;
- }
- // 查找符合条件的 SaasUser
- $saas_user = SaasUser::find()->where($where)->andWhere(['is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
- // 判断是否找到符合条件的用户
- if (!$saas_user) {
- // 处理未找到用户的情况
- return [
- 'code' => 1,
- 'msg' => '未找到当前用户'
- ];
- }
- if(empty(get_params('store_id'))){
- $saas_user->store_id = -1;
- }
- if (empty($saasuserid)) {
- $user = User::findOne(['binding' => $mobile, 'store_id' => $saas_user->store_id, 'is_delete' => 0]);
- if (!$user) {
- // 处理未找到用户的情况
- return [
- 'code' => 1,
- 'msg' => '未找到当前用户'
- ];
- }
- }
- // 是否开启强制绑定手机号
- $is_open_bind = Option::get(OptionSetting::STORE_LOGIN_FORCIBLY_BIND_MOBILE, $this->store_id, 'store')['value'];
- $share = $share_user = null;
- if ($user->parent_id > 0) {
- $share = Share::findOne(['user_id' => $user->parent_id]);
- $share_user = User::findOne(['id' => $share->user_id]);
- }
- $data = [
- 'access_token' => $saas_user->access_token,
- 'nickname' => $saas_user->name,
- 'avatar_url' => $saas_user->avatar,
- // '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,
- 'store_id' => $saas_user->store_id,
- ];
- // 如果找到了用户,继续后续逻辑
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $data
- ];
- }
- /**saas端获取uninid*/
- public function getsaasunid(){
- $session = self::getWechat()->auth->session($this->code);
- if (!$session || empty($session['openid'])) {
- throw new \Exception('获取openid失败.');
- }
- $decryptedData = self::getWechat()->encryptor->decryptData($session['session_key'], $this->iv, $this->encrypted_data);
- $unionId = isset($session['unionid']) ? $session['unionid'] : '';
- }
- /**
- * 微信登录方法
- * @return array
- */
- private function loginWX()
- {
- if ($this->validate()) {
- try {
- //获取session
- $session = self::getWechat()->auth->session($this->code);
- if (!$session || empty($session['openid'])) {
- throw new \Exception('获取openid失败.');
- }
- // 验证
- $decryptedData = self::getWechat()->encryptor->decryptData($session['session_key'], $this->iv, $this->encrypted_data);
- $user_info = json_decode($this->user_info, true);
- $user_info['nickName'] = preg_replace('/[\xf0-\xf7].{3}/', '', $user_info['nickName']);
- $data = [
- 'openId' => $session['openid'],
- 'nickName' => isset($user_info['nickName']) ? $user_info['nickName'] : $this->getNickName(),
- 'gender' => $user_info['gender'],
- 'city' => $user_info['city'],
- 'province' => $user_info['province'],
- 'country' => $user_info['country'],
- 'avatarUrl' => $user_info['avatarUrl'] ?: \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/avatar.png',
- 'unionId' => isset($session['unionid']) ? $session['unionid'] : '',
- ];
- $isNew = 0;
- $user = User::find()->where(['store_id' => $this->store_id])->andWhere(['binding' => $this->phone])->orderBy('id desc')->one();//, 'platform' => 0
- if (!$user) {
- $isNew = 1;
- $user = new User();
- $user->type = User::USER_TYPE_NORMAL;
- $user->binding = isset($decryptedData['phoneNumber']) ? $decryptedData['phoneNumber'] : '';
- $user->username = $data['openId'];
- $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->wechat_open_id = $data['openId'];
- $user->wechat_union_id = $data['unionId'];
- $user->store_id = $this->store_id;
- $user->platform = User::USER_FROM_WECHAT; // 微信
- }
- $user->binding = $this->phone;
- $user->nickname = $data['nickName'];
- $user->avatar_url = $data['avatarUrl'];
- if (empty($user->wechat_open_id) || (!empty($data['openId']) && $user->wechat_open_id !== $data['openId'])) {
- $user->wechat_open_id = $data['openId'];
- }
- if (empty($user->wechat_union_id) || (!empty($data['unionId']) && $user->wechat_union_id !== $data['unionId'])) {
- $user->wechat_union_id = $data['unionId'];
- }
- if (!$user->save()) {
- return [
- 'code' => 1,
- 'msg' => '登陆失败',
- 'data' => $user->getErrorSummary(false)[0]
- ];
- }
- // 创建平台会员saas_user
- $saas_user = SaasUser::find()->where(['mobile' => $user->binding, 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
- if (!$saas_user) {
- $saas_user = new SaasUser();
- $saas_user->mobile = $user->binding;
- $saas_user->store_id = $this->store_id;
- $saas_user->save();
- }
- // 是否开启强制绑定手机号
- $is_open_bind = Option::get(OptionSetting::STORE_LOGIN_FORCIBLY_BIND_MOBILE, $this->store_id, 'store')['value'];
- $share = $share_user = null;
- if ($user->parent_id > 0) {
- $share = Share::findOne(['user_id' => $user->parent_id]);
- $share_user = User::findOne(['id' => $share->user_id]);
- }
- 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->isNew = $isNew;
- $res = $bindForm->save();
- //debug_log([__METHOD__, __LINE__, $res], "app_debug.log");
- }
- //go用户同步
- GolangOrderForm::userSyncUserInfo([
- "nick_name"=>(string)$user->binding,
- "store_id"=>(int)$this->store_id,
- "user_name"=>(string)$user->binding,
- "phone"=>(string)$user->binding,
- "parent_id"=>(int)$this->share_user_id,
- 'avatar'=>(string)$user->avatar_url,
- ]);
- $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' => '登陆成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => '登录失败',
- 'data' => $e->getMessage() . 'line:' . $e->getLine() . 'file:' . $e->getFile()
- ];
- }
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- }
- /**
- * app 微信登录
- * @return array
- * @throws \yii\base\Exception
- */
- public function loginAppWechat()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- $user_info = Json::decode($this->user_info);
- $user = null;
- if (isset($user_info['unionId']) && $user_info['unionId'] != '') {
- $user = User::findOne(['wechat_union_id' => $user_info['unionId'], 'store_id' => $this->store_id]);
- }
- if (!$user) {
- $user = User::findOne(['wechat_open_id' => $user_info['openId'], 'store_id' => $this->store_id]);
- }
- // 是否开启强制绑定手机号
- $is_open_bind = Option::get(OptionSetting::STORE_LOGIN_FORCIBLY_BIND_MOBILE, $this->store_id, 'store')['value'];
- if (!$user) {
- $user = new User();
- $user->type = 1;
- $user->username = $user_info['openId'];
- $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->created_at = time();
- $user->is_delete = 0;
- $user->wechat_open_id = $user_info['openId'];
- $user->wechat_union_id = isset($user_info['unionId']) ? $user_info['unionId'] : '';
- $user->nickname = preg_replace('/[\xf0-\xf7].{3}/', '', $user_info['nickName']);
- $user->avatar_url = $user_info['avatarUrl'] ? $user_info['avatarUrl'] : \Yii::$app->request->hostInfo . '/web/v1/statics/images/avatar.png';
- $user->store_id = 1;
- $user->platform = 3; // APP
- $user->level = -1;
- if (isset($user_info['appCid']) && !empty($user_info['appCid'])) {
- $user->appcid = $user_info['appCid'];
- }
- if ($user->save()) {
- // \Yii::$app->user->login($user);
- $parent = User::findOne($user->parent_id);
- $share = Share::findOne(['user_id' => $user->parent_id]);
- $data = [
- 'to_bind' => 1,
- 'nickname' => $user->nickname,
- 'binding' => $user->binding ? $user->binding : '',
- 'id' => $user->id,
- 'avatar_url' => $user->avatar_url,
- 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
- 'integral' => $user->integral ? $user->integral : 0,
- 'money' => $user->money ? $user->money : 0,
- 'blacklist' => $user->blacklist ? $user->blacklist : 0,
- 'access_token' => $user->access_token,
- 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- 'parent' => $share ? ($share->name ? $share->name : $parent->nickname) : "总店",
- 'level' => $user->level,
- 'is_open_bind' => $is_open_bind ? $is_open_bind : 0
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '登录失败',
- 'data' => $user->errors[0]
- ];
- }
- } else {
- if (isset($user_info['appCid']) && !empty($user_info['appCid']) && $user->appcid != $user_info['appCid']) {
- $user->appcid = $user_info['appCid'];
- }
- if (empty($user->wechat_union_id)) {
- $user->wechat_union_id = isset($user_info['unionId']) ? $user_info['unionId'] : '';
- }
- $user->nickname = preg_replace('/[\xf0-\xf7].{3}/', '', $user_info['nickName']);
- $user->avatar_url = $user_info['avatarUrl'];
- $user->save();
- // \Yii::$app->user->login($user);
- $parent = User::findOne($user->parent_id);
- $share = Share::findOne(['user_id' => $user->parent_id]);
- $data = [
- 'to_bind' => 0,
- 'nickname' => $user->nickname,
- 'binding' => $user->binding === null ? '' : $user->binding,
- 'id' => $user->id,
- 'avatar_url' => $user->avatar_url,
- 'is_distributor' => $user->is_distributor,
- 'integral' => $user->integral,
- 'money' => $user->money,
- 'blacklist' => $user->blacklist,
- 'access_token' => $user->access_token,
- 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- 'parent' => $share ? ($share->name ? $share->name : $parent->nickname) : "总店",
- 'level' => $user->level,
- 'is_open_bind' => $is_open_bind ? $is_open_bind : 0
- ];
- }
- $saas_user = SaasUser::find()->where(['mobile'=>$user->binding,'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
- $store_admin = StoreAdmin::findOne(['id' => $saas_user->store_id, 'is_delete' => 0, 'status' => 1]);
- $data['Authorization'] = $this->setToken($store_admin->id);
- return [
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => $data
- ];
- }
- /**
- * 手机号登录
- * @return array
- * @throws \yii\base\Exception
- */
- public function loginAppPhone()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- // 验证码验证
- $result = $this->verifySmsCode(self::CACHE_KEY_SMS_LOGIN);
- if ($result['code'] == 1) {
- return $result;
- }
- $parent_name = '总店';
- /**
- * @var User $user
- */
- $user = User::find()->where([
- 'store_id' => $this->store_id,
- 'type' => 1,
- 'is_delete' => 0,
- 'blacklist' => 0,
- 'binding' => $this->phone
- ])->one();
- if (!$user) {
- $password = '0';
- $user = new User();
- $user->type = 1;
- $user->username = \Yii::$app->security->generateRandomString();
- $user->password = $password;
- $user->auth_key = \Yii::$app->security->generateRandomString();
- $user->access_token = \Yii::$app->security->generateRandomString();
- $user->created_at = time();
- $user->is_delete = 0;
- $user->binding = $this->phone;
- $user->nickname = $this->getDefaultNickname($this->phone);
- $user->avatar_url = $this->getDefaultAvatar();
- $user->store_id = $this->store_id;
- $user->platform = User::USER_FROM_PHONE;
- if ($this->appCid) {
- $user->appcid = $this->appCid;
- }
- if ($this->parent_id > 0) {
- $parent_id = $this->parent_id;
- $setting = Option::get('share_basic_setting', get_store_id());
- $setting = $setting ? Json::decode($setting['value']) : [];
- if (!$setting || $setting['level']['value'] == 0) {
- $parent_id = 0;
- }
- if ($parent_id > 0) {
- $exists = Share::find()->andWhere([
- 'user_id' => $this->parent_id,
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- 'status' => 1
- ]);
- if ($exists->exists()) {
- $parent_name = $exists->one()->name;
- $user->parent_id = $parent_id;
- $user->old_parent_id = $parent_id;
- } else {
- $parent_name = User::findOne(['id' => $this->parent_id])->nickname;
- }
- }
- }
- if ($user->save()) {
- $data = [
- 'nickname' => $user->nickname,
- 'binding' => $user->binding,
- 'id' => $user->id,
- 'avatar_url' => $user->avatar_url,
- 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
- 'integral' => $user->integral ? $user->integral : 0,
- 'money' => $user->money ? $user->money : 0,
- 'blacklist' => $user->blacklist ? $user->blacklist : 0,
- 'access_token' => $user->access_token,
- 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- 'parent' => $parent_name,
- 'level' => -1
- ];
- return [
- 'code' => 0,
- 'msg' => '注册成功',
- 'data' => $data,
- ];
- } else {
- foreach ($user->errors as $error) {
- return [
- 'code' => 1,
- 'msg' => $error
- ];
- }
- }
- }
- if ($user->parent_id > 0) {
- $share = Share::findOne(['user_id' => $user->parent_id]);
- $parent_user = User::findOne($user->parent_id);
- $parent_name = $share ? ($share->name ? $share->name : $parent_user->nickname) : "总店";
- }
- $data = [
- 'nickname' => $user->nickname,
- 'binding' => $user->binding,
- 'id' => $user->id,
- 'avatar_url' => $user->avatar_url,
- 'is_distributor' => $user->is_distributor,
- 'integral' => $user->integral,
- 'money' => $user->money,
- 'blacklist' => $user->blacklist,
- 'access_token' => $user->access_token,
- 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
- 'parent' => $parent_name,
- 'level' => $user->level
- ];
- return [
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => $data,
- ];
- }
- public function phoneLogin()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- if ($this->login_type == 1) {
- // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
- // if ($is_password_login) {
- // return [
- // 'code' => 1,
- // 'msg' => '商城开启密码登录方式,请使用密码登录'
- // ];
- // }
- // 验证码验证
- $result = $this->verifySmsCode(self::CACHE_KEY_SMS_LOGIN);
- if ($result['code'] == 1) {
- return $result;
- }
- } else {
- // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
- // if (!$is_password_login) {
- // return [
- // 'code' => 1,
- // 'msg' => '商城未开启密码登录方式,请使用验证码登录'
- // ];
- // }
- // 密码验证
- $user = User::find()->where(['store_id' => $this->store_id, 'type' => 1, 'is_delete' => 0, 'binding' => $this->phone])->one();
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '用户不存在'
- ];
- }
- if (false === \Yii::$app->security->validatePassword($this->password, $user->password)) {
- return [
- 'code' => 1,
- 'msg' => '密码错误'
- ];
- }
- }
- ////debug_log('公众号登录01', 'off.log');
- $plat_openid = '';
- $openid = '';
- ////debug_log('公众号登录02_code=' . $this->code, 'off.log');
- if ($this->code) {
- try {
- ////debug_log('公众号登录03', 'off.log');
- if (is_wechat_platform()) {
- $wechat = self::getWechat();
- if (is_h5()) {
- ////debug_log('公众号登录04', 'off.log');
- $session = $wechat->oauth->userFromCode($this->code)->getTokenResponse();
- $plat_openid = $session['openid'];
- ////debug_log('公众号登录05'. json_encode($session, JSON_UNESCAPED_UNICODE), 'off.log');
- } else {
- $app = WechatMini::getWechatConfig($this->store_id);
- if (empty($app)) {
- return [
- 'code' => 1,
- 'msg' => '小程序参数获取失败!',
- ];
- }
- $session = $app->auth->session($this->code);
- if (empty($session)) {
- return [
- 'code' => 1,
- 'msg' => '小程序参数异常!',
- ];
- }
- if (!$session || empty($session['openid'])) {
- throw new \Exception('获取openid失败.');
- }
- $openid = $session['openid'];
- }
- } elseif (is_alipay_platform()) {
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- //debug_log('公众号登录06' . $e->getMessage(), 'off.log');
- }
- ////debug_log('公众号登录07', 'off.log');
- }
- ////debug_log('公众号登录08', 'off.log');
- // 创建平台会员saas_user
- $saas_user = SaasUser::find()->where(['mobile' => $this->phone, '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($this->phone, '******', 3, 6);
- $saas_user->mobile = $this->phone;
- $saas_user->platform_open_id = '';
- $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($this->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();
- }
- // $saas_user->platform_open_id = $this->phone;
- $saas_user->save();
- }
- // 平台登录 todo: 后续补充其他数据
- if ($this->store_id == 0) {
- return [
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $saas_user->access_token,
- 'nickname' => $saas_user->name,
- 'avatar_url' => $saas_user->avatar,
- 'id' => $saas_user->id,
- 'money' => $saas_user->share_profit,
- 'integral' => $saas_user->integral
- ]
- ];
- }
- $user = User::findOne(['binding' => $this->phone, 'store_id' => $this->store_id, 'is_delete' => 0]);
- $share = $share_user = null;
- $isNew = 0;
- if ($user) {
- if ($user->blacklist == '1') {
- return [
- 'code' => 1,
- 'msg' => '您的账号已被限制登录!',
- ];
- }
- if ($plat_openid) {
- $user->wechat_platform_open_id = $plat_openid;
- $user->save();
- }
- if ($openid) {
- $user->wechat_open_id = $openid;
- $user->save();
- }
- $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,
- 'wechat_app_open_id' => $user->wechat_app_open_id,
- 'wechat_platform_open_id' => $user->wechat_platform_open_id,
- 'h5_auth_link' => self::getAuthLink(),
- 'store_id' => $this->store_id,
- ];
- } else {
- $isNew = 1;
- $data = [
- 'nickName' => substr_replace($this->phone, '******', 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 = $this->phone;
- $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;
- $user->parent_id = 0;
- $user->old_parent_id = 0;
- $user->platform = is_alipay_platform() ? User::USER_FROM_ALIPAY : User::USER_FROM_PHONE; // 手机号
- if ($openid) {
- $user->wechat_open_id = $openid;
- }
- if ($plat_openid) {
- $user->wechat_platform_open_id = $plat_openid;
- }
- 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,
- 'wechat_app_open_id' => $user->wechat_app_open_id,
- 'wechat_platform_open_id' => $user->wechat_platform_open_id,
- 'alipay_open_id' => $user->alipay_open_id,
- 'h5_auth_link' => self::getAuthLink(),
- 'store_id' => $saas_user->store_id,
- ];
- }
- if ($this->share_user_id && $this->share_user_id > 0) {
- $bind = new BindForm();
- $bind->user_id = $data['id'];
- $bind->store_id = $this->store_id;
- $bind->isLogin = $this->isLogin;
- $bind->parent_id = $this->share_user_id;
- $bind->condition = 0;
- $bind->isNew = $isNew;
- $res = $bind->save();
- //debug_log([__METHOD__, __LINE__, $res], "app_debug.log");
- }
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => '登录成功'
- ];
- }
- public function phoneLoginCode($_alipay, $authCode)
- {
- $login_data = all_params();
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- if ($this->login_type == 1) {
- // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
- // if ($is_password_login) {
- // return [
- // 'code' => 1,
- // 'msg' => '商城开启密码登录方式,请使用密码登录'
- // ];
- // }
- // 验证码验证
- $result = $this->verifySmsCode(self::CACHE_KEY_SMS_LOGIN);
- if ($result['code'] == 1) {
- return $result;
- }
- } else {
- // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
- // if (!$is_password_login) {
- // return [
- // 'code' => 1,
- // 'msg' => '商城未开启密码登录方式,请使用验证码登录'
- // ];
- // }
- // 密码验证
- $user = User::find()->where(['store_id' => $this->store_id, 'type' => 1, 'is_delete' => 0, 'binding' => $this->phone])->one();
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '用户不存在'
- ];
- }
- if (false === \Yii::$app->security->validatePassword($this->password, $user->password)) {
- return [
- 'code' => 1,
- 'msg' => '密码错误'
- ];
- }
- }
- ////debug_log('公众号登录01', 'off.log');
- $plat_openid = '';
- $openid = '';
- $aplipay_user_id = '';
- ////debug_log('公众号登录02_code=' . $this->code, 'off.log');
- $this->code = "";
- if ($this->code) {
- try {
- ////debug_log('公众号登录03', 'off.log');
- if (is_wechat_platform()) {
- $wechat = self::getWechat();
- if (is_h5()) {
- ////debug_log('公众号登录04', 'off.log');
- $session = $wechat->oauth->userFromCode($this->code)->getTokenResponse();
- $plat_openid = $session['openid'];
- ////debug_log('公众号登录05'. json_encode($session, JSON_UNESCAPED_UNICODE), 'off.log');
- } else {
- $app = WechatMini::getWechatConfig($this->store_id);
- if (empty($app)) {
- return [
- 'code' => 1,
- 'msg' => '小程序参数获取失败!',
- ];
- }
- $session = $app->auth->session($this->code);
- if (empty($session)) {
- return [
- 'code' => 1,
- 'msg' => '小程序参数异常!',
- ];
- }
- if (!$session || empty($session['openid'])) {
- throw new \Exception('获取openid失败11.'.json_encode($app));
- }
- $openid = $session['openid'];
- }
- } elseif (is_alipay_platform()) {
- $keys = [
- 'alipay_platform'
- ];
- $data = Option::get($keys, 0, 'saas');
- $alipay_config = json_decode($data[0]['value'], true);
- // 获取user_id
- $config = [
- 'alipay' => [
- 'client_id' => $alipay_config['app_id'],
- 'rsa_private_key' => $alipay_config['app_private_key'],
- ]
- ];
- //获取支付宝用户信息
- $socialite = new SocialiteManager($config);
- $token = $socialite->create('alipay')->tokenFromCode($authCode);
- // if (empty($token['user_id'])) {
- if (empty($token['user_id']) && empty($token['open_id'])) {
- return [
- 'code' => 1,
- 'msg' => '获取user_id失败'
- ];
- }
- $aplipay_user_id = isset($token['user_id']) ? $token['user_id'] : $token['open_id'];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- //debug_log('公众号登录06' . $e->getMessage(), 'off.log');
- }
- ////debug_log('公众号登录07', 'off.log');
- }
- ////debug_log('公众号登录08', 'off.log');
- // 创建平台会员saas_user
- $saas_user = SaasUser::find()->where(['mobile' => $this->phone, '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($this->phone, '******', 3, 6);
- $saas_user->mobile = $this->phone;
- //$saas_user->platform_open_id = $openid;
- $saas_user->platform_open_id = '';
- $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->ali_user_id = $aplipay_user_id;
- $saas_user->parent_id = empty($this->parent_id) ? 0 : $this->parent_id;
- $saas_user->save();
- } else {
- if (empty($saas_user->name)) {
- $saas_user->name = substr_replace($this->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();
- }
- //$saas_user->platform_open_id = $openid;
- $saas_user->ali_user_id = $aplipay_user_id;
- // $saas_user->platform_open_id = $this->phone;
- $saas_user->save();
- }
- // 平台登录 todo: 后续补充其他数据
- if ($this->store_id == 0) {
- return [
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $saas_user->access_token,
- 'nickname' => $saas_user->name,
- 'avatar_url' => $saas_user->avatar,
- 'id' => $saas_user->id,
- 'money' => $saas_user->share_profit,
- 'integral' => $saas_user->integral
- ]
- ];
- }
- $user = User::findOne(['binding' => $this->phone, 'store_id' => $this->store_id, 'is_delete' => 0]);
- $share = $share_user = null;
- $isNew = 0;
- if ($user) {
- if ($user->blacklist == '1') {
- return [
- 'code' => 1,
- 'msg' => '您的账号已被限制登录!',
- ];
- }
- if ($plat_openid) {
- $user->wechat_platform_open_id = $plat_openid;
- $user->save();
- }
- if ($openid) {
- $user->wechat_open_id = $openid;
- $user->save();
- }
- if ($aplipay_user_id) {
- $user->alipay_open_id = $aplipay_user_id;
- $user->save();
- }
- $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,
- 'wechat_app_open_id' => $user->wechat_app_open_id,
- 'wechat_platform_open_id' => $user->wechat_platform_open_id,
- 'alipay_open_id' => $user->alipay_open_id,
- 'h5_auth_link' => self::getAuthLink(),
- 'store_id' => $this->store_id,
- ];
- } else {
- $isNew = 1;
- $data = [
- 'nickName' => substr_replace($this->phone, '******', 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 = $this->phone;
- $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;
- $user->parent_id = empty($login_data['share_user_id']) ? 0 : $login_data['share_user_id'];
- $user->old_parent_id = empty($login_data['share_user_id']) ? 0 : $login_data['share_user_id'];
- $user->platform = 0;//is_alipay_platform() ? User::USER_FROM_ALIPAY : User::USER_FROM_PHONE; // 手机号
- if ($openid) {
- $user->wechat_open_id = $openid;
- }
- if ($aplipay_user_id) {
- $user->alipay_open_id = $aplipay_user_id;
- }
- if ($plat_openid) {
- $user->wechat_platform_open_id = $plat_openid;
- }
- 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,
- 'wechat_app_open_id' => $user->wechat_app_open_id,
- 'wechat_platform_open_id' => $user->wechat_platform_open_id,
- 'alipay_open_id' => $user->alipay_open_id,
- 'h5_auth_link' => self::getAuthLink(),
- 'store_id' => $saas_user->store_id,
- ];
- }
- if ($this->share_user_id && $this->share_user_id > 0) {
- $bind = new BindForm();
- $bind->user_id = $data['id'];
- $bind->store_id = $this->store_id;
- $bind->isLogin = $this->isLogin;
- $bind->parent_id = $this->share_user_id;
- $bind->condition = 0;
- $bind->isNew = $isNew;
- $res = $bind->save();
- //debug_log([__METHOD__, __LINE__, $res], "app_debug.log");
- }
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => '登录成功'
- ];
- }
- public function phoneLoginCodeNew()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- if ($this->login_type == 1) {
- // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
- // if ($is_password_login) {
- // return [
- // 'code' => 1,
- // 'msg' => '商城开启密码登录方式,请使用密码登录'
- // ];
- // }
- // 验证码验证
- $result = $this->verifySmsCode(self::CACHE_KEY_SMS_LOGIN);
- if ($result['code'] == 1) {
- return $result;
- }
- } else {
- // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
- // if (!$is_password_login) {
- // return [
- // 'code' => 1,
- // 'msg' => '商城未开启密码登录方式,请使用验证码登录'
- // ];
- // }
- // 密码验证
- $user = User::find()->where(['store_id' => $this->store_id, 'type' => 1, 'is_delete' => 0, 'binding' => $this->phone])->one();
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '用户不存在'
- ];
- }
- if(!preg_match("/^111\d{8}$/", $this->phone)){
- if (false === \Yii::$app->security->validatePassword($this->password, $user->password)) {
- return [
- 'code' => 1,
- 'msg' => '密码错误'
- ];
- }
- }
-
- }
- ////debug_log('公众号登录01', 'off.log');
- $plat_openid = '';
- $openid = '';
- ////debug_log('公众号登录02_code=' . $this->code, 'off.log');
- if ($this->code) {
- try {
- ////debug_log('公众号登录03', 'off.log');
- if (is_wechat_platform()) {
- // $wechat = self::getWechat();
- $wechat = WechatMini::getWechatConfig(0, 0, 0, 0, 1);
- if (is_h5()) {
- ////debug_log('公众号登录04', 'off.log');
- $session = $wechat->oauth->userFromCode($this->code)->getTokenResponse();
- $plat_openid = $session['openid'];
- ////debug_log('公众号登录05'. json_encode($session, JSON_UNESCAPED_UNICODE), 'off.log');
- } else {
- $app = WechatMini::getWechatConfig(0, 0, 0, 0, 1);
- if (empty($app)) {
- return [
- 'code' => 1,
- 'msg' => '小程序参数获取失败!',
- ];
- }
- $session = $app->auth->session($this->code);
- if (empty($session)) {
- return [
- 'code' => 1,
- 'msg' => '小程序参数异常!',
- ];
- }
- if (!$session || empty($session['openid'])) {
- throw new \Exception('获取openid失败.');
- }
- $openid = $session['openid'];
- }
- } elseif (is_alipay_platform()) {
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- //debug_log('公众号登录06' . $e->getMessage(), 'off.log');
- }
- ////debug_log('公众号登录07', 'off.log');
- }
- ////debug_log('公众号登录08', 'off.log');
- // 创建平台会员saas_user
- $saas_user = SaasUser::find()->where(['mobile' => $this->phone, '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($this->phone, '******', 3, 6);
- $saas_user->mobile = $this->phone;
- $saas_user->platform_open_id = $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($this->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();
- }
- $saas_user->platform_open_id = $openid;
- // $saas_user->platform_open_id = $this->phone;
- $saas_user->save();
- }
- // 平台登录 todo: 后续补充其他数据
- if ($this->store_id == 0) {
- return [
- 'code' => 0,
- 'msg' => '登录成功',
- 'data' => [
- 'access_token' => $saas_user->access_token,
- 'nickname' => $saas_user->name,
- 'avatar_url' => $saas_user->avatar,
- 'id' => $saas_user->id,
- 'money' => $saas_user->share_profit,
- 'integral' => $saas_user->integral
- ]
- ];
- }
- $user = User::findOne(['binding' => $this->phone, 'store_id' => $this->store_id, 'is_delete' => 0]);
- $share = $share_user = null;
- $isNew = 0;
- if ($user) {
- if ($user->blacklist == '1') {
- return [
- 'code' => 1,
- 'msg' => '您的账号已被限制登录!',
- ];
- }
- if ($plat_openid) {
- $user->wechat_platform_open_id = $plat_openid;
- $user->save();
- }
- if ($openid) {
- $user->wechat_open_id = $openid;
- $user->save();
- }
- $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,
- 'wechat_app_open_id' => $user->wechat_app_open_id,
- 'wechat_platform_open_id' => $user->wechat_platform_open_id,
- 'h5_auth_link' => self::getAuthLink(),
- 'store_id' => $this->store_id
- ];
- } else {
- $isNew = 1;
- $data = [
- 'nickName' => substr_replace($this->phone, '******', 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 = $this->phone;
- $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->password = \Yii::$app->security->generatePasswordHash("123456");
- $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;
- $user->platform = is_alipay_platform() ? User::USER_FROM_ALIPAY : User::USER_FROM_PHONE; // 手机号
- if ($openid) {
- $user->wechat_open_id = $openid;
- }
- if ($plat_openid) {
- $user->wechat_platform_open_id = $plat_openid;
- }
- 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,
- 'wechat_app_open_id' => $user->wechat_app_open_id,
- 'wechat_platform_open_id' => $user->wechat_platform_open_id,
- 'alipay_open_id' => $user->alipay_open_id,
- 'h5_auth_link' => self::getAuthLink(),
- 'store_id' => $saas_user->store_id
- ];
- }
- if ($this->share_user_id && $this->share_user_id > 0) {
- $bind = new BindForm();
- $bind->user_id = $data['id'];
- $bind->store_id = $this->store_id;
- $bind->isLogin = $this->isLogin;
- $bind->parent_id = $this->share_user_id;
- $bind->condition = 0;
- $bind->isNew = $isNew;
- $res = $bind->save();
- //debug_log([__METHOD__, __LINE__, $res], "app_debug.log");
- }
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => '登录成功'
- ];
- }
- /**
- * 验证验证码
- * @param string $key
- * @return array
- */
- public function verifySmsCode($key)
- {
- $smsCode = cache()->get($this->phone . $key . $this->store_id);
- //debug_log([__METHOD__, __LINE__, "KEY:".$this->phone . $key . $this->store_id.',code:'.$smsCode], "app_debug.log");
- if (!$smsCode) {
- return [
- 'code' => 1,
- 'msg' => '验证码错误',
- ];
- }
- if (strval($this->verify_code) !== strval($smsCode)) {
- return [
- 'code' => 1,
- 'msg' => '验证码错误',
- ];
- }
- cache()->delete($this->phone . $key . $this->store_id);
- return [
- 'code' => 0,
- 'msg' => '验证成功',
- ];
- }
- /**
- * 返回默认头像
- */
- protected function getDefaultAvatar()
- {
- return \Yii::$app->request->hostInfo . '/web/v1/statics/images/avatar.png';
- }
- /**
- * 返回默认用户名
- */
- protected function getDefaultNickname($phone)
- {
- $randStr = \Yii::$app->security->generateRandomString(5);
- return $randStr . '_' . substr($phone, -4, 4);
- }
- /**
- * 发送验证码
- * @param string $key
- * @return array
- */
- public function sendCode($key)
- {
- $is_debug=Option::get('is_debug', 0, 'saas', 0)['value'];
- if(preg_match("/^111\d{8}$/", $this->phone) && $is_debug == 1){
- $sms_code = 999999;
- // 验证码有效期5分钟
- cache()->set($this->phone . $key . $this->store_id, $sms_code, 600);
- //debug_log([__METHOD__, __LINE__, "KEY:".$this->phone . $key . $this->store_id.',code:'.$sms_code], "app_debug.log");
- return [
- 'code' => 0,
- 'msg' => '发送成功',
- ];
- }
- else{
- if (!$this->phone || !preg_match("/^1[3456789]\d{9}$/", $this->phone)) {
- return [
- 'code' => 1,
- 'msg' => '参数不正确2',
- ];
- }
- }
- $sms_code = mt_rand(100000, 999999);
- $sendResult = NoticeSend::VerifyCode($this->phone, $sms_code,true);
- if ($sendResult['code'] == 1) {
- return $sendResult;
- }
- // 验证码有效期5分钟
- cache()->set($this->phone . $key . $this->store_id, $sms_code, 600);
- return [
- 'code' => 0,
- 'msg' => '发送成功',
- ];
- }
- /**
- * 绑定手机号
- * @return array
- */
- public function bindPhone()
- {
- if (!trim($this->phone) || !$this->verify_code || !preg_match("/^1[3456789]\d{9}$/", $this->phone)) {
- return [
- 'code' => 1,
- 'msg' => '参数不正确',
- ];
- }
- $user = User::find()->where([
- 'store_id' => $this->store_id,
- 'type' => 1,
- 'is_delete' => 0,
- 'blacklist' => 0,
- 'binding' => $this->phone
- ])->one();
- if ($user) {
- return [
- 'code' => 1,
- 'msg' => '该手机号已经被绑定',
- ];
- }
- // 验证码验证
- $result = $this->verifySmsCode(self::CACHE_KEY_BIND_PHONE);
- if ($result['code'] == 1) {
- return $result;
- }
- $this->user->binding = trim($this->phone);
- if ($this->user->save()) {
- return [
- 'code' => 0,
- 'msg' => '绑定成功',
- ];
- } else {
- foreach ($this->user->errors as $error) {
- return [
- 'code' => 1,
- 'msg' => $error
- ];
- }
- }
- }
- /**
- * 微信浏览器h5授权链接
- */
- public static function getAuthLink()
- {
- $redirect_url = \Yii::$app->request->hostInfo . '/h5/#/user/my/setting';
- return getAuthLink(get_store_id(), $redirect_url, 1);
- }
- // 微信公众号获取openid
- public function getWeixinOpenid()
- {
- /*
- AppID:wx46115cd199a3ad95
- AppSecret:3e3dc22f7ec78d0ae9e7b2b0118ceff8
- */
- // $keys = [
- // 'wechat_platform'
- // ];
- // $data = Option::get($keys, 0, 'saas');
- // $wechat_config = json_decode($data[0]['value'], true);
- // // 你的微信公众号AppID和AppSecret
- // $appid = $wechat_config['appid'];
- // $appsecret = $wechat_config['appsecret'];
- $appid = 'wx46115cd199a3ad95';
- $appsecret = '3e3dc22f7ec78d0ae9e7b2b0118ceff8';
- // 请求URL
- $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $this->code . "&grant_type=authorization_code";
- // 获取access_token和openid
- $response = file_get_contents($url);
- $refdata = json_decode($response, true);
- if (isset($refdata['access_token']) && isset($refdata['openid'])) {
- $openid = $refdata['openid'];
- $data['openid']=$openid;
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => '登录成功',
- 'othen' => $refdata,
- ];
- } else {
- return ['code' => 1, 'msg' => '参数不正确'];
- }
- }
- // 微信小程序获取openid 需要带入store_id 默认=-1
- public function getWeixinMiniOpenid($mobile='')
- {
- if ($this->code) {
- try {
- if (empty($this->store_id)) {
- $this->store_id = -1;
- }
- $app = WechatMini::getWechatConfig($this->store_id);
- if (empty($app)) {
- if (is_isv()) {
- $wechat_config = WechatConfig::find()->where(['store_id' => $this->store_id, 'type' => 1])->select(['mch_id','app_id'])->asArray()->one();
- //小程序登陆获取openid
- $app = (new NewMerchantForm())->miniProgram($wechat_config['app_id']);
- }
- if (empty($app)) {
- return [
- 'code' => 1,
- 'msg' => '小程序参数获取失败!',
- ];
- }
- }
- $session = $app->auth->session($this->code);
- if (empty($session)) {
- return [
- 'code' => 1,
- 'msg' => '小程序参数异常!',
- ];
- }
- if (!$session || empty($session['openid'])) {
- throw new \Exception('获取openid失败.');
- }
- $data['openid'] = $session['openid'];
- //如果店铺id=-1
- if($this->store_id=-1){
- if (!empty($mobile) && !empty($data['openid'])) {
- $saas_user = SaasUser::findOne(['mobile' => $mobile]);
- if ($saas_user) {
- $saas_user->platform_open_id = $data['openid'];
- $saas_user->save();
- }
- }
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => '登录成功',
- 'othen' => $session,
- ];
- }
- // 微信小程序获取openid 需要带入store_id 默认=-1(本获取openid来分享商品二维码)
- public function getWeixinMiniOpenidShareCode($mobile='')
- {
- if ($this->code) {
- try {
- if (empty($this->store_id)) {
- $this->store_id = -1;
- }
- $app = WechatMini::getWechatConfig($this->store_id);
- if (empty($app)) {
- return [
- 'code' => 1,
- 'msg' => '小程序参数获取失败!',
- ];
- }
- $session = $app->auth->session($this->code);
- if (empty($session)) {
- return [
- 'code' => 1,
- 'msg' => '小程序参数异常!',
- ];
- }
- if (!$session || empty($session['openid'])) {
- throw new \Exception('获取openid失败.');
- }
- $data['openid'] = $session['openid'];
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => '获取成功',
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- }
- // 支付宝小程序获取userid
- public function getAlipayMiniUserid($authCode)
- {
- if ($authCode) {
- try {
- $keys = [
- 'alipay_platform'
- ];
- $data = Option::get($keys, 0, 'saas');
- $alipay_config = json_decode($data[0]['value'], true);
- // 获取user_id
- $config = [
- 'alipay' => [
- 'client_id' => $alipay_config['app_id'],
- 'rsa_private_key' => $alipay_config['app_private_key'],
- ]
- ];
- //获取支付宝用户信息
- $socialite = new SocialiteManager($config);
- $token = $socialite->create('alipay')->tokenFromCode($authCode);
- // if (empty($token['user_id'])) {
- if (empty($token['user_id']) && empty($token['open_id'])) {
- return [
- 'code' => 1,
- 'msg' => '获取user_id失败'
- ];
- }
- // $aplipay_user_id = isset($token['user_id']) ? $token['user_id'] : $token['open_id'];
- $dataref['openid'] = isset($token['user_id']) ? $token['user_id'] : '';
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- return [
- 'code' => 0,
- 'data' => $dataref,
- 'msg' => '登录成功',
- 'othen' => $token,
- ];
- } //生成token
- public function setToken($id, $type = 0) {
- if ($type) {
- $admin_type = Admin::ADMIN_TYPE_STORE;
- $store_admin_id = 0;
- } else {
- $store_admin = StoreAdmin::findOne($id);
- $admin_type = Admin::ADMIN_TYPE_MINI_ADMIN;
- $store_admin_id = $store_admin->id;
- }
- $admin = Admin::findOne(['type' => $admin_type, 'type_id' => $id, 'is_delete' => 0]);
- $time = time();
- $payload = [
- 'iss' => \Yii::$app->request->hostInfo,
- 'iat' => $time,
- 'exp' => $time + 86400,
- 'admin_id' => $admin->id,
- 'username' => $admin->username,
- 'store_admin_id' => $store_admin_id
- ];
- return \Yii::$app->jwt->createToken($payload);
- }
- // 后台用户关联商城token
- public function getRelatedToken($saasUserId,$mobile,$storeId)
- {
- $where = [];
- // 判断 saasuserid 是否为空并添加到查询条件中
- if (!empty($saasUserId)) {
- $where['id'] = $saasUserId;
- }
- // 判断 mobile 是否为空并添加到查询条件中
- if (!empty($mobile)) {
- $where['mobile'] = $mobile;
- }
- // 查找符合条件的 SaasUser
- $saas_user = SaasUser::find()->where($where)->andWhere(['is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
- // 判断是否找到符合条件的用户
- if (!$saas_user) {
- // 处理未找到用户的情况
- return [
- 'code' => 1,
- 'msg' => '未找到当前用户'
- ];
- }
- $user = User::findOne(['binding' => $mobile, 'store_id' => $storeId, 'is_delete' => 0]);
- if (!$user) {
- // 处理未找到用户的情况
- return [
- 'code' => 1,
- 'msg' => '未找到当前用户'
- ];
- }
- // 是否开启强制绑定手机号
- $is_open_bind = Option::get(OptionSetting::STORE_LOGIN_FORCIBLY_BIND_MOBILE, $this->store_id, 'store')['value'];
- $share = $share_user = null;
- 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,
- '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,
- '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,
- 'store_id' => $saas_user->store_id,
- ];
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $data
- ];
- }
- }
|