UserController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace addons\ReservationService\api\modules\v1\controllers;
  3. use addons\ReservationService\common\enums\ReservationServiceEnum;
  4. use addons\ReservationService\common\models\ReservationServiceUser;
  5. use addons\ReservationService\common\models\ReservationServiceWorkTime;
  6. use addons\ReservationService\common\services\GoodsService;
  7. use addons\ReservationService\common\services\TechniciansService;
  8. use addons\ReservationService\common\services\UserService;
  9. use api\controllers\OnAuthController;
  10. use common\enums\StatusEnum;
  11. use common\helpers\ApiCode;
  12. use common\helpers\LocationHelper;
  13. use Yii;
  14. use yii\helpers\Json;
  15. class UserController extends OnAuthController
  16. {
  17. public $modelClass = '';
  18. //不需要验证技师身份操作
  19. public $no_user_action = ['settle', 'project', 'detail'];
  20. public function beforeAction($action)
  21. {
  22. parent::beforeAction($action);
  23. if(!in_array(Yii::$app->controller->action->id,$this->no_user_action)){
  24. try{
  25. $service = new UserService();
  26. $service->fetch($this->mall_id, $this->user_id);
  27. }catch(\Exception $e){
  28. \Yii::$app->response->data = ['code' => 1, 'msg' => $e->getMessage()];
  29. \Yii::$app->response->send();
  30. return;
  31. }
  32. }
  33. return $action;
  34. }
  35. /**
  36. * 不用进行登录验证的方法
  37. *
  38. * 例如: ['index', 'update', 'create', 'view', 'delete']
  39. * 默认全部需要验证
  40. *
  41. * @var array
  42. */
  43. protected $authOptional = [];
  44. public function actionIndex()
  45. {
  46. // 用户信息
  47. $data['user_id'] = empty($this->user_id) ? 0 : $this->user_id;
  48. $data['nickname'] = empty($this->user->nickname) ? '' : $this->user->nickname;
  49. $data['avatar_url'] = empty($this->user->avatar_url) ? '' : $this->user->avatar_url;
  50. $data['mobile'] = empty($this->user->mobile) ? '' : $this->user->mobile;
  51. $data['level_name'] = empty($user_level_arr[$this->user->level]) ? '普通会员' : $user_level_arr[$this->user->level];
  52. $data['level'] = empty($this->user->level) ? '' : $this->user->level;
  53. $data['tag'] = [];
  54. $service = new UserService();
  55. try {
  56. $user = $service->customByUserId($this->mall_id, $this->user_id);
  57. $data['store'] = $user['store'] ?? null;
  58. $data['staff_level_name'] = empty($user['level']) ? '默认等级' : $user['level']['name'];
  59. $statistics = $service->frontStatisticsByMonth($this->mall_id, $this->user_id);
  60. $month = $service->statisticsByStaff($this->mall_id, $this->user_id);
  61. $data['statistics'] = [
  62. 'order_month' => $statistics['order_month'],
  63. 'income_month' => empty($month['month_commission']) ? 0 : $month['month_commission'],
  64. 'income_total' => $user['commission'] ?? 0,
  65. ];
  66. $reservation_service_user = ReservationServiceUser::findOne(['user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED]);
  67. if(!empty($reservation_service_user['avatar'])){
  68. $data['avatar_url'] = $reservation_service_user['avatar'];
  69. }
  70. if(!empty($reservation_service_user['name'])){
  71. $data['nickname'] = $reservation_service_user['name'];
  72. }
  73. $order_stastics = $service->orderStastics(['mall_id'=>$this->mall_id,'staff_user_id'=>$this->user_id]);
  74. $data = array_merge($data,$order_stastics);
  75. } catch (\Exception $e) {
  76. return $this->error($e->getMessage());
  77. }
  78. return $this->success('获取成功', $data);
  79. }
  80. /**
  81. * 入驻
  82. * @return void
  83. */
  84. public function actionSettle()
  85. {
  86. $service = new UserService();
  87. if (\Yii::$app->request->isGet) { // 获取数据
  88. try {
  89. $ret = $service->detail($this->mall_id, $this->user_id, 0, true);
  90. if (!empty($ret['qualification_certificate'])) {
  91. $ret['qualification_certificate'] = Json::decode($ret['qualification_certificate']);
  92. } else {
  93. $ret['qualification_certificate'] = [];
  94. }
  95. return $this->success('获取成功', $ret, ApiCode::CODE_SUCCESS, JSON_BIGINT_AS_STRING);
  96. } catch (\Exception $e) {
  97. return $this->error($e->getMessage());
  98. }
  99. } else if (\Yii::$app->request->isPost) { // 数据提交
  100. $data = Yii::$app->request->post();
  101. $valid = Yii::$app->services->validate->validate($data, [
  102. [['age', 'gender'], 'required'],
  103. [['age', 'gender'], 'integer'],
  104. [['idcard', 'idcard_front', 'idcard_back', 'address', 'longitude', 'latitude', 'name', 'mobile','avatar',
  105. 'province','city','district'], 'string'],
  106. [['qualification_certificate'], 'each', 'rule' => ['string']]
  107. ]);
  108. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  109. $ret = $service->save($this->mall_id, $this->user_id, $data);
  110. return is_string($ret) ? $this->error($ret) : $this->success('提交成功');
  111. }
  112. }
  113. /**
  114. * 修改
  115. * @return mixed|null
  116. */
  117. public function actionEdit()
  118. {
  119. $data = Yii::$app->request->post();
  120. $valid = Yii::$app->services->validate->validate($data, [
  121. [['address', 'longitude', 'latitude', 'mobile', 'qualification_certificate', 'avatar'], 'required'],
  122. [['address', 'mobile', 'avatar','desc'], 'string'],
  123. [['qualification_certificate'], 'each', 'rule' => ['string']],
  124. [['longitude', 'latitude'], 'number'],
  125. [['id'], 'safe']
  126. ]);
  127. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  128. $service = new UserService();
  129. $ret = $service->save($this->mall_id, $this->user_id, $data, true);
  130. return is_string($ret) ? $this->error($ret) : $this->success('提交成功');
  131. }
  132. /**
  133. * 接单状态
  134. * @return mixed|null
  135. */
  136. public function actionWorking()
  137. {
  138. $service = new UserService();
  139. try {
  140. $service->setWorking($this->mall_id, $this->user_id);
  141. } catch (\Exception $e) {
  142. return $this->error($e->getMessage());
  143. }
  144. return $this->success('提交成功');
  145. }
  146. /**
  147. * 工作时间设置
  148. * @return mixed|null
  149. */
  150. public function actionWorkingTime()
  151. {
  152. $data = Yii::$app->request->post();
  153. if(empty($data)) return $this->error('请选择时间');
  154. $times_arr = [];
  155. foreach($data as $key => $val){
  156. if(empty($val['times'])){
  157. unset($data[$key]);
  158. continue;
  159. }
  160. if($val['day'] > 6) continue;
  161. $times = $val['times'];
  162. foreach($times as $k => $v){
  163. if(strpos($v['start_time'],':') === false || strpos($v['end_time'],':') === false){
  164. //时间格式错误
  165. unset($times[$k]);
  166. continue;
  167. }
  168. $v['text'] = $v['start_time'] .'-'.$v['end_time'];
  169. $start = explode(':',$v['start_time']);
  170. $v['start_time'] = $start[0] *100 + $start[1];
  171. $end = explode(':',$v['end_time']);
  172. $v['end_time'] = $end[0] *100 + $end[1];
  173. $v['day'] = $val['day'];
  174. $times_arr[] = $v;
  175. //$times[$k] = $v;
  176. }
  177. //$val['times'] = $times;
  178. //$data[$key] = $val;
  179. }
  180. //$data = array_column($data,null,'day');
  181. $service = new UserService();
  182. $ret = $service->setWorkingTime($this->mall_id, $this->user_id, $times_arr);
  183. return is_string($ret) ? $this->error($ret) : $this->success('提交成功');
  184. }
  185. /**
  186. * 接单设置详情
  187. */
  188. public function actionWorkingDetail()
  189. {
  190. $service = new UserService();
  191. try {
  192. $service_user = $service->fetch($this->mall_id, $this->user_id);
  193. //查询工作时间
  194. $times = $service->getWorkingTime($this->mall_id, $this->user_id);
  195. foreach($times as $k => $date){
  196. foreach($date as $key => $time_spec){
  197. $text_arr = explode('-',$time_spec['text']);
  198. $time_spec['start_time'] = $text_arr[0];
  199. $time_spec['end_time'] = $text_arr[1];
  200. $date[$key] = $time_spec;
  201. }
  202. $times[$k] = $date;
  203. }
  204. $return = [
  205. 'is_working' => $service_user->is_working,
  206. 'times' => $times,
  207. 'nickname' => empty($this->user->nickname) ? '' : $this->user->nickname,
  208. 'avatar_url' => empty($this->user->avatar_url) ? '' : $this->user->avatar_url,
  209. 'week_day' => ["周日","周一","周二","周三","周四","周五","周六"]
  210. ];
  211. } catch (\Exception $e) {
  212. return $this->error($e->getMessage());
  213. }
  214. return $this->success('success',$return);
  215. }
  216. /**
  217. * 技师信息
  218. * @return mixed|null
  219. */
  220. public function actionDetail()
  221. {
  222. $data = Yii::$app->request->get();
  223. $valid = Yii::$app->services->validate->validate($data, [
  224. [['id'], 'integer'],
  225. [['id', 'lng', 'lat'], 'required'],
  226. [['lng', 'lat'], 'number']
  227. ], ['id' => '技师']);
  228. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  229. $service = new UserService();
  230. $ret = $service->fetchById($this->mall_id, $data['id']);
  231. // 距离
  232. if (is_array($ret)) {
  233. $ret['distance'] =LocationHelper::formatDistance((new LocationHelper())->getDistanceByGeo($data['lat'], $data['lng'], $ret['latitude'], $ret['longitude']));
  234. if (!empty($ret['is_working'])) {
  235. // 再判断是否能约/什么时候能约
  236. $ret['reserve_text'] = "";
  237. // 判断什么时候能约
  238. $reserve_text = TechniciansService::getTechLatelyCanReserveAt($ret);
  239. if (empty($reserve_text)) {
  240. $ret['is_working'] = StatusEnum::DISABLED;
  241. } else {
  242. $ret['reserve_text'] = "{$reserve_text}可约";
  243. }
  244. $ret['work_times'] = ReservationServiceWorkTime::find()->where(['mall_id' => $this->mall_id])
  245. ->andWhere(['user_id' => $ret['user_id']])
  246. ->andWhere(['status' => StatusEnum::ENABLED])
  247. ->andWhere(['week_day' => date('w')])
  248. ->select('text')->column();
  249. }
  250. }
  251. return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
  252. }
  253. /**
  254. * 技师项目列表
  255. * @return mixed|null
  256. */
  257. public function actionProject()
  258. {
  259. $data = Yii::$app->request->get();
  260. $valid = Yii::$app->services->validate->validate($data, [
  261. [['id'], 'integer'],
  262. [['id'], 'required']
  263. ], ['id' => '技师']);
  264. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  265. // 取出用户ID
  266. $service = new GoodsService();
  267. $ret = $service->getStaffBinGoods($this->mall_id, $data['id']);
  268. return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
  269. }
  270. /**
  271. * 校验扫一扫
  272. * @return mixed|null
  273. */
  274. public function actionCheckScan()
  275. {
  276. try {
  277. $service = new UserService();
  278. $params = [
  279. 'mall_id'=>$this->mall_id,
  280. 'user_id'=>$this->user_id,
  281. ];
  282. $service->checkScan($params);
  283. return $this->success();
  284. }catch (\Exception $e){
  285. return $this->error($e->getMessage());
  286. }
  287. }
  288. }