Common.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 公共模型,所有模型都可继承此模型,基于RESTFUL CRUD操作
  4. // +----------------------------------------------------------------------
  5. // | Author:
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\model;
  8. use think\Model;
  9. use think\Request;
  10. class Common extends Model
  11. {
  12. /**
  13. * [Request 请求参数]
  14. * @Michael_xu
  15. * @return [array]
  16. */
  17. protected function requestData()
  18. {
  19. $m = strtolower(request()->module());
  20. $c = strtolower(request()->controller());
  21. $a = strtolower(request()->action());
  22. $ret = [
  23. 'm' => $m,
  24. 'c' => $c,
  25. 'a' => $a
  26. ];
  27. return $ret;
  28. }
  29. /**
  30. * [fmtRequest 格式化请求参数]
  31. * @Michael_xu
  32. * @param [array] $request [参数]
  33. * @return [array]
  34. */
  35. public function fmtRequest($request = [])
  36. {
  37. $pageType = $request['pageType'] ? 'all' : ''; //all全部(不分页)
  38. $page = 1;
  39. if (isset($request['page']) && is_numeric($request['page'])) {
  40. $page = $request['page'];
  41. unset($request['page']);
  42. }
  43. $limit = 15;
  44. if (isset($request['limit']) && is_numeric($request['limit'])) {
  45. $limit = $request['limit'];
  46. unset($request['limit']);
  47. }
  48. $offset = $length = null;
  49. if ($pageType == 'all') {
  50. $page = 1;
  51. $limit = 0;
  52. unset($request['pageType']);
  53. } else {
  54. // 大数据处理-滚动加载-子分页
  55. if ($request['sub_page'] && $request['sub_limit']) {
  56. $sub_page = $request['sub_page'];
  57. $sub_limit = $request['sub_limit'];
  58. unset($request['sub_page']);
  59. unset($request['sub_limit']);
  60. $offset = ($page - 1) * $limit + ($sub_page - 1) * $sub_limit;
  61. if ($sub_limit * $sub_page > $limit) {
  62. $length = ($page - 1) * $limit + $limit - $offset;
  63. } else {
  64. $length = $sub_limit;
  65. }
  66. if ($length < 0) {
  67. $offset = $length = 0;
  68. }
  69. }
  70. }
  71. if (!isset($offset) || !isset($length)) {
  72. $offset = ($page - 1) * $limit;
  73. $length = $limit;
  74. }
  75. $ret = [
  76. 'page' => $page,
  77. 'limit' => $limit,
  78. 'map' => $request,
  79. 'offset' => $offset,
  80. 'length' => $length,
  81. ];
  82. return $ret;
  83. }
  84. /**
  85. * [getDataById 根据主键获取详情]
  86. * @param string $id [主键]
  87. * @return [array]
  88. */
  89. public function getDataById($id = '')
  90. {
  91. $data = $this->get($id);
  92. if (!$data) {
  93. $this->error = '暂无此数据';
  94. return false;
  95. }
  96. return $data;
  97. }
  98. /**
  99. * [createData 新建]
  100. * @param array $param [description]
  101. * @return [array] [description]
  102. */
  103. public function createData($param)
  104. {
  105. // 验证
  106. $validate = validate($this->name);
  107. if (!$validate->check($param)) {
  108. $this->error = $validate->getError();
  109. return false;
  110. }
  111. try {
  112. $this->data($param)->allowField(true)->save();
  113. return true;
  114. } catch (\Exception $e) {
  115. $this->error = '添加失败';
  116. return false;
  117. }
  118. }
  119. /**
  120. * [updateDataById 编辑]
  121. * @param [type] $param [description]
  122. * @param [type] $id [description]
  123. * @return [type] [description]
  124. */
  125. public function updateDataById($param, $id)
  126. {
  127. $checkData = $this->get($id);
  128. if (!$checkData) {
  129. $this->error = '暂无此数据';
  130. return false;
  131. }
  132. // 验证
  133. $validate = validate($this->name);
  134. if (!$validate->scene('edit')->check($param)) {
  135. $this->error = $validate->getError();
  136. return false;
  137. }
  138. try {
  139. $this->allowField(true)->save($param, [$this->getPk() => $id]);
  140. return true;
  141. } catch (\Exception $e) {
  142. $this->error = '编辑失败';
  143. return false;
  144. }
  145. }
  146. /**
  147. * [delDataById 根据id删除数据]
  148. * @param string $id [主键]
  149. * @param boolean $delSon [是否删除子孙数据]
  150. * @return [type] [description]
  151. */
  152. public function delDataById($id = '', $delSon = false)
  153. {
  154. if (!$id) {
  155. $this->error = '删除失败';
  156. return false;
  157. }
  158. $this->startTrans();
  159. try {
  160. $this->where($this->getPk(), $id)->delete();
  161. if ($delSon && is_numeric($id)) {
  162. // 删除子孙
  163. $childIds = $this->getAllChild($id);
  164. if ($childIds) {
  165. $this->where($this->getPk(), 'in', $childIds)->delete();
  166. }
  167. }
  168. $this->commit();
  169. return true;
  170. } catch (\Exception $e) {
  171. $this->error = '删除失败';
  172. $this->rollback();
  173. return false;
  174. }
  175. }
  176. /**
  177. * [delDatas 批量删除数据]
  178. * @param array $ids [主键数组]
  179. * @param boolean $delSon [是否删除子孙数据]
  180. * @return [type] [description]
  181. */
  182. public function delDatas($ids = [], $delSon = false)
  183. {
  184. if (empty($ids)) {
  185. $this->error = '删除失败';
  186. return false;
  187. }
  188. // 查找所有子元素
  189. if ($delSon) {
  190. foreach ($ids as $k => $v) {
  191. if (!is_numeric($v)) continue;
  192. $childIds = $this->getAllChild($v);
  193. $ids = array_merge($ids, $childIds);
  194. }
  195. $ids = array_unique($ids);
  196. }
  197. try {
  198. $this->where($this->getPk(), 'in', $ids)->delete();
  199. return true;
  200. } catch (\Exception $e) {
  201. $this->error = '操作失败';
  202. return false;
  203. }
  204. }
  205. /**
  206. * [enableDatas 批量启用、禁用]
  207. * @param string $ids [主键数组]
  208. * @param integer $status [状态1启用0禁用]
  209. * @param [boolean] $delSon [是否删除子孙数组]
  210. * @return [type] [description]
  211. */
  212. public function enableDatas($ids = [], $status = 1, $delSon = false)
  213. {
  214. if (empty($ids)) {
  215. $this->error = '参数错误';
  216. return false;
  217. }
  218. // 查找所有子元素
  219. if ($delSon && $status === '0') {
  220. foreach ($ids as $k => $v) {
  221. $childIds = $this->getAllChild($v);
  222. $ids = array_merge($ids, $childIds);
  223. }
  224. $ids = array_unique($ids);
  225. }
  226. try {
  227. $this->where($this->getPk(), 'in', $ids)->setField('status', $status);
  228. return true;
  229. } catch (\Exception $e) {
  230. $this->error = '操作失败';
  231. return false;
  232. }
  233. }
  234. /**
  235. * 获取所有子孙
  236. */
  237. public function getAllChild($id, &$data = [])
  238. {
  239. $map['pid'] = $id;
  240. $childIds = $this->where($map)->column($this->getPk());
  241. if (!empty($childIds)) {
  242. foreach ($childIds as $v) {
  243. $data[] = $v;
  244. $this->getAllChild($v, $data);
  245. }
  246. }
  247. return $data;
  248. }
  249. /**
  250. * 逻辑删除,将数据标记为删除状态
  251. * @author Michael_xu
  252. */
  253. public function signDelById($id)
  254. {
  255. if (!$id) {
  256. $this->error = '删除失败';
  257. return false;
  258. }
  259. $this->startTrans();
  260. try {
  261. $data['is_deleted'] = 1;
  262. $data['delete_time'] = time();
  263. $this->allowField(true)->save($data, [$this->getPk() => $id]);
  264. $this->commit();
  265. return true;
  266. } catch (\Exception $e) {
  267. $this->error = '删除失败';
  268. $this->rollback();
  269. return false;
  270. }
  271. }
  272. /**
  273. * 导出数据处理
  274. */
  275. public function exportHandle($list, $field_list, $type = '')
  276. {
  277. foreach ($list as &$val) {
  278. foreach ($field_list as $field) {
  279. switch ($field['form_type']) {
  280. case 'user':
  281. if (isset($val[$field['field'] . 'name'])) {
  282. $val[$field['field']] = $val[$field['field'] . 'name'];
  283. }
  284. if ($field['field'] == 'order_user_id') {
  285. $val[$field['field']] = $val[$field['field'] . '_name'];
  286. }
  287. break;
  288. case 'structure':
  289. // $temp = array_map(function ($val) { return $val->toarray(); }, $val[$field['field'] . '_name']);
  290. // $val[$field['field']] = implode(',', array_column($temp, 'name'));
  291. // $val[$field['field']] = implode(',', array_column($temp, 'name'));
  292. break;
  293. case 'datetime':
  294. $val[$field['field']] = strtotime($val[$field['field']]) ? $val[$field['field']] : '';
  295. break;
  296. case 'customer':break;
  297. case 'business':break;
  298. case 'contacts':
  299. $val[$field['field']] = $val[$field['field'] . '_info']['name'];
  300. break;
  301. default :
  302. switch ($field['field']) {
  303. // 商机销售阶段、商机状态组
  304. case 'status_id':
  305. if ($val['is_end'] != 0) {
  306. $val[$field['field']] = $val['is_end'];
  307. }
  308. break;
  309. case 'type_id':
  310. break;
  311. // $val[$field['field']] = $val[$field['field'] . '_info'];
  312. case 'check_status' :
  313. $val[$field['field']] = $val[$field['field'] . '_info'];
  314. break;
  315. case 'plan_id' :
  316. $val[$field['field']] = $val[$field['field'] . '_info'];
  317. break;
  318. }
  319. }
  320. }
  321. }
  322. return $list;
  323. }
  324. }