Dispatch.php 35 KB

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