Cooperation.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. class Cooperation extends Api
  7. {
  8. protected $noNeedLogin = [];
  9. protected $noNeedRight = '*';
  10. protected $model = null;
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = Db::name('cooperation');
  15. }
  16. /**
  17. * 保存
  18. * @return void
  19. */
  20. public function save()
  21. {
  22. try {
  23. //验证参数
  24. $id = $this->request->param('id',0);
  25. $userId = $this->auth->id;
  26. $scene = !empty($id) ? 'edit' : 'add';
  27. $validate = validate('Cooperation');
  28. if(!$validate->check($this->request->param(),[],$scene)){
  29. throw new Exception($validate->getError());
  30. }
  31. $time = time();
  32. $data = [
  33. 'name' => $this->request->param('name', ''),
  34. 'mobile' => $this->request->param('mobile', ''),
  35. 'servicetype_id' => $this->request->param('servicetype_id', 0),
  36. ];
  37. if (empty($id)) {
  38. $data['user_id'] = $userId;
  39. $data['createtime'] = $time;
  40. $res = $this->model->insertGetId($data);
  41. } else {
  42. $data['updatetime'] = $time;
  43. $where['id'] = $id;
  44. $where['user_id'] = $userId;
  45. $res = $this->model->where($where)->update($data);
  46. }
  47. if (!$res) {
  48. throw new Exception('操作失败');
  49. }
  50. $this->success('操作成功');
  51. } catch (Exception $e) {
  52. $this->error($e->getMessage());
  53. }
  54. }
  55. /**
  56. * 详情
  57. * @return void
  58. */
  59. public function getInfo()
  60. {
  61. try {
  62. $userId = $this->auth->id;
  63. $c = 'cooperation';
  64. $s = 'servicetype';
  65. $field = $c.'.id,'.$c.'.name,mobile,servicetype_id,cooperation_status,'.$c.'.remark,'.$c.'.createtime,'.$s.'.title as `service_title`';
  66. $where[$c.'.user_id'] = $userId;
  67. $result = $this->model->alias($c)
  68. ->join($s,$s.'.id = '.$c.'.servicetype_id','LEFT')
  69. ->where($where)->field($field)->find();
  70. if (!empty($result)) {
  71. $statusArr = model('Cooperation')->getCooperationStatusList();
  72. $result['cooperation_status_text'] = isset($statusArr[$result['cooperation_status']]) ? $statusArr[$result['cooperation_status']] : '';
  73. $result = info_domain_image($result,['aptitude_images']);
  74. }
  75. $this->success('获取成功',$result);
  76. } catch (Exception $e) {
  77. $this->error($e->getMessage());
  78. }
  79. }
  80. }