Customqrcode.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\WeChatObject;
  5. use app\common\utility\DiyQRCode;
  6. use app\main\constants\CacheConstants;
  7. use think\Controller;
  8. use think\Request;
  9. use app\common\library\Ssdb;
  10. use think\Config;
  11. use EasyWeChat\Factory;
  12. use app\common\library\Redis;
  13. use Symfony\Component\Cache\Simple\RedisCache;
  14. use app\common\model\WechatResponse;
  15. /**
  16. * 自定义二维码
  17. *
  18. * @icon fa fa-circle-o
  19. */
  20. class Customqrcode extends Backend
  21. {
  22. /**
  23. * CustomQrcode模型对象
  24. */
  25. protected $model = null;
  26. /**
  27. * 是否开启数据限制
  28. * 支持auth/personal
  29. * 表示按权限判断/仅限个人
  30. * 默认为禁用,若启用请务必保证表中存在admin_id字段
  31. */
  32. protected $dataLimit = 'personal';
  33. public function _initialize()
  34. {
  35. parent::_initialize();
  36. $this->model = model('CustomQrcode');
  37. $this->view->assign("typeList", $this->model->getTypeList());
  38. }
  39. /**
  40. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  41. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  42. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  43. */
  44. /**
  45. * 查看
  46. */
  47. public function index()
  48. {
  49. $redis = Redis::instance();
  50. //设置过滤方法
  51. $this->request->filter(['strip_tags']);
  52. if ($this->request->isAjax())
  53. {
  54. //如果发送的来源是Selectpage,则转发到Selectpage
  55. if ($this->request->request('pkey_name'))
  56. {
  57. return $this->selectpage();
  58. }
  59. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  60. $total = $this->model
  61. ->where($where)
  62. ->order($sort, $order)
  63. ->count();
  64. $list = $this->model
  65. ->where($where)
  66. ->order($sort, $order)
  67. ->limit($offset, $limit)
  68. ->select();
  69. foreach($list as $val){
  70. $val['url_list'] = DiyQRCode::getTplPath($val['url']);
  71. //有推广链接获取,推广链接信息
  72. if(intval($val['referral_id']) && intval($val['type']) == 3){
  73. $ref = model('Referral')->where('id',$val['referral_id'])->find();
  74. //累计阅读UV
  75. $val['all_read_uv'] = $ref['uv'];
  76. //当日阅读UV
  77. $val['day_read_uv'] = (int)Redis::instance()->get(CacheConstants::getReadOfReferralIdKey($val['referral_id']));
  78. //累计充值金额
  79. $val['all_pay_money'] = $ref['money'];
  80. //当日充值金额
  81. $dayMTkey = "M-T:".$val['referral_id'].":".date("d"); //今日充值金额key
  82. $val['day_pay_money'] = (int)Redis::instance()->get($dayMTkey)? round(Redis::instance()->get($dayMTkey) / 100, 2) :0; //今日充值金额
  83. //推广成本
  84. $val['cost'] = $ref['cost'];
  85. }else{
  86. $val['day_read_uv'] = 0;
  87. $val['all_read_uv'] = 0;
  88. $val['day_pay_money'] = 0;
  89. $val['all_pay_money'] = sprintf("%.2f",0);
  90. $val['cost'] = sprintf("%.2f",0);
  91. }
  92. $uv_key = "QR_UV:{$this->auth->id}:{$val['index']}";
  93. $uv = $redis->pfCount($uv_key);
  94. if( ($uv && !$val['uv']) || $uv > $val['uv']){
  95. $val['uv'] = $uv;
  96. }
  97. $uv_per_day_key = "QR_UV:".date('Ymd').":{$this->auth->id}:{$val['index']}";
  98. $val['uv_per_day'] = $redis->pfCount($uv_per_day_key);
  99. }
  100. $result = array("total" => $total, "rows" => $list);
  101. return json($result);
  102. }
  103. return $this->view->fetch();
  104. }
  105. /**
  106. * 添加
  107. */
  108. public function add()
  109. {
  110. $channel_id = $this->auth->agent_id ? $this->auth->agent_id : $this->auth->channel_id;
  111. if ($this->request->isPost())
  112. {
  113. $params = $this->request->post("row/a");
  114. if ($params)
  115. {
  116. if ($this->dataLimit)
  117. {
  118. $params[$this->dataLimitField] = $this->auth->id;
  119. }
  120. try
  121. {
  122. //是否采用模型验证
  123. if ($this->modelValidate)
  124. {
  125. $name = basename(str_replace('\\', '/', get_class($this->model)));
  126. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  127. $this->model->validate($validate);
  128. }
  129. $params['index'] = $this->getLastQrCodeIndex($channel_id);
  130. //生成二维码
  131. $params['url'] = $this->createWechatQrCode($channel_id,$params['index']);
  132. //如果使用模板,则根据模板创建二维码
  133. if(isset($params['template_id']) && $params['template_id']){
  134. DiyQRCode::createTplQRCode($params['url'],$params['template_id']);
  135. }
  136. $result = $this->model->allowField(true)->save($params);
  137. if ($result !== false)
  138. {
  139. $this->success();
  140. }
  141. else
  142. {
  143. $this->error($this->model->getError());
  144. }
  145. }
  146. catch (\think\exception\PDOException $e)
  147. {
  148. $this->error($e->getMessage());
  149. }
  150. }
  151. $this->error(__('Parameter %s can not be empty', ''));
  152. }
  153. return $this->view->fetch();
  154. }
  155. /**
  156. * 获取自定义二维码索引
  157. * @param $channel_id
  158. * @return int
  159. */
  160. private function getLastQrCodeIndex($channel_id){
  161. $index = 1;
  162. $last_index = $this->model->field('index')->where(['admin_id'=>$channel_id])->order('`index` desc')->find();
  163. if($last_index){
  164. if($last_index['index'] >= 100000){
  165. //大于十万时检查,是否有被删除没有使用的索引
  166. $indexs = $this->model->where(['admin_id'=>$channel_id])->column('index');
  167. $full_index = range(1,100000);
  168. if($rep_indexs = array_diff($full_index, $indexs)){
  169. return current($rep_indexs);
  170. }else{
  171. $this->error(__('WeChat QRCode Max 100000'));
  172. }
  173. }
  174. $index = $last_index['index'] + 1;
  175. }
  176. return $index;
  177. }
  178. /**
  179. * 获取自定义二维码URL
  180. * @param $index
  181. * @return mixed
  182. */
  183. private function createWechatQrCode($channel_id,$index){
  184. $adminConfig = model('AdminConfig')->getAdminInfoAll($channel_id);
  185. $wechat = new WeChatObject($adminConfig);
  186. $officialAccount = $wechat->getOfficialAccount();
  187. $result = $officialAccount->qrcode->forever($index);
  188. if(empty($result) || isset($result['errcode'])){
  189. $this->error(__('Get WeChat QRCode Error'));
  190. }
  191. return $officialAccount->qrcode->url($result['ticket']);
  192. }
  193. /**
  194. * 编辑
  195. */
  196. public function edit($ids = NULL)
  197. {
  198. $row = $this->model->get($ids);
  199. if (!$row)
  200. $this->error(__('No Results were found'));
  201. $adminIds = $this->getDataLimitAdminIds();
  202. if (is_array($adminIds))
  203. {
  204. if (!in_array($row[$this->dataLimitField], $adminIds))
  205. {
  206. $this->error(__('You have no permission'));
  207. }
  208. }
  209. if ($this->request->isPost())
  210. {
  211. $params = $this->request->post("row/a");
  212. if ($params)
  213. {
  214. try
  215. {
  216. //是否采用模型验证
  217. if ($this->modelValidate)
  218. {
  219. $name = basename(str_replace('\\', '/', get_class($this->model)));
  220. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  221. $row->validate($validate);
  222. }
  223. //如果模板不一致,生成新的模板
  224. if(intval($params['template_id']) != $row['template_id'] && intval($params['template_id'])){
  225. DiyQRCode::createTplQRCode($row['url'],$params['template_id']);
  226. }
  227. $result = $row->allowField(true)->save($params);
  228. if ($result !== false)
  229. {
  230. $this->success();
  231. }
  232. else
  233. {
  234. $this->error($row->getError());
  235. }
  236. }
  237. catch (\think\exception\PDOException $e)
  238. {
  239. $this->error($e->getMessage());
  240. }
  241. }
  242. $this->error(__('Parameter %s can not be empty', ''));
  243. }
  244. $response = WechatResponse::get(['eventkey' => $row['eventkey'], 'admin_id' => $this->auth->id]);
  245. $this->view->assign('qrcode_list',DiyQRCode::getTplPath($row['url']));
  246. $this->view->assign('referral_url',model('referral')->where('id',$row['referral_id'])->value('source_url'));
  247. $this->view->assign("response", $response);
  248. $this->view->assign("row", $row);
  249. $this->assignconfig('ids', $ids);
  250. return $this->view->fetch();
  251. }
  252. /**
  253. * 删除
  254. */
  255. public function del($ids = "")
  256. {
  257. if ($ids)
  258. {
  259. $pk = $this->model->getPk();
  260. $adminIds = $this->getDataLimitAdminIds();
  261. if (is_array($adminIds))
  262. {
  263. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  264. }
  265. $list = $this->model->where($pk, 'in', $ids)->select();
  266. $count = 0;
  267. foreach ($list as $k => $v)
  268. {
  269. //删除二维码图片
  270. DiyQRCode::unlinkQRCode($v['url']);
  271. //删除Redis缓存
  272. $redis = Redis::instance();
  273. $redis->del("QR_UV:{$v['admin_id']}:{$v['index']}");
  274. //删除数据库
  275. $count += $v->delete();
  276. }
  277. if ($count)
  278. {
  279. $this->success();
  280. }
  281. else
  282. {
  283. $this->error(__('No rows were deleted'));
  284. }
  285. }
  286. $this->error(__('Parameter %s can not be empty', 'ids'));
  287. }
  288. /**
  289. * 创建二维码
  290. * @param $ids
  291. * @param $tpl_id
  292. * @throws \Exception
  293. */
  294. public function createimage($ids, $tpl_id)
  295. {
  296. if ($this->request->isPost())
  297. {
  298. if (empty($ids) || !is_numeric($ids) || empty($tpl_id) || !is_numeric($tpl_id)) {
  299. $this->error(__('Parameter %s can not be empty', ''));
  300. }
  301. $row = $this->model->get($ids);
  302. if (!$row)
  303. $this->error(__('No Results were found'));
  304. $adminIds = $this->getDataLimitAdminIds();
  305. if (is_array($adminIds))
  306. {
  307. if (!in_array($row[$this->dataLimitField], $adminIds))
  308. {
  309. $this->error(__('You have no permission'));
  310. }
  311. }
  312. $url = $row['url'];
  313. $qrcode_save_path = ROOT_PATH.'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'qrcode'.DIRECTORY_SEPARATOR;
  314. if(file_exists($qrcode_save_path .md5($url.$tpl_id).'.png')){
  315. $imgUrl = cdnurl("/uploads/qrcode/" .md5($url.$tpl_id).'.png');
  316. } else {
  317. $imgUrl = DiyQRCode::createTplQRCode($url, $tpl_id);
  318. }
  319. $this->success('二维码图片生成成功', '', $imgUrl);
  320. }
  321. }
  322. /**
  323. * 下载二维码图片
  324. * @param $ids
  325. * @param $tpl_id
  326. */
  327. public function download($ids, $tpl_id)
  328. {
  329. if (empty($ids) || !is_numeric($ids)) {
  330. $this->error(__('Parameter %s can not be empty', ''));
  331. }
  332. $row = $this->model->get($ids);
  333. if (!$row)
  334. $this->error(__('No Results were found'));
  335. $adminIds = $this->getDataLimitAdminIds();
  336. if (is_array($adminIds))
  337. {
  338. if (!in_array($row[$this->dataLimitField], $adminIds))
  339. {
  340. $this->error(__('You have no permission'));
  341. }
  342. }
  343. $url = $row['url'];
  344. if ($tpl_id) {
  345. $imgUrl = DiyQRCode::getTplPath($url, $tpl_id);
  346. if (empty($imgUrl)) {
  347. $this->error(__('二维码图片不存在', ''));
  348. }
  349. } else {
  350. $imgUrl = DiyQRCode::createQRCodeByUrl($url);
  351. }
  352. header("Cache-Control: public");
  353. header('Content-disposition: attachment; filename='.basename($imgUrl)); //文件名
  354. header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  355. header('Content-Length: '. @filesize($imgUrl)); //告诉浏览器,文件大小
  356. @readfile($imgUrl);
  357. }
  358. }