Package.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 = $this->whereop('package.company_id');
  51. $list = $this->model
  52. ->with(['company','servicetype'])
  53. ->where($where)
  54. ->where($where_op)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. foreach ($list as $row) {
  58. $row->getRelation('company')->visible(['name']);
  59. $row->getRelation('servicetype')->visible(['title']);
  60. }
  61. $result = array("total" => $list->total(), "rows" => $list->items());
  62. return json($result);
  63. }
  64. return $this->view->fetch();
  65. }
  66. /**
  67. * 添加
  68. */
  69. public function add()
  70. {
  71. if (false === $this->request->isPost()) {
  72. return $this->view->fetch();
  73. }
  74. $params = $this->request->post('row/a');
  75. if (empty($params)) {
  76. $this->error(__('Parameter %s can not be empty', ''));
  77. }
  78. $params = $this->preExcludeFields($params);
  79. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  80. $params[$this->dataLimitField] = $this->auth->id;
  81. }
  82. $result = false;
  83. Db::startTrans();
  84. try {
  85. //是否采用模型验证
  86. if ($this->modelValidate) {
  87. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  88. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  89. $this->model->validateFailException()->validate($validate);
  90. }
  91. $result = $this->model->allowField(true)->save($params);
  92. $fileName = md5($this->model->id);
  93. $fileUrl = '/uploads/package/'.$fileName.'.png';
  94. Db::name('package')->where('id',$this->model->id)->update(['mini_code'=>$fileUrl]);
  95. Db::commit();
  96. $this->getMiniCode($this->model->id,[],$params['company_id']);
  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. $oldrow = $row;
  138. $fileName = md5($ids);
  139. $fileUrl = '/uploads/package/'.$fileName.'.png';
  140. $params['mini_code'] = $fileUrl;
  141. $result = $row->allowField(true)->save($params);
  142. Db::commit();
  143. $this->getMiniCode($ids,[],$row['company_id']);
  144. } catch (ValidateException|PDOException|Exception $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. }
  148. if (false === $result) {
  149. $this->error(__('No rows were updated'));
  150. }
  151. $this->success();
  152. }
  153. //仅返回套餐二维码
  154. private function getMiniCode($id,$package = [],$companyid)
  155. {
  156. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  157. if (empty($package) || empty($package['mini_code'])) {
  158. $client = new Client();
  159. $tk = getAccessToken();
  160. $miniCodeConfig = config('param.mini_code');
  161. $miniCodeConfig['scene'] = 'id='.$id.'&shopId='.$companyid;
  162. $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
  163. 'json' => $miniCodeConfig,
  164. ]);
  165. $fileName = md5($id);
  166. $fileUrl = '/uploads/package/'.$fileName.'.png';
  167. $code = $res2->getBody()->getContents();
  168. file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
  169. $miniCode = $httpStr.$fileUrl;
  170. } else {
  171. $miniCode = $httpStr.$package['mini_code'];
  172. }
  173. return $miniCode;
  174. }
  175. }