Equipment.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\admin\controller\qingdongams\customer;
  3. use app\admin\controller\qingdongams\Base;
  4. use addons\qingdongams\model\CustomerProduct;
  5. use fast\Random;
  6. use think\Db;
  7. use think\Exception;
  8. /**
  9. * 设备
  10. * @icon fa fa-user
  11. */
  12. class Equipment extends Base {
  13. protected $relationSearch = true;
  14. // protected $searchFields = 'id,username,nickname';
  15. /**
  16. * @var \addons\qingdongams\model\Equipment
  17. */
  18. protected $model = null;
  19. protected $proModel = null;
  20. protected $productList = null;
  21. protected $customerProductModel = null;
  22. protected $customerProductList = null;
  23. public function _initialize() {
  24. parent::_initialize();
  25. $this->model = new \addons\qingdongams\model\Equipment;
  26. $this->proModel = new \addons\qingdongams\model\Product();
  27. $this->customerProductModel = new CustomerProduct();
  28. $this->productList = collection($this->proModel->select())->toArray();
  29. $productList = [0 => __('None')];
  30. foreach ($this->productList as $k => &$v) {
  31. $productList[$v['id']] = $v['name'];
  32. }
  33. $this->customerProductList = collection($this->customerProductModel->select())->toArray();
  34. $customerProductList = [0 => __('None')];
  35. foreach ($this->customerProductList as $k => &$v) {
  36. $customerProductList[$v['id']] = $v['number'];
  37. }
  38. $this->view->assign('productList', $productList);
  39. $this->view->assign('customerProductList', $customerProductList);
  40. }
  41. /**
  42. * 列表
  43. */
  44. public function lists($ids = null) {
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model->where([
  47. 'pro_id' => $ids,
  48. ])->paginate($limit);
  49. $result = array("total" => $list->total(), "rows" => $list->items());
  50. return json($result);
  51. }
  52. /**
  53. * 查看
  54. */
  55. public function index() {
  56. //设置过滤方法
  57. $this->request->filter(['strip_tags', 'trim']);
  58. if ($this->request->isAjax()) {
  59. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  60. //
  61. //负责人为空
  62. $list = $this->model->where([])->where($where)->with('product')->order('id', 'desc')->paginate($limit);
  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. if ($this->request->isPost()) {
  73. $params = $this->request->post("row/a");
  74. if ($params) {
  75. $params = $this->preExcludeFields($params);
  76. $result = false;
  77. Db::startTrans();
  78. try {
  79. $productModel = new \addons\qingdongams\model\Product();
  80. $proName = $productModel->where(['id'=>$params['pro_id']])->value('name');
  81. $params['number'] = date("md").rand(100000,999999);
  82. $img = $proName.'_'.$params['number'];
  83. if(create_logo_qrcode($img) == 'ok'){
  84. $params['logo'] = "/qrcode/".$img.'.jpg';
  85. }else{
  86. throw new \Exception('二维码生成有误');
  87. }
  88. $params['binding_status'] = 1;
  89. $result = $this->model->allowField(true)->save($params);
  90. Db::commit();
  91. } catch (Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. if ($result !== false) {
  96. $this->success();
  97. } else {
  98. $this->error(__('No rows were inserted'));
  99. }
  100. }
  101. $this->error(__('Parameter %s can not be empty', ''));
  102. }
  103. return $this->view->fetch();
  104. }
  105. /**修改
  106. */
  107. public function edit($ids = null) {
  108. $map['id'] = $ids;
  109. if ($this->request->isAjax()) {
  110. $params = $this->request->post('row/a');
  111. $params = $this->preExcludeFields($params);
  112. $result = false;
  113. Db::startTrans();
  114. try {
  115. unset($params['logo']);
  116. unset($params['number']);
  117. $result = $this->model->allowField(true)->update($params, $map);
  118. Db::commit();
  119. } catch (Exception $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. }
  123. if ($result !== false) {
  124. $this->success('修改成功');
  125. } else {
  126. $this->error('修改失败');
  127. }
  128. }
  129. $data = $this->model->where($map)->find();
  130. $this->view->assign("row", $data);
  131. return $this->view->fetch();
  132. }
  133. /**详情
  134. */
  135. public function detail($ids=null){
  136. $row=$this->model->get($ids);
  137. $this->assign('row',$row);
  138. $this->assign('ids',$ids);
  139. return $this->view->fetch();
  140. }
  141. /**
  142. * 删除
  143. */
  144. public function del($ids = "") {
  145. if (!$this->request->isPost()) {
  146. $this->error(__("Invalid parameters"));
  147. }
  148. $ids = $ids ? $ids : $this->request->post("ids");
  149. $ids = explode(',',$ids);
  150. $this->model->destroy(function ($query) use($ids){
  151. $query->where('id','in',$ids);
  152. });
  153. $this->success();
  154. }
  155. /**
  156. * 导出二维码 -- 格式为所有二维码图片
  157. * @param $ercode_list 二维码链接数组,我的数组包含两个参数,url 和 unique_id
  158. * url 二维码链接
  159. * unique_id 主要为生成独一无二的二维码文件名称
  160. * @return string
  161. */
  162. public function export_ercode()
  163. {
  164. $ids = $this->request->get('ids');
  165. $ercode_list = collection($this->model->where('id','in',explode(',',$ids))->select())->toArray();
  166. $img_arr = array();
  167. foreach ($ercode_list as $ercode) {
  168. $img_name = ROOT_PATH."public".$ercode['logo'];
  169. $img_arr[] = $img_name;
  170. }
  171. $zip_name = ROOT_PATH.'public/uploads/ercode/ercode_' . date('YmdHis') . '.zip';
  172. makeZip($zip_name, $img_arr);
  173. download($zip_name);
  174. $this->redirect('index');
  175. }
  176. }