Dispatch.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. $userModel = new \app\common\model\User();
  365. $where = [];
  366. $where["id"] = $this->auth->id;
  367. $jewel = $userModel->where($where)->value("jewel");
  368. $price = 0;
  369. $authInfo["price"] >= 0 || $price = 0;
  370. $authInfo["price"] >= 0 && $price = $authInfo["price"];
  371. $money = $price * $num;
  372. if($money > $jewel) {
  373. $this->error(__('钻石余额不足,请先充值!'),[],100);
  374. }
  375. // 禁止自己下单
  376. if($this->auth->id == $authInfo["user_id"]) {
  377. $this->error(__('禁止自己下单!'),[],101);
  378. }
  379. // 查看当前用户是否有未支付订单
  380. $where = [];
  381. $where["user_id"] = $this->auth->id;
  382. $where["status"] = 0;
  383. $orderInfo = $orderModel->where($where)->find();
  384. // $orderInfo && $this->error(__('您有未支付订单,请先支付!'),[],102);
  385. Db::startTrans();
  386. try{
  387. // 添加订单信息
  388. $out_trade_no = date("YmdHis").rand(100000,999999);// 产生订单号
  389. $data = [];
  390. $data["user_id"] = $this->auth->id;
  391. $data["order_no"] = $out_trade_no;
  392. $data["recive_id"] = $authInfo["user_id"];
  393. $data["auth_id"] = $authInfo["id"];
  394. $data["price"] = $authInfo["price"];
  395. $data["num"] = $num;
  396. $data["skill_image"] = $skillInfo["image"];
  397. $data["skill_name"] = $skillInfo["name"];
  398. $data["skill_unit"] = $skillInfo["unit"];
  399. $data["make_time"] = $make_time;
  400. $data["describe"] = $describe;
  401. $data["status"] = 0;
  402. $data["createtime"] = time();
  403. $res = $orderModel->insertGetId($data);
  404. if($res) {
  405. \app\common\model\Message::addMessage($authInfo["user_id"],"派单通知","您的技能:".$skillInfo["name"]."有人下单啦,请注意查看!");
  406. Db::commit();
  407. $this->success('订单创建成功!',["order_no"=>$out_trade_no]);
  408. } else {
  409. $this->error(__('订单创建失败!'));
  410. }
  411. }catch (ValidateException $e) {
  412. Db::rollback();
  413. $this->error($e->getMessage());
  414. } catch (PDOException $e) {
  415. Db::rollback();
  416. $this->error($e->getMessage());
  417. } catch (Exception $e) {
  418. Db::rollback();
  419. $this->error($e->getMessage());
  420. }
  421. }
  422. /**
  423. * 添加订单评价内容
  424. */
  425. public function addOrderComment() {
  426. $order_no = $this->request->request('order_no'); // 订单编号
  427. $content = $this->request->request('content'); // 评价内容
  428. if (!$order_no && !$content) {
  429. $this->error(__('Invalid parameters'));
  430. }
  431. Db::startTrans();
  432. try{
  433. $ordercommentModel = new \app\common\model\DispatchOrderComment();
  434. $orderModel = new \app\common\model\DispatchOrder();
  435. $orderInfo = $orderModel->where(["order_no"=>$order_no])->find();
  436. if(!$orderInfo) $this->error(__('未查询到订单信息!'));
  437. if($orderInfo["status"] != 3) $this->error(__('当前订单状态不允许评价!'));
  438. $data= [];
  439. $data["user_id"] = $this->auth->id;
  440. $data["order_id"] = $orderInfo["id"];
  441. $data["content"] = $content;
  442. $data["createtime"] = time();
  443. $res1 = $ordercommentModel->insert($data);
  444. $res2 = $orderModel->update(["is_comment"=>1],["order_no"=>$order_no]);
  445. if($res1 && $res2) {
  446. Db::commit();
  447. $this->success('评价成功!');
  448. } else {
  449. $this->error(__('订单创建失败!'));
  450. }
  451. }catch (ValidateException $e) {
  452. Db::rollback();
  453. $this->error($e->getMessage());
  454. } catch (PDOException $e) {
  455. Db::rollback();
  456. $this->error($e->getMessage());
  457. } catch (Exception $e) {
  458. Db::rollback();
  459. $this->error($e->getMessage());
  460. }
  461. }
  462. /**
  463. * 钻石余额支付订单
  464. */
  465. public function jewelPay() {
  466. $order_no = $this->request->request('order_no'); // 订单号
  467. if (!$order_no) {
  468. $this->error(__('Invalid parameters'));
  469. }
  470. // 获取订单信息
  471. $orderModel = new \app\common\model\DispatchOrder();
  472. $where = [];
  473. $where["order_no"] = $order_no;
  474. $orderInfo = $orderModel->where($where)->find();
  475. if(!$orderInfo) {
  476. $this->error(__('订单信息获取失败!'));
  477. }
  478. if($orderInfo["status"] != 0) {
  479. $this->error(__('订单状态有误!'),[],104);
  480. }
  481. // 判断用户余额
  482. $userModel = new \app\common\model\User();
  483. $where = [];
  484. $where["id"] = $this->auth->id;
  485. $jewel = $userModel->where($where)->value("jewel");
  486. $money = $orderInfo["price"] * $orderInfo["num"];
  487. if($money > $jewel) {
  488. $this->error(__('钻石余额不足,请先充值!'),[],100);
  489. }
  490. // 去支付
  491. Db::startTrans();
  492. try{
  493. // 扣除用户余额
  494. $where = [];
  495. $where["id"] = $this->auth->id;
  496. $userInfo = $userModel->where($where)->find();
  497. $res1 = $userModel->where($where)->setDec("jewel",$money);
  498. $res2 = $userModel->where($where)->setInc("frozen",$money);
  499. // 添加用户余额变动记录
  500. $userjewellogModel = new \app\common\model\UserJewelLog();
  501. $data = [];
  502. $data["user_id"] = $this->auth->id;
  503. $data["value"] = $money;
  504. $data["mode"] = "-";
  505. $data["before"] = $userInfo["jewel"];
  506. $data["balance"] = $userInfo["jewel"]-$money;
  507. $data["detail"] = "下单扣除余额";
  508. $data["createtime"] = time();
  509. $res3 = $userjewellogModel->insertGetId($data);
  510. // 添加订单信息
  511. $orderModel = new \app\common\model\DispatchOrder();
  512. // 修改订单状态
  513. $where = [];
  514. $where["order_no"] = $order_no;
  515. $data = [];
  516. $data["status"] = 1;
  517. $res4 = $orderModel->update($data,$where);
  518. if($res1 && $res2 && $res3 && $res4) {
  519. Db::commit();
  520. // 短信通知
  521. $reciveuserInfo = $userModel->where(["id"=>$orderInfo["recive_id"]])->find();
  522. //Smslib::notice($reciveuserInfo->mobile, $orderInfo["skill_name"],"有人接单啦",'orderNotice');
  523. // 系统消息通知
  524. \app\common\model\Message::addMessage($orderInfo["recive_id"],"派单通知","您的派单,技能:".$orderInfo["skill_name"]."有人接单啦,请注意查看!");
  525. $this->success('支付成功!');
  526. } else {
  527. $this->error(__('订单创建失败!'));
  528. }
  529. }catch (ValidateException $e) {
  530. Db::rollback();
  531. $this->error($e->getMessage());
  532. } catch (PDOException $e) {
  533. Db::rollback();
  534. $this->error($e->getMessage());
  535. } catch (Exception $e) {
  536. Db::rollback();
  537. $this->error($e->getMessage());
  538. }
  539. }
  540. /**
  541. * 更新订单状态
  542. */
  543. public function changeOrder() {
  544. $order_no = $this->request->request('order_no'); // 订单号
  545. $status = $this->request->request('status'); // 状态:-2=已取消,-1=拒绝接单,2=进行中,3=已完成
  546. if (!$order_no || !$status) {
  547. $this->error(__('Invalid parameters'));
  548. }
  549. if(!in_array($status,["-2","-1","2","3"])) {
  550. $this->error("非法的订单状态参数");
  551. }
  552. // 获取订单信息
  553. $orderModel = new \app\common\model\DispatchOrder();
  554. $where = [];
  555. $where["order_no"] = $order_no;
  556. $orderInfo = $orderModel->where($where)->find();
  557. if(!$orderInfo) {
  558. $this->error(__('订单信息获取失败!'));
  559. }
  560. if($orderInfo["status"] == $status) {
  561. $this->error(__('当前订单状态无变更要求!'),[],104);
  562. }
  563. // 验证用户权限
  564. if($status == -1 && $orderInfo["recive_id"] != $this->auth->id) {
  565. $this->error(__('无权限操作!'),[],103);
  566. }
  567. // 验证用户权限
  568. if(($status == -2 || $status == 3) && $orderInfo["user_id"] != $this->auth->id) {
  569. $this->error(__('无权限操作!'),[],103);
  570. }
  571. if($status == -1) { // 拒绝订单
  572. if($orderInfo["status"] != 1) {
  573. $this->error(__('当前订单状态不支持拒绝订单!'),[],104);
  574. }
  575. // 完成订单操作需要更新余额记录
  576. Db::startTrans();
  577. try{
  578. $userModel = new \app\common\model\User();
  579. $money = $orderInfo["price"] * $orderInfo["num"];
  580. // 解冻用户余额
  581. $where = [];
  582. $where["id"] = $this->auth->id;
  583. $userInfo = $userModel->where($where)->find();
  584. if($userInfo["frozen"] - $money < 0) {
  585. $this->error(__('账户资金异常,请联系管理员!'),[],105);
  586. }
  587. $res1 = $userModel->where($where)->setDec("frozen",$money);
  588. $res2 = $userModel->where($where)->setInc("jewel",$money);
  589. // 添加用户余额变动记录
  590. $userjewellogModel = new \app\common\model\UserJewelLog();
  591. $res3 = $userjewellogModel->addUserJewelLog($orderInfo["recive_id"], $money, "+", $userInfo["jewel"], "拒绝订单返还余额", 7);
  592. // 更新订单状态
  593. $data = [];
  594. $data["status"] = $status;
  595. $where = [];
  596. $where["order_no"] = $order_no;
  597. $res4 = $orderModel->update($data,$where);
  598. if($res1 && $res2 && $res3 && $res4) {
  599. Db::commit();
  600. // 短信通知
  601. //$reciveuserInfo = $userModel->where(["id"=>$orderInfo["user_id"]])->find();
  602. //Smslib::notice($reciveuserInfo->mobile, $orderInfo["skill_name"],"被拒绝接单",'orderNotice');
  603. // 系统消息通知
  604. \app\common\model\Message::addMessage($orderInfo["user_id"],"派单通知","您的订单,技能:".$orderInfo["skill_name"]."已被拒绝接单,请注意查看!");
  605. $this->success('订单完成。');
  606. } else {
  607. $this->error(__('订单更新失败!请稍后重试'));
  608. }
  609. }catch (ValidateException $e) {
  610. Db::rollback();
  611. $this->error($e->getMessage());
  612. } catch (PDOException $e) {
  613. Db::rollback();
  614. $this->error($e->getMessage());
  615. } catch (Exception $e) {
  616. Db::rollback();
  617. $this->error($e->getMessage());
  618. }
  619. } elseif($status == 3) { // 订单完成
  620. if($orderInfo["status"] != 2) {
  621. $this->error(__('当前订单状态不支持直接变更为已完成!'),[],104);
  622. }
  623. // 完成订单操作需要更新余额记录
  624. Db::startTrans();
  625. try{
  626. $userModel = new \app\common\model\User();
  627. $money = $orderInfo["price"] * $orderInfo["num"];
  628. // 增加用户余额
  629. $where = [];
  630. $where["id"] = $orderInfo["recive_id"];
  631. $userInfo = $userModel->where($where)->find();
  632. //$res1 = $userModel->where($where)->setInc("jewel",$money);
  633. $jewelMoneyRate = config('site.money_to_jewel');//1人民币兑换钻石数
  634. $moneyRmb = bcdiv($money,$jewelMoneyRate);
  635. $res1 = $userModel->where($where)->setInc("money",$moneyRmb);
  636. $where = [];
  637. $where["id"] = $this->auth->id;
  638. $res2 = $userModel->where($where)->setDec("frozen",$money);
  639. // 添加用户余额变动记录
  640. /*$userjewellogModel = new \app\common\model\UserJewelLog();
  641. $res3 = $userjewellogModel->addUserJewelLog($orderInfo["recive_id"], $money, "+", $userInfo["jewel"], "完成用户订单获得收益", 8);*/
  642. $remark = $orderInfo['skill_name'].'服务';
  643. $rs_wallet = model('wallet')->lockChangeAccountRemain($orderInfo["recive_id"],$moneyRmb,'+',$userInfo['money'],$remark,106,'money');
  644. $res3 = false;
  645. if($rs_wallet['status'] == false){
  646. $this->error($rs_wallet['msg']);
  647. Db::rollback();
  648. } else {
  649. $res3 = true;
  650. }
  651. // 更新订单状态
  652. $data = [];
  653. $data["status"] = $status;
  654. $data["is_comment"] = 1;//要求直接自动已评价,为了前端不显示立即评价按钮
  655. $where = [];
  656. $where["order_no"] = $order_no;
  657. $res4 = $orderModel->update($data,$where);
  658. if($res1 && $res2 && $res3 && $res4) {
  659. Db::commit();
  660. // 短信通知
  661. //$reciveuserInfo = $userModel->where(["id"=>$orderInfo["recive_id"]])->find();
  662. //Smslib::send($reciveuserInfo->mobile, $orderInfo["skill_name"], "顺利完成啦!",'orderNotice');
  663. // 系统消息通知
  664. \app\common\model\Message::addMessage($orderInfo["recive_id"],"派单通知","您的派单,技能:".$orderInfo["skill_name"]." 完成啦!,请注意查看!");
  665. $this->success('恭喜!订单完成。');
  666. } else {
  667. $this->error(__('订单更新失败!请稍后重试'));
  668. }
  669. }catch (ValidateException $e) {
  670. Db::rollback();
  671. $this->error($e->getMessage());
  672. } catch (PDOException $e) {
  673. Db::rollback();
  674. $this->error($e->getMessage());
  675. } catch (Exception $e) {
  676. Db::rollback();
  677. $this->error($e->getMessage());
  678. }
  679. }if($status == -2) { // 取消订单
  680. if($orderInfo["status"] != 1) {
  681. $this->error(__('当前订单状态不支持拒绝订单!'),[],104);
  682. }
  683. // 完成订单操作需要更新余额记录
  684. Db::startTrans();
  685. try{
  686. $userModel = new \app\common\model\User();
  687. $money = $orderInfo["price"] * $orderInfo["num"];
  688. // 解冻用户余额
  689. $where = [];
  690. $where["id"] = $this->auth->id;
  691. $userInfo = $userModel->where($where)->find();
  692. if($userInfo["frozen"] - $money < 0) {
  693. $this->error(__('账户资金异常,请联系管理员!'),[],105);
  694. }
  695. $res1 = $userModel->where($where)->setDec("frozen",$money);
  696. $res2 = $userModel->where($where)->setInc("jewel",$money);
  697. // 添加用户余额变动记录
  698. $userjewellogModel = new \app\common\model\UserJewelLog();
  699. $res3 = $userjewellogModel->addUserJewelLog($orderInfo["recive_id"], $money, "+", $userInfo["jewel"], "拒绝订单返还余额", 7);
  700. // 更新订单状态
  701. $data = [];
  702. $data["status"] = $status;
  703. $where = [];
  704. $where["order_no"] = $order_no;
  705. $res4 = $orderModel->update($data,$where);
  706. if($res1 && $res2 && $res3 && $res4) {
  707. Db::commit();
  708. // 短信通知
  709. //$reciveuserInfo = $userModel->where(["id"=>$orderInfo["user_id"]])->find();
  710. //Smslib::notice($reciveuserInfo->mobile, $orderInfo["skill_name"],"被拒绝接单",'orderNotice');
  711. // 系统消息通知
  712. \app\common\model\Message::addMessage($orderInfo["recive_id"],"派单通知","您的订单,技能:".$orderInfo["skill_name"]."已被取消,请登录查看!");
  713. $this->success('订单取消成功。');
  714. } else {
  715. $this->error(__('订单更新失败!请稍后重试'));
  716. }
  717. }catch (ValidateException $e) {
  718. Db::rollback();
  719. $this->error($e->getMessage());
  720. } catch (PDOException $e) {
  721. Db::rollback();
  722. $this->error($e->getMessage());
  723. } catch (Exception $e) {
  724. Db::rollback();
  725. $this->error($e->getMessage());
  726. }
  727. } else {
  728. $data = [];
  729. $data["status"] = $status;
  730. $where = [];
  731. $where["order_no"] = $order_no;
  732. $res = $orderModel->update($data,$where);
  733. if($res) {
  734. if ($status == 2) {//接单发
  735. \app\common\model\Message::addMessage($orderInfo['user_id'],"订单单通知","您的订单:".$order_no."已被接单,请登录查看!");
  736. }
  737. $this->success('更新成功!');
  738. } else {
  739. $this->error(__('订单创建失败!'));
  740. }
  741. }
  742. }
  743. /**
  744. * 获取技能列表
  745. */
  746. public function getSkillList() {
  747. $skillModel = new \app\common\model\DispatchSkill();
  748. $skillList = $skillModel->select();
  749. if ($skillList) {
  750. $this->success(__('获取成功!'), $skillList);
  751. } else {
  752. $this->success(__('数据为空!'));
  753. }
  754. }
  755. /**
  756. * 获取技能等级列表
  757. */
  758. public function getSkillLevelList() {
  759. $skill_id = $this->request->request('skill_id'); // 技能ID
  760. if (!$skill_id) {
  761. $this->error(__('Invalid parameters'));
  762. }
  763. $skilllevelModel = new \app\common\model\DispatchSkillLeaver();
  764. $where = [];
  765. $where["skill_id"] = $skill_id;
  766. $skilllevelList = $skilllevelModel->where($where)->select();
  767. if ($skilllevelList) {
  768. $this->success(__('获取成功!'), $skilllevelList);
  769. } else {
  770. $this->success(__('数据为空!'));
  771. }
  772. }
  773. /**
  774. * 获取技能区域列表
  775. */
  776. public function getSkillAreaList() {
  777. $skill_id = $this->request->request('skill_id'); // 技能ID
  778. if (!$skill_id) {
  779. $this->error(__('Invalid parameters'));
  780. }
  781. $skillareaModel = new \app\common\model\DispatchSkillArea();
  782. $where = [];
  783. $where["skill_id"] = $skill_id;
  784. $skillareaList = $skillareaModel->where($where)->select();
  785. if ($skillareaList) {
  786. $this->success(__('获取成功!'), $skillareaList);
  787. } else {
  788. $this->success(__('数据为空!'));
  789. }
  790. }
  791. /**
  792. * 获取订单列表
  793. */
  794. public function getOrderList() {
  795. $type = $this->request->request('type',1); // 订单类型:1=我的下单,2=我的接单
  796. $status = $this->request->request('status',999); // 订单状态:999=全部,-2=已取消,-1=拒绝接单,0=待付款,1=待确定,2=进行中,3=已完成
  797. $page = $this->request->request('page',1); // 分页
  798. $pageNum = $this->request->request('pageNum',10); // 分页
  799. // 分页搜索构建
  800. $pageStart = ($page-1)*$pageNum;
  801. $user = "o.recive_id";
  802. $type == 1 && $user = "o.recive_id";
  803. $type == 2 && $user = "o.user_id";
  804. // 主体列表信息
  805. $orderModel = new \app\common\model\DispatchOrder();
  806. $where = [];
  807. $type == 1 && $where["o.user_id"] = $this->auth->id;
  808. $type == 2 && $where["o.recive_id"] = $this->auth->id;
  809. $status != 999 && $where["o.status"] = $status;
  810. $orderList = $orderModel->alias("o")
  811. ->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")
  812. ->join("hx_user u","u.id = ".$user)
  813. ->where($where)
  814. ->order("createtime","desc")
  815. ->limit($pageStart,$pageNum)
  816. ->select();
  817. if(!$orderList) {
  818. $this->success(__('数据为空!'),[]);
  819. }
  820. foreach($orderList as $k => &$v) {
  821. $v["make_time"] = date("Y-m-d H:i",$v["make_time"]);
  822. $v["createtime"] = date("Y-m-d",$v["createtime"]);
  823. $v["status_text"] = $orderModel->getStateAttr($v["status"]);
  824. }
  825. $this->success(__('获取成功!'), $orderList);
  826. }
  827. }