| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\constants\OptionSetting;
- use app\models\Option;
- use app\models\Share;
- use app\models\StoreMini;
- use app\models\User;
- use app\models\SaasUser;
- use app\modules\alliance\models\ApiModel;
- use EasyWeChat\Factory;
- use yii\base\BaseObject;
- use app\models\StoreCloud;
- class AuthLoginForm extends ApiModel
- {
- // 商城id
- public $store_id;
- //绑定店铺来源
- public $store_id_first;
- public $password; // 密码
- // 微信小程序
- public $code;
- public $encryptedData;
- public $iv;
- public $mini_type;
- // 分销商user_id
- public $share_user_id;
- // 微信小程序
- const PLATFORM_WX = 'wx';
- // 微信公众号
- const PLATFORM_MP = 'mp';
- // app端微信
- const PLATFORM_APP = 'app';
- // 手机号注册登录
- const PLATFORM_PHONE = 'phone';
- public $_platform;
- public $platform; // wechat(微信), alipay(支付宝), douyin(抖音)
- public $nickname;
- public $avatar_url;
- /**
- * @var \EasyWeChat\MiniProgram\Application
- */
- public $wechat;
- public function rules()
- {
- return [
- ['_platform', 'in', 'range' => [self::PLATFORM_WX, self::PLATFORM_MP, self::PLATFORM_APP, self::PLATFORM_PHONE]],
- ['_platform', 'required'],
- [['share_user_id', 'store_id', 'store_id_first'], 'integer'],
- [['code', 'encryptedData', 'iv', 'platform', 'mini_type', 'avatar_url', 'nickname'], 'string'],
- [['code', 'encryptedData', 'iv', 'password'], 'trim'],
- ];
- }
- public function editPassword()
- {
- $user = get_saas_user();
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '用户不存在',
- ];
- }
- $user = User::find()->where(['store_id' => -1, 'is_delete' => 0, 'binding' => $user->mobile])->one();
- if (!$user) {
- return [
- 'code' => 1,
- 'msg' => '用户不存在',
- ];
- }
- $user->password = \Yii::$app->security->generatePasswordHash($this->password);
- if ($user->save()) {
- return [
- 'code' => 0,
- 'msg' => '修改成功',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '修改失败',
- ];
- }
- }
- public function platformPhoneAuth()
- {
- try {
- if (is_isv()) {
- $config = [
- 'app_id' => Option::get("platform_third_appid",0,'saas')['value'],
- 'secret' => Option::get("platform_third_secret",0,'saas')['value'],
- 'token' => Option::get("platform_token",0,'saas')['value'],
- 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value']
- ];
- $openPlatform = Factory::openPlatform($config);
- if (!empty(get_mini_id())) {
- $mini = StoreMini::findOne(get_mini_id());
- $app = $openPlatform->miniProgram($mini->appid,$mini->authorizer_refresh_token);
- $session = $app->auth->session($this->code);
- $decryptedData = $app->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
- } else {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- } elseif ($this->platform == 'bytedance') {
- /**
- * @var \ByteDance\MiniProgram\Application $byteDance
- */
- $byteDance = \Yii::$app->controller->byteDance;
- if (!$byteDance) {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- $session = $byteDance->auth->session($this->code);
- $decryptedData = $byteDance->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
- } else {
- if ($this->mini_type) {
- $wechat = Option::getSaasPlatformMchWechat();
- } else {
- $wechat = Option::getSaasPlatformWechat();
- }
- $config = [
- 'app_id' => $wechat['appid'],
- 'secret' => $wechat['key'],
- 'response_type' => 'array'
- ];
- $this->wechat = Factory::miniProgram($config);
- if (!$this->wechat) {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- $session = $this->wechat->auth->session($this->code);
- $decryptedData = $this->wechat->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
- }
- if (isset($decryptedData['phoneNumber']) && !empty($decryptedData['phoneNumber'])) {
- // 创建平台会员saas_user
- $saas_user = SaasUser::find()->where(['mobile' => $decryptedData['phoneNumber'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
- if (!$saas_user) {
- // 针对同一个微信号使用不同的手机号问题,直接更新最新的手机号
- if ($session['openid']) {
- if(is_merchant()){
- $saas_user = SaasUser::findOne(['platform_open_id_merchant' => $session['openid'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE]);
- }else{
- $saas_user = SaasUser::findOne(['platform_open_id' => $session['openid'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE]);
- }
- }
- if ($saas_user) {
- $saas_user->mobile = $decryptedData['phoneNumber'];
- if (empty($saas_user->name)) {
- $saas_user->name = substr_replace($decryptedData['phoneNumber'], '****', 3, 4);
- }
- if (empty($saas_user->avatar)) {
- $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- }
- if (empty($saas_user->access_token)) {
- $saas_user->access_token = \Yii::$app->security->generateRandomString();
- }
- if($this->store_id_first > 0){
- $saas_user->store_id = $this->store_id_first;
- }
- if(is_merchant()){
- $saas_user->platform_open_id_merchant = $session['openid'];
- }else{
- $saas_user->platform_open_id = $session['openid'];
- }
- $saas_user->save();
- } else {
- $saas_user = new SaasUser();
- $saas_user->price = '0.00';
- $saas_user->access_token = \Yii::$app->security->generateRandomString();
- $saas_user->name = substr_replace($decryptedData['phoneNumber'], '******', 3, 6);
- $saas_user->mobile = $decryptedData['phoneNumber'];
- if ($this->platform == 'bytedance') {
- $saas_user->bytedance_open_id = $session['openid'];
- } else {
- if(is_merchant()){
- $saas_user->platform_open_id_merchant = $session['openid'];
- }else{
- $saas_user->platform_open_id = $session['openid'];
- }
- }
- $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- $saas_user->store_id = $this->store_id;
- if($this->store_id_first > 0){
- $saas_user->store_id = $this->store_id_first;
- }
- $saas_user->save();
- }
- } else {
- if (empty($saas_user->name)) {
- $saas_user->name = substr_replace($decryptedData['phoneNumber'], '****', 3, 4);
- }
- if (empty($saas_user->avatar)) {
- $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- }
- if (empty($saas_user->access_token)) {
- $saas_user->access_token = \Yii::$app->security->generateRandomString();
- }
- if ($this->platform == 'bytedance') {
- $saas_user->bytedance_open_id = $session['openid'];
- } else {
- if(is_merchant()){
- $saas_user->platform_open_id_merchant = $session['openid'];
- }else{
- $saas_user->platform_open_id = $session['openid'];
- }
- }
- $saas_user->save();
- }
- //绑定上下级
- if ($this->share_user_id && $this->share_user_id > 0) {
- $saas_user->parent_id = $this->share_user_id;
- $saas_user->save();
- }
- if($this->nickname || $this->avatar_url){
- $saas_user->name = $this->nickname;
- $saas_user->avatar = $this->avatar_url;
- if (!$saas_user->save()) {
- throw new \Exception(json_encode($saas_user->errors));
- }
- }
- $storeCloud = StoreCloud::find()
- ->where(['is_delete' => 0, 'is_enable' => 1, 'saas_user_id' => $saas_user->id])
- ->one();
-
- // 平台登录 todo: 后续补充其他数据
- 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,
- 'is_can_distribution' => $storeCloud && $storeCloud->store_id > 0 ? 1 : 0, // 是否有商城(是否可以铺货)
- 'mobile' => $saas_user->mobile
- ]
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '登录失败',
- ];
- }
- } catch (\Exception $e) {
- \Yii::error($e->getMessage() . ' file => ' . $e->getFile() . ' line => ' . $e->getLine());
- return [
- 'code' => 1,
- 'msg' => '登录失败',
- ];
- }
- }
- public function code() {
- $data = [
- 'avatar_url' => '',
- 'nickname' => '',
- ];
- try {
- if (is_merchant()) {
- $wechat = Option::getSaasPlatformMchWechat();
- } else {
- $wechat = Option::getSaasPlatformWechat();
- }
- $config = [
- 'app_id' => $wechat['appid'],
- 'secret' => $wechat['key'],
- 'response_type' => 'array'
- ];
- $this->wechat = Factory::miniProgram($config);
- if (!$this->wechat) {
- return [
- 'code' => 1,
- 'msg' => '登录异常'
- ];
- }
- $session = $this->wechat->auth->session($this->code);
- //获取session
- // $session = self::getWechat()->auth->session($this->code);
- if (!$session || empty($session['openid'])) {
- throw new \Exception('获取openid失败.');
- }
- $openid = $session['openid'];
- if(is_merchant()){
- $saas_user = SaasUser::findOne(['platform_open_id_merchant' => $openid]);
- }else{
- $saas_user = SaasUser::findOne(['platform_open_id' => $openid]);
- }
- if ($saas_user) {
- $data['avatar_url'] = $saas_user->avatar;
- $data['nickname'] = $saas_user->name;
- $data['id'] = $saas_user->id;
- }
- return [
- 'code' => 0,
- 'data' => $data,
- 'msg' => 'ok'
- ];
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, $e]);
- return [
- 'code' => 0,
- 'msg' => 'error',
- 'data' => $data,
- ];
- }
- }
- }
|