| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- /*
- * @Author: your name
- * @Date: 2021-03-02 09:50:20
- * @LastEditTime: 2021-04-29 14:40:28
- * @LastEditors: Please set LastEditors
- * @Description: In User Settings Edit
- * @FilePath: \admin_php\modules\client\behaviors\Auth.php
- */
- namespace app\modules\client\behaviors;
- use app\constants\OptionSetting;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\User;
- use app\models\UserAuditLog;
- use app\models\UserAuditSetting;
- use yii\base\ActionFilter;
- use Yii;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- use yii\web\Response;
- /**
- * Class Auth
- * @package app\modules\client\behaviors
- */
- class Auth extends ActionFilter
- {
- // 不验证登录白名单
- const ALLOW_LIST = [
- 'client/v1/store/search-params',
- 'client/v1/user/index',
- 'client/v1/default/store',
- 'client/v1/diy/index',
- 'client/v1/default/coupon-list',
- 'client/v1/diy/goods',
- 'client/v1/diy/goods',
- 'client/v1/default/topic-type',
- 'client/v1/diy/mch',
- 'client/v1/default/topic-list',
- 'client/v1/passport/login',
- 'client/v1/passport/code',
- 'client/v1/diy/template-unit',
- 'client/v1/diy/template',
- 'client/v1/passport/login',
- 'client/v1/passport/send-code',
- 'client/v1/user/user-empower',
- 'client/v1/user/session-get',
- // 'client/v1/user/user-binding',
- 'client/v1/upload/upload-image',
- 'client/v1/upload/upload-video',
- 'client/v1/diy/shop',
- 'client/v1/default/cat-list',
- 'client/v1/default/goods-list',
- 'client/v1/goods/goods',
- 'client/v1/default/browse',
- 'client/v1/goods/address',
- 'client/v1/goods/goods-recommend',
- 'client/v1/goods/mch-recommend',
- 'client/v1/default/comment-list',
- 'client/v1/goods/get-goods-youhui',
- 'client/v1/default/goods-attr-info',
- 'client/v1/mch/index/location-info',
- 'client/v1/default/district',
- 'client/v1/mch/index/mch-list',
- 'client/v1/mch/index/shop',
- 'client/v1/shop/nearby',
- 'client/v1/default/get-brand-list',
- 'client/v1/cart/list',
- 'client/v1/goods/guess-like',
- 'client/v1/default/buy-data',
- 'client/v1/passport/auth-phone',
- 'client/v1/store/list',
- 'client/v1/store/search-params',
- 'client/v1/passport/platform-auth-phone',
- 'client/v1/alipay/passport/auth',
- 'client/v1/alipay/passport/phone',
- 'client/v1/saas/search-keyword',
- 'client/v1/saas/add-history',
- 'client/v1/diy/new-index',
- 'client/v1/diy/system-template-list',
- 'client/v1/goods/new-goods-recommend',
- 'client/v1/default/article-detail',
- 'client/v1/default/location-content',
- 'client/v1/store/mp-audit',
- 'client/v1/new-merchant/face-pay-config',
- 'client/v1/new-merchant/face-pay',
- 'client/v1/store/goods_list',
- 'client/v1/new-merchant/get-give-integral',
- 'client/v1/md-food/get-md-info',
- 'client/v1/passport/login-by-openid',
- 'client/v1/alipay/wifi/index',
- 'client/v1/alipay/wifi/query',
- 'client/v1/alipay/wifi/connect',
- 'client/v1/alipay/wifi/notify',
- 'client/v1/diy/seckill-activity',
- 'client/v1/store/get-store-by-aggregate',
- 'client/v1/store/get-info',
- 'client/v1/diy/get-store',
- 'client/v1/diy/get-cat',
- 'client/v1/md/list',
- 'client/v1/md/md-category',
- 'client/v1/coupon/share',
- 'client/v1/activity/new-user',
- 'client/v1/activity/new-user-conf',
- 'client/v1/activity/new-user-push-goods',
- 'client/v1/goods/md-address',
- 'client/v1/diy/get-foods-cat-list',
- 'client/v1/diy/get-foods-list',
- 'client/v1/store/js-config',
- 'client/v1/store/goods-list',
- 'client/v1/content/search-video',
- 'client/v1/store/store-app-info',
- 'client/v1/store/recommend-key',
- 'client/v1/worker/worker-index',
- 'client/v1/worker/worker-list',
- 'client/v1/worker/worker-info',
- 'client/v1/worker/worker-goods-info',
- 'client/v1/diy/get-worker-goods',
- 'client/v1/worker/worker-goods-list',
- 'client/v1/worker/worker-goods-cate-list',
- 'client/v1/passport/get-user-info-by-id',
- 'client/v1/passport/get-user-info-by-mobile',
- 'client/v1/pt/pt-activity/get-pt-goods',
- 'client/v1/order/goldplan',
- 'client/v1/store/goods-keyword',
- 'client/v1/passport/get-option-by-h5',
- 'client/v1/passport/md-food/get-qrcode-info',
- 'client/v1/passport/get-redirect-uri',
- 'client/v1/video-goods/cat-list',
- 'client/v1/video-goods/audit-apply-config',
- 'client/v1/video-goods/list',
- 'client/v1/question-template/list',
- 'client/v1/store/get-store-switch',
- // 'client/v1/default/goods-qrcode',
- 'client/v1/default/buy-goods-log',
- 'client/v1/goods-brand/get-brand-list',
- 'client/v1/goods-brand/get-md-info',
- 'client/v1/user/user-audit',
- 'client/v1/user/user-audit-setting',
- 'client/v1/passport/forget-password',
- 'client/v1/passport/user-password-regedit',
- //分销中心
- 'client/v1/share/get-info',
- 'client/v1/video-shop/user-sharer-list',
- 'client/v1/share/index',
- 'client/v1/quanzi/contents-list',
- 'client/v1/quanzi/contents-info',
- 'client/v1/quanzi/comment-list',
- 'client/v1/quanzi/cat-list',
- //股东合伙人中心
- 'client/v1/share-holder/audit-status',
- 'client/v1/share-holder/other-info',
- 'client/v1/share-holder/index',
- 'client/v1/share-holder/get-team-info',
- 'client/v1/share-holder/profit-list',
- 'client/v1/share-holder/frost-list',
- 'client/v1/alipay/order/get-freight',
- 'client/v1/alipay/order/create-order',
- 'client/v1/material/material-category',
- 'client/v1/material/material-list',
- 'client/v1/material/material-item',
- 'client/v1/material/get-qrcode',
- 'client/v1/material/download-statistics',
- 'client/v1/passport/captcha-gen',
- 'client/v1/passport/captcha-check',
- 'client/v1/default/get-store-category',
- 'client/v1/default/get-store-list',
- 'client/v1/user/get-geo',
- 'client/v1/diy/get-mch-list',
- 'adopt/device/play',
- 'client/v1/passport/get-user-info-by-invite',
- ];
- public function beforeAction($action)
- {
- $this->defaultStoreId();
- $access_token = input_params('access_token');
- $ignore_user = intval(input_params('ignore_user'));
- if (!in_array(Yii::$app->controller->getRoute(), [
- 'client/v1/store/mp-audit',
- 'client/v1/user/user-audit',
- 'client/v1/user/user-audit-setting',
- 'client/v1/default/store',
- 'client/v1/passport/login',
- 'client/v1/passport/auth-phone',
- 'client/v1/passport/code',
- 'client/v1/passport/send-code',
- 'client/v1/passport/login',
- 'client/v1/alipay/passport/auth',
- 'client/v1/alipay/passport/phone',
- 'client/v1/alipay/passport/login',
- 'client/v1/diy/template-unit',
- 'client/v1/upload/upload-image'
- ]) && !$access_token) {
- $result = $this->userAuditSetting();
- \Yii::$app->response->format = Response::FORMAT_JSON;
- if ($result['code'] !== 0) {
- \Yii::$app->response->data = $result;
- return false;
- }
- }
- if (get_params('version')) {
- $version = get_params('version');
- $cache = cacheLock();
- $key = 'store_' . get_store_id(). '_version';
- if (\Yii::$app->prod_is_dandianpu()) {
- $self_mini = Option::get('self_mini', get_store_id(), 'store', 0)['value'];
- if (!$self_mini) {
- $key = 'store_dandianpu_version';
- }
- }
- if (!cache_lock($key . '_time', 60 * 5)) {
- $cache->set($key, $version);
- }
- }
- if (!$access_token && (in_array(Yii::$app->controller->getRoute(), self::ALLOW_LIST) || in_array($_GET['callback'],self::ALLOW_LIST))) {
- return true;
- }
- // 支付宝依然取user表数据
- // if (input_params('platform') == 'alipay') {
- // $user = User::findIdentityByAccessToken($access_token);
- // if ($user) {
- // \Yii::$app->jwt->setUser($user);
- // } else {
- // \Yii::$app->response->format = Response::FORMAT_JSON;
- // \Yii::$app->response->data = [
- // 'code' => 401,
- // 'msg' => '登陆失败'
- // ];
- // return false;
- // }
- // } else {
- // 微信平台目前统一走saas_user表,统一用户
- $saas_user = SaasUser::findIdentityByAccessToken($access_token);
- if (empty($access_token) && $saas_user) {
- \Yii::$app->response->format = Response::FORMAT_JSON;
- \Yii::$app->response->data = [
- 'code' => 401,
- 'msg' => '登陆失败'
- ];
- return false;
- }
- if ($saas_user) {
- \Yii::$app->jwt->setSaasUser($saas_user);
- $store_id = intval(post_params('store_id')) ?: get_params('store_id');
- if ($store_id > 0) {
-
- //判断当前没有store_id,给绑定上
- if ($saas_user->store_id <= 0) {
- $store = Store::findOne(['id' => $store_id, 'is_delete' => 0]);
- if ($store) {
- $saas_user->store_id = $store_id;
- }
- $saas_user->save();
- }
- $user = User::findOne(['binding' => $saas_user->mobile, 'store_id' => $store_id]);
- if (is_alipay_platform() && !$user) {
- $user = User::findOne(['alipay_open_id' => $saas_user->ali_user_id, 'store_id' => $store_id]);
- }
- if (is_wechat_platform() && !$user) {
- $user = User::findOne(['wechat_open_id' => $saas_user->platform_open_id, 'store_id' => $store_id]);
- }
- if (!$user) {
- try {
- $user = new User();
- $user->access_token = \Yii::$app->security->generateRandomString();
- $user->binding = $saas_user->mobile;
- $user->type = User::USER_TYPE_NORMAL;
- $user->nickname = substr_replace($saas_user->mobile, '******', 3, 6);
- $user->avatar_url = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
- $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->is_delete = User::USER_NOT_DELETE;
- $user->store_id = $store_id;
- $user->platform = input_params('platform') == 'alipay'?User::USER_FROM_ALIPAY:User::USER_FROM_WECHAT; // 微信
- $user->alipay_open_id = $saas_user->ali_user_id;
- $user->wechat_open_id = $saas_user->platform_open_id;
- $user->save();
- } catch (\Exception $e){}
- } else {
- $user->binding = $saas_user->mobile;
- $user->alipay_open_id = $saas_user->ali_user_id;
- $user->save();
- }
- // if (!$user) {
- // $user = User::findOne(['binding' => $saas_user->mobile, 'store_id' => input_params('store_id')]);
- // if (is_alipay_platform()) {
- // $user = User::findOne(['alipay_open_id' => $saas_user->ali_user_id, 'store_id' => input_params('store_id')]);
- // }
- //
- // }
- if ((int)$user->blacklist === 1) {
- \Yii::$app->response->format = Response::FORMAT_JSON;
- \Yii::$app->response->data = [
- 'code' => 401,
- 'msg' => '身份验证失败'
- ];
- return false;
- }
- if ($user && $user->platform == User::USER_FROM_WECHAT) {
- if(empty($user->wechat_open_id)){
- $user->wechat_open_id = $saas_user['platform_open_id'];
- $user->save();
- }
- }
- }
- $result = $this->userAuditSetting($user);
- if ($result['code'] !== 0) {
- \Yii::$app->response->format = Response::FORMAT_JSON;
- \Yii::$app->response->data = $result;
- return false;
- }
- if (!empty($user) && !$ignore_user) {
- \Yii::$app->jwt->setUser($user);
- }
- } else {
- if (in_array(Yii::$app->controller->getRoute(), self::ALLOW_LIST)) {
- return true;
- }
- $result = $this->userAuditSetting();
- \Yii::$app->response->format = Response::FORMAT_JSON;
- if ($result['code'] !== 0) {
- \Yii::$app->response->data = $result;
- return false;
- }
- \Yii::$app->response->data = [
- 'code' => 401,
- 'msg' => '登陆失败'
- ];
- return false;
- }
- // }
- return true;
- }
- public function userAuditSetting($user = null) {
- if (is_wechat_platform() || is_alipay_platform()) {
- //判断是否开启审核设置
- $userAuditSetting = UserAuditSetting::findOne(['store_id' => get_store_id()]);
- if ($userAuditSetting) {
- $is_store_audit = (int)$userAuditSetting->status;
- //用户提交/获取审核接口
- if (intval($is_store_audit) && !in_array(Yii::$app->controller->getRoute(), ['client/v1/store/mp-audit', 'client/v1/user/user-audit', 'client/v1/default/store', 'client/v1/store/get-info', 'client/v1/upload/upload-image'])) {
- //开启
- $userAuditLog = UserAuditLog::findOne(['user_id' => $user->id ?? 0, 'is_delete' => 0]);
- if (empty($userAuditLog)) {
- \Yii::$app->response->format = Response::FORMAT_JSON;
- return [
- 'code' => 402,
- 'msg' => '需要提交用户申请'
- ];
- }
- if (in_array(intval($userAuditLog->status), [UserAuditLog::STATUS_NOT_AGREE, UserAuditLog::STATUS_PASS])) {
- $msg = "用户申请审核暂未通过";
- if (intval($userAuditLog->status) === UserAuditLog::STATUS_PASS) {
- $msg = "用户申请审核被拒绝 " . $userAuditLog->pass_remark;
- }
- \Yii::$app->response->format = Response::FORMAT_JSON;
- return [
- 'code' => 402,
- 'msg' => $msg
- ];
- }
- }
- }
- }
- return [
- 'code' => 0
- ];
- }
- //解决供应链store_id为空 就给默认店铺的store_id
- public function defaultStoreId() {
- $store_id = get_store_id();
- if (intval($store_id) <= 0) {
- if (\Yii::$app->prod_is_dandianpu()) {
- $default_store = \app\models\Option::get('one_store_default_store', 0, 'saas', 1)['value'];
- debug_log(['default_store' => $default_store], 'default.log');
- $store_id = $default_store;
- }
- }
- $_GET['store_id'] = $_POST['store_id'] = $store_id;
- }
- }
|