Product.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use GuzzleHttp\Client;
  6. use app\common\library\Wechat;
  7. /**
  8. * 商品
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Product extends Backend
  13. {
  14. /**
  15. * Product模型对象
  16. * @var \app\admin\model\Product
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Product;
  23. $this->view->assign("isShowList", $this->model->getIsShowList());
  24. $this->view->assign("isRecommendList", $this->model->getIsRecommendList());
  25. $this->view->assign("jxtypeList", $this->model->getJxtypeList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = true;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->with(['procate','protag','productxieyi'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as $row) {
  53. $row->getRelation('procate')->visible(['name']);
  54. $row->getRelation('protag')->visible(['name']);
  55. $row->getRelation('productxieyi')->visible(['name']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 经销商码
  64. */
  65. public function jxqrcode(){
  66. $id = input('id',0);
  67. $product = Db::name('product')->where('id',$id)->find();
  68. $codeurl = $this->getMiniCode($product['id'],$product);
  69. $this->view->assign("codeurl", $codeurl.'?v='.time());
  70. $this->view->assign("product", $product);
  71. return $this->view->fetch();
  72. }
  73. //仅返回套餐二维码
  74. //https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html
  75. private function getMiniCode($id,$package = [])
  76. {
  77. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  78. //if (empty($package) || empty($package['mini_code'])) {
  79. $wechat_config = config('wxMiniProgram');
  80. $wechat = new Wechat($wechat_config['appid'],$wechat_config['secret']);
  81. $tk = $wechat->getPublicAccessToken();
  82. $miniCodeConfig = config('mini_code');
  83. $miniCodeConfig['scene'] = 'id='.$id;
  84. $res2 = curl_post('https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk,json_encode($miniCodeConfig));
  85. $fileName = md5($id);
  86. $fileUrl = '/uploads/product/'.$fileName.'.png';
  87. $code = $res2;
  88. file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
  89. $miniCode = $httpStr.$fileUrl;
  90. /*} else {
  91. $miniCode = $httpStr.$package['mini_code'];
  92. }*/
  93. return $miniCode;
  94. }
  95. }