| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models\salesman;
- use AopClient;
- use app\models\AggregateQrcode;
- use app\models\AggregateSaasQrcode;
- use app\models\MdFoodsQrcode;
- use app\models\Option;
- use app\models\SalesmanNewStore;
- use app\models\Store;
- use app\models\StoreAliMini;
- use app\models\StoreMini;
- use app\modules\alliance\models\third\wechat\BaseForm;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniQrcodeBindRequest;
- use app\utils\QrCode;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\BaseClient;
- use yii\base\Model;
- include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
- class StoreAdminPatternForm extends Model
- {
- public $model;
- public $openPlatform;
- public $id;
- public $store_id;
- public $mini_id;
- public $ali_mini_id;
- public $business_model;
- public $qrcode_id;
- public $s_id;
- public function rules()
- {
- return [
- [['id', 'business_model', 'qrcode_id', 's_id'], 'integer'],
- ];
- }
- //微信初始化配置
- public function initWx()
- {
- //微信三方配置信息
- $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']
- ];
- $openPlatform = Factory::openPlatform($wx_config);
- //获取小程序信息
- $store_mini = StoreMini::findOne($this->mini_id);
- if (empty($store_mini->appid) || empty($store_mini->authorizer_refresh_token)) {
- return false;
- } else {
- return $openPlatform->miniProgram($store_mini->appid, $store_mini->authorizer_refresh_token);
- }
- }
- //支付宝初始化配置
- public function initAli($request, $biz_content)
- {
- $ali_mini_id = $this->ali_mini_id;
- $store_mini = StoreAliMini::findOne($ali_mini_id);
- if (!empty($store_mini->auth_token)) {
- $auth_token = $store_mini->auth_token;
- } else {
- return false;
- }
- $aop = new AopClient();
- //设置appid
- $aop->appId = Option::get("alipay_appid", 0, 'saas')['value'];
- //设置私钥
- $aop->rsaPrivateKey = Option::get("alipay_app_private_key", 0, 'saas')['value'];
- //设置公钥
- $aop->alipayrsaPublicKey = Option::get("alipay_public_key", 0, 'saas')['value'];
- //设置ase_key,加签方式
- $aop->encryptKey = Option::get("alipay_aes_key", 0, 'saas')['value'];
- //设置加解密方式
- $aop->signType = "RSA2";
- //处理没有biz_content情况
- if ($biz_content) {
- $request->setBizContent($biz_content);
- }
- $result = $aop->execute($request, null, $auth_token);
- //请求后的数据处理
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- return $result->$responseNode;
- }
- //物料配置
- public function getConfig() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $store_id = $this->store_id;
- $mini_id = $this->mini_id;
- $ali_mini_id = $this->ali_mini_id;
- $s_id = $this->s_id;
- $business_model = Store::findOne($store_id)->business_model;
- $self_mini = \app\models\Option::get('self_mini', $store_id, 'store', 0)['value']; //
- if ((int)$business_model === 1 || (\Yii::$app->prod_is_dandianpu() && $self_mini)) {
- $qrcode = AggregateQrcode::findOne(['store_id' => $store_id]);
- if (!$qrcode) {
- $qrcode_ = AggregateQrcode::findOne(['store_id' => $store_id]);
- if (!$qrcode_) {
- throw new \Exception("后台缺少配置信息");
- }
- $qrcode_->store_id = $store_id;
- $qrcode_->save();
- $qrcode = AggregateQrcode::findOne(['store_id' => $store_id]);
- }
- $data = [
- 'param_url' => $qrcode->param_url,
- 'wx_url' => $qrcode->wx_url,
- 'ali_url' => $qrcode->ali_url
- ];
- //判断微信是否绑定
- $result = $this->bindQrcode($qrcode, $data, 1);
- if ($result['code'] !== 0) {
- return $result;
- }
- $type = 'store';
- } else {
- $qrcode = AggregateSaasQrcode::findOne(['store_id' => $store_id]);
- if (!$qrcode) {
- $qrcode_ = AggregateSaasQrcode::findOne(['store_id' => $store_id]);
- if (!$qrcode_) {
- throw new \Exception("后台缺少配置信息");
- }
- $qrcode_->store_id = $store_id;
- $qrcode_->save();
- $qrcode = AggregateSaasQrcode::findOne(['store_id' => $store_id]);
- }
- $type = 'saas';
- }
- $filename = md5(time() . $type . $qrcode->id);
- $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
- $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
- if(empty($qrcode->qrcode_url) || !file_exists(str_replace(\Yii::$app->request->hostInfo,\Yii::$app->basePath,$qrcode->qrcode_url))){
- $text = $qrcode->param_url;
- QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
- $qrcode->qrcode_url = $pic_url;
- $qrcode->save();
- }
- $new_store = SalesmanNewStore::findOne($s_id);
- $store_params = json_decode($new_store->store_params, true);
- $foods_qrcode = "";
- if ($store_params['foods_qrcode_id']) {
- $md_foods_qrcode = MdFoodsQrcode::findOne(['id' => $store_params['foods_qrcode_id'], 'is_delete' => 0]);
- $foods_qrcode = $md_foods_qrcode->qrcode_url ?: '';
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作完成',
- 'data' => [
- 'qrcode' => $qrcode->qrcode_url,
- 'business_model' => $business_model,
- 'foods_qrcode' => $foods_qrcode
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- public function setFoodsQrcode() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $store_id = $this->store_id;
- $qrcode_id = $this->qrcode_id;
- // $mini_id = $this->mini_id;
- // $ali_mini_id = $this->ali_mini_id;
- $store = Store::findOne($store_id);
- $self_mini = Option::get('self_mini', $store_id, 'store', 0)['value'];
- //联盟、供应链端都可适用
- $url_path = \Yii::$app->request->hostInfo . "/web/food/saas?scene=store:{$store_id},store_id:{$store_id}";
- //独立版小程序使用
- if (
- (!\Yii::$app->prod_is_dandianpu() && intval($store->business_model) === 1) ||
- (\Yii::$app->prod_is_dandianpu() && $self_mini)
- ) {
- $url_path = \Yii::$app->request->hostInfo . '/web/food/store/' . $qrcode_id . '/?id='. $qrcode_id;
- }
- $md_foods_qrcode = MdFoodsQrcode::findOne(['id' => $qrcode_id, 'is_delete' => 0]);
- if (!$md_foods_qrcode) {
- throw new \Exception('查询失败,请检查状态是否被删除');
- }
- if (!$store) {
- throw new \Exception('商城信息查询失败');
- }
- if ($md_foods_qrcode->store_id && $md_foods_qrcode->store_id !== $store_id) {
- throw new \Exception('二维码已经绑定其他商城');
- }
- $md_foods_qrcode->url_path = $url_path;
- $md_foods_qrcode->save();
- //TODO 此处是将子父级全部替换为同一商城
- // if ($md_foods_qrcode->parent_id) {
- // $parent_ = MdFoodsQrcode::findOne(['id' => $md_foods_qrcode->parent_id, 'is_delete' => 0]);
- // if ($parent_) {
- // if ($parent_->store_id && $parent_->store_id !== $store_id) {
- // throw new \Exception('父级二维码已经绑定其他商城');
- // }
- // if ($parent_->parent_id) {
- // $_parent_ = MdFoodsQrcode::findOne(['id' => $parent_->parent_id, 'is_delete' => 0]);
- // if ($_parent_) {
- // if ($_parent_->store_id && $_parent_->store_id !== $store_id) {
- // throw new \Exception('父级二维码已经绑定其他商城');
- // }
- // $_parent_->store_id = $store_id;
- // $_parent_->save();
- // }
- // }
- // $parent_->store_id = $store_id;
- // $parent_->save();
- // }
- // } else {
- $md_foods_qrcode->store_id = $store_id;
- // }
- if (!$md_foods_qrcode->save()) {
- throw new \Exception('保存失败:' . json_encode($md_foods_qrcode->errors, JSON_UNESCAPED_UNICODE));
- }
- $data = [
- 'param_url' => $md_foods_qrcode->url_path,
- 'wx_url' => $md_foods_qrcode->mini_path,
- 'ali_url' => $md_foods_qrcode->mini_path
- ];
- //判断微信是否绑定
- $result = $this->bindQrcode($md_foods_qrcode, $data);
- if ($result['code'] !== 0) {
- return $result;
- }
- $s_id = $this->s_id;
- $new_store = SalesmanNewStore::findOne($s_id);
- $store_params = json_decode($new_store->store_params, true);
- $store_params = array_merge($store_params, [
- 'foods_qrcode_id' => $qrcode_id
- ]);
- $new_store->store_params = json_encode($store_params, JSON_UNESCAPED_UNICODE);
- if (!$new_store->save()) {
- throw new \Exception($new_store->errors, JSON_UNESCAPED_UNICODE);
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getLine() . $e->getFile()
- ];
- }
- }
- public function bindQrcode($qrcode, $data, $type = 0) {
- try {
- $mini_id = $this->mini_id;
- $ali_mini_id = $this->ali_mini_id;
- //判断微信是否绑定
- if (!$qrcode->wx_status) {
- $init = $this->initWx();
- if ($init) {
- //保存文件到本地
- $url = '/web/food/store/' . $qrcode->id;
- $result = $this->saveFile($init, $type, $url);
- if ($result['code'] !== 0) {
- return $result;
- }
- //生成规则
- $data_ = [
- 'prefix' => $data['param_url'],
- 'permit_sub_rule' => 1,
- 'path' => $data['wx_url'],
- 'open_version' => 2,
- 'debug_url' => [
- $data['param_url']
- ],
- 'is_edit' => 0
- ];
- //自定义请求接口
- $client = new BaseClient($init);
- //请求接口
- $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data_);
- if ($result['errcode'] != 0) {
- $msg = (new BaseForm())->getZnMsg($result);
- debug_log($msg, 'salesman_store_qrcode.log');
- //获取中文错误信息(微信返回为英文信息)
- // $msg = (new BaseForm())->getZnMsg($result);
- // throw new \Exception($msg);
- } else {
- throw new \Exception("数据错误");
- }
- //发布规则
- //所需参数
- $data_ = [
- 'prefix' => $data['param_url']
- ];
- //自定义接口提交地址
- $client = new BaseClient($init);
- //发布二维码规则接口
- $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data_);
- if ($result['errcode'] === 0) {
- $qrcode->wx_status = 1;
- if ($type) {
- $qrcode->wx_mini_id = $mini_id;
- if ((int)$qrcode->ali_status === 1) {
- $qrcode->is_use = 2;
- }
- }
- if (!$qrcode->save()) {
- throw new \Exception(json_encode($qrcode->errors));
- }
- } elseif ($result['errcode'] != 0) {
- $msg = (new BaseForm())->getZnMsg($result);
- debug_log($msg, 'salesman_store_qrcode.log');
- //获取中文错误信息(微信返回为英文信息)
- //
- // throw new \Exception($msg);
- } else {
- throw new \Exception("数据错误");
- }
- }
- }
- //判断支付宝是否绑定
- if (!$qrcode->ali_status) {
- //
- //判断关键参数是否为空
- if (empty($data['param_url']) || empty($data['ali_url'])) {
- throw new \Exception("参数错误");
- }
- //配置请求参数
- $data_ = json_encode([
- "route_url" => $data['param_url'],
- 'mode' => "EXACT",
- "page_redirection" => $data['ali_url']
- ]);
- //获取接口信息
- $request = new AlipayOpenMiniQrcodeBindRequest();
- //请求接口
- $result = $this->initAli($request, $data_);
- //请求接口错误
- if ($result && $result->code != 10000) {
- debug_log($result->sub_msg, 'salesman_store_qrcode.log');
- // throw new \Exception($result->sub_msg);
- } else {
- //将当前二维码支付宝状态改为已经被使用
- $qrcode->ali_status = 1;
- if ($type) {
- $qrcode->ali_mini_id = $ali_mini_id;
- if ((int)$qrcode->wx_status === 1) {
- $qrcode->is_use = 2;
- }
- }
- if (!$qrcode->save()) {
- throw new \Exception('保存数据失败,绑定二维码成功');
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function saveConfig() {
- try {
- if (!\Yii::$app->prod_is_shangmengduli()) {
- throw new \Exception('当前版本不可切换');
- }
- $store_id = $this->store_id;
- $store = Store::findOne($store_id);
- $store->business_model = $this->business_model;
- if (!$store->save()) {
- throw new \Exception(json_encode($store->errors));
- }
- return [
- 'code' => 0,
- 'msg' => '配置成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- //获取校验文件,并保存本地
- public function saveFile($result, $type = 0, $url = '')
- {
- try {
- //获取微信配置
- $miniProgram = $result;
- //使用自定义接口
- $client = new BaseClient($miniProgram);
- //请求自定义接口
- $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload');
- if ($result['errcode'] === 0) {
- if ($type) {
- $url = '/web/face/pay';
- }
- //判断目录是否存在
- if (!is_dir(\Yii::$app->basePath . $url)) {
- //不存在则创建目录
- mkdir(\Yii::$app->basePath . $url, 0777, true);
- }
- //文件写入
- $file_result = file_put_contents(\Yii::$app->basePath . $url . '/' . $result['file_name'], $result['file_content']);
- //写入成功
- if ($file_result) {
- return [
- 'code' => 0,
- 'msg' => "文件写入成功"
- ];
- }
- //写入失败
- \Yii::error(json_encode($result));
- throw new \Exception("文件写入失败,请重新请求或手动添加。");
- } elseif ($result['errcode'] != 0) {
- //获取中文错误信息(微信返回为英文信息)
- $msg = (new BaseForm())->getZnMsg($result);
- throw new \Exception($msg);
- } else {
- throw new \Exception("数据错误");
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|