Package.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use GuzzleHttp\Client;
  6. /**
  7. * 套餐管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Package extends Backend
  12. {
  13. /**
  14. * Package模型对象
  15. * @var \app\admin\model\Package
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Package;
  22. $listArr = [
  23. 'statusList' => $this->model->getStatusList(),
  24. 'typeList' => $this->model->getTypeList(),
  25. ];
  26. $this->view->assign($listArr);
  27. $this->assignconfig($listArr);
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //当前是否为关联查询
  40. $this->relationSearch = true;
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags', 'trim']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. //只能看自己的
  50. $where_op = [];
  51. if($this->auth->company_id){
  52. $where_op['package.company_id'] = $this->auth->company_id;
  53. }
  54. $list = $this->model
  55. ->with(['company','servicetype'])
  56. ->where($where)
  57. ->where($where_op)
  58. ->order($sort, $order)
  59. ->paginate($limit);
  60. foreach ($list as $row) {
  61. $row->getRelation('company')->visible(['name']);
  62. $row->getRelation('servicetype')->visible(['title']);
  63. }
  64. $result = array("total" => $list->total(), "rows" => $list->items());
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 添加
  71. */
  72. public function add()
  73. {
  74. if (false === $this->request->isPost()) {
  75. return $this->view->fetch();
  76. }
  77. $params = $this->request->post('row/a');
  78. if (empty($params)) {
  79. $this->error(__('Parameter %s can not be empty', ''));
  80. }
  81. $params = $this->preExcludeFields($params);
  82. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  83. $params[$this->dataLimitField] = $this->auth->id;
  84. }
  85. $result = false;
  86. Db::startTrans();
  87. try {
  88. //是否采用模型验证
  89. if ($this->modelValidate) {
  90. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  91. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  92. $this->model->validateFailException()->validate($validate);
  93. }
  94. $result = $this->model->allowField(true)->save($params);
  95. $this->getMiniCode($this->model->id,[],$this->auth->company_id);
  96. Db::commit();
  97. } catch (ValidateException|PDOException|Exception $e) {
  98. Db::rollback();
  99. $this->error($e->getMessage());
  100. }
  101. if ($result === false) {
  102. $this->error(__('No rows were inserted'));
  103. }
  104. $this->success();
  105. }
  106. /**
  107. * 编辑
  108. */
  109. public function edit($ids = null)
  110. {
  111. $row = $this->model->get($ids);
  112. if (!$row) {
  113. $this->error(__('No Results were found'));
  114. }
  115. $adminIds = $this->getDataLimitAdminIds();
  116. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  117. $this->error(__('You have no permission'));
  118. }
  119. if (false === $this->request->isPost()) {
  120. $this->view->assign('row', $row);
  121. return $this->view->fetch();
  122. }
  123. $params = $this->request->post('row/a');
  124. if (empty($params)) {
  125. $this->error(__('Parameter %s can not be empty', ''));
  126. }
  127. $params = $this->preExcludeFields($params);
  128. $result = false;
  129. Db::startTrans();
  130. try {
  131. //是否采用模型验证
  132. if ($this->modelValidate) {
  133. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  134. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  135. $row->validateFailException()->validate($validate);
  136. }
  137. $result = $row->allowField(true)->save($params);
  138. $this->getMiniCode($ids,$row,$row['company_id']);
  139. Db::commit();
  140. } catch (ValidateException|PDOException|Exception $e) {
  141. Db::rollback();
  142. $this->error($e->getMessage());
  143. }
  144. if (false === $result) {
  145. $this->error(__('No rows were updated'));
  146. }
  147. $this->success();
  148. }
  149. //仅返回套餐二维码
  150. private function getMiniCode($id,$package = [],$companyid)
  151. {
  152. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  153. if (empty($package) || empty($package['mini_code'])) {
  154. $client = new Client();
  155. $tk = getAccessToken();
  156. $miniCodeConfig = config('param.mini_code');
  157. //$miniCodeConfig['env_version'] = 'trial'; //强制体验版
  158. $miniCodeConfig['scene'] = 'id='.$id.'&shopId='.$companyid;
  159. $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
  160. 'json' => $miniCodeConfig,
  161. ]);
  162. $fileName = md5($id);
  163. $fileUrl = '/uploads/package/'.$fileName.'.png';
  164. $code = $res2->getBody()->getContents();
  165. file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
  166. $companyRes = Db::name('package')->where('id',$id)->update(['mini_code'=>$fileUrl]);
  167. if ($companyRes === false) {
  168. //$this->error('生成套餐小程序码失败');
  169. }
  170. $miniCode = $httpStr.$fileUrl;
  171. } else {
  172. $miniCode = $httpStr.$package['mini_code'];
  173. }
  174. return $miniCode;
  175. }
  176. }