Learn.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace app\Nutrition\controller;
  3. use app\common\model\User;
  4. use app\common\model\UserLearn;
  5. use library\Controller;
  6. use library\tools\Data;
  7. use think\Db;
  8. /**
  9. * 会员学习服务
  10. * Class Label
  11. * @package app\utrition\controller
  12. */
  13. class Learn extends Controller
  14. {
  15. /**
  16. * 绑定数据表
  17. * @var string
  18. */
  19. protected $table = 'UserLearn';
  20. /**
  21. * 列表
  22. * @auth true
  23. * @menu true
  24. * @throws \think\Exception
  25. * @throws \think\db\exception\DataNotFoundException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. * @throws \think\exception\DbException
  28. * @throws \think\exception\PDOException
  29. */
  30. public function index()
  31. {
  32. $this->title = '列表';
  33. $where= [];
  34. $where[] = ['type','=',input('type',1)];
  35. $where[] = ['l.is_deleted','in','0,1'];
  36. if(input('first_id')) $where[] = ['first_id','=',input('first_id')];
  37. if(input('second_id')) $where[] = ['second_id','=',input('second_id')];
  38. if(input('phone')) $where[] = ['m.phone','=',input('phone')];
  39. $query = $this->_query($this->table)
  40. ->alias('l')
  41. ->field('l.*,m.name,m.headimg,m.phone')
  42. ->leftJoin('store_member m','m.id = l.user_id')
  43. ->where($where);
  44. $query->order(' sort desc , id desc')->page();
  45. }
  46. /**
  47. * 数据列表处理
  48. * @auth true
  49. * @menu true
  50. * @param array $data
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @throws \think\exception\DbException
  54. */
  55. protected function _index_page_filter(&$data)
  56. {
  57. }
  58. protected function _form_filter(&$data)
  59. {
  60. if($this->request->isGet() && $this->request->action() == 'edit')
  61. {
  62. $info = UserLearn::where('id',input('id'))->find()->toArray();
  63. $user_info = User::where('id',$info['user_id'])->find()->toArray();
  64. $data['phone'] = $user_info['phone'] ? $user_info['phone'] : $user_info['email'];
  65. $this->first_id =$info['first_id'];
  66. $this->second_id =$info['second_id'];
  67. $this->type =$info['type'];
  68. }
  69. if($this->request->isPost()){
  70. $phone = input('phone');
  71. $user_id = User::where('phone|email',$phone)->value('id');
  72. if(!$user_id)$this->error('用户不存在');
  73. if(empty($data['end_time']))$this->error('请选择时间');
  74. $data['user_id'] = $user_id;
  75. if(isset($data['end_time'])) $data['end_int'] = strtotime($data['end_time']);
  76. if($this->request->action() == 'add')
  77. {
  78. $check = UserLearn::where(['user_id'=>$user_id,'type'=>$data['type'],'first_id'=>$data['first_id']])->where('is_deleted','in','0,1')->value('id');
  79. if($check) {
  80. UserLearn::where(['id'=>$check])->update(['end_time'=>$data['end_time'],'end_int'=>$data['end_int']]);
  81. $this->success('保存成功');
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * 添加
  88. * @auth true
  89. * @menu true
  90. * @throws \think\Exception
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. * @throws \think\exception\DbException
  94. * @throws \think\exception\PDOException
  95. */
  96. public function add()
  97. {
  98. $this->title = '添加';
  99. $this->first_id = input('first_id');
  100. $this->second_id = input('second_id');
  101. $this->type = input('type');
  102. $this->_form($this->table, 'form');
  103. }
  104. /**
  105. * 标签
  106. * @auth true
  107. * @menu true
  108. * @throws \think\Exception
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. * @throws \think\exception\DbException
  112. * @throws \think\exception\PDOException
  113. */
  114. public function edit()
  115. {
  116. $this->title = '编辑';
  117. $this->_form($this->table, 'form');
  118. }
  119. /**
  120. * 删除
  121. * @auth true
  122. * @menu true
  123. * @throws \think\Exception
  124. * @throws \think\exception\PDOException
  125. */
  126. public function del()
  127. {
  128. $this->_save($this->table, ['is_deleted' => 2]);
  129. }
  130. /**
  131. * 批量删除
  132. * @auth true
  133. * @menu true
  134. * @throws \think\Exception
  135. * @throws \think\exception\PDOException
  136. */
  137. public function remove()
  138. {
  139. $this->_save($this->table, ['is_deleted' => 2]);
  140. }
  141. /**
  142. * 导入
  143. * @auth true
  144. * @menu true
  145. * @throws \think\Exception
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. * @throws \think\exception\DbException
  149. * @throws \think\exception\PDOException
  150. */
  151. public function import(){
  152. $data = $this->request->get();
  153. $file = request()->file('file');
  154. $file_size = $_FILES['file']['size'];
  155. if ($file_size > 5 * 1024 * 1024) $this->error('文件大小不能超过5M');
  156. //限制上传表格类型
  157. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  158. if ( $fileExtendName != 'xlsx') $this->error('必须为excel表格,且必须为xlsx格式!');
  159. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/upload';
  160. if (!file_exists($dir)) mkdir($dir, 0777, true);
  161. $info = $file->move($dir);
  162. $fileName = $info->getSaveName();
  163. $filePath = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . "/public/upload/{$fileName}";
  164. /* $reader = \PHPExcel_IOFactory::createReader('Excel2007');
  165. if(!$reader->canRead($filePath)) $reader = \PHPExcel_IOFactory::createReader('Excel2015');
  166. */
  167. $objPHPExcelReader = \PHPExcel_IOFactory::load($filePath);
  168. $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始)
  169. $highestRow = $sheet->getHighestRow(); // 取得总行数
  170. $arr = array('A','B');
  171. // 一次读取一列
  172. $res_arr = [];
  173. for ($row = 2; $row <= $highestRow; $row++) {
  174. $row_arr = array();
  175. for ($column = 0 ;$column < count($arr) ; $column++) {
  176. $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
  177. $row_arr[] = $val;
  178. }
  179. $res_arr[] = $row_arr;
  180. }
  181. $success_num = 0;
  182. foreach ($res_arr as $new_user) {
  183. if (!trim($new_user['0']) || empty($new_user['0']) || !trim($new_user['1']) || empty($new_user['1'])) continue;
  184. $phone = $new_user['0'];
  185. $user_id = User::where('phone',$phone)->value('id');
  186. if(!$user_id) continue;
  187. $insert= [];
  188. $insert['user_id'] = $user_id;
  189. $insert['end_time'] =trim($new_user['1']);
  190. $insert['end_int'] = strtotime($new_user['1']);
  191. $insert['first_id'] = $data['first_id'];
  192. $insert['type'] = $data['type'];
  193. $check = UserLearn::where(['user_id'=>$user_id,'type'=>$data['type'],'first_id'=>$data['first_id']])->where('is_deleted','in','0,1')->value('id');
  194. if($check) {
  195. UserLearn::where(['id'=>$check])->update(['end_time'=>$new_user['1'],'end_int'=>strtotime($new_user['1'])]);
  196. }else{
  197. UserLearn::create($insert);
  198. }
  199. $success_num++;
  200. }
  201. $this->success('成功导入:'.$success_num);
  202. }
  203. }