WechatMini.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\utils\Wechat;
  3. use app\models\Option;
  4. use EasyWeChat\Factory;
  5. class WechatMini
  6. {
  7. /**
  8. * 小程序类型
  9. */
  10. const TYPE_MINI = 0;
  11. /**
  12. * 视频号小店类型
  13. */
  14. const TYPE_VIDEO_SHOP = 1;
  15. /**
  16. * 公众号类型
  17. */
  18. const TYPE_OFFICIAL = 2;
  19. public static $mini_id;
  20. public $store_id;
  21. /**
  22. * @param int $store_id 商城id
  23. * @param int $mini_id 小程序id
  24. * @param int $type 类型
  25. * @return \EasyWeChat\MiniProgram\Application|\EasyWeChat\OpenPlatform\Authorizer\MiniProgram\Application|null
  26. */
  27. public static function getWechatConfig($store_id = 0, $mini_id = 0, $type = self::TYPE_MINI, $is_platform = 0, $is_serve = 0) {
  28. if(\app\modules\admin\models\WastoreForm::$test && $mini_id == 88){
  29. $config =[];
  30. $config['app_id'] = 'wx1ce41243db1e4373';
  31. $config['secret'] = '6563aceb2ef97e0d2f5b884f11e43544';
  32. $config['response_type'] = 'array';
  33. $app = Factory::miniProgram($config);
  34. $mini_id = 88;
  35. return $app;
  36. }
  37. $app = null;
  38. if (intval($type) == self::TYPE_VIDEO_SHOP) {
  39. goto video_shop;
  40. }
  41. $lianmeng_mini = 0;
  42. $dandianpu_mini = 0;
  43. if ($is_platform) {
  44. if (\Yii::$app->prod_is_dandianpu()) {
  45. $dandianpu_mini = 1;
  46. } else {
  47. $lianmeng_mini = 1;
  48. }
  49. } elseif ($is_serve) {
  50. } else {
  51. $store = \app\models\Store::findOne(['id' => $store_id]);
  52. if($store->business_model != 1){
  53. $lianmeng_mini = 1;
  54. }
  55. $self_mini = \app\models\Option::get('self_mini', $store_id, 'store', 0)['value'];
  56. if (\Yii::$app->prod_is_dandianpu() && !$self_mini) {
  57. $dandianpu_mini = 1;
  58. }
  59. }
  60. // 独立部署
  61. if (!is_open_platform()) {
  62. $wechat_config = \app\models\WechatConfig::findOne(['store_id' => $store_id, 'type' => 1, 'is_delete' => 0]);
  63. if ($type === self::TYPE_OFFICIAL) {
  64. $wechat_config = \app\models\WechatConfig::findOne(['store_id' => $store_id, 'type' => \app\models\WechatConfig::TYPE_CONFIG_MP, 'is_delete' => 0]);
  65. }
  66. if ($wechat_config) {
  67. $ext = $wechat_config->ext ? json_decode($wechat_config->ext, true) : [];
  68. $config =[];
  69. $config['app_id'] = $wechat_config->app_id;
  70. $config['secret'] = $wechat_config->app_secret;
  71. $ext['token'] && $config['token'] = $ext['token'];
  72. $ext['aes_key'] && $config['aes_key'] = $ext['aes_key'];
  73. $config['response_type'] = 'array';
  74. $app = Factory::miniProgram($config);
  75. if ($type === self::TYPE_OFFICIAL) {
  76. $app = Factory::officialAccount($config);
  77. }
  78. }
  79. }elseif($dandianpu_mini){
  80. $config =[];
  81. $config['app_id'] = Option::get("one_store_wechat_appid", 0, 'saas')['value'];
  82. $config['secret'] = Option::get("one_store_wechat_secret", 0, 'saas')['value'];
  83. $config['response_type'] = 'array';
  84. $app = Factory::miniProgram($config);
  85. if ($type == self::TYPE_OFFICIAL) {
  86. $config['app_id'] = Option::get("one_store_wechat_official_appid", 0, 'saas')['value'];
  87. $config['secret'] = Option::get("one_store_wechat_official_secret", 0, 'saas')['value'];
  88. $config['token'] = Option::get("one_store_wechat_official_token", 0, 'saas', '')['value'];
  89. $config['response_type'] = 'array';
  90. $app = Factory::officialAccount($config);
  91. }
  92. }elseif($lianmeng_mini){
  93. $config =[];
  94. $config['app_id'] = Option::get("platform_appid", 0, 'saas')['value'];
  95. $config['secret'] = Option::get("platform_key", 0, 'saas')['value'];
  96. $config['response_type'] = 'array';
  97. $app = Factory::miniProgram($config);
  98. }elseif($is_serve){
  99. $config =[];
  100. $config['app_id'] = Option::get("platform_mch_appid", 0, 'saas')['value'];
  101. $config['secret'] = Option::get("platform_mch_key", 0, 'saas')['value'];
  102. $config['response_type'] = 'array';
  103. $app = Factory::miniProgram($config);
  104. } else {
  105. if ($type === self::TYPE_OFFICIAL) {
  106. $wechat_config = \app\models\WechatConfig::findOne(['store_id' => $store_id, 'type' => \app\models\WechatConfig::TYPE_CONFIG_MP, 'is_delete' => 0]);
  107. if ($wechat_config) {
  108. $ext = $wechat_config->ext ? json_decode($wechat_config->ext, true) : [];
  109. $config = [];
  110. $config['app_id'] = $wechat_config->app_id;
  111. $config['secret'] = $wechat_config->app_secret;
  112. $ext['token'] && $config['token'] = $ext['token'];
  113. $ext['aes_key'] && $config['aes_key'] = $ext['aes_key'];
  114. $config['response_type'] = 'array';
  115. $app = Factory::officialAccount($config);
  116. }
  117. goto app_end;
  118. }
  119. video_shop:
  120. $verify_ticket = Option::get("component_verify_ticket",0,'saas')['value'];
  121. if (!empty($verify_ticket)) {
  122. $config_ = [
  123. 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
  124. 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
  125. 'token' => Option::get("platform_token", 0, 'saas')['value'],
  126. 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
  127. ];
  128. $openPlatform = Factory::openPlatform($config_);
  129. }
  130. $where = [
  131. 'store_id' => $store_id,
  132. 'is_cancle' => 0
  133. ];
  134. if ($type == self::TYPE_VIDEO_SHOP) {
  135. $where = array_merge($where, [
  136. 'fuwu_type' => 1
  137. ]);
  138. }
  139. if ($mini_id) {
  140. $where = [
  141. 'id' => $mini_id,
  142. 'is_cancle' => 0
  143. ];
  144. }
  145. $store_mini_list = \app\models\StoreMini::find()->where($where)
  146. ->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();
  147. $wechat_config = [];
  148. foreach ($store_mini_list as $item) {
  149. if (intval($item['is_use']) === 1) {
  150. $wechat_config = $item;
  151. break;
  152. }
  153. if (intval($item['mini_up']) === 5) {
  154. $wechat_config = $item;
  155. break;
  156. }
  157. if (intval($item['mini_up']) === 2) {
  158. $wechat_config = $item;
  159. break;
  160. }
  161. if (intval($item['mini_up']) === 1) {
  162. $wechat_config = $item;
  163. break;
  164. }
  165. if (intval($item['mini_up']) === -1) {
  166. $wechat_config = $item;
  167. break;
  168. }
  169. if ($mini_id) {
  170. $wechat_config = $item;
  171. }
  172. }
  173. if ($wechat_config) {
  174. self::$mini_id = $wechat_config['id'];
  175. if (intval($wechat_config['is_self']) && $wechat_config['app_secret']) {
  176. $config =[];
  177. $config['app_id'] = $wechat_config['appid'];
  178. $config['secret'] = $wechat_config['app_secret'];
  179. $config['response_type'] = 'array';
  180. $app = Factory::miniProgram($config);
  181. } else {
  182. if (!empty($openPlatform)) {
  183. $app = $openPlatform->miniProgram($wechat_config['appid'], $wechat_config['authorizer_refresh_token']);
  184. }
  185. }
  186. }
  187. }
  188. app_end:
  189. return $app;
  190. }
  191. }