| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\WechatConfig;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\Exceptions\HttpException;
- use yii\base\Model;
- use GuzzleHttp\Exception\RequestException;
- class WechatConfigForm extends Model
- {
- public $store_id;
- public $type;
- public $app_id;
- public $app_name;
- public $app_secret;
- public $pay_key;
- public $cert_pem;
- public $key_pem;
- public $cert_pem_url;
- public $key_pem_url;
- public $mch_id;
- public $auto_reply;
- public $token;
- public $aes_key;
- public $app;
- public $model;
- public $name;
- public $gh_wechat_app_id;
- public function rules()
- {
- return [
- [['app_id', 'app_secret', 'mch_id', 'pay_key', 'cert_pem', 'key_pem', 'token', 'app_name', 'auto_reply', 'name', 'gh_wechat_app_id', 'cert_pem_url', 'key_pem_url'],
- 'trim'],
- [['type'], 'integer'],
- [['app_id'], 'required'],
- [['aes_key'], 'safe'],
- ['app_secret', 'default', 'value'=> '0'],
- ];
- }
- /**
- * 存储配置
- */
- public function saveConfig() {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => '参数错误'
- ];
- }
- // if (\Yii::$app->isSaas()) {
- // $this->model->app_id = $this->app_id;
- // $this->model->mch_id = $this->mch_id;
- // $this->model->store_id = $this->store_id;
- // $this->model->name = $this->name;
- // $this->model->gh_wechat_app_id = $this->gh_wechat_app_id;
- // if ($this->model->save()) {
- // return [
- // 'code' => 0,
- // 'msg' => '保存成功',
- // ];
- // }
- // return [
- // 'code' => 1,
- // 'msg' => json_encode($this->model->errors),
- // ];
- // }
- if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
- mkdir(\Yii::$app->runtimePath . '/pem');
- file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
- }
- if (!empty($this->cert_pem_url)) {
- $this->cert_pem = file_get_contents(str_replace('https://' . \Yii::$app->request->hostName, \Yii::$app->basePath, $this->cert_pem_url));
- } else {
- if (!empty($this->cert_pem)) {
- $this->cert_pem_url = $this->handlePemFile($this->cert_pem);
- }
- }
- if (!empty($this->key_pem_url)) {
- $this->key_pem = file_get_contents(str_replace('https://' . \Yii::$app->request->hostName, \Yii::$app->basePath, $this->key_pem_url));
- } else {
- if (!empty($this->key_pem)) {
- $this->key_pem_url = $this->handlePemFile($this->key_pem);
- }
- }
- $config = [
- 'app_id' => $this->app_id,
- 'secret' => $this->app_secret,
- 'key' => $this->pay_key,
- 'mch_id' => $this->mch_id,
- 'cert_path' => self::handlePemFile($this->cert_pem, 1),
- 'key_path' => self::handlePemFile($this->key_pem, 1)
- ];
- // 验证appid appsecret 是否合法
- if ($this->app_secret) {
- try {
- if ($this->type == WechatConfig::TYPE_CONFIG_MINI) {
- $this->app = Factory::miniProgram($config);
- }
- if ($this->type == WechatConfig::TYPE_CONFIG_MP) {
- $this->app = Factory::officialAccount($config);
- }
- if ($this->type == WechatConfig::TYPE_CONFIG_APP) {
- \Yii::error($config);
- $this->app = Factory::miniProgram($config);
- }
- $this->app->access_token->getToken();
- } catch (HttpException $e) {
- if ($e->formattedResponse['errcode'] == 40013) {
- return [
- 'code' => 1,
- 'msg' => 'AppId错误'
- ];
- }
- if ($e->formattedResponse['errcode'] == 40125) {
- return [
- 'code' => 1,
- 'msg' => 'AppSecret错误'
- ];
- }
- }
- }
-
- //$app = Factory::payment($config);
- if (!empty($this->cert_pem) || !empty($this->cert_pem)) {
- if ($this->type == 2 && empty($this->mch_id)) {
- return [
- 'code' => 1,
- 'msg' => '请填写商户号'
- ];
- }
- if ($this->type == 2 && empty($this->pay_key)) {
- return [
- 'code' => 1,
- 'msg' => '请填写微信支付Api支付密钥'
- ];
- }
- // 验证证书
- try {
- $app = Factory::payment($config);
- $app->security->getPublicKey();
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => '证书验证失败'
- ];
- } catch (RequestException $e) {
- if ($e->getHandlerContext()['errno'] == 58) {
- return [
- 'code' => 1,
- 'msg' => '微信支付证书错误'
- ];
- }
- }
- }
- if (($this->mch_id && $this->pay_key)) {
- if (empty($this->cert_pem) || empty($this->key_pem)) {
- return [
- 'code' => 1,
- 'msg' => '请填写微信支付证书'
- ];
- }
- $order_no = date('YmdHis') . mt_rand(1000, 9999);
- $res = $app->order->queryByOutTradeNumber($order_no);
- if ($res['return_code'] == "FAIL") {
- return [
- 'code' => 1,
- 'msg' => '微信支付商户号或微信支付Api密钥错误--('.$res['return_msg'].')'
- ];
- }
- }
- $ext_arr = [];
- if ($this->model->isNewRecord) {
- $this->model->store_id = $this->store_id;
- $this->model->type = $this->type;
- if ($this->type == WechatConfig::TYPE_CONFIG_APP && !empty($this->app_name)) {
- $ext_arr = [
- 'app_name' => ''
- ];
- if (!empty($this->app_name)) {
- $ext_arr['app_name'] = $this->app_name;
- }
- $this->model->ext = json_encode($ext_arr);
- }
- if ($this->type == WechatConfig::TYPE_CONFIG_MP) {
- $ext_arr = [
- 'token' => '',
- 'auto_reply' => ''
- ];
- if (!empty($this->token)) {
- $ext_arr['token'] = $this->token;
- }
- if (!empty($this->auto_reply)) {
- $ext_arr['auto_reply'] = $this->auto_reply;
- }
- $this->model->ext = json_encode($ext_arr);
- }
- $this->model->created_at = $this->model->updated_at = time();
- } else {
- $ext_arr = json_decode($this->model->ext, true);
- if ($this->type == WechatConfig::TYPE_CONFIG_APP && !empty($this->app_name)) {
- if (!empty($this->app_name)) {
- $ext_arr['app_name'] = $this->app_name;
- }
- $this->model->ext = json_encode($ext_arr);
- }
- if ($this->type == WechatConfig::TYPE_CONFIG_MP) {
- if (!empty($this->token)) {
- $ext_arr['token'] = $this->token;
- }
- if (!empty($this->auto_reply)) {
- $ext_arr['auto_reply'] = $this->auto_reply;
- }
- $this->model->ext = json_encode($ext_arr);
- }
- $this->model->updated_at = time();
- }
-
- empty($this->token) || $ext_arr['token'] = $this->token;
- empty($this->aes_key) || $ext_arr['aes_key'] = $this->aes_key;
- $this->model->ext = json_encode($ext_arr);
-
- $this->model->app_id = $this->app_id;
- $this->model->app_secret = $this->app_secret;
- $this->model->mch_id = $this->mch_id;
- $this->model->pay_key = $this->pay_key;
- $this->model->name = $this->name;
- $this->model->gh_wechat_app_id = $this->gh_wechat_app_id;
- if ($this->cert_pem) {
- $this->model->cert_pem = $this->cert_pem;
- }
- if ($this->key_pem) {
- $this->model->key_pem = $this->key_pem;
- }
- if ($this->cert_pem_url) {
- $this->model->cert_pem_url = $this->cert_pem_url;
- }
- if ($this->cert_pem_url) {
- $this->model->cert_pem_url = $this->cert_pem_url;
- }
- if ($this->model->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功',
- ];
- }
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($this->model->firstErrors))
- ];
- }
- /**
- * 获取配置
- */
- public function getConfig() {
- $returnArray = [
- 'mini' => [],
- 'mp' => [],
- 'app' => [],
- 'self_mini' =>0,
- 'is_saas' => 0,
- 'store_id' => $this->store_id,
- ];
- $wechatConfig = WechatConfig::find()->where(['store_id' => $this->store_id, 'is_delete' => 0]);
- if ($wechatConfig) {
- //是商盟和saas
- if(\Yii::$app->isSaas()){
- // $wechatConfig->select(['app_id','app_secret','name','mch_id','ext','type', 'gh_wechat_app_id']);
- $returnArray['is_saas'] = 1;
- }
- $self_mini = \app\models\Option::get('self_mini', $this->store_id, 'store', 0)['value'];
- if($self_mini){
- $returnArray['self_mini'] = 1;
- }
- foreach ($wechatConfig->asArray()->all() as $value) {
- if (empty($value['key_pem_url']) && !empty($value['key_pem'])) {
- $value['key_pem_url'] = self::handlePemFile($value['key_pem']);
- }
- if (empty($value['cert_pem_url']) && !empty($value['cert_pem'])) {
- $value['cert_pem_url'] = self::handlePemFile($value['cert_pem']);
- }
- $ext = json_decode($value['ext'], true);
- switch ($value['type']) {
- case WechatConfig::TYPE_CONFIG_MINI:
- $value['token'] = !empty($ext['token']) ? $ext['token'] : '';
- $value['aes_key'] = !empty($ext['aes_key']) ? $ext['aes_key'] : '';
- $returnArray['mini'] = $value;
- break;
- case WechatConfig::TYPE_CONFIG_MP:
- $ext = json_decode($value['ext'], true);
- $value['token'] = !empty($ext['token']) ? $ext['token'] : '';
- $value['auto_reply'] = !empty($ext['auto_reply']) ? $ext['auto_reply'] : '';
- // unset($value['ext']);
- $returnArray['mp'] = $value;
- break;
- case WechatConfig::TYPE_CONFIG_APP:
- $ext = json_decode($value['ext'], true);
- $value['app_name'] = !empty($ext['app_name']) ? $ext['app_name'] : '';
- unset($value['ext']);
- $returnArray['app'] = $value;
- break;
- default:
- break;
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $returnArray
- ];
- }
- //处理上传证书
- public static function handlePemFile($pem_content, $type = 0) {
- // 证书
- if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
- mkdir(\Yii::$app->runtimePath . '/pem');
- file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
- }
- $pem_file = null;
- $pem_url = '';
- if ($pem_content) {
- $pem_file_name = md5($pem_content) . '.pem';
- $pem_file = \Yii::$app->runtimePath . '/pem/' . $pem_file_name;
- if (!file_exists($pem_file)) {
- file_put_contents($pem_file, $pem_content);
- }
- $pem_url = 'https://' . \Yii::$app->request->hostName . '/runtime/pem/' . $pem_file_name;
- }
- if ($type) {
- return $pem_file;
- }
- return $pem_url;
- }
- }
|