Dispatch.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\library\Sms as Smslib;
  6. /**
  7. * 派单接口
  8. */
  9. class Dispatch extends Common
  10. {
  11. protected $noNeedLogin = ['getSkillList','getReciveList'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 添加技能认证
  15. */
  16. public function addSkillAuth() {
  17. $skill_id = $this->request->request('skill_id',0,"intval"); // 技能ID
  18. $leaver_id = $this->request->request('leaver_id',0,"intval"); // 技能等级ID
  19. $area_id = $this->request->request('area_id',0,"intval"); // 区服ID
  20. $image = $this->request->request('image'); // 图片说明
  21. $voice = $this->request->request('voice'); // 语音说明
  22. $voice_time = $this->request->request('voice_time'); // 声音时长
  23. $price = $this->request->request('price'); // 价格说明
  24. $remarks = $this->request->request('remarks'); // 技能介绍
  25. if (!$skill_id || !$image || !$voice || !$remarks || !$price) {
  26. $this->error(__('Invalid parameters'));
  27. }
  28. $is_main = 0;
  29. // 获取技能信息
  30. $skillModel = new \app\common\model\DispatchSkill();
  31. $skillInfo = $skillModel->getSkillInfo($skill_id);
  32. if(!$skillInfo) {
  33. $this->error(__('未找到技能信息!'));
  34. }
  35. $authModel = new \app\common\model\DispatchAuth();
  36. // 是否已经认证过
  37. $where = [];
  38. $where["user_id"] = $this->auth->id;
  39. $where["skill_id"] = $skill_id;
  40. $authInfo = $authModel->where($where)->find();
  41. if($authInfo) {
  42. if($authInfo["status"] == 0) $this->error(__('您已提交过认证申请,请耐心等待审核!'));
  43. if($authInfo["status"] == 1) $this->error(__('您已经认证过了'));
  44. }
  45. if($authModel->where(["user_id"=>$this->auth->id])->count()<=0) {
  46. $is_main = 1;
  47. }
  48. // 添加认证信息
  49. $data = [];
  50. $data["user_id"] = $this->auth->id;
  51. $data["skill_id"] = $skill_id;
  52. $data["leaver_id"] = $leaver_id;
  53. $data["area_id"] = $area_id;
  54. $data["image"] = $image;
  55. $data["voice"] = $voice;
  56. $data["voice_time"] = $voice_time;
  57. $data["price"] = $price;
  58. $data["is_main"] = $is_main;
  59. $data["image"] = $image;
  60. $data["remarks"] = $remarks;
  61. $data["createtime"] = time();
  62. $res = $authModel->insertGetId($data);
  63. if($res) {
  64. $this->success('申请发送成功,请耐心等待审核!');
  65. } else {
  66. $this->error(__('网络错误,请稍后重试!'));
  67. }
  68. }
  69. /**
  70. * 判断当前技能是否已认证
  71. */
  72. public function skillIsAuth() {
  73. $skill_id = $this->request->request('skill_id',0,"intval"); // 技能ID
  74. if (!$skill_id) {
  75. $this->error(__('Invalid parameters'));
  76. }
  77. $authModel = new \app\common\model\DispatchAuth();
  78. // 是否已经认证过
  79. $where = [];
  80. $where["user_id"] = $this->auth->id;
  81. $where["skill_id"] = $skill_id;
  82. $authInfo = $authModel->field("status")->where($where)->find();
  83. if($authInfo) {
  84. if($authInfo["status"] == 0) $this->error(__('当前技能已提交过认证申请,请耐心等待审核!'));
  85. if($authInfo["status"] == 1) $this->error(__('当前技能已经认证过了'));
  86. }
  87. $skillLevelInfo = \app\common\model\DispatchSkillLeaver::where(["skill_id"=>$skill_id])->find();
  88. $skillAreaInfo = \app\common\model\DispatchSkillArea::where(["skill_id"=>$skill_id])->find();
  89. $res = [];
  90. $res["level"] = $skillLevelInfo?1:0;
  91. $res["area"] = $skillAreaInfo?1:0;
  92. $this->success("没有认证过,可以去认证!", $res);
  93. }
  94. // /**
  95. // * 创建接单信息
  96. // */
  97. // public function addRecive() {
  98. // $skill_id = $this->request->request('skill_id',0,"intval"); // 技能ID
  99. // $back_image = $this->request->request('back_image'); // 背景图
  100. // $price = $this->request->request('price'); // 服务价格
  101. // $describe = $this->request->request('describe'); // 一句话描述
  102. //// $tags = $this->request->request('tags'); // 标签列表(中文半角逗号隔开)
  103. // if (!$skill_id || !$back_image || !$describe) {
  104. // $this->error(__('Invalid parameters'));
  105. // }
  106. // $reciveModel = new \app\common\model\DispatchRecive();
  107. // $where = [];
  108. // $where["user_id"] = $this->auth->id;
  109. // $where["skill_id"] = $skill_id;
  110. // $where["status"] = 1;
  111. // $reciveInfo = $reciveModel->where($where)->find();
  112. // if($reciveInfo) {
  113. // $this->error(__('您已存在当前技能的接单信息,请勿重复提单'));
  114. // }
  115. //
  116. // // 获取技能信息
  117. // $skillModel = new \app\common\model\DispatchSkill();
  118. // $skillInfo = $skillModel->getSkillInfo($skill_id);
  119. // if(!$skillInfo) {
  120. // $this->error(__('未找到技能信息!'));
  121. // }
  122. //
  123. // // 添加接单信息
  124. // $data = [];
  125. // $data["user_id"] = $this->auth->id;
  126. // $data["skill_id"] = $skill_id;
  127. // $data["back_image"] = $back_image;
  128. // $data["price"] = $price;
  129. // $data["unit"] = $skillInfo["unit"];
  130. // $data["describe"] = $describe;
  131. //// $data["tags"] = $tags;
  132. // $data["createtime"] = time();
  133. // $res = $reciveModel->insertGetId($data);
  134. // if($res) {
  135. // $this->success('恭喜,接单信息已发送!');
  136. // } else {
  137. // $this->error(__('网络错误,请稍后重试!'));
  138. // }
  139. // }
  140. // /**
  141. // * 创建订单
  142. // */
  143. // public function addOrder() {
  144. // $recive_id = $this->request->request('recive_id',0,"intval"); // 接单ID
  145. // $num = $this->request->request('num',0,"intval"); // 数量
  146. // $describe = $this->request->request('describe'); // 备注要求
  147. // if (!$recive_id || !$num || !$describe) {
  148. // $this->error(__('Invalid parameters'));
  149. // }
  150. //
  151. // // 获取接单信息
  152. // $reciveModel = new \app\common\model\DispatchRecive();
  153. // $where = [];
  154. // $where["id"] = $recive_id;
  155. // $reciveInfo = $reciveModel->where($where)->find();
  156. // if(!$reciveInfo) {
  157. // $this->error(__('接单信息不存在!'));
  158. // }
  159. // if($reciveInfo["status"] == 2) {
  160. // $this->error(__('订单已关闭!'));
  161. // }
  162. // // 判断用户余额
  163. // $userModel = new \app\common\model\User();
  164. // $where = [];
  165. // $where["id"] = $this->auth->id;
  166. // $jewel = $userModel->where($where)->value("jewel");
  167. // $price = 0;
  168. // $reciveInfo["price"] >= 0 || $price = 0;
  169. // $reciveInfo["price"] >= 0 && $price = $reciveInfo["price"];
  170. // if($price * $num >= $jewel) {
  171. // $this->error(__('钻石余额不足,请先充值!'));
  172. // }
  173. // // 添加订单信息
  174. // $orderModel = new \app\common\model\DispatchOrder();
  175. // $data = [];
  176. // $data["user_id"] = $this->auth->id;
  177. // $data["recive_id"] = $recive_id;
  178. // $data["price"] = $reciveInfo["price"];
  179. // $data["num"] = $num;
  180. // $data["describe"] = $describe;
  181. // $data["createtime"] = time();
  182. // $res = $orderModel->insertGetId($data);
  183. // if($res) {
  184. // $this->success('订单创建成功!');
  185. // } else {
  186. // $this->error(__('订单创建失败!'));
  187. // }
  188. //
  189. // }
  190. // /**
  191. // * 获取接单信息
  192. // */
  193. // public function getReciveInfo() {
  194. // $recive_id = $this->request->request('recive_id',0,"intval"); // 接单ID
  195. // if (!$recive_id) {
  196. // $this->error(__('Invalid parameters'));
  197. // }
  198. // // 获取接单信息
  199. // $reciveModel = new \app\common\model\DispatchRecive();
  200. // $where = [];
  201. // $where["r.id"] = $recive_id;
  202. // $reciveInfo = $reciveModel->alias("r")
  203. // ->field("r.id,u.id as user_id,r.back_image,a.voice,u.avatar,u.nickname,s.name as skill_name,r.price,s.unit,r.service_time,r.describe,r.tags")
  204. // ->join("hx_user u","u.id = r.user_id")
  205. // ->join("hx_dispatch_skill s","s.id = r.skill_id")
  206. // ->join("hx_dispatch_auth a","a.skill_id = s.id")
  207. // ->where($where)
  208. // ->find();
  209. // if(!$reciveInfo) {
  210. // $this->error(__('数据为空!'));
  211. // }
  212. // // 在线状态和ID
  213. // $reciveInfo["is_online"] = 1;
  214. // $reciveInfo["line_id"] = 234224;
  215. // // 已服务人数
  216. // $where = [];
  217. // $where["user_id"] = $reciveInfo["user_id"];
  218. // $where["status"] = 2;
  219. // $reciveInfo["service_num"] = \app\common\model\DispatchOrder::where(["user_id"])->count();
  220. // // 主播认证通过的所有技能标签
  221. // $authModel = new \app\common\model\DispatchAuth();
  222. // $where = [];
  223. // $where["a.user_id"] = $reciveInfo["user_id"];
  224. // $where["a.status"] = $reciveInfo["user_id"];
  225. // $skillArr = $authModel->alias("a")
  226. // ->field("s.id,s.name")
  227. // ->join("hx_dispatch_skill s","s.id = a.skill_id")
  228. // ->where(["user_id"])->select();
  229. //
  230. // $reciveInfo["skills"] = $skillArr;
  231. //
  232. // $this->success('获取成功!',$reciveInfo);
  233. // }
  234. /**
  235. * 获取接单列表
  236. */
  237. public function getReciveList() {
  238. $skill_id = $this->request->request('skill_id',0,"intval"); // 技能ID
  239. $is_recommend = $this->request->request('is_recommend'); // 是否推荐:1=是,0=否
  240. $gender = $this->request->request('gender',-1); // 性别:1=男,0=女,-1=全部
  241. $page = $this->request->request('page',1); // 分页
  242. $pageNum = $this->request->request('pageNum',10); // 分页
  243. // 分页搜索构建
  244. $pageStart = ($page-1)*$pageNum;
  245. if(!in_array($gender,[-1,0,1])) {
  246. $gender = -1;
  247. }
  248. $authModel = new \app\common\model\DispatchAuth();
  249. $where = [];
  250. if ($skill_id > 0) {
  251. $where["a.skill_id"] = $skill_id;
  252. } else if($is_recommend == 1) {
  253. $where["a.is_recommend"] = $is_recommend;
  254. $where["a.is_main"] = 1;
  255. }
  256. $where["a.status"] = 1;
  257. $gender == -1 || $where["u.gender"] = $gender;
  258. $authList = $authModel->alias("a")
  259. ->field("a.id,u.avatar,ds.name as skill_name,a.voice,a.voice_time,a.price,ds.unit,u.nickname,u.gender")
  260. ->join("hx_user u","u.id = a.user_id")
  261. ->join("hx_dispatch_skill ds","ds.id = a.skill_id")
  262. ->limit($pageStart,$pageNum)
  263. ->where($where)
  264. ->select();
  265. $this->success('获取成功!',$authList);
  266. }
  267. /**
  268. * 获取推荐列表
  269. */
  270. public function getReciveCommentList() {
  271. $page = $this->request->request('page',1); // 分页
  272. $pageNum = $this->request->request('pageNum',10); // 分页
  273. // 分页搜索构建
  274. $pageStart = ($page-1)*$pageNum;
  275. $authModel = new \app\common\model\DispatchAuth();
  276. $where = [];
  277. $where["a.is_main"] = 1;
  278. $where["a.status"] = 1;
  279. $where["a.is_recommend"] = 1;
  280. $authList = $authModel->alias("a")
  281. ->field("a.id,u.avatar,ds.name as skill_name,a.voice,a.voice_time,a.price,ds.unit,u.nickname,u.gender")
  282. ->join("hx_user u","u.id = a.user_id")
  283. ->join("hx_dispatch_skill ds","ds.id = a.skill_id")
  284. ->limit($pageStart,$pageNum)
  285. ->where($where)
  286. ->select();
  287. $this->success('获取成功!',$authList);
  288. }
  289. /**
  290. * 获取接单信息
  291. */
  292. public function getReciveInfo() {
  293. $auth_id = $this->request->request('auth_id',0,"intval"); // 技能认证ID
  294. if (!$auth_id) {
  295. $this->error(__('Invalid parameters'));
  296. }
  297. // 获取接单信息
  298. $authModel = new \app\common\model\DispatchAuth();
  299. $where = [];
  300. $where["a.id"] = $auth_id;
  301. $authInfo = $authModel->alias("a")
  302. ->field("a.id,a.user_id,a.image,a.voice,a.voice_time,u.avatar,u.nickname,u.u_id,u.is_online,s.id as skill_id,s.name as skill_name,s.image as skill_icon, a.price,s.unit,a.remarks")
  303. ->join("hx_user u","u.id = a.user_id")
  304. ->join("hx_dispatch_skill s","s.id = a.skill_id")
  305. ->where($where)
  306. ->find();
  307. if(!$authInfo) {
  308. $this->error(__('数据为空!'));
  309. }
  310. // 已服务人数
  311. $where = [];
  312. $where["recive_id"] = $authInfo["user_id"];
  313. $where["status"] = 3;
  314. $authInfo["service_num"] = \app\common\model\DispatchOrder::where($where)->count();
  315. // 主播认证通过的所有技能标签
  316. $viewuserskillModel = new \app\common\model\ViewUserSkill();
  317. $skillArr = $viewuserskillModel->getSkillInfoId($authInfo["user_id"]);
  318. // 查看者是否已关注
  319. $followid = \app\common\model\UserFansFollow::where(["fans_id"=>$this->auth->id,"user_id"=>$authInfo["user_id"]])->value("id");
  320. $authInfo["is_follow"] = $followid>0?1:0;
  321. $authInfo["skills"] = $skillArr;
  322. $this->success('获取成功!',$authInfo);
  323. }
  324. /**
  325. * 创建订单
  326. */
  327. public function addOrder() {
  328. $user_id = $this->request->request('user_id',0,"intval"); // 主播用户ID
  329. $skill_id = $this->request->request('skill_id',0,"intval"); // 技能ID
  330. $num = $this->request->request('num',0,"intval"); // 数量
  331. $make_time = $this->request->request('make_time'); // 预约时间 格式:时间戳。
  332. $describe = $this->request->request('describe'); // 备注要求
  333. if (!$skill_id || !$num) {
  334. $this->error(__('Invalid parameters'));
  335. }
  336. $orderModel = new \app\common\model\DispatchOrder();
  337. // 获取接单信息
  338. $authModel = new \app\common\model\DispatchAuth();
  339. $where = [];
  340. $where["user_id"] = $user_id;
  341. $where["skill_id"] = $skill_id;
  342. $where["status"] = 1;
  343. $authInfo = $authModel->where($where)->find();
  344. if(!$authInfo) {
  345. $this->error(__('技能认证信息不存在,或认证未通过!'),[],103);
  346. }
  347. // 获取技能信息
  348. $skillModel = new \app\common\model\DispatchSkill();
  349. $where = [];
  350. $where["id"] = $skill_id;
  351. $skillInfo = $skillModel->where($where)->find();
  352. if(!$skillInfo) {
  353. $this->error(__('技能信息未找到,请稍后重试!'));
  354. }
  355. // 判断用户余额
  356. $userModel = new \app\common\model\User();
  357. $where = [];
  358. $where["id"] = $this->auth->id;
  359. $jewel = $userModel->where($where)->value("jewel");
  360. $price = 0;
  361. $authInfo["price"] >= 0 || $price = 0;
  362. $authInfo["price"] >= 0 && $price = $authInfo["price"];
  363. $money = $price * $num;
  364. if($money > $jewel) {
  365. $this->error(__('钻石余额不足,请先充值!'),[],100);
  366. }
  367. // 禁止自己下单
  368. if($this->auth->id == $authInfo["user_id"]) {
  369. $this->error(__('禁止自己下单!'),[],101);
  370. }
  371. // 查看当前用户是否有未支付订单
  372. $where = [];
  373. $where["user_id"] = $this->auth->id;
  374. $where["status"] = 0;
  375. $orderInfo = $orderModel->where($where)->find();
  376. // $orderInfo && $this->error(__('您有未支付订单,请先支付!'),[],102);
  377. Db::startTrans();
  378. try{
  379. // 添加订单信息
  380. $out_trade_no = date("YmdHis").rand(100000,999999);// 产生订单号
  381. $data = [];
  382. $data["user_id"] = $this->auth->id;
  383. $data["order_no"] = $out_trade_no;
  384. $data["recive_id"] = $authInfo["user_id"];
  385. $data["auth_id"] = $authInfo["id"];
  386. $data["price"] = $authInfo["price"];
  387. $data["num"] = $num;
  388. $data["skill_image"] = $skillInfo["image"];
  389. $data["skill_name"] = $skillInfo["name"];
  390. $data["skill_unit"] = $skillInfo["unit"];
  391. $data["make_time"] = $make_time;
  392. $data["describe"] = $describe;
  393. $data["status"] = 0;
  394. $data["createtime"] = time();
  395. $res = $orderModel->insertGetId($data);
  396. if($res) {
  397. Db::commit();
  398. $this->success('订单创建成功!',["order_no"=>$out_trade_no]);
  399. } else {
  400. $this->error(__('订单创建失败!'));
  401. }
  402. }catch (ValidateException $e) {
  403. Db::rollback();
  404. $this->error($e->getMessage());
  405. } catch (PDOException $e) {
  406. Db::rollback();
  407. $this->error($e->getMessage());
  408. } catch (Exception $e) {
  409. Db::rollback();
  410. $this->error($e->getMessage());
  411. }
  412. }
  413. /**
  414. * 添加订单评价内容
  415. */
  416. public function addOrderComment() {
  417. $order_no = $this->request->request('order_no'); // 订单编号
  418. $content = $this->request->request('content'); // 评价内容
  419. if (!$order_no && !$content) {
  420. $this->error(__('Invalid parameters'));
  421. }
  422. Db::startTrans();
  423. try{
  424. $ordercommentModel = new \app\common\model\DispatchOrderComment();
  425. $orderModel = new \app\common\model\DispatchOrder();
  426. $orderInfo = $orderModel->where(["order_no"=>$order_no])->find();
  427. if(!$orderInfo) $this->error(__('未查询到订单信息!'));
  428. if($orderInfo["status"] != 3) $this->error(__('当前订单状态不允许评价!'));
  429. $data= [];
  430. $data["user_id"] = $this->auth->id;
  431. $data["order_id"] = $orderInfo["id"];
  432. $data["content"] = $content;
  433. $data["createtime"] = time();
  434. $res1 = $ordercommentModel->insert($data);
  435. $res2 = $orderModel->update(["is_comment"=>1],["order_no"=>$order_no]);
  436. if($res1 && $res2) {
  437. Db::commit();
  438. $this->success('评价成功!');
  439. } else {
  440. $this->error(__('订单创建失败!'));
  441. }
  442. }catch (ValidateException $e) {
  443. Db::rollback();
  444. $this->error($e->getMessage());
  445. } catch (PDOException $e) {
  446. Db::rollback();
  447. $this->error($e->getMessage());
  448. } catch (Exception $e) {
  449. Db::rollback();
  450. $this->error($e->getMessage());
  451. }
  452. }
  453. /**
  454. * 钻石余额支付订单
  455. */
  456. public function jewelPay() {
  457. $order_no = $this->request->request('order_no'); // 订单号
  458. if (!$order_no) {
  459. $this->error(__('Invalid parameters'));
  460. }
  461. // 获取订单信息
  462. $orderModel = new \app\common\model\DispatchOrder();
  463. $where = [];
  464. $where["order_no"] = $order_no;
  465. $orderInfo = $orderModel->where($where)->find();
  466. if(!$orderInfo) {
  467. $this->error(__('订单信息获取失败!'));
  468. }
  469. if($orderInfo["status"] != 0) {
  470. $this->error(__('订单状态有误!'),[],104);
  471. }
  472. // 判断用户余额
  473. $userModel = new \app\common\model\User();
  474. $where = [];
  475. $where["id"] = $this->auth->id;
  476. $jewel = $userModel->where($where)->value("jewel");
  477. $money = $orderInfo["price"] * $orderInfo["num"];
  478. if($money > $jewel) {
  479. $this->error(__('钻石余额不足,请先充值!'),[],100);
  480. }
  481. // 去支付
  482. Db::startTrans();
  483. try{
  484. // 扣除用户余额
  485. $where = [];
  486. $where["id"] = $this->auth->id;
  487. $userInfo = $userModel->where($where)->find();
  488. $res1 = $userModel->where($where)->setDec("jewel",$money);
  489. $res2 = $userModel->where($where)->setInc("frozen",$money);
  490. // 添加用户余额变动记录
  491. $userjewellogModel = new \app\common\model\UserJewelLog();
  492. $data = [];
  493. $data["user_id"] = $this->auth->id;
  494. $data["value"] = $money;
  495. $data["mode"] = "-";
  496. $data["before"] = $userInfo["jewel"];
  497. $data["balance"] = $userInfo["jewel"]-$money;
  498. $data["detail"] = "下单扣除余额";
  499. $data["createtime"] = time();
  500. $res3 = $userjewellogModel->insertGetId($data);
  501. // 添加订单信息
  502. $orderModel = new \app\common\model\DispatchOrder();
  503. // 修改订单状态
  504. $where = [];
  505. $where["order_no"] = $order_no;
  506. $data = [];
  507. $data["status"] = 1;
  508. $res4 = $orderModel->update($data,$where);
  509. if($res1 && $res2 && $res3 && $res4) {
  510. Db::commit();
  511. // 短信通知
  512. $reciveuserInfo = $userModel->where(["id"=>$orderInfo["recive_id"]])->find();
  513. //Smslib::notice($reciveuserInfo->mobile, $orderInfo["skill_name"],"有人接单啦",'orderNotice');
  514. // 系统消息通知
  515. \app\common\model\Message::addMessage($orderInfo["recive_id"],"派单通知","您的派单,技能:".$orderInfo["skill_name"]."有人接单啦,请登录伴声app查看!");
  516. $this->success('支付成功!');
  517. } else {
  518. $this->error(__('订单创建失败!'));
  519. }
  520. }catch (ValidateException $e) {
  521. Db::rollback();
  522. $this->error($e->getMessage());
  523. } catch (PDOException $e) {
  524. Db::rollback();
  525. $this->error($e->getMessage());
  526. } catch (Exception $e) {
  527. Db::rollback();
  528. $this->error($e->getMessage());
  529. }
  530. }
  531. /**
  532. * 更新订单状态
  533. */
  534. public function changeOrder() {
  535. $order_no = $this->request->request('order_no'); // 订单号
  536. $status = $this->request->request('status'); // 状态:-2=已取消,-1=拒绝接单,2=进行中,3=已完成
  537. if (!$order_no || !$status) {
  538. $this->error(__('Invalid parameters'));
  539. }
  540. if(!in_array($status,["-2","-1","2","3"])) {
  541. $this->error("非法的订单状态参数");
  542. }
  543. // 获取订单信息
  544. $orderModel = new \app\common\model\DispatchOrder();
  545. $where = [];
  546. $where["order_no"] = $order_no;
  547. $orderInfo = $orderModel->where($where)->find();
  548. if(!$orderInfo) {
  549. $this->error(__('订单信息获取失败!'));
  550. }
  551. if($orderInfo["status"] == $status) {
  552. $this->error(__('当前订单状态无变更要求!'),[],104);
  553. }
  554. // 验证用户权限
  555. if($status == -1 && $orderInfo["recive_id"] != $this->auth->id) {
  556. $this->error(__('无权限操作!'),[],103);
  557. }
  558. // 验证用户权限
  559. if(($status == -2 || $status == 3) && $orderInfo["user_id"] != $this->auth->id) {
  560. $this->error(__('无权限操作!'),[],103);
  561. }
  562. if($status == -1) { // 拒绝订单
  563. if($orderInfo["status"] != 1) {
  564. $this->error(__('当前订单状态不支持拒绝订单!'),[],104);
  565. }
  566. // 完成订单操作需要更新余额记录
  567. Db::startTrans();
  568. try{
  569. $userModel = new \app\common\model\User();
  570. $money = $orderInfo["price"] * $orderInfo["num"];
  571. // 解冻用户余额
  572. $where = [];
  573. $where["id"] = $this->auth->id;
  574. $userInfo = $userModel->where($where)->find();
  575. if($userInfo["frozen"] - $money < 0) {
  576. $this->error(__('账户资金异常,请联系管理员!'),[],105);
  577. }
  578. $res1 = $userModel->where($where)->setDec("frozen",$money);
  579. $res2 = $userModel->where($where)->setInc("jewel",$money);
  580. // 添加用户余额变动记录
  581. $userjewellogModel = new \app\common\model\UserJewelLog();
  582. $res3 = $userjewellogModel->addUserJewelLog($this->auth->id, $money, "+", $userInfo["jewel"], "拒绝订单返还余额", 7);
  583. // 更新订单状态
  584. $data = [];
  585. $data["status"] = $status;
  586. $where = [];
  587. $where["order_no"] = $order_no;
  588. $res4 = $orderModel->update($data,$where);
  589. if($res1 && $res2 && $res3 && $res4) {
  590. Db::commit();
  591. // 短信通知
  592. $reciveuserInfo = $userModel->where(["id"=>$orderInfo["user_id"]])->find();
  593. //Smslib::notice($reciveuserInfo->mobile, $orderInfo["skill_name"],"被拒绝接单",'orderNotice');
  594. // 系统消息通知
  595. \app\common\model\Message::addMessage($orderInfo["user_id"],"派单通知","您的订单,技能:".$orderInfo["skill_name"]."已被拒绝接单,请登录伴声app查看!");
  596. $this->success('订单完成。');
  597. } else {
  598. $this->error(__('订单更新失败!请稍后重试'));
  599. }
  600. }catch (ValidateException $e) {
  601. Db::rollback();
  602. $this->error($e->getMessage());
  603. } catch (PDOException $e) {
  604. Db::rollback();
  605. $this->error($e->getMessage());
  606. } catch (Exception $e) {
  607. Db::rollback();
  608. $this->error($e->getMessage());
  609. }
  610. } elseif($status == 3) { // 订单完成
  611. if($orderInfo["status"] != 2) {
  612. $this->error(__('当前订单状态不支持直接变更为已完成!'),[],104);
  613. }
  614. // 完成订单操作需要更新余额记录
  615. Db::startTrans();
  616. try{
  617. $userModel = new \app\common\model\User();
  618. $money = $orderInfo["price"] * $orderInfo["num"];
  619. // 增加用户余额
  620. $where = [];
  621. $where["id"] = $orderInfo["recive_id"];
  622. $userInfo = $userModel->where($where)->find();
  623. $res1 = $userModel->where($where)->setInc("jewel",$money);
  624. $where = [];
  625. $where["id"] = $this->auth->id;
  626. $res2 = $userModel->where($where)->setDec("frozen",$money);
  627. // 添加用户余额变动记录
  628. $userjewellogModel = new \app\common\model\UserJewelLog();
  629. $res3 = $userjewellogModel->addUserJewelLog($orderInfo["recive_id"], $money, "+", $userInfo["jewel"], "完成用户订单获得收益", 8);
  630. // 更新订单状态
  631. $data = [];
  632. $data["status"] = $status;
  633. $where = [];
  634. $where["order_no"] = $order_no;
  635. $res4 = $orderModel->update($data,$where);
  636. if($res1 && $res2 && $res3 && $res4) {
  637. Db::commit();
  638. // 短信通知
  639. $reciveuserInfo = $userModel->where(["id"=>$orderInfo["recive_id"]])->find();
  640. //Smslib::send($reciveuserInfo->mobile, $orderInfo["skill_name"], "顺利完成啦!",'orderNotice');
  641. // 系统消息通知
  642. \app\common\model\Message::addMessage($orderInfo["recive_id"],"派单通知","您的派单,技能:".$orderInfo["skill_name"]." 完成啦!,请登录伴声app查看!");
  643. $this->success('恭喜!订单完成。');
  644. } else {
  645. $this->error(__('订单更新失败!请稍后重试'));
  646. }
  647. }catch (ValidateException $e) {
  648. Db::rollback();
  649. $this->error($e->getMessage());
  650. } catch (PDOException $e) {
  651. Db::rollback();
  652. $this->error($e->getMessage());
  653. } catch (Exception $e) {
  654. Db::rollback();
  655. $this->error($e->getMessage());
  656. }
  657. } else {
  658. $data = [];
  659. $data["status"] = $status;
  660. $where = [];
  661. $where["order_no"] = $order_no;
  662. $res = $orderModel->update($data,$where);
  663. if($res) {
  664. $this->success('更新成功!');
  665. } else {
  666. $this->error(__('订单创建失败!'));
  667. }
  668. }
  669. }
  670. /**
  671. * 获取技能列表
  672. */
  673. public function getSkillList() {
  674. $skillModel = new \app\common\model\DispatchSkill();
  675. $skillList = $skillModel->select();
  676. if ($skillList) {
  677. $this->success(__('获取成功!'), $skillList);
  678. } else {
  679. $this->success(__('数据为空!'));
  680. }
  681. }
  682. /**
  683. * 获取技能等级列表
  684. */
  685. public function getSkillLevelList() {
  686. $skill_id = $this->request->request('skill_id'); // 技能ID
  687. if (!$skill_id) {
  688. $this->error(__('Invalid parameters'));
  689. }
  690. $skilllevelModel = new \app\common\model\DispatchSkillLeaver();
  691. $where = [];
  692. $where["skill_id"] = $skill_id;
  693. $skilllevelList = $skilllevelModel->where($where)->select();
  694. if ($skilllevelList) {
  695. $this->success(__('获取成功!'), $skilllevelList);
  696. } else {
  697. $this->success(__('数据为空!'));
  698. }
  699. }
  700. /**
  701. * 获取技能区域列表
  702. */
  703. public function getSkillAreaList() {
  704. $skill_id = $this->request->request('skill_id'); // 技能ID
  705. if (!$skill_id) {
  706. $this->error(__('Invalid parameters'));
  707. }
  708. $skillareaModel = new \app\common\model\DispatchSkillArea();
  709. $where = [];
  710. $where["skill_id"] = $skill_id;
  711. $skillareaList = $skillareaModel->where($where)->select();
  712. if ($skillareaList) {
  713. $this->success(__('获取成功!'), $skillareaList);
  714. } else {
  715. $this->success(__('数据为空!'));
  716. }
  717. }
  718. /**
  719. * 获取订单列表
  720. */
  721. public function getOrderList() {
  722. $type = $this->request->request('type',1); // 订单类型:1=我的下单,2=我的接单
  723. $status = $this->request->request('status',999); // 订单状态:999=全部,-1=拒绝接单,0=待付款,1=待确定,2=进行中,3=已完成
  724. $page = $this->request->request('page',1); // 分页
  725. $pageNum = $this->request->request('pageNum',10); // 分页
  726. // 分页搜索构建
  727. $pageStart = ($page-1)*$pageNum;
  728. $user = "o.recive_id";
  729. $type == 1 && $user = "o.recive_id";
  730. $type == 2 && $user = "o.user_id";
  731. // 主体列表信息
  732. $orderModel = new \app\common\model\DispatchOrder();
  733. $where = [];
  734. $type == 1 && $where["o.user_id"] = $this->auth->id;
  735. $type == 2 && $where["o.recive_id"] = $this->auth->id;
  736. $status != 999 && $where["o.status"] = $status;
  737. $orderList = $orderModel->alias("o")
  738. ->field("o.id,".$user." as user_id,o.make_time,o.describe,o.order_no,u.avatar,u.nickname,o.skill_image,o.skill_name,o.price,o.skill_unit,o.num,o.status,o.is_comment,o.createtime")
  739. ->join("hx_user u","u.id = ".$user)
  740. ->where($where)
  741. ->order("createtime","desc")
  742. ->limit($pageStart,$pageNum)
  743. ->select();
  744. if(!$orderList) {
  745. $this->success(__('数据为空!'),[]);
  746. }
  747. foreach($orderList as $k => &$v) {
  748. $v["make_time"] = date("Y-m-d H:i",$v["make_time"]);
  749. $v["createtime"] = date("Y-m-d",$v["createtime"]);
  750. $v["status_text"] = $orderModel->getStateAttr($v["status"]);
  751. }
  752. $this->success(__('获取成功!'), $orderList);
  753. }
  754. }