Package.php 6.5 KB

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