Cooperation.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. $field = 'id,name,mobile,servicetype_id,cooperation_status,remark,createtime';
  64. $where['user_id'] = $userId;
  65. $result = $this->model->where($where)->field($field)->find();
  66. if (!empty($result)) {
  67. $statusArr = model('Cooperation')->getCooperationStatusList();
  68. $result['cooperation_status_text'] = isset($statusArr[$result['cooperation_status']]) ? $statusArr[$result['cooperation_status']] : '';
  69. $result = info_domain_image($result,['aptitude_images']);
  70. }
  71. $this->success('获取成功',$result);
  72. } catch (Exception $e) {
  73. $this->error($e->getMessage());
  74. }
  75. }
  76. }