| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Qrcode;
- use app\models\StoreMini;
- use app\models\WechatConfig;
- use EasyWeChat\Factory;
- use yii\helpers\Json;
- class QrcodeForm extends ApiModel
- {
- public $qrcode;
- public $store_id;
- public $user_id;
- public $type = 0;
- public $qrcode_bg;//推广海报背景图
- public $avatar_w;//头像宽度
- public $avatar_x;//头像x坐标
- public $avatar_y;//头像y坐标
- public $qrcode_c;//二维码样式
- public $qrcode_w;//二维码宽度
- public $qrcode_x;//二维码x坐标
- public $qrcode_y;//二维码y坐标
- public $font_w;//字体大小
- public $font_c;//字体颜色
- public $font_x;//字体x坐标
- public $font_y;//字体y坐标
- public $invite_x;//邀请码x坐标
- public $invite_y;//邀请码y坐标
- public function rules()
- {
- return [
- [['user_id','type'],'integer'],
- [['avatar_w', 'avatar_x', 'avatar_y', 'qrcode_w', 'qrcode_x', 'qrcode_y', 'font_w', 'font_x', 'font_y','invite_x', 'invite_y'],
- 'required'],
- [['avatar_w', 'avatar_x', 'avatar_y', 'qrcode_w', 'qrcode_x', 'qrcode_y', 'font_w', 'font_x', 'font_y','invite_x', 'invite_y'],
- 'number', 'min' => 0],
- [['qrcode_bg'], 'string'],
- [['font_c','type'], 'default', 'value' => 0],
- [['qrcode_bg'], 'default', 'value' => \Yii::$app->request->baseUrl.'/images/2.png'],
- [['qrcode_c'], 'in', 'range' => [0,1]]
- ];
- }
- public function attributeLabels()
- {
- return [
- 'qrcode_bg' => '推广海报背景图',
- 'avatar_w' => '头像宽度',
- 'avatar_x' => '头像x坐标',
- 'avatar_y' => '头像y坐标',
- 'qrcode_c' => '二维码样式',
- 'qrcode_w' => '二维码宽度',
- 'qrcode_x' => '二维码x坐标',
- 'qrcode_y' => '二维码y坐标',
- 'font_c' => '字体颜色',
- 'font_w' => '字体大小',
- 'font_x' => '字体x坐标',
- 'font_y' => '字体y坐标',
- 'type' => '海报类型',
- 'invite_x' => '邀请码x坐标',
- 'invite_y' => '邀请码y坐标'
- ];
- }
- public function save()
- {
- if ($this->validate()) {
- if ($this->qrcode->isNewRecord) {
- $this->qrcode->is_delete = 0;
- $this->qrcode->created_at = time();
- }
- $this->qrcode->store_id = $this->store_id;
- $this->qrcode->qrcode_bg = $this->qrcode_bg;
- $this->qrcode->preview = $this->qrcode_bg;
- $avatar_size = [
- 'w' => $this->avatar_w,
- 'h' => $this->avatar_w,
- ];
- $avatar_position = [
- 'x'=>$this->avatar_x,
- 'y'=>$this->avatar_y
- ];
- $qrcode_size = [
- 'w' => $this->qrcode_w,
- 'h' => $this->qrcode_w,
- 'c' => ($this->qrcode_c === '0') ? 'false' : 'true'
- ];
- $qrcode_position = [
- 'x' => $this->qrcode_x,
- 'y' => $this->qrcode_y
- ];
- $font_position = [
- 'x' => $this->font_x,
- 'y' => $this->font_y
- ];
- $font = [
- 'size' => $this->font_w,
- 'color' => $this->font_c
- ];
- $invite_position = [
- 'x' => $this->invite_x,
- 'y' => $this->invite_y
- ];
- $this->qrcode->avatar_size = Json::encode($avatar_size);
- $this->qrcode->avatar_position = Json::encode($avatar_position);
- $this->qrcode->qrcode_size = Json::encode($qrcode_size);
- $this->qrcode->qrcode_position = Json::encode($qrcode_position);
- $this->qrcode->font_position = Json::encode($font_position);
- $this->qrcode->font = Json::encode($font);
- $this->qrcode->invite_position = Json::encode($invite_position);
- $this->qrcode->type = $this->type;
- if ($this->qrcode->save()) {
- return [
- 'code' => 0,
- 'msg' => '成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '网络异常'
- ];
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- }
- public function search()
- {
- $save_root = \Yii::$app->basePath . '/web/temp/';
- if (!is_dir($save_root)) {
- mkdir($save_root);
- file_put_contents($save_root . '.gitignore', "*\r\n!.gitignore");
- }
- $version = cyy_version();
- $save_name = sha1("v={$version}&store_id={$this->store_id}&user_id={$this->user_id}&type=qrcode") . '.jpg';
- $pic_url = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/temp/' . $save_name;
- $wdcp_patch = false;
- $wdcp_patch_file = \Yii::$app->basePath . '/patch/wdcp.json';
- if (file_exists($wdcp_patch_file)) {
- $wdcp_patch = json_decode(file_get_contents($wdcp_patch_file), true);
- if ($wdcp_patch && in_array(\Yii::$app->request->hostName, $wdcp_patch)) {
- $wdcp_patch = true;
- } else {
- $wdcp_patch = false;
- }
- }
- if ($wdcp_patch) {
- $pic_url = str_replace('http://', 'https://', $pic_url);
- }
- if (file_exists($save_root . $save_name)) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'url'=>$pic_url . '?v=' . time()
- ]
- ];
- }
- /** @var Factory $wechat */
- $data = [
- 'width' => 430,
- ];
- $scene = "uid:$this->user_id";
- if (!is_open_platform() || \Yii::$app->prod_is_dandianpu()) {
- if (\Yii::$app->prod_is_dandianpu()) {
- $scene = "store:$this->store_id,uid:$this->user_id";
- }
- $wechat = self::getWechat();
- if (!$wechat) {
- return [
- 'code' => 1,
- 'msg' => '该功能目前仅支持支付宝小程序',
- ];
- }
- \Yii::warning("GET WXAPP QRCODE:" . "scene => ".$scene." data => ".$data);
- $response = $wechat->app_code->getUnlimit($scene, $data);
- \Yii::warning($response);
- // 保存小程序码到文件
- if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
- $response->saveAs($save_root, $save_name);
- //返回图片
- return [
- 'code' => 0,
- 'data' => [
- 'url' => $pic_url
- ]
- ];
- } else {
- //返回文字
- return [
- 'code' => 1,
- 'msg' => $response['errmsg'],
- ];
- }
- } else {
- $WechatConfig = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]);
- $storeMini = StoreMini::find()->where(['appid' => $WechatConfig->app_id, 'is_cancle' => 0])->select('id')->one();
- if (empty($storeMini)) {
- return [
- 'code' => 1,
- 'msg' => "小程序为空"
- ];
- }
- $form = new WechatThirdForm();
- $form->mini_id = $storeMini->id;
- $result = $form->getMiniQr($storeMini->id, $scene);
- if ($result['code'] === 0) {
- //返回图片
- return [
- 'code' => 0,
- 'data' => [
- 'url' => $result['data']
- ]
- ];
- }
- return $result;
- }
- }
- }
|