SettingController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace addons\Community\api\modules\v1\controllers;
  3. use addons\Community\common\service\HeadService;
  4. use api\controllers\OnAuthController;
  5. use common\models\common\Region;
  6. use Yii;
  7. class SettingController extends OnAuthController
  8. {
  9. public $modelClass = '';
  10. /**
  11. * 不用进行登录验证的方法
  12. *
  13. * 例如: ['index', 'update', 'create', 'view', 'delete']
  14. * 默认全部需要验证
  15. *
  16. * @var array
  17. */
  18. protected $authOptional = [];
  19. public function actionEdit()
  20. {
  21. $data = Yii::$app->request->post();
  22. $valid = Yii::$app->services->validate->validate($data, [
  23. [['intro', 'street', 'name', 'logo', 'license_img'], 'string'],
  24. [['province_id', 'city_id', 'district_id', 'contact_phone'], 'integer'],
  25. [['lng', 'lat'], 'number'],
  26. [['contact_phone', 'intro', 'street', 'province_id', 'city_id', 'district_id', 'lng', 'lat', 'name'], 'required'],
  27. [['id_card_img'], 'safe']
  28. ], [
  29. 'contact_phone' => '手机号码',
  30. 'intro' => '自提点介绍',
  31. 'name' => '自提点名称',
  32. 'province_id' => '省份',
  33. 'city_id' => '城市',
  34. 'district_id' => '区县',
  35. 'lng' => '经度',
  36. 'lat' => '纬度',
  37. 'license_img' => '营业执照',
  38. 'street' => '详细地址',
  39. 'logo' => 'Logo',
  40. 'id_card_img' => '证件照片'
  41. ]);
  42. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  43. if (empty($data['id_card_img'])) return $this->error('证件照片不能为空');
  44. if (!is_array($data['id_card_img'])) return $this->error('证件照片格式错误');
  45. if (count($data['id_card_img']) != 2) return $this->error('证件照片数量错误');
  46. $regions = Region::find()->where(['in', 'id', [$data['province_id'], $data['city_id'], $data['district_id']]])
  47. ->select('name,id,parent_id,level')->orderBy('id asc')->asArray()->all();
  48. if (count($regions) != 3) return $this->error('地区错误');
  49. $regions = array_reduce($regions, function (&$regions, $val) {
  50. $regions[$val['id']] = $val;
  51. return $regions;
  52. });
  53. $province = $regions[$data['province_id']] ?? '';
  54. if (empty($province)) return $this->error('省份不存在');
  55. $city = $regions[$data['city_id']] ?? '';
  56. if (empty($city)) return $this->error('城市不存在');
  57. $district = $regions[$data['district_id']] ?? '';
  58. if (empty($district)) return $this->error('区县不存在');
  59. $data['street'] = "{$province['name']}{$city['name']}{$district['name']}{$data['street']}";
  60. $ret = (new HeadService())->update($this->mall_id, $data, $this->user_id);
  61. return is_string($ret) ? $this->error($ret) : $this->success('修改成功');
  62. }
  63. /**
  64. * 获取详情
  65. * @return mixed|null
  66. */
  67. public function actionDetail()
  68. {
  69. $ret = (new HeadService())->detail($this->mall_id, $this->user_id);
  70. return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
  71. }
  72. }