QrcodeForm.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Qrcode;
  9. use app\models\StoreMini;
  10. use app\models\WechatConfig;
  11. use EasyWeChat\Factory;
  12. use yii\helpers\Json;
  13. class QrcodeForm extends ApiModel
  14. {
  15. public $qrcode;
  16. public $store_id;
  17. public $user_id;
  18. public $type = 0;
  19. public $qrcode_bg;//推广海报背景图
  20. public $avatar_w;//头像宽度
  21. public $avatar_x;//头像x坐标
  22. public $avatar_y;//头像y坐标
  23. public $qrcode_c;//二维码样式
  24. public $qrcode_w;//二维码宽度
  25. public $qrcode_x;//二维码x坐标
  26. public $qrcode_y;//二维码y坐标
  27. public $font_w;//字体大小
  28. public $font_c;//字体颜色
  29. public $font_x;//字体x坐标
  30. public $font_y;//字体y坐标
  31. public $invite_x;//邀请码x坐标
  32. public $invite_y;//邀请码y坐标
  33. public function rules()
  34. {
  35. return [
  36. [['user_id','type'],'integer'],
  37. [['avatar_w', 'avatar_x', 'avatar_y', 'qrcode_w', 'qrcode_x', 'qrcode_y', 'font_w', 'font_x', 'font_y','invite_x', 'invite_y'],
  38. 'required'],
  39. [['avatar_w', 'avatar_x', 'avatar_y', 'qrcode_w', 'qrcode_x', 'qrcode_y', 'font_w', 'font_x', 'font_y','invite_x', 'invite_y'],
  40. 'number', 'min' => 0],
  41. [['qrcode_bg'], 'string'],
  42. [['font_c','type'], 'default', 'value' => 0],
  43. [['qrcode_bg'], 'default', 'value' => \Yii::$app->request->baseUrl.'/images/2.png'],
  44. [['qrcode_c'], 'in', 'range' => [0,1]]
  45. ];
  46. }
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'qrcode_bg' => '推广海报背景图',
  51. 'avatar_w' => '头像宽度',
  52. 'avatar_x' => '头像x坐标',
  53. 'avatar_y' => '头像y坐标',
  54. 'qrcode_c' => '二维码样式',
  55. 'qrcode_w' => '二维码宽度',
  56. 'qrcode_x' => '二维码x坐标',
  57. 'qrcode_y' => '二维码y坐标',
  58. 'font_c' => '字体颜色',
  59. 'font_w' => '字体大小',
  60. 'font_x' => '字体x坐标',
  61. 'font_y' => '字体y坐标',
  62. 'type' => '海报类型',
  63. 'invite_x' => '邀请码x坐标',
  64. 'invite_y' => '邀请码y坐标'
  65. ];
  66. }
  67. public function save()
  68. {
  69. if ($this->validate()) {
  70. if ($this->qrcode->isNewRecord) {
  71. $this->qrcode->is_delete = 0;
  72. $this->qrcode->created_at = time();
  73. }
  74. $this->qrcode->store_id = $this->store_id;
  75. $this->qrcode->qrcode_bg = $this->qrcode_bg;
  76. $this->qrcode->preview = $this->qrcode_bg;
  77. $avatar_size = [
  78. 'w' => $this->avatar_w,
  79. 'h' => $this->avatar_w,
  80. ];
  81. $avatar_position = [
  82. 'x'=>$this->avatar_x,
  83. 'y'=>$this->avatar_y
  84. ];
  85. $qrcode_size = [
  86. 'w' => $this->qrcode_w,
  87. 'h' => $this->qrcode_w,
  88. 'c' => ($this->qrcode_c === '0') ? 'false' : 'true'
  89. ];
  90. $qrcode_position = [
  91. 'x' => $this->qrcode_x,
  92. 'y' => $this->qrcode_y
  93. ];
  94. $font_position = [
  95. 'x' => $this->font_x,
  96. 'y' => $this->font_y
  97. ];
  98. $font = [
  99. 'size' => $this->font_w,
  100. 'color' => $this->font_c
  101. ];
  102. $invite_position = [
  103. 'x' => $this->invite_x,
  104. 'y' => $this->invite_y
  105. ];
  106. $this->qrcode->avatar_size = Json::encode($avatar_size);
  107. $this->qrcode->avatar_position = Json::encode($avatar_position);
  108. $this->qrcode->qrcode_size = Json::encode($qrcode_size);
  109. $this->qrcode->qrcode_position = Json::encode($qrcode_position);
  110. $this->qrcode->font_position = Json::encode($font_position);
  111. $this->qrcode->font = Json::encode($font);
  112. $this->qrcode->invite_position = Json::encode($invite_position);
  113. $this->qrcode->type = $this->type;
  114. if ($this->qrcode->save()) {
  115. return [
  116. 'code' => 0,
  117. 'msg' => '成功'
  118. ];
  119. } else {
  120. return [
  121. 'code' => 1,
  122. 'msg' => '网络异常'
  123. ];
  124. }
  125. } else {
  126. return [
  127. 'code' => 1,
  128. 'msg' => $this->getErrorSummary(false)[0],
  129. ];
  130. }
  131. }
  132. public function search()
  133. {
  134. $save_root = \Yii::$app->basePath . '/web/temp/';
  135. if (!is_dir($save_root)) {
  136. mkdir($save_root);
  137. file_put_contents($save_root . '.gitignore', "*\r\n!.gitignore");
  138. }
  139. $version = cyy_version();
  140. $save_name = sha1("v={$version}&store_id={$this->store_id}&user_id={$this->user_id}&type=qrcode") . '.jpg';
  141. $pic_url = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/temp/' . $save_name;
  142. $wdcp_patch = false;
  143. $wdcp_patch_file = \Yii::$app->basePath . '/patch/wdcp.json';
  144. if (file_exists($wdcp_patch_file)) {
  145. $wdcp_patch = json_decode(file_get_contents($wdcp_patch_file), true);
  146. if ($wdcp_patch && in_array(\Yii::$app->request->hostName, $wdcp_patch)) {
  147. $wdcp_patch = true;
  148. } else {
  149. $wdcp_patch = false;
  150. }
  151. }
  152. if ($wdcp_patch) {
  153. $pic_url = str_replace('http://', 'https://', $pic_url);
  154. }
  155. if (file_exists($save_root . $save_name)) {
  156. return [
  157. 'code' => 0,
  158. 'msg' => 'success',
  159. 'data' => [
  160. 'url'=>$pic_url . '?v=' . time()
  161. ]
  162. ];
  163. }
  164. /** @var Factory $wechat */
  165. $data = [
  166. 'width' => 430,
  167. ];
  168. $scene = "uid:$this->user_id";
  169. if (!is_open_platform() || \Yii::$app->prod_is_dandianpu()) {
  170. if (\Yii::$app->prod_is_dandianpu()) {
  171. $scene = "store:$this->store_id,uid:$this->user_id";
  172. }
  173. $wechat = self::getWechat();
  174. if (!$wechat) {
  175. return [
  176. 'code' => 1,
  177. 'msg' => '该功能目前仅支持支付宝小程序',
  178. ];
  179. }
  180. \Yii::warning("GET WXAPP QRCODE:" . "scene => ".$scene." data => ".$data);
  181. $response = $wechat->app_code->getUnlimit($scene, $data);
  182. \Yii::warning($response);
  183. // 保存小程序码到文件
  184. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  185. $response->saveAs($save_root, $save_name);
  186. //返回图片
  187. return [
  188. 'code' => 0,
  189. 'data' => [
  190. 'url' => $pic_url
  191. ]
  192. ];
  193. } else {
  194. //返回文字
  195. return [
  196. 'code' => 1,
  197. 'msg' => $response['errmsg'],
  198. ];
  199. }
  200. } else {
  201. $WechatConfig = WechatConfig::findOne(['store_id' => get_store_id(), 'type' => 1]);
  202. $storeMini = StoreMini::find()->where(['appid' => $WechatConfig->app_id, 'is_cancle' => 0])->select('id')->one();
  203. if (empty($storeMini)) {
  204. return [
  205. 'code' => 1,
  206. 'msg' => "小程序为空"
  207. ];
  208. }
  209. $form = new WechatThirdForm();
  210. $form->mini_id = $storeMini->id;
  211. $result = $form->getMiniQr($storeMini->id, $scene);
  212. if ($result['code'] === 0) {
  213. //返回图片
  214. return [
  215. 'code' => 0,
  216. 'data' => [
  217. 'url' => $result['data']
  218. ]
  219. ];
  220. }
  221. return $result;
  222. }
  223. }
  224. }