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 ]; } }