Dispatch.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. $res1 = true;
  492. $res2 = $userModel->where($where)->setInc("frozen",$money);
  493. // 添加用户余额变动记录
  494. $res3 = true;
  495. //增加用户钻石余额
  496. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id, $money, "-", 0, "下单扣除余额", 0,'jewel');
  497. if($rs_wallet['status'] == false){
  498. Db::rollback();
  499. $this->error($rs_wallet['msg']);
  500. }
  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"]."有人下单啦,请注意查看!");
  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"] = $orderInfo["user_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 = true;//下单用户
  580. // 添加用户余额变动记录
  581. $res3 = true;
  582. //增加用户钻石余额
  583. $rs_wallet = model('wallet')->lockChangeAccountRemain($orderInfo["user_id"], $money, "+", 0, "拒绝订单返还余额", 7,'jewel');
  584. if($rs_wallet['status'] == false){
  585. Db::rollback();
  586. $this->error($rs_wallet['msg']);
  587. }
  588. // 更新订单状态
  589. $data = [];
  590. $data["status"] = $status;
  591. $where = [];
  592. $where["order_no"] = $order_no;
  593. $res4 = $orderModel->update($data,$where);
  594. if($res1 && $res2 && $res3 && $res4) {
  595. Db::commit();
  596. // 短信通知
  597. //$reciveuserInfo = $userModel->where(["id"=>$orderInfo["user_id"]])->find();
  598. //Smslib::notice($reciveuserInfo->mobile, $orderInfo["skill_name"],"被拒绝接单",'orderNotice');
  599. // 系统消息通知
  600. \app\common\model\Message::addMessage($orderInfo["user_id"],"派单通知","您的订单,技能:".$orderInfo["skill_name"]."已被拒绝接单,请注意查看!");
  601. $this->success('订单已拒绝。');
  602. } else {
  603. $this->error(__('订单更新失败!请稍后重试'));
  604. }
  605. }catch (ValidateException $e) {
  606. Db::rollback();
  607. $this->error($e->getMessage());
  608. } catch (PDOException $e) {
  609. Db::rollback();
  610. $this->error($e->getMessage());
  611. } catch (Exception $e) {
  612. Db::rollback();
  613. $this->error($e->getMessage());
  614. }
  615. } elseif($status == 3) { // 订单完成
  616. if($orderInfo["status"] != 2) {
  617. $this->error(__('当前订单状态不支持直接变更为已完成!'),[],104);
  618. }
  619. // 完成订单操作需要更新余额记录
  620. Db::startTrans();
  621. try{
  622. $userModel = new \app\common\model\User();
  623. $money = $orderInfo["price"] * $orderInfo["num"];
  624. // 增加用户余额
  625. $res1 = true;
  626. //
  627. $where = [];
  628. $where["id"] = $orderInfo['user_id'];
  629. $res2 = $userModel->where($where)->setDec("frozen",$money);
  630. // 添加用户余额变动记录
  631. $res3 = true;
  632. $remark = $orderInfo['skill_name'].'服务';
  633. $jewelMoneyRate = config('site.money_to_jewel');//1人民币兑换钻石数
  634. $moneyRmb = bcdiv($money,$jewelMoneyRate);
  635. $rs_wallet = model('wallet')->lockChangeAccountRemain($orderInfo["recive_id"],$moneyRmb,'+',0,$remark,106,'money');
  636. if($rs_wallet['status'] == false){
  637. Db::rollback();
  638. $this->error($rs_wallet['msg']);
  639. }
  640. // 更新订单状态
  641. $data = [];
  642. $data["status"] = $status;
  643. $data["is_comment"] = 1;//要求直接自动已评价,为了前端不显示立即评价按钮
  644. $where = [];
  645. $where["order_no"] = $order_no;
  646. $res4 = $orderModel->update($data,$where);
  647. if($res1 && $res2 && $res3 && $res4) {
  648. Db::commit();
  649. // 短信通知
  650. //$reciveuserInfo = $userModel->where(["id"=>$orderInfo["recive_id"]])->find();
  651. //Smslib::send($reciveuserInfo->mobile, $orderInfo["skill_name"], "顺利完成啦!",'orderNotice');
  652. // 系统消息通知
  653. \app\common\model\Message::addMessage($orderInfo["recive_id"],"派单通知","您的派单,技能:".$orderInfo["skill_name"]." 完成啦!,请注意查看!");
  654. $this->success('恭喜!订单完成。');
  655. } else {
  656. $this->error(__('订单更新失败!请稍后重试'));
  657. }
  658. }catch (ValidateException $e) {
  659. Db::rollback();
  660. $this->error($e->getMessage());
  661. } catch (PDOException $e) {
  662. Db::rollback();
  663. $this->error($e->getMessage());
  664. } catch (Exception $e) {
  665. Db::rollback();
  666. $this->error($e->getMessage());
  667. }
  668. }if($status == -2) { // 取消订单
  669. if($orderInfo["status"] != 1) {
  670. $this->error(__('当前订单状态不支持拒绝订单!'),[],104);
  671. }
  672. // 完成订单操作需要更新余额记录
  673. Db::startTrans();
  674. try{
  675. $userModel = new \app\common\model\User();
  676. $money = $orderInfo["price"] * $orderInfo["num"];
  677. // 解冻用户余额
  678. $where = [];
  679. $where["id"] = $orderInfo['user_id'];
  680. $userInfo = $userModel->where($where)->find();
  681. if($userInfo["frozen"] - $money < 0) {
  682. $this->error(__('账户资金异常,请联系管理员!'),[],105);
  683. }
  684. $res1 = $userModel->where($where)->setDec("frozen",$money);
  685. $res2 = true;
  686. // 添加用户余额变动记录
  687. $res3 = true;
  688. $rs_wallet = model('wallet')->lockChangeAccountRemain($orderInfo["user_id"], $money, "+", 0, "取消订单返还余额", 7,'money');
  689. if($rs_wallet['status'] == false){
  690. Db::rollback();
  691. $this->error($rs_wallet['msg']);
  692. }
  693. // 更新订单状态
  694. $data = [];
  695. $data["status"] = $status;
  696. $where = [];
  697. $where["order_no"] = $order_no;
  698. $res4 = $orderModel->update($data,$where);
  699. if($res1 && $res2 && $res3 && $res4) {
  700. Db::commit();
  701. // 短信通知
  702. //$reciveuserInfo = $userModel->where(["id"=>$orderInfo["user_id"]])->find();
  703. //Smslib::notice($reciveuserInfo->mobile, $orderInfo["skill_name"],"被拒绝接单",'orderNotice');
  704. // 系统消息通知
  705. \app\common\model\Message::addMessage($orderInfo["recive_id"],"派单通知","您的订单,技能:".$orderInfo["skill_name"]."已被取消,请登录查看!");
  706. $this->success('订单取消成功。');
  707. } else {
  708. $this->error(__('订单更新失败!请稍后重试'));
  709. }
  710. }catch (ValidateException $e) {
  711. Db::rollback();
  712. $this->error($e->getMessage());
  713. } catch (PDOException $e) {
  714. Db::rollback();
  715. $this->error($e->getMessage());
  716. } catch (Exception $e) {
  717. Db::rollback();
  718. $this->error($e->getMessage());
  719. }
  720. } else {
  721. $data = [];
  722. $data["status"] = $status;
  723. $where = [];
  724. $where["order_no"] = $order_no;
  725. $res = $orderModel->update($data,$where);
  726. if($res) {
  727. if ($status == 2) {//接单发
  728. \app\common\model\Message::addMessage($orderInfo['user_id'],"订单单通知","您的订单:".$order_no."已被接单,请登录查看!");
  729. }
  730. $this->success('更新成功!');
  731. } else {
  732. $this->error(__('订单创建失败!'));
  733. }
  734. }
  735. }
  736. /**
  737. * 获取技能列表
  738. */
  739. public function getSkillList() {
  740. $skillModel = new \app\common\model\DispatchSkill();
  741. $skillList = $skillModel->select();
  742. if ($skillList) {
  743. $this->success(__('获取成功!'), $skillList);
  744. } else {
  745. $this->success(__('数据为空!'));
  746. }
  747. }
  748. /**
  749. * 获取技能等级列表
  750. */
  751. public function getSkillLevelList() {
  752. $skill_id = $this->request->request('skill_id'); // 技能ID
  753. if (!$skill_id) {
  754. $this->error(__('Invalid parameters'));
  755. }
  756. $skilllevelModel = new \app\common\model\DispatchSkillLeaver();
  757. $where = [];
  758. $where["skill_id"] = $skill_id;
  759. $skilllevelList = $skilllevelModel->where($where)->select();
  760. if ($skilllevelList) {
  761. $this->success(__('获取成功!'), $skilllevelList);
  762. } else {
  763. $this->success(__('数据为空!'));
  764. }
  765. }
  766. /**
  767. * 获取技能区域列表
  768. */
  769. public function getSkillAreaList() {
  770. $skill_id = $this->request->request('skill_id'); // 技能ID
  771. if (!$skill_id) {
  772. $this->error(__('Invalid parameters'));
  773. }
  774. $skillareaModel = new \app\common\model\DispatchSkillArea();
  775. $where = [];
  776. $where["skill_id"] = $skill_id;
  777. $skillareaList = $skillareaModel->where($where)->select();
  778. if ($skillareaList) {
  779. $this->success(__('获取成功!'), $skillareaList);
  780. } else {
  781. $this->success(__('数据为空!'));
  782. }
  783. }
  784. /**
  785. * 获取订单列表
  786. */
  787. public function getOrderList() {
  788. $type = $this->request->request('type',1); // 订单类型:1=我的下单,2=我的接单
  789. $status = $this->request->request('status',999); // 订单状态:999=全部,-2=已取消,-1=拒绝接单,0=待付款,1=待确定,2=进行中,3=已完成
  790. $page = $this->request->request('page',1); // 分页
  791. $pageNum = $this->request->request('pageNum',10); // 分页
  792. // 分页搜索构建
  793. $pageStart = ($page-1)*$pageNum;
  794. $user = "o.recive_id";
  795. $type == 1 && $user = "o.recive_id";
  796. $type == 2 && $user = "o.user_id";
  797. // 主体列表信息
  798. $orderModel = new \app\common\model\DispatchOrder();
  799. $where = [];
  800. $type == 1 && $where["o.user_id"] = $this->auth->id;
  801. $type == 2 && $where["o.recive_id"] = $this->auth->id;
  802. $status != 999 && $where["o.status"] = $status;
  803. $orderList = $orderModel->alias("o")
  804. ->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")
  805. ->join("hx_user u","u.id = ".$user)
  806. ->where($where)
  807. ->order("createtime","desc")
  808. ->limit($pageStart,$pageNum)
  809. ->select();
  810. if(!$orderList) {
  811. $this->success(__('数据为空!'),[]);
  812. }
  813. foreach($orderList as $k => &$v) {
  814. $v["make_time"] = date("Y-m-d H:i",$v["make_time"]);
  815. $v["createtime"] = date("Y-m-d",$v["createtime"]);
  816. $v["status_text"] = $orderModel->getStateAttr($v["status"]);
  817. }
  818. $this->success(__('获取成功!'), $orderList);
  819. }
  820. }