| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <?php
- namespace addons\ReservationService\api\modules\v1\controllers;
- use addons\ReservationService\common\enums\ReservationServiceEnum;
- use addons\ReservationService\common\models\ReservationServiceUser;
- use addons\ReservationService\common\models\ReservationServiceWorkTime;
- use addons\ReservationService\common\services\GoodsService;
- use addons\ReservationService\common\services\TechniciansService;
- use addons\ReservationService\common\services\UserService;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- use common\helpers\ApiCode;
- use common\helpers\LocationHelper;
- use Yii;
- use yii\helpers\Json;
- class UserController extends OnAuthController
- {
- public $modelClass = '';
- //不需要验证技师身份操作
- public $no_user_action = ['settle', 'project', 'detail'];
- public function beforeAction($action)
- {
- parent::beforeAction($action);
- if(!in_array(Yii::$app->controller->action->id,$this->no_user_action)){
- try{
- $service = new UserService();
- $service->fetch($this->mall_id, $this->user_id);
- }catch(\Exception $e){
- \Yii::$app->response->data = ['code' => 1, 'msg' => $e->getMessage()];
- \Yii::$app->response->send();
- return;
- }
- }
- return $action;
- }
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = [];
- public function actionIndex()
- {
- // 用户信息
- $data['user_id'] = empty($this->user_id) ? 0 : $this->user_id;
- $data['nickname'] = empty($this->user->nickname) ? '' : $this->user->nickname;
- $data['avatar_url'] = empty($this->user->avatar_url) ? '' : $this->user->avatar_url;
- $data['mobile'] = empty($this->user->mobile) ? '' : $this->user->mobile;
- $data['level_name'] = empty($user_level_arr[$this->user->level]) ? '普通会员' : $user_level_arr[$this->user->level];
- $data['level'] = empty($this->user->level) ? '' : $this->user->level;
- $data['tag'] = [];
- $service = new UserService();
- try {
- $user = $service->customByUserId($this->mall_id, $this->user_id);
- $data['store'] = $user['store'] ?? null;
- $data['staff_level_name'] = empty($user['level']) ? '默认等级' : $user['level']['name'];
- $statistics = $service->frontStatisticsByMonth($this->mall_id, $this->user_id);
- $month = $service->statisticsByStaff($this->mall_id, $this->user_id);
- $data['statistics'] = [
- 'order_month' => $statistics['order_month'],
- 'income_month' => empty($month['month_commission']) ? 0 : $month['month_commission'],
- 'income_total' => $user['commission'] ?? 0,
- ];
- $reservation_service_user = ReservationServiceUser::findOne(['user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED]);
- if(!empty($reservation_service_user['avatar'])){
- $data['avatar_url'] = $reservation_service_user['avatar'];
- }
- if(!empty($reservation_service_user['name'])){
- $data['nickname'] = $reservation_service_user['name'];
- }
- $order_stastics = $service->orderStastics(['mall_id'=>$this->mall_id,'staff_user_id'=>$this->user_id]);
- $data = array_merge($data,$order_stastics);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('获取成功', $data);
- }
- /**
- * 入驻
- * @return void
- */
- public function actionSettle()
- {
- $service = new UserService();
- if (\Yii::$app->request->isGet) { // 获取数据
- try {
- $ret = $service->detail($this->mall_id, $this->user_id, 0, true);
- if (!empty($ret['qualification_certificate'])) {
- $ret['qualification_certificate'] = Json::decode($ret['qualification_certificate']);
- } else {
- $ret['qualification_certificate'] = [];
- }
- return $this->success('获取成功', $ret, ApiCode::CODE_SUCCESS, JSON_BIGINT_AS_STRING);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- } else if (\Yii::$app->request->isPost) { // 数据提交
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['age', 'gender'], 'required'],
- [['age', 'gender'], 'integer'],
- [['idcard', 'idcard_front', 'idcard_back', 'address', 'longitude', 'latitude', 'name', 'mobile','avatar',
- 'province','city','district'], 'string'],
- [['qualification_certificate'], 'each', 'rule' => ['string']]
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $ret = $service->save($this->mall_id, $this->user_id, $data);
- return is_string($ret) ? $this->error($ret) : $this->success('提交成功');
- }
- }
- /**
- * 修改
- * @return mixed|null
- */
- public function actionEdit()
- {
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['address', 'longitude', 'latitude', 'mobile', 'qualification_certificate', 'avatar'], 'required'],
- [['address', 'mobile', 'avatar','desc'], 'string'],
- [['qualification_certificate'], 'each', 'rule' => ['string']],
- [['longitude', 'latitude'], 'number'],
- [['id'], 'safe']
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $service = new UserService();
- $ret = $service->save($this->mall_id, $this->user_id, $data, true);
- return is_string($ret) ? $this->error($ret) : $this->success('提交成功');
- }
- /**
- * 接单状态
- * @return mixed|null
- */
- public function actionWorking()
- {
- $service = new UserService();
- try {
- $service->setWorking($this->mall_id, $this->user_id);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('提交成功');
- }
- /**
- * 工作时间设置
- * @return mixed|null
- */
- public function actionWorkingTime()
- {
- $data = Yii::$app->request->post();
- if(empty($data)) return $this->error('请选择时间');
- $times_arr = [];
- foreach($data as $key => $val){
- if(empty($val['times'])){
- unset($data[$key]);
- continue;
- }
- if($val['day'] > 6) continue;
- $times = $val['times'];
- foreach($times as $k => $v){
- if(strpos($v['start_time'],':') === false || strpos($v['end_time'],':') === false){
- //时间格式错误
- unset($times[$k]);
- continue;
- }
- $v['text'] = $v['start_time'] .'-'.$v['end_time'];
- $start = explode(':',$v['start_time']);
- $v['start_time'] = $start[0] *100 + $start[1];
- $end = explode(':',$v['end_time']);
- $v['end_time'] = $end[0] *100 + $end[1];
- $v['day'] = $val['day'];
- $times_arr[] = $v;
- //$times[$k] = $v;
- }
- //$val['times'] = $times;
- //$data[$key] = $val;
- }
- //$data = array_column($data,null,'day');
- $service = new UserService();
- $ret = $service->setWorkingTime($this->mall_id, $this->user_id, $times_arr);
- return is_string($ret) ? $this->error($ret) : $this->success('提交成功');
- }
- /**
- * 接单设置详情
- */
- public function actionWorkingDetail()
- {
- $service = new UserService();
- try {
- $service_user = $service->fetch($this->mall_id, $this->user_id);
- //查询工作时间
- $times = $service->getWorkingTime($this->mall_id, $this->user_id);
- foreach($times as $k => $date){
- foreach($date as $key => $time_spec){
- $text_arr = explode('-',$time_spec['text']);
- $time_spec['start_time'] = $text_arr[0];
- $time_spec['end_time'] = $text_arr[1];
- $date[$key] = $time_spec;
- }
- $times[$k] = $date;
- }
-
- $return = [
- 'is_working' => $service_user->is_working,
- 'times' => $times,
- 'nickname' => empty($this->user->nickname) ? '' : $this->user->nickname,
- 'avatar_url' => empty($this->user->avatar_url) ? '' : $this->user->avatar_url,
- 'week_day' => ["周日","周一","周二","周三","周四","周五","周六"]
- ];
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- return $this->success('success',$return);
- }
- /**
- * 技师信息
- * @return mixed|null
- */
- public function actionDetail()
- {
- $data = Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($data, [
- [['id'], 'integer'],
- [['id', 'lng', 'lat'], 'required'],
- [['lng', 'lat'], 'number']
- ], ['id' => '技师']);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $service = new UserService();
- $ret = $service->fetchById($this->mall_id, $data['id']);
- // 距离
- if (is_array($ret)) {
- $ret['distance'] =LocationHelper::formatDistance((new LocationHelper())->getDistanceByGeo($data['lat'], $data['lng'], $ret['latitude'], $ret['longitude']));
- if (!empty($ret['is_working'])) {
- // 再判断是否能约/什么时候能约
- $ret['reserve_text'] = "";
- // 判断什么时候能约
- $reserve_text = TechniciansService::getTechLatelyCanReserveAt($ret);
- if (empty($reserve_text)) {
- $ret['is_working'] = StatusEnum::DISABLED;
- } else {
- $ret['reserve_text'] = "{$reserve_text}可约";
- }
- $ret['work_times'] = ReservationServiceWorkTime::find()->where(['mall_id' => $this->mall_id])
- ->andWhere(['user_id' => $ret['user_id']])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['week_day' => date('w')])
- ->select('text')->column();
- }
- }
- return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
- }
- /**
- * 技师项目列表
- * @return mixed|null
- */
- public function actionProject()
- {
- $data = Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($data, [
- [['id'], 'integer'],
- [['id'], 'required']
- ], ['id' => '技师']);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 取出用户ID
- $service = new GoodsService();
- $ret = $service->getStaffBinGoods($this->mall_id, $data['id']);
- return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
- }
- /**
- * 校验扫一扫
- * @return mixed|null
- */
- public function actionCheckScan()
- {
- try {
- $service = new UserService();
- $params = [
- 'mall_id'=>$this->mall_id,
- 'user_id'=>$this->user_id,
- ];
- $service->checkScan($params);
- return $this->success();
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- }
|