| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models\third\wechat;
- use app\models\Option;
- use app\models\StoreMini;
- use app\modules\alliance\models\WechatThirdErrorMsgForm as ErrorMsg;
- use app\utils\CurlHelper;
- use EasyWeChat\Factory;
- use yii\base\Model;
- class BaseForm extends Model
- {
- /**
- * @var \EasyWeChat\OpenPlatform\Application
- */
- public $openPlatform;
- //商城ID
- public $store_id;
- //小程序ID
- public $mini_id;
- //设置规则
- public function rules()
- {
- return [
- [['store_id', 'mini_id'], 'integer']
- ];
- }
- public function __construct ($config = [])
- {
- parent::__construct($config);
- //微信三方配置信息
- $wx_config = [
- //微信三方appid
- 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
- //微信三方appSecret
- 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
- //微信三方平台设置的token
- 'token' => Option::get("platform_token", 0, 'saas')['value'],
- //微信三方平台设置的加密key
- 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
- ];
- $this->openPlatform = Factory::openPlatform($wx_config);
- }
- /**
- * 微信业务实现
- */
- public function miniProgram ($appid = "")
- {
- try {
- $store_id = $this->store_id;
- //获取小程序信息
- $store_mini = StoreMini::find()->where(
- ['or',
- ['id' => $this->mini_id],
- ['appid' => $appid, 'store_id' => $store_id],
- ['store_id' => $store_id]
- ])->orderBy('id desc')->one();
- if (empty($store_mini->appid) || empty($store_mini->authorizer_refresh_token)) {
- throw new \Exception("获取小程序信息失败");
- }
- //配置easyWechat
- return $this->openPlatform->miniProgram($store_mini->appid, $store_mini->authorizer_refresh_token);
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取网络图片到临时目录
- public function saveTempImage($url)
- {
- if (strpos($url, 'http') === false) {
- $url = 'http:' . trim($url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
- CurlHelper::download($url, $save_path);
- return $save_path;
- }
- //递归
- public function getdata($data, $id = [0])
- {
- foreach ($data as $k => $v) {
- if (in_array($v['id'], $id)) {
- $v['children'] = $this->getdata($data, $v['children']);
- $arr[] = $v;
- }
- }
- return $arr;
- }
- //返回中文错误信息
- public function getZnMsg($result)
- {
- $ErrorMsg = new ErrorMsg();
- $arr = $ErrorMsg->getArray();
- $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg'];
- return $msg;
- }
- }
|