| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace app\utils\Wechat;
- use app\models\Option;
- use EasyWeChat\Factory;
- class WechatMini
- {
- /**
- * 小程序类型
- */
- const TYPE_MINI = 0;
- /**
- * 视频号小店类型
- */
- const TYPE_VIDEO_SHOP = 1;
- /**
- * 公众号类型
- */
- const TYPE_OFFICIAL = 2;
- public static $mini_id;
- public $store_id;
- /**
- * @param int $store_id 商城id
- * @param int $mini_id 小程序id
- * @param int $type 类型
- * @return \EasyWeChat\MiniProgram\Application|\EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Application|null
- */
- public static function getWechatConfig($store_id = 0, $mini_id = 0, $type = self::TYPE_MINI, $is_platform = 0, $is_serve = 0) {
- if(\app\modules\admin\models\WastoreForm::$test && $mini_id == 88){
- $config =[];
- $config['app_id'] = 'wx1ce41243db1e4373';
- $config['secret'] = '6563aceb2ef97e0d2f5b884f11e43544';
- $config['response_type'] = 'array';
- $app = Factory::miniProgram($config);
- $mini_id = 88;
- return $app;
- }
- $app = null;
- if (intval($type) == self::TYPE_VIDEO_SHOP) {
- goto video_shop;
- }
- $lianmeng_mini = 0;
- $dandianpu_mini = 0;
- if ($is_platform) {
- if (\Yii::$app->prod_is_dandianpu()) {
- $dandianpu_mini = 1;
- } else {
- $lianmeng_mini = 1;
- }
- } elseif ($is_serve) {
- } else {
- $store = \app\models\Store::findOne(['id' => $store_id]);
- if($store->business_model != 1){
- $lianmeng_mini = 1;
- }
- $self_mini = \app\models\Option::get('self_mini', $store_id, 'store', 0)['value'];
- if (\Yii::$app->prod_is_dandianpu() && !$self_mini) {
- $dandianpu_mini = 1;
- }
- }
- // 独立部署
- if (!is_open_platform()) {
- $wechat_config = \app\models\WechatConfig::findOne(['store_id' => $store_id, 'type' => 1, 'is_delete' => 0]);
- if ($type === self::TYPE_OFFICIAL) {
- $wechat_config = \app\models\WechatConfig::findOne(['store_id' => $store_id, 'type' => \app\models\WechatConfig::TYPE_CONFIG_MP, 'is_delete' => 0]);
- }
- if ($wechat_config) {
- $ext = $wechat_config->ext ? json_decode($wechat_config->ext, true) : [];
- $config =[];
- $config['app_id'] = $wechat_config->app_id;
- $config['secret'] = $wechat_config->app_secret;
- $ext['token'] && $config['token'] = $ext['token'];
- $ext['aes_key'] && $config['aes_key'] = $ext['aes_key'];
- $config['response_type'] = 'array';
- $app = Factory::miniProgram($config);
- if ($type === self::TYPE_OFFICIAL) {
- $app = Factory::officialAccount($config);
- }
- }
- }elseif($dandianpu_mini){
- $config =[];
- $config['app_id'] = Option::get("one_store_wechat_appid", 0, 'saas')['value'];
- $config['secret'] = Option::get("one_store_wechat_secret", 0, 'saas')['value'];
- $config['response_type'] = 'array';
- $app = Factory::miniProgram($config);
- if ($type == self::TYPE_OFFICIAL) {
- $config['app_id'] = Option::get("one_store_wechat_official_appid", 0, 'saas')['value'];
- $config['secret'] = Option::get("one_store_wechat_official_secret", 0, 'saas')['value'];
- $config['token'] = Option::get("one_store_wechat_official_token", 0, 'saas', '')['value'];
- $config['response_type'] = 'array';
- $app = Factory::officialAccount($config);
- }
- }elseif($lianmeng_mini){
- $config =[];
- $config['app_id'] = Option::get("platform_appid", 0, 'saas')['value'];
- $config['secret'] = Option::get("platform_key", 0, 'saas')['value'];
- $config['response_type'] = 'array';
- $app = Factory::miniProgram($config);
- }elseif($is_serve){
- $config =[];
- $config['app_id'] = Option::get("platform_mch_appid", 0, 'saas')['value'];
- $config['secret'] = Option::get("platform_mch_key", 0, 'saas')['value'];
- $config['response_type'] = 'array';
- $app = Factory::miniProgram($config);
- } else {
- if ($type === self::TYPE_OFFICIAL) {
- $wechat_config = \app\models\WechatConfig::findOne(['store_id' => $store_id, 'type' => \app\models\WechatConfig::TYPE_CONFIG_MP, 'is_delete' => 0]);
- if ($wechat_config) {
- $ext = $wechat_config->ext ? json_decode($wechat_config->ext, true) : [];
- $config = [];
- $config['app_id'] = $wechat_config->app_id;
- $config['secret'] = $wechat_config->app_secret;
- $ext['token'] && $config['token'] = $ext['token'];
- $ext['aes_key'] && $config['aes_key'] = $ext['aes_key'];
- $config['response_type'] = 'array';
- $app = Factory::officialAccount($config);
- }
- goto app_end;
- }
- video_shop:
- $verify_ticket = Option::get("component_verify_ticket",0,'saas')['value'];
- if (!empty($verify_ticket)) {
- $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_);
- }
- $where = [
- 'store_id' => $store_id,
- 'is_cancle' => 0
- ];
- if ($type == self::TYPE_VIDEO_SHOP) {
- $where = array_merge($where, [
- 'fuwu_type' => 1
- ]);
- }
- if ($mini_id) {
- $where = [
- 'id' => $mini_id,
- 'is_cancle' => 0
- ];
- }
- $store_mini_list = \app\models\StoreMini::find()->where($where)
- ->select('id, mini_up, appid, authorizer_refresh_token, is_use, is_self, app_secret')->orderBy(['is_use' => SORT_ASC, 'mini_up' => SORT_DESC])->asArray()->all();
- $wechat_config = [];
- foreach ($store_mini_list as $item) {
- if (intval($item['is_use']) === 1) {
- $wechat_config = $item;
- break;
- }
- if (intval($item['mini_up']) === 5) {
- $wechat_config = $item;
- break;
- }
- if (intval($item['mini_up']) === 2) {
- $wechat_config = $item;
- break;
- }
- if (intval($item['mini_up']) === 1) {
- $wechat_config = $item;
- break;
- }
- if (intval($item['mini_up']) === -1) {
- $wechat_config = $item;
- break;
- }
- if ($mini_id) {
- $wechat_config = $item;
- }
- }
- if ($wechat_config) {
- self::$mini_id = $wechat_config['id'];
- if (intval($wechat_config['is_self']) && $wechat_config['app_secret']) {
- $config =[];
- $config['app_id'] = $wechat_config['appid'];
- $config['secret'] = $wechat_config['app_secret'];
- $config['response_type'] = 'array';
- $app = Factory::miniProgram($config);
- } else {
- if (!empty($openPlatform)) {
- $app = $openPlatform->miniProgram($wechat_config['appid'], $wechat_config['authorizer_refresh_token']);
- }
- }
- }
- }
- app_end:
- return $app;
- }
- }
|