Package.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. $fileName = md5($this->model->id);
  96. $fileUrl = '/uploads/package/'.$fileName.'.png';
  97. Db::name('package')->where('id',$this->model->id)->update(['mini_code'=>$fileUrl]);
  98. Db::commit();
  99. $this->getMiniCode($this->model->id,[],$this->auth->company_id);
  100. } catch (ValidateException|PDOException|Exception $e) {
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. }
  104. if ($result === false) {
  105. $this->error(__('No rows were inserted'));
  106. }
  107. $this->success();
  108. }
  109. /**
  110. * 编辑
  111. */
  112. public function edit($ids = null)
  113. {
  114. $row = $this->model->get($ids);
  115. if (!$row) {
  116. $this->error(__('No Results were found'));
  117. }
  118. $adminIds = $this->getDataLimitAdminIds();
  119. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  120. $this->error(__('You have no permission'));
  121. }
  122. if (false === $this->request->isPost()) {
  123. $this->view->assign('row', $row);
  124. return $this->view->fetch();
  125. }
  126. $params = $this->request->post('row/a');
  127. if (empty($params)) {
  128. $this->error(__('Parameter %s can not be empty', ''));
  129. }
  130. $params = $this->preExcludeFields($params);
  131. $result = false;
  132. Db::startTrans();
  133. try {
  134. //是否采用模型验证
  135. if ($this->modelValidate) {
  136. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  137. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  138. $row->validateFailException()->validate($validate);
  139. }
  140. $fileName = md5($ids);
  141. $fileUrl = '/uploads/package/'.$fileName.'.png';
  142. $params['mini_code'] = $fileUrl;
  143. $result = $row->allowField(true)->save($params);
  144. Db::commit();
  145. $this->getMiniCode($ids,$row,$row['company_id']);
  146. } catch (ValidateException|PDOException|Exception $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. }
  150. if (false === $result) {
  151. $this->error(__('No rows were updated'));
  152. }
  153. $this->success();
  154. }
  155. //仅返回套餐二维码
  156. private function getMiniCode($id,$package = [],$companyid)
  157. {
  158. $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
  159. if (empty($package) || empty($package['mini_code'])) {
  160. $client = new Client();
  161. $tk = getAccessToken();
  162. $miniCodeConfig = config('param.mini_code');
  163. //$miniCodeConfig['env_version'] = 'trial'; //强制体验版
  164. $miniCodeConfig['scene'] = 'id='.$id.'&shopId='.$companyid;
  165. $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
  166. 'json' => $miniCodeConfig,
  167. ]);
  168. $fileName = md5($id);
  169. $fileUrl = '/uploads/package/'.$fileName.'.png';
  170. $code = $res2->getBody()->getContents();
  171. file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
  172. $miniCode = $httpStr.$fileUrl;
  173. } else {
  174. $miniCode = $httpStr.$package['mini_code'];
  175. }
  176. return $miniCode;
  177. }
  178. }